2020-01-29 20:37:25 +01:00
|
|
|
import Vuex from 'vuex';
|
|
|
|
import createPersistedState from 'vuex-persistedstate';
|
|
|
|
import * as nestedProperty from 'nested-property';
|
|
|
|
|
|
|
|
import MiOS from './mios';
|
|
|
|
|
|
|
|
const defaultSettings = {
|
2020-02-09 13:31:17 +01:00
|
|
|
tutorial: 0,
|
2020-01-29 20:37:25 +01:00
|
|
|
keepCw: false,
|
|
|
|
showFullAcct: false,
|
|
|
|
rememberNoteVisibility: false,
|
|
|
|
defaultNoteVisibility: 'public',
|
2020-02-08 12:02:15 +01:00
|
|
|
defaultNoteLocalOnly: false,
|
2020-01-29 20:37:25 +01:00
|
|
|
uploadFolder: null,
|
|
|
|
pastedFileName: 'yyyy-MM-dd HH-mm-ss [{{number}}]',
|
|
|
|
wallpaper: null,
|
|
|
|
memo: null,
|
|
|
|
reactions: ['👍', '❤️', '😆', '🤔', '😮', '🎉', '💢', '😥', '😇', '🍮'],
|
|
|
|
widgets: []
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultDeviceSettings = {
|
|
|
|
lang: null,
|
|
|
|
loadRawImages: false,
|
|
|
|
alwaysShowNsfw: false,
|
|
|
|
useOsDefaultEmojis: false,
|
2020-02-06 09:21:28 +01:00
|
|
|
autoReload: false,
|
2020-01-29 20:37:25 +01:00
|
|
|
accounts: [],
|
|
|
|
recentEmojis: [],
|
2020-02-05 01:42:58 +01:00
|
|
|
visibility: 'public',
|
|
|
|
localOnly: false,
|
2020-01-29 20:37:25 +01:00
|
|
|
themes: [],
|
|
|
|
theme: 'light',
|
2020-02-06 11:11:14 +01:00
|
|
|
animation: true,
|
2020-01-29 20:37:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default (os: MiOS) => new Vuex.Store({
|
|
|
|
plugins: [createPersistedState({
|
|
|
|
paths: ['i', 'device', 'settings']
|
|
|
|
})],
|
|
|
|
|
|
|
|
state: {
|
|
|
|
i: null,
|
|
|
|
},
|
|
|
|
|
|
|
|
getters: {
|
|
|
|
isSignedIn: state => state.i != null,
|
|
|
|
},
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
updateI(state, x) {
|
|
|
|
state.i = x;
|
|
|
|
},
|
|
|
|
|
|
|
|
updateIKeyValue(state, x) {
|
|
|
|
state.i[x.key] = x.value;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
login(ctx, i) {
|
|
|
|
ctx.commit('updateI', i);
|
|
|
|
ctx.dispatch('settings/merge', i.clientData);
|
|
|
|
ctx.dispatch('addAcount', { id: i.id, i: localStorage.getItem('i') });
|
|
|
|
},
|
|
|
|
|
|
|
|
addAcount(ctx, info) {
|
|
|
|
if (!ctx.state.device.accounts.some(x => x.id === info.id)) {
|
|
|
|
ctx.commit('device/set', {
|
|
|
|
key: 'accounts',
|
|
|
|
value: ctx.state.device.accounts.concat([{ id: info.id, token: info.i }])
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
logout(ctx) {
|
|
|
|
ctx.commit('updateI', null);
|
|
|
|
localStorage.removeItem('i');
|
|
|
|
},
|
|
|
|
|
|
|
|
switchAccount(ctx, i) {
|
|
|
|
ctx.commit('updateI', i);
|
|
|
|
ctx.commit('settings/init', i.clientData);
|
|
|
|
localStorage.setItem('i', i.token);
|
|
|
|
},
|
|
|
|
|
|
|
|
mergeMe(ctx, me) {
|
|
|
|
for (const [key, value] of Object.entries(me)) {
|
|
|
|
ctx.commit('updateIKeyValue', { key, value });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (me.clientData) {
|
|
|
|
ctx.dispatch('settings/merge', me.clientData);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
modules: {
|
|
|
|
device: {
|
|
|
|
namespaced: true,
|
|
|
|
|
|
|
|
state: defaultDeviceSettings,
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
set(state, x: { key: string; value: any }) {
|
|
|
|
state[x.key] = x.value;
|
|
|
|
},
|
|
|
|
|
|
|
|
setTl(state, x) {
|
|
|
|
state.tl = {
|
|
|
|
src: x.src,
|
|
|
|
arg: x.arg
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
setVisibility(state, visibility) {
|
|
|
|
state.visibility = visibility;
|
|
|
|
},
|
2020-02-05 01:42:58 +01:00
|
|
|
|
|
|
|
setLocalOnly(state, localOnly) {
|
|
|
|
state.localOnly = localOnly;
|
|
|
|
},
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
settings: {
|
|
|
|
namespaced: true,
|
|
|
|
|
|
|
|
state: defaultSettings,
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
set(state, x: { key: string; value: any }) {
|
|
|
|
nestedProperty.set(state, x.key, x.value);
|
|
|
|
},
|
|
|
|
|
|
|
|
init(state, x) {
|
|
|
|
for (const [key, value] of Object.entries(defaultSettings)) {
|
|
|
|
if (x[key]) {
|
|
|
|
state[key] = x[key];
|
|
|
|
} else {
|
|
|
|
state[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
merge(ctx, settings) {
|
|
|
|
if (settings == null) return;
|
|
|
|
for (const [key, value] of Object.entries(settings)) {
|
|
|
|
ctx.commit('set', { key, value });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
set(ctx, x) {
|
|
|
|
ctx.commit('set', x);
|
|
|
|
|
|
|
|
if (ctx.rootGetters.isSignedIn) {
|
|
|
|
os.api('i/update-client-setting', {
|
|
|
|
name: x.key,
|
|
|
|
value: x.value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setWidgets(ctx, widgets) {
|
|
|
|
ctx.state.widgets = widgets;
|
|
|
|
ctx.dispatch('updateWidgets');
|
|
|
|
},
|
|
|
|
|
|
|
|
addWidget(ctx, widget) {
|
|
|
|
ctx.state.widgets.unshift(widget);
|
|
|
|
ctx.dispatch('updateWidgets');
|
|
|
|
},
|
|
|
|
|
|
|
|
removeWidget(ctx, widget) {
|
|
|
|
ctx.state.widgets = ctx.state.widgets.filter(w => w.id != widget.id);
|
|
|
|
ctx.dispatch('updateWidgets');
|
|
|
|
},
|
|
|
|
|
|
|
|
updateWidget(ctx, x) {
|
|
|
|
const w = ctx.state.widgets.find(w => w.id == x.id);
|
|
|
|
if (w) {
|
|
|
|
w.data = x.data;
|
|
|
|
ctx.dispatch('updateWidgets');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateWidgets(ctx) {
|
|
|
|
const widgets = ctx.state.widgets;
|
|
|
|
ctx.commit('set', {
|
|
|
|
key: 'widgets',
|
|
|
|
value: widgets
|
|
|
|
});
|
|
|
|
os.api('i/update-client-setting', {
|
|
|
|
name: 'widgets',
|
|
|
|
value: widgets
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|