diff --git a/src/client/init.ts b/src/client/init.ts index 9081238af2..75c2be2507 100644 --- a/src/client/init.ts +++ b/src/client/init.ts @@ -12,9 +12,9 @@ import { deserialize } from '@syuilo/aiscript/built/serializer'; import VueHotkey from './scripts/hotkey'; import Root from './root.vue'; -import MiOS from './mios'; +import Stream from './scripts/stream'; import widgets from './widgets'; -import { version, langs, getLocale } from './config'; +import { version, langs, getLocale, apiUrl } from './config'; import { store } from './store'; import { router } from './router'; import { applyTheme, lightTheme } from './scripts/theme'; @@ -23,35 +23,6 @@ import { clientDb, get, count } from './db'; import { setI18nContexts } from './scripts/set-i18n-contexts'; 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}`); if (localStorage.getItem('theme') == null) { @@ -107,9 +78,105 @@ const html = document.documentElement; html.setAttribute('lang', lang); //#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 = '
Oops!
'; + } + } +} +//#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 = '
'; +app.mount('#app'); + // 他のタブと永続化されたstateを同期 window.addEventListener('storage', e => { if (e.key === 'vuex') { @@ -122,164 +189,160 @@ window.addEventListener('storage', e => { } }, false); -const os = new MiOS(store); - -os.init(async () => { - app.mount('#app'); - - store.watch(state => state.device.darkMode, darkMode => { - import('./scripts/theme').then(({ builtinThemes }) => { - const themes = builtinThemes.concat(store.state.device.themes); - applyTheme(themes.find(x => x.id === (darkMode ? store.state.device.darkTheme : store.state.device.lightTheme))); - }); +store.watch(state => state.device.darkMode, darkMode => { + import('./scripts/theme').then(({ builtinThemes }) => { + const themes = builtinThemes.concat(store.state.device.themes); + 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) { + store.commit('device/set', { key: 'darkMode', value: isDeviceDarkmode() }); +} + +window.matchMedia('(prefers-color-scheme: dark)').addListener(mql => { if (store.state.device.syncDeviceDarkMode) { - store.commit('device/set', { key: 'darkMode', value: isDeviceDarkmode() }); - } - - window.matchMedia('(prefers-color-scheme: dark)').addListener(mql => { - if (store.state.device.syncDeviceDarkMode) { - store.commit('device/set', { key: 'darkMode', value: mql.matches }); - } - }); - //#endregion - - store.watch(state => state.device.useBlurEffectForModal, v => { - document.documentElement.style.setProperty('--modalBgFilter', v ? 'blur(4px)' : 'none'); - }, { immediate: true }); - - os.stream.on('emojiAdded', data => { - // TODO - //store.commit('instance/set', ); - }); - - for (const plugin of store.state.deviceUser.plugins) { - console.info('Plugin installed:', plugin.name, 'v' + plugin.version); - - const aiscript = new AiScript(createPluginEnv(app, { - plugin: plugin, - storageKey: 'plugins:' + plugin.id - }), { - in: (q) => { - return new Promise(ok => { - app.dialog({ - title: q, - input: {} - }).then(({ canceled, result: a }) => { - ok(a); - }); - }); - }, - out: (value) => { - console.log(value); - }, - log: (type, params) => { - }, - }); - - store.commit('initPlugin', { plugin, aiscript }); - - aiscript.exec(deserialize(plugin.ast)); - } - - if (store.getters.isSignedIn) { - if ('Notification' in window) { - // 許可を得ていなかったらリクエスト - if (Notification.permission === 'default') { - Notification.requestPermission(); - } - } - - const main = os.stream.useSharedConnection('main'); - - // 自分の情報が更新されたとき - main.on('meUpdated', i => { - store.dispatch('mergeMe', i); - }); - - main.on('readAllNotifications', () => { - store.dispatch('mergeMe', { - hasUnreadNotification: false - }); - }); - - main.on('unreadNotification', () => { - store.dispatch('mergeMe', { - hasUnreadNotification: true - }); - }); - - main.on('unreadMention', () => { - store.dispatch('mergeMe', { - hasUnreadMentions: true - }); - }); - - main.on('readAllUnreadMentions', () => { - store.dispatch('mergeMe', { - hasUnreadMentions: false - }); - }); - - main.on('unreadSpecifiedNote', () => { - store.dispatch('mergeMe', { - hasUnreadSpecifiedNotes: true - }); - }); - - main.on('readAllUnreadSpecifiedNotes', () => { - store.dispatch('mergeMe', { - hasUnreadSpecifiedNotes: false - }); - }); - - main.on('readAllMessagingMessages', () => { - store.dispatch('mergeMe', { - hasUnreadMessagingMessage: false - }); - }); - - main.on('unreadMessagingMessage', () => { - store.dispatch('mergeMe', { - hasUnreadMessagingMessage: true - }); - - app.sound('chatBg'); - }); - - main.on('readAllAntennas', () => { - store.dispatch('mergeMe', { - hasUnreadAntenna: false - }); - }); - - main.on('unreadAntenna', () => { - store.dispatch('mergeMe', { - hasUnreadAntenna: true - }); - - app.sound('antenna'); - }); - - main.on('readAllAnnouncements', () => { - store.dispatch('mergeMe', { - hasUnreadAnnouncement: false - }); - }); - - main.on('clientSettingUpdated', x => { - store.commit('settings/set', { - key: x.key, - value: x.value - }); - }); - - // トークンが再生成されたとき - // このままではMisskeyが利用できないので強制的にサインアウトさせる - main.on('myTokenRegenerated', () => { - os.signout(); - }); + store.commit('device/set', { key: 'darkMode', value: mql.matches }); } }); +//#endregion + +store.watch(state => state.device.useBlurEffectForModal, v => { + document.documentElement.style.setProperty('--modalBgFilter', v ? 'blur(4px)' : 'none'); +}, { immediate: true }); + + +stream.on('emojiAdded', data => { + // TODO + //store.commit('instance/set', ); +}); + +for (const plugin of store.state.deviceUser.plugins) { + console.info('Plugin installed:', plugin.name, 'v' + plugin.version); + + const aiscript = new AiScript(createPluginEnv(app, { + plugin: plugin, + storageKey: 'plugins:' + plugin.id + }), { + in: (q) => { + return new Promise(ok => { + app.dialog({ + title: q, + input: {} + }).then(({ canceled, result: a }) => { + ok(a); + }); + }); + }, + out: (value) => { + console.log(value); + }, + log: (type, params) => { + }, + }); + + store.commit('initPlugin', { plugin, aiscript }); + + aiscript.exec(deserialize(plugin.ast)); +} + +if (store.getters.isSignedIn) { + if ('Notification' in window) { + // 許可を得ていなかったらリクエスト + if (Notification.permission === 'default') { + Notification.requestPermission(); + } + } + + const main = stream.useSharedConnection('main'); + + // 自分の情報が更新されたとき + main.on('meUpdated', i => { + store.dispatch('mergeMe', i); + }); + + main.on('readAllNotifications', () => { + store.dispatch('mergeMe', { + hasUnreadNotification: false + }); + }); + + main.on('unreadNotification', () => { + store.dispatch('mergeMe', { + hasUnreadNotification: true + }); + }); + + main.on('unreadMention', () => { + store.dispatch('mergeMe', { + hasUnreadMentions: true + }); + }); + + main.on('readAllUnreadMentions', () => { + store.dispatch('mergeMe', { + hasUnreadMentions: false + }); + }); + + main.on('unreadSpecifiedNote', () => { + store.dispatch('mergeMe', { + hasUnreadSpecifiedNotes: true + }); + }); + + main.on('readAllUnreadSpecifiedNotes', () => { + store.dispatch('mergeMe', { + hasUnreadSpecifiedNotes: false + }); + }); + + main.on('readAllMessagingMessages', () => { + store.dispatch('mergeMe', { + hasUnreadMessagingMessage: false + }); + }); + + main.on('unreadMessagingMessage', () => { + store.dispatch('mergeMe', { + hasUnreadMessagingMessage: true + }); + + app.sound('chatBg'); + }); + + main.on('readAllAntennas', () => { + store.dispatch('mergeMe', { + hasUnreadAntenna: false + }); + }); + + main.on('unreadAntenna', () => { + store.dispatch('mergeMe', { + hasUnreadAntenna: true + }); + + app.sound('antenna'); + }); + + main.on('readAllAnnouncements', () => { + store.dispatch('mergeMe', { + hasUnreadAnnouncement: false + }); + }); + + main.on('clientSettingUpdated', x => { + store.commit('settings/set', { + key: x.key, + value: x.value + }); + }); + + // トークンが再生成されたとき + // このままではMisskeyが利用できないので強制的にサインアウトさせる + main.on('myTokenRegenerated', () => { + signout(); + }); +} + diff --git a/src/client/mios.ts b/src/client/mios.ts deleted file mode 100644 index efeb630d7e..0000000000 --- a/src/client/mios.ts +++ /dev/null @@ -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; - - /** - * 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 = '
Oops!
'; - - 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; -} diff --git a/src/client/root.vue b/src/client/root.vue index ef30ac2499..a460dd95a0 100644 --- a/src/client/root.vue +++ b/src/client/root.vue @@ -1,5 +1,6 @@ @@ -20,10 +21,20 @@ export default defineComponent({ titleTemplate: title => title ? `${title} | ${(instanceName || 'Misskey')}` : (instanceName || 'Misskey') }, + props: { + stream: { + + }, + isMobile: { + type: Boolean, + required: false, + default: false, + } + }, + data() { return { - stream: os.stream, - isMobile: isMobile, + deckmode }; }, diff --git a/src/client/scripts/set-i18n-contexts.ts b/src/client/scripts/set-i18n-contexts.ts index 872153e0bd..acd12386fd 100644 --- a/src/client/scripts/set-i18n-contexts.ts +++ b/src/client/scripts/set-i18n-contexts.ts @@ -1,8 +1,8 @@ -import VueI18n from 'vue-i18n'; +import { I18n } from 'vue-i18n'; import { clientDb, clear, bulkSet } from '../db'; 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([ cleardb ? clear(clientDb.i18n) : Promise.resolve(), fetch(`/assets/locales/${lang}.${version}.json`) diff --git a/src/client/scripts/stream.ts b/src/client/scripts/stream.ts index 4dcd3f1b2e..7823c9af7b 100644 --- a/src/client/scripts/stream.ts +++ b/src/client/scripts/stream.ts @@ -2,7 +2,6 @@ import autobind from 'autobind-decorator'; import { EventEmitter } from 'eventemitter3'; import ReconnectingWebsocket from 'reconnecting-websocket'; import { wsUrl } from '../config'; -import MiOS from '../mios'; /** * Misskey stream connection @@ -14,13 +13,11 @@ export default class Stream extends EventEmitter { private sharedConnections: SharedConnection[] = []; private nonSharedConnections: NonSharedConnection[] = []; - constructor(os: MiOS) { + constructor(user) { super(); 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.addEventListener('open', this.onOpen); this.stream.addEventListener('close', this.onClose);