2020-01-29 20:37:25 +01:00
|
|
|
import Vuex from 'vuex';
|
|
|
|
import createPersistedState from 'vuex-persistedstate';
|
|
|
|
import * as nestedProperty from 'nested-property';
|
2020-03-31 02:11:43 +02:00
|
|
|
import { apiUrl } from './config';
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
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}}]',
|
|
|
|
memo: null,
|
|
|
|
reactions: ['👍', '❤️', '😆', '🤔', '😮', '🎉', '💢', '😥', '😇', '🍮'],
|
2020-02-09 23:23:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultDeviceUserSettings = {
|
|
|
|
visibility: 'public',
|
|
|
|
localOnly: false,
|
|
|
|
widgets: [],
|
|
|
|
tl: {
|
|
|
|
src: 'home'
|
|
|
|
},
|
2020-01-29 20:37:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultDeviceSettings = {
|
|
|
|
lang: null,
|
|
|
|
loadRawImages: false,
|
|
|
|
alwaysShowNsfw: false,
|
2020-02-12 19:11:37 +01:00
|
|
|
useOsNativeEmojis: false,
|
2020-02-06 09:21:28 +01:00
|
|
|
autoReload: false,
|
2020-01-29 20:37:25 +01:00
|
|
|
accounts: [],
|
|
|
|
recentEmojis: [],
|
|
|
|
themes: [],
|
2020-03-22 02:39:12 +01:00
|
|
|
darkTheme: '8c539dc1-0fab-4d47-9194-39c508e9bfe1',
|
|
|
|
lightTheme: '4eea646f-7afa-4645-83e9-83af0333cd37',
|
|
|
|
darkMode: false,
|
|
|
|
syncDeviceDarkMode: true,
|
2020-02-06 11:11:14 +01:00
|
|
|
animation: true,
|
2020-02-13 15:20:12 +01:00
|
|
|
animatedMfm: true,
|
2020-02-16 14:46:18 +01:00
|
|
|
imageNewTab: false,
|
2020-02-18 19:16:10 +01:00
|
|
|
showFixedPostForm: false,
|
2020-04-13 16:46:53 +02:00
|
|
|
disablePagesScript: true,
|
2020-02-19 18:40:53 +01:00
|
|
|
sfxVolume: 0.3,
|
2020-02-19 19:14:17 +01:00
|
|
|
sfxNote: 'syuilo/down',
|
|
|
|
sfxNoteMy: 'syuilo/up',
|
2020-02-19 18:40:53 +01:00
|
|
|
sfxNotification: 'syuilo/pope2',
|
2020-02-19 19:14:17 +01:00
|
|
|
sfxChat: 'syuilo/pope1',
|
|
|
|
sfxChatBg: 'syuilo/waon',
|
2020-02-19 22:08:49 +01:00
|
|
|
sfxAntenna: 'syuilo/triple',
|
2020-02-09 23:23:43 +01:00
|
|
|
userData: {},
|
2020-01-29 20:37:25 +01:00
|
|
|
};
|
|
|
|
|
2020-02-10 15:17:42 +01:00
|
|
|
function copy<T>(data: T): T {
|
2020-02-09 23:23:43 +01:00
|
|
|
return JSON.parse(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
2020-03-31 02:11:43 +02:00
|
|
|
export default () => new Vuex.Store({
|
2020-01-29 20:37:25 +01:00
|
|
|
plugins: [createPersistedState({
|
2020-02-10 15:17:42 +01:00
|
|
|
paths: ['i', 'device', 'deviceUser', 'settings', 'instance']
|
2020-01-29 20:37:25 +01:00
|
|
|
})],
|
|
|
|
|
|
|
|
state: {
|
|
|
|
i: null,
|
2020-03-31 02:11:43 +02:00
|
|
|
pendingApiRequestsCount: 0,
|
|
|
|
spinner: null
|
2020-01-29 20:37:25 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
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-09 23:23:43 +01:00
|
|
|
async login(ctx, i) {
|
2020-01-29 20:37:25 +01:00
|
|
|
ctx.commit('updateI', i);
|
2020-02-09 23:23:43 +01: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-29 20:37:25 +01: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-09 23:23:43 +01:00
|
|
|
ctx.commit('device/setUserData', { userId: ctx.state.i.id, data: ctx.state.deviceUser });
|
2020-01-29 20:37:25 +01:00
|
|
|
ctx.commit('updateI', null);
|
2020-02-09 23:23:43 +01:00
|
|
|
ctx.commit('settings/init', {});
|
|
|
|
ctx.commit('deviceUser/init', {});
|
2020-01-29 20:37:25 +01:00
|
|
|
localStorage.removeItem('i');
|
2020-03-20 05:56:22 +01:00
|
|
|
document.cookie = `igi=; path=/`;
|
2020-01-29 20:37:25 +01:00
|
|
|
},
|
|
|
|
|
2020-02-09 23:23:43 +01:00
|
|
|
async switchAccount(ctx, i) {
|
|
|
|
ctx.commit('device/setUserData', { userId: ctx.state.i.id, data: ctx.state.deviceUser });
|
2020-01-29 20:37:25 +01:00
|
|
|
localStorage.setItem('i', i.token);
|
2020-02-09 23:23:43 +01:00
|
|
|
await ctx.dispatch('login', i);
|
2020-01-29 20:37:25 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
mergeMe(ctx, me) {
|
|
|
|
for (const [key, value] of Object.entries(me)) {
|
|
|
|
ctx.commit('updateIKeyValue', { key, value });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (me.clientData) {
|
2020-02-09 23:23:43 +01:00
|
|
|
ctx.commit('settings/init', me.clientData);
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
},
|
2020-03-31 02:11:43 +02:00
|
|
|
|
|
|
|
api(ctx, { endpoint, data, token }) {
|
|
|
|
if (++ctx.state.pendingApiRequestsCount === 1) {
|
|
|
|
// TODO: spinnerの表示はstoreでやらない
|
|
|
|
ctx.state.spinner = document.createElement('div');
|
|
|
|
ctx.state.spinner.setAttribute('id', 'wait');
|
|
|
|
document.body.appendChild(ctx.state.spinner);
|
|
|
|
}
|
2020-04-01 22:41:03 +02:00
|
|
|
|
2020-03-31 02:11:43 +02:00
|
|
|
const onFinally = () => {
|
|
|
|
if (--ctx.state.pendingApiRequestsCount === 0) ctx.state.spinner.parentNode.removeChild(ctx.state.spinner);
|
|
|
|
};
|
2020-04-01 22:41:03 +02:00
|
|
|
|
2020-03-31 02:11:43 +02:00
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
// Append a credential
|
|
|
|
if (ctx.getters.isSignedIn) (data as any).i = ctx.state.i.token;
|
2020-04-12 12:38:19 +02:00
|
|
|
if (token !== undefined) (data as any).i = token;
|
2020-04-01 22:41:03 +02:00
|
|
|
|
2020-03-31 02:11:43 +02:00
|
|
|
// Send request
|
|
|
|
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
credentials: 'omit',
|
|
|
|
cache: 'no-cache'
|
|
|
|
}).then(async (res) => {
|
|
|
|
const body = res.status === 204 ? null : await res.json();
|
2020-04-01 22:41:03 +02:00
|
|
|
|
2020-03-31 02:11:43 +02:00
|
|
|
if (res.status === 200) {
|
|
|
|
resolve(body);
|
|
|
|
} else if (res.status === 204) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject(body.error);
|
|
|
|
}
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
2020-04-01 22:41:03 +02:00
|
|
|
|
2020-03-31 02:11:43 +02:00
|
|
|
promise.then(onFinally, onFinally);
|
2020-04-01 22:41:03 +02:00
|
|
|
|
2020-03-31 02:11:43 +02:00
|
|
|
return promise;
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
modules: {
|
2020-02-10 15:17:42 +01:00
|
|
|
instance: {
|
|
|
|
namespaced: true,
|
|
|
|
|
|
|
|
state: {
|
|
|
|
meta: null
|
|
|
|
},
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
set(state, meta) {
|
|
|
|
state.meta = meta;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
async fetch(ctx) {
|
2020-03-31 02:11:43 +02:00
|
|
|
const meta = await ctx.dispatch('api', {
|
|
|
|
endpoint: 'meta',
|
|
|
|
data: {
|
|
|
|
detail: false
|
|
|
|
}
|
|
|
|
}, { root: true });
|
2020-02-10 15:17:42 +01:00
|
|
|
|
|
|
|
ctx.commit('set', meta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
device: {
|
|
|
|
namespaced: true,
|
|
|
|
|
|
|
|
state: defaultDeviceSettings,
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
set(state, x: { key: string; value: any }) {
|
|
|
|
state[x.key] = x.value;
|
|
|
|
},
|
|
|
|
|
2020-02-09 23:23:43 +01: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-29 20:37:25 +01:00
|
|
|
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-02-09 23:23:43 +01: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) {
|
2020-04-04 01:46:54 +02:00
|
|
|
const w = state.widgets.find(w => w.id === x.id);
|
2020-02-09 23:23:43 +01:00
|
|
|
if (w) {
|
|
|
|
w.data = x.data;
|
|
|
|
}
|
|
|
|
},
|
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: {
|
|
|
|
set(ctx, x) {
|
|
|
|
ctx.commit('set', x);
|
|
|
|
|
|
|
|
if (ctx.rootGetters.isSignedIn) {
|
2020-03-31 02:11:43 +02:00
|
|
|
ctx.dispatch('api', {
|
|
|
|
endpoint: 'i/update-client-setting',
|
|
|
|
data: {
|
|
|
|
name: x.key,
|
|
|
|
value: x.value
|
|
|
|
}
|
|
|
|
}, { root: true });
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|