Sharkey/src/client/store.ts

308 lines
6.5 KiB
TypeScript
Raw Normal View History

import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate';
import * as nestedProperty from 'nested-property';
2020-03-31 00:11:43 +00:00
import { apiUrl } from './config';
export const defaultSettings = {
tutorial: 0,
keepCw: false,
showFullAcct: false,
rememberNoteVisibility: false,
defaultNoteVisibility: 'public',
defaultNoteLocalOnly: false,
uploadFolder: null,
pastedFileName: 'yyyy-MM-dd HH-mm-ss [{{number}}]',
memo: null,
reactions: ['👍', '❤️', '😆', '🤔', '😮', '🎉', '💢', '😥', '😇', '🍮'],
};
const defaultDeviceUserSettings = {
visibility: 'public',
localOnly: false,
widgets: [],
tl: {
src: 'home'
},
};
const defaultDeviceSettings = {
lang: null,
loadRawImages: false,
alwaysShowNsfw: false,
useOsNativeEmojis: false,
2020-02-06 08:21:28 +00:00
autoReload: false,
accounts: [],
recentEmojis: [],
themes: [],
2020-03-22 01:39:12 +00:00
darkTheme: '8c539dc1-0fab-4d47-9194-39c508e9bfe1',
lightTheme: '4eea646f-7afa-4645-83e9-83af0333cd37',
darkMode: false,
syncDeviceDarkMode: true,
2020-02-06 10:11:14 +00:00
animation: true,
animatedMfm: true,
2020-02-16 13:46:18 +00:00
imageNewTab: false,
2020-02-18 18:16:10 +00:00
showFixedPostForm: false,
2020-04-13 14:46:53 +00:00
disablePagesScript: true,
2020-02-19 17:40:53 +00:00
sfxVolume: 0.3,
2020-02-19 18:14:17 +00:00
sfxNote: 'syuilo/down',
sfxNoteMy: 'syuilo/up',
2020-02-19 17:40:53 +00:00
sfxNotification: 'syuilo/pope2',
2020-02-19 18:14:17 +00:00
sfxChat: 'syuilo/pope1',
sfxChatBg: 'syuilo/waon',
2020-02-19 21:08:49 +00:00
sfxAntenna: 'syuilo/triple',
userData: {},
};
function copy<T>(data: T): T {
return JSON.parse(JSON.stringify(data));
}
2020-03-31 00:11:43 +00:00
export default () => new Vuex.Store({
plugins: [createPersistedState({
paths: ['i', 'device', 'deviceUser', 'settings', 'instance']
})],
state: {
i: null,
2020-03-31 00:11:43 +00:00
pendingApiRequestsCount: 0,
spinner: null
},
getters: {
isSignedIn: state => state.i != null,
},
mutations: {
updateI(state, x) {
state.i = x;
},
updateIKeyValue(state, x) {
state.i[x.key] = x.value;
},
},
actions: {
async login(ctx, i) {
ctx.commit('updateI', i);
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') });
},
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('device/setUserData', { userId: ctx.state.i.id, data: ctx.state.deviceUser });
ctx.commit('updateI', null);
ctx.commit('settings/init', {});
ctx.commit('deviceUser/init', {});
localStorage.removeItem('i');
document.cookie = `igi=; path=/`;
},
async switchAccount(ctx, i) {
ctx.commit('device/setUserData', { userId: ctx.state.i.id, data: ctx.state.deviceUser });
localStorage.setItem('i', i.token);
await ctx.dispatch('login', i);
},
mergeMe(ctx, me) {
for (const [key, value] of Object.entries(me)) {
ctx.commit('updateIKeyValue', { key, value });
}
if (me.clientData) {
ctx.commit('settings/init', me.clientData);
}
},
2020-03-31 00:11:43 +00: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 20:41:03 +00:00
2020-03-31 00:11:43 +00:00
const onFinally = () => {
if (--ctx.state.pendingApiRequestsCount === 0) ctx.state.spinner.parentNode.removeChild(ctx.state.spinner);
};
2020-04-01 20:41:03 +00:00
2020-03-31 00:11:43 +00: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 10:38:19 +00:00
if (token !== undefined) (data as any).i = token;
2020-04-01 20:41:03 +00:00
2020-03-31 00:11:43 +00: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 20:41:03 +00:00
2020-03-31 00:11:43 +00:00
if (res.status === 200) {
resolve(body);
} else if (res.status === 204) {
resolve();
} else {
reject(body.error);
}
}).catch(reject);
});
2020-04-01 20:41:03 +00:00
2020-03-31 00:11:43 +00:00
promise.then(onFinally, onFinally);
2020-04-01 20:41:03 +00:00
2020-03-31 00:11:43 +00:00
return promise;
}
},
modules: {
instance: {
namespaced: true,
state: {
meta: null
},
mutations: {
set(state, meta) {
state.meta = meta;
},
},
actions: {
async fetch(ctx) {
2020-03-31 00:11:43 +00:00
const meta = await ctx.dispatch('api', {
endpoint: 'meta',
data: {
detail: false
}
}, { root: true });
ctx.commit('set', meta);
}
}
},
device: {
namespaced: true,
state: defaultDeviceSettings,
mutations: {
set(state, x: { key: string; value: any }) {
state[x.key] = x.value;
},
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;
},
setTl(state, x) {
state.tl = {
src: x.src,
arg: x.arg
};
},
setVisibility(state, visibility) {
state.visibility = visibility;
},
2020-02-05 00:42:58 +00:00
setLocalOnly(state, localOnly) {
state.localOnly = localOnly;
},
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-03 23:46:54 +00:00
const w = state.widgets.find(w => w.id === x.id);
if (w) {
w.data = x.data;
}
},
}
},
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 00:11:43 +00:00
ctx.dispatch('api', {
endpoint: 'i/update-client-setting',
data: {
name: x.key,
value: x.value
}
}, { root: true });
}
},
}
}
}
});