Compare commits

..

No commits in common. "99eedadc6bebd427f891c1a975e46899bf2ab740" and "8a47252d3314bcf5ec9561ae929f0b0347b9c7f2" have entirely different histories.

4 changed files with 103 additions and 123 deletions

View file

@ -1,6 +1,6 @@
import { throttle } from 'lodash'
import { mapState } from 'pinia'
import { defineAsyncComponent } from 'vue'
import { defineAsyncComponent, toValue } from 'vue'
import DesktopNav from './components/desktop_nav/desktop_nav.vue'
import EditStatusModal from './components/edit_status_modal/edit_status_modal.vue'
@ -33,7 +33,7 @@ import messages from 'src/i18n/messages'
import localeService from 'src/services/locale/locale.service.js'
// Helper to unwrap reactive proxies
window.toValue = (x) => JSON.parse(JSON.stringify(x))
window.toValue = toValue
export default {
name: 'app',

View file

@ -270,7 +270,7 @@ export const INSTANCE_DEFAULT_CONFIG_DEFINITIONS = {
},
interfaceLanguage: {
description: 'UI language',
default: [browserLocale],
default: browserLocale,
},
hideScopeNotice: {
description: 'Hide scope notification',
@ -744,7 +744,7 @@ export const validateSetting = ({
throw new Error(string)
} else {
console.error(string)
return undefined
return value
}
}

View file

@ -290,90 +290,87 @@ export const _mergePrefs = (recent, stale) => {
*/
const resultOutput = { ...recentData }
const totalJournal = _mergeJournal(staleJournal, recentJournal)
totalJournal
.filter(({ path, operation, args }) => {
const definition = path.startsWith('simple.muteFilters')
? { default: {} }
: ROOT_CONFIG_DEFINITIONS[path.split('.')[1]]
const finalValue = validateSetting({
path: path.split('.')[1],
value: args[0],
definition,
throwError: false,
defaultState: ROOT_CONFIG,
})
return finalValue !== undefined
})
.forEach(({ path, operation, args }) => {
if (path.startsWith('_')) {
throw new Error(
`journal contains entry to edit internal (starts with _) field '${path}', something is incorrect here, ignoring.`,
)
}
switch (operation) {
case 'set': {
if (path.startsWith('collections')) {
return console.error('Illegal operation "set" on a collection')
}
if (path.split(/\./g).length <= 1) {
return console.error(
`Calling set on depth <= 1 (path: ${path}) is not allowed`,
)
}
set(resultOutput, path, args[0])
break
totalJournal.forEach(({ path, operation, args }) => {
if (path.startsWith('_')) {
throw new Error(
`journal contains entry to edit internal (starts with _) field '${path}', something is incorrect here, ignoring.`,
)
}
switch (operation) {
case 'set': {
if (path.startsWith('collections')) {
return console.error('Illegal operation "set" on a collection')
}
case 'unset':
if (path.startsWith('collections')) {
return console.error('Illegal operation "unset" on a collection')
}
if (path.split(/\./g).length <= 2) {
return console.error(
`Calling unset on depth <= 2 (path: ${path}) is not allowed`,
)
}
unset(resultOutput, path)
break
case 'addToCollection':
if (!path.startsWith('collections')) {
return console.error(
'Illegal operation "addToCollection" on a non-collection',
)
}
set(
resultOutput,
path,
Array.from(new Set(get(resultOutput, path)).add(args[0])),
)
break
case 'removeFromCollection': {
if (!path.startsWith('collections')) {
return console.error(
'Illegal operation "removeFromCollection" on a non-collection',
)
}
const newSet = new Set(get(resultOutput, path))
newSet.delete(args[0])
set(resultOutput, path, Array.from(newSet))
break
}
case 'reorderCollection': {
const [value, movement] = args
set(
resultOutput,
path,
_moveItemInArray(get(resultOutput, path), value, movement),
)
break
}
default:
if (path.split(/\./g).length <= 1) {
return console.error(
`Unknown journal operation: '${operation}', did we forget to run reverse migrations beforehand?`,
`Calling set on depth <= 1 (path: ${path}) is not allowed`,
)
}
const definition = path.startsWith('simple.muteFilters')
? { default: {} }
: ROOT_CONFIG_DEFINITIONS[path.split('.')[1]]
const finalValue = validateSetting({
path: path.split('.')[1],
value: args[0],
definition,
throwError: false,
defaultState: ROOT_CONFIG,
})
set(resultOutput, path, finalValue)
break
}
})
case 'unset':
if (path.startsWith('collections')) {
return console.error('Illegal operation "unset" on a collection')
}
if (path.split(/\./g).length <= 2) {
return console.error(
`Calling unset on depth <= 2 (path: ${path}) is not allowed`,
)
}
unset(resultOutput, path)
break
case 'addToCollection':
if (!path.startsWith('collections')) {
return console.error(
'Illegal operation "addToCollection" on a non-collection',
)
}
set(
resultOutput,
path,
Array.from(new Set(get(resultOutput, path)).add(args[0])),
)
break
case 'removeFromCollection': {
if (!path.startsWith('collections')) {
return console.error(
'Illegal operation "removeFromCollection" on a non-collection',
)
}
const newSet = new Set(get(resultOutput, path))
newSet.delete(args[0])
set(resultOutput, path, Array.from(newSet))
break
}
case 'reorderCollection': {
const [value, movement] = args
set(
resultOutput,
path,
_moveItemInArray(get(resultOutput, path), value, movement),
)
break
}
default:
return console.error(
`Unknown journal operation: '${operation}', did we forget to run reverse migrations beforehand?`,
)
}
})
return { ...resultOutput, _journal: totalJournal }
}
@ -516,7 +513,7 @@ export const useSyncConfigStore = defineStore('sync_config', {
defaultState: ROOT_CONFIG,
})
if (finalValue !== undefined) set(this.prefsStorage, path, finalValue)
set(this.prefsStorage, path, finalValue)
this.prefsStorage._journal = [
...this.prefsStorage._journal,
@ -757,6 +754,7 @@ export const useSyncConfigStore = defineStore('sync_config', {
this.flagStorage = this.cache.flagStorage
this.prefsStorage = this.cache.prefsStorage
this.pushSyncConfig()
console.log('C', this.cache)
},
pushSyncConfig({ force = false } = {}) {
const needPush = this.dirty || force
@ -768,29 +766,7 @@ export const useSyncConfigStore = defineStore('sync_config', {
},
persist: {
afterLoad(state) {
console.log('Validating persisted state of SyncConfig')
const newState = { ...state }
const newEntries = Object.entries(newState.prefsStorage.simple).map(
([path, value]) => {
if (path === 'muteFilters') {
return value
}
const definition = ROOT_CONFIG_DEFINITIONS[path]
const finalValue = validateSetting({
path,
value,
definition,
throwError: false,
defaultState: ROOT_CONFIG,
})
return finalValue === undefined ? undefined : [path, finalValue]
},
)
newState.prefsStorage.simple = Object.fromEntries(
newEntries.filter((_) => _),
)
return newState
return state
},
},
})

View file

@ -174,11 +174,13 @@ describe('The UserHighlight store', () => {
it('should prefer recent and apply journal to it', () => {
expect(
_mergeHighlights(
// RECENT
{
// RECENT
'a@test.xyz': 1,
'b@test.xyz': 0,
'c@test.xyz': true,
highlight: {
'a@test.xyz': 1,
'b@test.xyz': 0,
'c@test.xyz': true,
},
_journal: [
{
user: 'b@test.xyz',
@ -194,11 +196,13 @@ describe('The UserHighlight store', () => {
},
],
},
// STALE
{
// STALE
'a@test.xyz': 1,
'b@test.xyz': 1,
'c@test.xyz': false,
highlight: {
'a@test.xyz': 1,
'b@test.xyz': 1,
'c@test.xyz': false,
},
_journal: [
{
user: 'a@test.xyz',
@ -216,9 +220,7 @@ describe('The UserHighlight store', () => {
},
),
).to.eql({
'a@test.xyz': 1,
'b@test.xyz': 1,
'c@test.xyz': true,
highlight: { 'a@test.xyz': 1, 'b@test.xyz': 1, 'c@test.xyz': true },
_journal: [
{ user: 'a@test.xyz', operation: 'set', args: [1], timestamp: 1 },
{ user: 'b@test.xyz', operation: 'set', args: [1], timestamp: 3 },
@ -237,7 +239,7 @@ describe('The UserHighlight store', () => {
_mergeHighlights(
// RECENT
{
'a@test.xyz': { type: 'foo' },
highlight: { 'a@test.xyz': { type: 'foo' } },
_journal: [
{
user: 'a@test.xyz',
@ -249,7 +251,7 @@ describe('The UserHighlight store', () => {
},
// STALE
{
'a@test.xyz': { type: 'bar' },
highlight: { 'a@test.xyz': { type: 'bar' } },
_journal: [
{
user: 'a@test.xyz',
@ -261,7 +263,7 @@ describe('The UserHighlight store', () => {
},
),
).to.eql({
'a@test.xyz': { type: 'bar' },
highlight: { 'a@test.xyz': { type: 'bar' } },
_journal: [
{
user: 'a@test.xyz',
@ -278,7 +280,7 @@ describe('The UserHighlight store', () => {
_mergeHighlights(
// RECENT
{
'a@test.xyz': { type: 'foo' },
highlight: { 'a@test.xyz': { type: 'foo' } },
_journal: [
{
user: 'a@test.xyz',
@ -290,6 +292,7 @@ describe('The UserHighlight store', () => {
},
// STALE
{
highlight: {},
_journal: [
{
user: 'a@test.xyz',
@ -301,6 +304,7 @@ describe('The UserHighlight store', () => {
},
),
).to.eql({
highlight: {},
_journal: [
{
user: 'a@test.xyz',