This commit is contained in:
syuilo 2020-07-24 11:59:26 +09:00
parent 7e9cee85b3
commit d4e6af00d9
5 changed files with 268 additions and 433 deletions

View File

@ -12,9 +12,9 @@ import { deserialize } from '@syuilo/aiscript/built/serializer';
import VueHotkey from './scripts/hotkey'; import VueHotkey from './scripts/hotkey';
import Root from './root.vue'; import Root from './root.vue';
import MiOS from './mios'; import Stream from './scripts/stream';
import widgets from './widgets'; import widgets from './widgets';
import { version, langs, getLocale } from './config'; import { version, langs, getLocale, apiUrl } from './config';
import { store } from './store'; import { store } from './store';
import { router } from './router'; import { router } from './router';
import { applyTheme, lightTheme } from './scripts/theme'; import { applyTheme, lightTheme } from './scripts/theme';
@ -23,35 +23,6 @@ import { clientDb, get, count } from './db';
import { setI18nContexts } from './scripts/set-i18n-contexts'; import { setI18nContexts } from './scripts/set-i18n-contexts';
import { createPluginEnv } from './scripts/aiscript/api'; import { createPluginEnv } from './scripts/aiscript/api';
//#region Fetch locale data
const i18n = createI18n({
legacy: true,
});
await count(clientDb.i18n).then(async n => {
if (n === 0) return setI18nContexts(lang, version, i18n);
if ((await get('_version_', clientDb.i18n) !== version)) return setI18nContexts(lang, version, i18n, true);
i18n.locale = lang;
i18n.setLocaleMessage(lang, await getLocale());
});
//#endregion
const app = createApp(Root);
app.use(store);
app.use(router);
app.use(VueHotkey);
app.use(VueMeta);
app.use(VAnimateCss);
app.use(i18n);
app.component('fa', FontAwesomeIcon);
widgets(app);
//require('./directives');
//require('./components');
console.info(`Misskey v${version}`); console.info(`Misskey v${version}`);
if (localStorage.getItem('theme') == null) { if (localStorage.getItem('theme') == null) {
@ -107,9 +78,105 @@ const html = document.documentElement;
html.setAttribute('lang', lang); html.setAttribute('lang', lang);
//#endregion //#endregion
// アプリ基底要素マウント const i18n = createI18n({
legacy: true,
});
//#region Fetch user
const signout = () => {
store.dispatch('logout');
location.href = '/';
};
// ユーザーをフェッチしてコールバックする
const fetchme = (token) => new Promise((done, fail) => {
// Fetch user
fetch(`${apiUrl}/i`, {
method: 'POST',
body: JSON.stringify({
i: token
})
})
.then(res => {
// When failed to authenticate user
if (res.status !== 200 && res.status < 500) {
return signout();
}
// Parse response
res.json().then(i => {
i.token = token;
done(i);
});
})
.catch(fail);
});
// キャッシュがあったとき
if (store.state.i != null) {
// TODO: i.token が null になるケースってどんな時だっけ?
if (store.state.i.token == null) {
this.signout();
}
// 後から新鮮なデータをフェッチ
fetchme(store.state.i.token).then(freshData => {
store.dispatch('mergeMe', freshData);
});
} else {
// Get token from localStorage
let i = localStorage.getItem('i');
// 連携ログインの場合用にCookieを参照する
if (i == null || i === 'null') {
i = (document.cookie.match(/igi=(\w+)/) || [null, null])[1];
}
if (i != null && i !== 'null') {
try {
const me = await fetchme(i);
store.dispatch('login', me);
} catch (e) {
// Render the error screen
// TODO: ちゃんとしたコンポーネントをレンダリングする(v10とかのトラブルシューティングゲーム付きのやつみたいな)
document.body.innerHTML = '<div id="err">Oops!</div>';
}
}
}
//#endregion
const stream = new Stream(store.state.i);
const app = createApp(Root, {
stream
});
app.use(store);
app.use(router);
app.use(VueHotkey);
app.use(VAnimateCss);
app.use(i18n);
app.component('fa', FontAwesomeIcon);
//#region Fetch locale data
/*await count(clientDb.i18n).then(async n => {
if (n === 0) return setI18nContexts(lang, version, i18n);
if ((await get('_version_', clientDb.i18n) !== version)) return setI18nContexts(lang, version, i18n, true);
i18n.locale = lang;
i18n.setLocaleMessage(lang, await getLocale());
});*/
//#endregion
widgets(app);
//require('./directives');
//require('./components');
document.body.innerHTML = '<div id="app"></div>'; document.body.innerHTML = '<div id="app"></div>';
app.mount('#app');
// 他のタブと永続化されたstateを同期 // 他のタブと永続化されたstateを同期
window.addEventListener('storage', e => { window.addEventListener('storage', e => {
if (e.key === 'vuex') { if (e.key === 'vuex') {
@ -122,40 +189,36 @@ window.addEventListener('storage', e => {
} }
}, false); }, false);
const os = new MiOS(store); store.watch(state => state.device.darkMode, darkMode => {
os.init(async () => {
app.mount('#app');
store.watch(state => state.device.darkMode, darkMode => {
import('./scripts/theme').then(({ builtinThemes }) => { import('./scripts/theme').then(({ builtinThemes }) => {
const themes = builtinThemes.concat(store.state.device.themes); const themes = builtinThemes.concat(store.state.device.themes);
applyTheme(themes.find(x => x.id === (darkMode ? store.state.device.darkTheme : store.state.device.lightTheme))); applyTheme(themes.find(x => x.id === (darkMode ? store.state.device.darkTheme : store.state.device.lightTheme)));
}); });
}); });
//#region Sync dark mode //#region Sync dark mode
if (store.state.device.syncDeviceDarkMode) { if (store.state.device.syncDeviceDarkMode) {
store.commit('device/set', { key: 'darkMode', value: isDeviceDarkmode() }); store.commit('device/set', { key: 'darkMode', value: isDeviceDarkmode() });
} }
window.matchMedia('(prefers-color-scheme: dark)').addListener(mql => { window.matchMedia('(prefers-color-scheme: dark)').addListener(mql => {
if (store.state.device.syncDeviceDarkMode) { if (store.state.device.syncDeviceDarkMode) {
store.commit('device/set', { key: 'darkMode', value: mql.matches }); store.commit('device/set', { key: 'darkMode', value: mql.matches });
} }
}); });
//#endregion //#endregion
store.watch(state => state.device.useBlurEffectForModal, v => { store.watch(state => state.device.useBlurEffectForModal, v => {
document.documentElement.style.setProperty('--modalBgFilter', v ? 'blur(4px)' : 'none'); document.documentElement.style.setProperty('--modalBgFilter', v ? 'blur(4px)' : 'none');
}, { immediate: true }); }, { immediate: true });
os.stream.on('emojiAdded', data => {
stream.on('emojiAdded', data => {
// TODO // TODO
//store.commit('instance/set', ); //store.commit('instance/set', );
}); });
for (const plugin of store.state.deviceUser.plugins) { for (const plugin of store.state.deviceUser.plugins) {
console.info('Plugin installed:', plugin.name, 'v' + plugin.version); console.info('Plugin installed:', plugin.name, 'v' + plugin.version);
const aiscript = new AiScript(createPluginEnv(app, { const aiscript = new AiScript(createPluginEnv(app, {
@ -182,9 +245,9 @@ os.init(async () => {
store.commit('initPlugin', { plugin, aiscript }); store.commit('initPlugin', { plugin, aiscript });
aiscript.exec(deserialize(plugin.ast)); aiscript.exec(deserialize(plugin.ast));
} }
if (store.getters.isSignedIn) { if (store.getters.isSignedIn) {
if ('Notification' in window) { if ('Notification' in window) {
// 許可を得ていなかったらリクエスト // 許可を得ていなかったらリクエスト
if (Notification.permission === 'default') { if (Notification.permission === 'default') {
@ -192,7 +255,7 @@ os.init(async () => {
} }
} }
const main = os.stream.useSharedConnection('main'); const main = stream.useSharedConnection('main');
// 自分の情報が更新されたとき // 自分の情報が更新されたとき
main.on('meUpdated', i => { main.on('meUpdated', i => {
@ -279,7 +342,7 @@ os.init(async () => {
// トークンが再生成されたとき // トークンが再生成されたとき
// このままではMisskeyが利用できないので強制的にサインアウトさせる // このままではMisskeyが利用できないので強制的にサインアウトさせる
main.on('myTokenRegenerated', () => { main.on('myTokenRegenerated', () => {
os.signout(); signout();
}); });
} }
});

View File

@ -1,236 +0,0 @@
// TODO: このファイル消したい
import autobind from 'autobind-decorator';
import { EventEmitter } from 'eventemitter3';
import { apiUrl, version } from './config';
import Progress from './scripts/loading';
import Stream from './scripts/stream';
import store from './store';
/**
* Misskey Operating System
*/
export default class MiOS extends EventEmitter {
public store: ReturnType<typeof store>;
/**
* A connection manager of home stream
*/
public stream: Stream;
/**
* A registration of service worker
*/
private swRegistration: ServiceWorkerRegistration = null;
constructor(vuex: MiOS['store']) {
super();
this.store = vuex;
}
@autobind
public signout() {
this.store.dispatch('logout');
location.href = '/';
}
/**
* Initialize MiOS (boot)
* @param callback A function that call when initialized
*/
@autobind
public async init(callback) {
const finish = () => {
callback();
this.store.dispatch('instance/fetch').then(() => {
// Init service worker
if (this.store.state.instance.meta.swPublickey) this.registerSw(this.store.state.instance.meta.swPublickey);
});
};
// ユーザーをフェッチしてコールバックする
const fetchme = (token, cb) => {
let me = null;
// Return when not signed in
if (token == null || token === 'null') {
return done();
}
// Fetch user
fetch(`${apiUrl}/i`, {
method: 'POST',
body: JSON.stringify({
i: token
})
})
// When success
.then(res => {
// When failed to authenticate user
if (res.status !== 200 && res.status < 500) {
return this.signout();
}
// Parse response
res.json().then(i => {
me = i;
me.token = token;
done();
});
})
// When failure
.catch(() => {
// Render the error screen
document.body.innerHTML = '<div id="err">Oops!</div>';
Progress.done();
});
function done() {
if (cb) cb(me);
}
};
// フェッチが完了したとき
const fetched = () => {
this.emit('signedin');
this.initStream();
// Finish init
finish();
};
// キャッシュがあったとき
if (this.store.state.i != null) {
if (this.store.state.i.token == null) {
this.signout();
return;
}
// とりあえずキャッシュされたデータでお茶を濁して(?)おいて、
fetched();
// 後から新鮮なデータをフェッチ
fetchme(this.store.state.i.token, freshData => {
this.store.dispatch('mergeMe', freshData);
});
} else {
// Get token from localStorage
let i = localStorage.getItem('i');
// 連携ログインの場合用にCookieを参照する
if (i == null || i === 'null') {
i = (document.cookie.match(/igi=(\w+)/) || [null, null])[1];
}
fetchme(i, me => {
if (me) {
this.store.dispatch('login', me);
fetched();
} else {
this.initStream();
// Finish init
finish();
}
});
}
}
@autobind
private initStream() {
this.stream = new Stream(this);
}
/**
* Register service worker
*/
@autobind
private registerSw(swPublickey: string) {
// Check whether service worker and push manager supported
const isSwSupported =
('serviceWorker' in navigator) && ('PushManager' in window);
// Reject when browser not service worker supported
if (!isSwSupported) return;
// Reject when not signed in to Misskey
if (!this.store.getters.isSignedIn) return;
// When service worker activated
navigator.serviceWorker.ready.then(registration => {
this.swRegistration = registration;
// Options of pushManager.subscribe
// SEE: https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe#Parameters
const opts = {
// A boolean indicating that the returned push subscription
// will only be used for messages whose effect is made visible to the user.
userVisibleOnly: true,
// A public key your push server will use to send
// messages to client apps via a push server.
applicationServerKey: urlBase64ToUint8Array(swPublickey)
};
// Subscribe push notification
this.swRegistration.pushManager.subscribe(opts).then(subscription => {
function encode(buffer: ArrayBuffer) {
return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
}
// Register
this.store.dispatch('api', {
endpoint: 'sw/register',
data: {
endpoint: subscription.endpoint,
auth: encode(subscription.getKey('auth')),
publickey: encode(subscription.getKey('p256dh'))
}
});
})
// When subscribe failed
.catch(async (err: Error) => {
// 通知が許可されていなかったとき
if (err.name === 'NotAllowedError') {
return;
}
// 違うapplicationServerKey (または gcm_sender_id)のサブスクリプションが
// 既に存在していることが原因でエラーになった可能性があるので、
// そのサブスクリプションを解除しておく
const subscription = await this.swRegistration.pushManager.getSubscription();
if (subscription) subscription.unsubscribe();
});
});
// The path of service worker script
const sw = `/sw.${version}.js`;
// Register service worker
navigator.serviceWorker.register(sw);
}
}
/**
* Convert the URL safe base64 string to a Uint8Array
* @param base64String base64 string
*/
function urlBase64ToUint8Array(base64String: string): Uint8Array {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}

View File

@ -1,5 +1,6 @@
<template> <template>
<component :is="deck ? DeckUI : DefaultUI"/> <DeckUI v-if="deckmode"/>
<DefaultUI v-else/>
<!-- Render modals here --> <!-- Render modals here -->
</template> </template>
@ -20,10 +21,20 @@ export default defineComponent({
titleTemplate: title => title ? `${title} | ${(instanceName || 'Misskey')}` : (instanceName || 'Misskey') titleTemplate: title => title ? `${title} | ${(instanceName || 'Misskey')}` : (instanceName || 'Misskey')
}, },
props: {
stream: {
},
isMobile: {
type: Boolean,
required: false,
default: false,
}
},
data() { data() {
return { return {
stream: os.stream, deckmode
isMobile: isMobile,
}; };
}, },

View File

@ -1,8 +1,8 @@
import VueI18n from 'vue-i18n'; import { I18n } from 'vue-i18n';
import { clientDb, clear, bulkSet } from '../db'; import { clientDb, clear, bulkSet } from '../db';
import { deepEntries, delimitEntry } from 'deep-entries'; import { deepEntries, delimitEntry } from 'deep-entries';
export function setI18nContexts(lang: string, version: string, i18n: VueI18n, cleardb = false) { export function setI18nContexts(lang: string, version: string, i18n: I18n, cleardb = false) {
return Promise.all([ return Promise.all([
cleardb ? clear(clientDb.i18n) : Promise.resolve(), cleardb ? clear(clientDb.i18n) : Promise.resolve(),
fetch(`/assets/locales/${lang}.${version}.json`) fetch(`/assets/locales/${lang}.${version}.json`)

View File

@ -2,7 +2,6 @@ import autobind from 'autobind-decorator';
import { EventEmitter } from 'eventemitter3'; import { EventEmitter } from 'eventemitter3';
import ReconnectingWebsocket from 'reconnecting-websocket'; import ReconnectingWebsocket from 'reconnecting-websocket';
import { wsUrl } from '../config'; import { wsUrl } from '../config';
import MiOS from '../mios';
/** /**
* Misskey stream connection * Misskey stream connection
@ -14,13 +13,11 @@ export default class Stream extends EventEmitter {
private sharedConnections: SharedConnection[] = []; private sharedConnections: SharedConnection[] = [];
private nonSharedConnections: NonSharedConnection[] = []; private nonSharedConnections: NonSharedConnection[] = [];
constructor(os: MiOS) { constructor(user) {
super(); super();
this.state = 'initializing'; this.state = 'initializing';
const user = os.store.state.i;
this.stream = new ReconnectingWebsocket(wsUrl + (user ? `?i=${user.token}` : ''), '', { minReconnectionDelay: 1 }); // https://github.com/pladaria/reconnecting-websocket/issues/91 this.stream = new ReconnectingWebsocket(wsUrl + (user ? `?i=${user.token}` : ''), '', { minReconnectionDelay: 1 }); // https://github.com/pladaria/reconnecting-websocket/issues/91
this.stream.addEventListener('open', this.onOpen); this.stream.addEventListener('open', this.onOpen);
this.stream.addEventListener('close', this.onClose); this.stream.addEventListener('close', this.onClose);