2020-01-30 04:37:25 +09: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 21:31:17 +09:00
|
|
|
tutorial: 0,
|
2020-01-30 04:37:25 +09:00
|
|
|
keepCw: false,
|
|
|
|
showFullAcct: false,
|
|
|
|
rememberNoteVisibility: false,
|
|
|
|
defaultNoteVisibility: 'public',
|
2020-02-08 20:02:15 +09:00
|
|
|
defaultNoteLocalOnly: false,
|
2020-01-30 04:37:25 +09:00
|
|
|
uploadFolder: null,
|
|
|
|
pastedFileName: 'yyyy-MM-dd HH-mm-ss [{{number}}]',
|
|
|
|
memo: null,
|
|
|
|
reactions: ['👍', '❤️', '😆', '🤔', '😮', '🎉', '💢', '😥', '😇', '🍮'],
|
2020-02-10 07:23:43 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultDeviceUserSettings = {
|
|
|
|
visibility: 'public',
|
|
|
|
localOnly: false,
|
|
|
|
widgets: [],
|
|
|
|
tl: {
|
|
|
|
src: 'home'
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultDeviceSettings = {
|
|
|
|
lang: null,
|
|
|
|
loadRawImages: false,
|
|
|
|
alwaysShowNsfw: false,
|
2020-02-13 03:11:37 +09:00
|
|
|
useOsNativeEmojis: false,
|
2020-02-06 17:21:28 +09:00
|
|
|
autoReload: false,
|
2020-01-30 04:37:25 +09:00
|
|
|
accounts: [],
|
|
|
|
recentEmojis: [],
|
|
|
|
themes: [],
|
|
|
|
theme: 'light',
|
2020-02-06 19:11:14 +09:00
|
|
|
animation: true,
|
2020-02-13 23:20:12 +09:00
|
|
|
animatedMfm: true,
|
2020-02-16 22:46:18 +09:00
|
|
|
imageNewTab: false,
|
2020-02-19 03:16:10 +09:00
|
|
|
showFixedPostForm: false,
|
2020-02-19 06:16:49 +09:00
|
|
|
useNotificationsPopup: true,
|
2020-02-10 07:23:43 +09:00
|
|
|
userData: {},
|
2020-01-30 04:37:25 +09:00
|
|
|
};
|
|
|
|
|
2020-02-10 23:17:42 +09:00
|
|
|
function copy<T>(data: T): T {
|
2020-02-10 07:23:43 +09:00
|
|
|
return JSON.parse(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
export default (os: MiOS) => new Vuex.Store({
|
|
|
|
plugins: [createPersistedState({
|
2020-02-10 23:17:42 +09:00
|
|
|
paths: ['i', 'device', 'deviceUser', 'settings', 'instance']
|
2020-01-30 04:37:25 +09:00
|
|
|
})],
|
|
|
|
|
|
|
|
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: {
|
2020-02-10 07:23:43 +09:00
|
|
|
async login(ctx, i) {
|
2020-01-30 04:37:25 +09:00
|
|
|
ctx.commit('updateI', i);
|
2020-02-10 07:23:43 +09:00
|
|
|
ctx.commit('settings/init', i.clientData);
|
|
|
|
ctx.commit('deviceUser/init', ctx.state.device.userData[i.id] || {});
|
|
|
|
await ctx.dispatch('addAcount', { id: i.id, i: localStorage.getItem('i') });
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
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) {
|
2020-02-10 07:23:43 +09:00
|
|
|
ctx.commit('device/setUserData', { userId: ctx.state.i.id, data: ctx.state.deviceUser });
|
2020-01-30 04:37:25 +09:00
|
|
|
ctx.commit('updateI', null);
|
2020-02-10 07:23:43 +09:00
|
|
|
ctx.commit('settings/init', {});
|
|
|
|
ctx.commit('deviceUser/init', {});
|
2020-01-30 04:37:25 +09:00
|
|
|
localStorage.removeItem('i');
|
|
|
|
},
|
|
|
|
|
2020-02-10 07:23:43 +09:00
|
|
|
async switchAccount(ctx, i) {
|
|
|
|
ctx.commit('device/setUserData', { userId: ctx.state.i.id, data: ctx.state.deviceUser });
|
2020-01-30 04:37:25 +09:00
|
|
|
localStorage.setItem('i', i.token);
|
2020-02-10 07:23:43 +09:00
|
|
|
await ctx.dispatch('login', i);
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
mergeMe(ctx, me) {
|
|
|
|
for (const [key, value] of Object.entries(me)) {
|
|
|
|
ctx.commit('updateIKeyValue', { key, value });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (me.clientData) {
|
2020-02-10 07:23:43 +09:00
|
|
|
ctx.commit('settings/init', me.clientData);
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
modules: {
|
2020-02-10 23:17:42 +09:00
|
|
|
instance: {
|
|
|
|
namespaced: true,
|
|
|
|
|
|
|
|
state: {
|
|
|
|
meta: null
|
|
|
|
},
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
set(state, meta) {
|
|
|
|
state.meta = meta;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
async fetch(ctx) {
|
|
|
|
const meta = await os.api('meta', {
|
|
|
|
detail: false
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.commit('set', meta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
device: {
|
|
|
|
namespaced: true,
|
|
|
|
|
|
|
|
state: defaultDeviceSettings,
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
set(state, x: { key: string; value: any }) {
|
|
|
|
state[x.key] = x.value;
|
|
|
|
},
|
|
|
|
|
2020-02-10 07:23:43 +09:00
|
|
|
setUserData(state, x: { userId: string; data: any }) {
|
|
|
|
state.userData[x.userId] = copy(x.data);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
deviceUser: {
|
|
|
|
namespaced: true,
|
|
|
|
|
|
|
|
state: defaultDeviceUserSettings,
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
init(state, x) {
|
|
|
|
for (const [key, value] of Object.entries(defaultDeviceUserSettings)) {
|
|
|
|
if (x[key]) {
|
|
|
|
state[key] = x[key];
|
|
|
|
} else {
|
|
|
|
state[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
set(state, x: { key: string; value: any }) {
|
|
|
|
state[x.key] = x.value;
|
|
|
|
},
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
setTl(state, x) {
|
|
|
|
state.tl = {
|
|
|
|
src: x.src,
|
|
|
|
arg: x.arg
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
setVisibility(state, visibility) {
|
|
|
|
state.visibility = visibility;
|
|
|
|
},
|
2020-02-05 09:42:58 +09:00
|
|
|
|
|
|
|
setLocalOnly(state, localOnly) {
|
|
|
|
state.localOnly = localOnly;
|
|
|
|
},
|
2020-02-10 07:23:43 +09:00
|
|
|
|
|
|
|
setWidgets(state, widgets) {
|
|
|
|
state.widgets = widgets;
|
|
|
|
},
|
|
|
|
|
|
|
|
addWidget(state, widget) {
|
|
|
|
state.widgets.unshift(widget);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeWidget(state, widget) {
|
|
|
|
state.widgets = state.widgets.filter(w => w.id != widget.id);
|
|
|
|
},
|
|
|
|
|
|
|
|
updateWidget(state, x) {
|
|
|
|
const w = state.widgets.find(w => w.id == x.id);
|
|
|
|
if (w) {
|
|
|
|
w.data = x.data;
|
|
|
|
}
|
|
|
|
},
|
2020-01-30 04:37:25 +09: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: {
|
|
|
|
set(ctx, x) {
|
|
|
|
ctx.commit('set', x);
|
|
|
|
|
|
|
|
if (ctx.rootGetters.isSignedIn) {
|
|
|
|
os.api('i/update-client-setting', {
|
|
|
|
name: x.key,
|
|
|
|
value: x.value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|