2020-01-29 21:37:25 +02:00
|
|
|
/**
|
2020-07-11 04:13:11 +03:00
|
|
|
* Client entry point
|
2020-01-29 21:37:25 +02:00
|
|
|
*/
|
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
import '@/style.scss';
|
|
|
|
|
|
|
|
import { createApp } from 'vue';
|
2020-01-29 21:37:25 +02:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
|
2020-10-17 14:12:00 +03:00
|
|
|
|
|
|
|
import Root from './root.vue';
|
|
|
|
import widgets from './widgets';
|
|
|
|
import directives from './directives';
|
|
|
|
import components from '@/components';
|
|
|
|
import { version, apiUrl } from '@/config';
|
|
|
|
import { store } from './store';
|
2020-01-29 21:37:25 +02:00
|
|
|
import { router } from './router';
|
2020-10-17 14:12:00 +03:00
|
|
|
import { applyTheme } from '@/scripts/theme';
|
|
|
|
import { isDeviceDarkmode } from '@/scripts/is-device-darkmode';
|
|
|
|
import { i18n, lang } from './i18n';
|
|
|
|
import { stream, sound, isMobile, dialog } from '@/os';
|
2020-01-29 21:37:25 +02:00
|
|
|
|
|
|
|
console.info(`Misskey v${version}`);
|
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
if (_DEV_) {
|
|
|
|
console.warn('Development mode!!!');
|
|
|
|
|
|
|
|
window.addEventListener('error', event => {
|
|
|
|
console.error(event);
|
|
|
|
/*
|
|
|
|
dialog({
|
|
|
|
type: 'error',
|
|
|
|
title: 'DEV: Unhandled error',
|
|
|
|
text: event.message
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener('unhandledrejection', event => {
|
|
|
|
console.error(event);
|
|
|
|
/*
|
|
|
|
dialog({
|
|
|
|
type: 'error',
|
|
|
|
title: 'DEV: Unhandled promise rejection',
|
|
|
|
text: event.reason
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// タッチデバイスでCSSの:hoverを機能させる
|
|
|
|
document.addEventListener('touchend', () => {}, { passive: true });
|
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
if (localStorage.getItem('theme') == null) {
|
2020-10-19 07:17:11 +03:00
|
|
|
applyTheme(require('@/themes/l-white.json5'));
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 12:22:19 +03:00
|
|
|
//#region SEE: https://css-tricks.com/the-trick-to-viewport-units-on-mobile/
|
|
|
|
// TODO: いつの日にか消したい
|
|
|
|
const vh = window.innerHeight * 0.01;
|
|
|
|
document.documentElement.style.setProperty('--vh', `${vh}px`);
|
|
|
|
window.addEventListener('resize', () => {
|
|
|
|
const vh = window.innerHeight * 0.01;
|
|
|
|
document.documentElement.style.setProperty('--vh', `${vh}px`);
|
|
|
|
});
|
|
|
|
//#endregion
|
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
// Get the <head> element
|
|
|
|
const head = document.getElementsByTagName('head')[0];
|
|
|
|
|
|
|
|
// If mobile, insert the viewport meta tag
|
|
|
|
if (isMobile || window.innerWidth <= 1024) {
|
2020-03-04 04:45:33 +02:00
|
|
|
const viewport = document.getElementsByName('viewport').item(0);
|
2020-01-29 21:37:25 +02:00
|
|
|
viewport.setAttribute('content',
|
|
|
|
`${viewport.getAttribute('content')},minimum-scale=1,maximum-scale=1,user-scalable=no`);
|
|
|
|
head.appendChild(viewport);
|
|
|
|
}
|
|
|
|
|
|
|
|
//#region Set lang attr
|
|
|
|
const html = document.documentElement;
|
|
|
|
html.setAttribute('lang', lang);
|
|
|
|
//#endregion
|
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
//#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) {
|
|
|
|
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 {
|
|
|
|
document.body.innerHTML = '<div>Please wait...</div>';
|
|
|
|
const me = await fetchme(i);
|
|
|
|
await store.dispatch('login', me);
|
|
|
|
location.reload();
|
|
|
|
} catch (e) {
|
|
|
|
// Render the error screen
|
|
|
|
// TODO: ちゃんとしたコンポーネントをレンダリングする(v10とかのトラブルシューティングゲーム付きのやつみたいな)
|
|
|
|
document.body.innerHTML = '<div id="err">Oops!</div>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
store.dispatch('instance/fetch').then(() => {
|
|
|
|
// Init service worker
|
|
|
|
//if (this.store.state.instance.meta.swPublickey) this.registerSw(this.store.state.instance.meta.swPublickey);
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.init(store.state.i);
|
|
|
|
|
|
|
|
const app = createApp(Root);
|
|
|
|
|
|
|
|
if (_DEV_) {
|
|
|
|
app.config.performance = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.use(store);
|
|
|
|
app.use(router);
|
|
|
|
app.use(i18n);
|
|
|
|
// eslint-disable-next-line vue/component-definition-name-casing
|
|
|
|
app.component('Fa', FontAwesomeIcon);
|
|
|
|
|
|
|
|
widgets(app);
|
|
|
|
directives(app);
|
|
|
|
components(app);
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
await router.isReady();
|
|
|
|
|
|
|
|
//document.body.innerHTML = '<div id="app"></div>';
|
|
|
|
|
|
|
|
app.mount('body');
|
2020-03-31 03:11:43 +03:00
|
|
|
|
2020-07-15 12:03:08 +03:00
|
|
|
// 他のタブと永続化されたstateを同期
|
2020-07-12 12:14:59 +03:00
|
|
|
window.addEventListener('storage', e => {
|
|
|
|
if (e.key === 'vuex') {
|
2020-07-15 12:03:08 +03:00
|
|
|
store.replaceState({
|
|
|
|
...store.state,
|
|
|
|
...JSON.parse(e.newValue)
|
|
|
|
});
|
2020-07-12 12:14:59 +03:00
|
|
|
} else if (e.key === 'i') {
|
|
|
|
location.reload();
|
2020-03-22 13:23:35 +02:00
|
|
|
}
|
2020-07-12 12:14:59 +03:00
|
|
|
}, false);
|
2020-03-22 13:23:35 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
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)));
|
2020-05-23 07:19:31 +03:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
//#region Sync dark mode
|
|
|
|
if (store.state.device.syncDeviceDarkMode) {
|
|
|
|
store.commit('device/set', { key: 'darkMode', value: isDeviceDarkmode() });
|
|
|
|
}
|
2020-07-12 12:14:59 +03:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
window.matchMedia('(prefers-color-scheme: dark)').addListener(mql => {
|
2020-07-12 12:14:59 +03:00
|
|
|
if (store.state.device.syncDeviceDarkMode) {
|
2020-10-17 14:12:00 +03:00
|
|
|
store.commit('device/set', { key: 'darkMode', value: mql.matches });
|
2020-07-12 12:14:59 +03:00
|
|
|
}
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
|
|
|
//#endregion
|
2020-07-12 12:14:59 +03:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
store.watch(state => state.device.useBlurEffectForModal, v => {
|
|
|
|
document.documentElement.style.setProperty('--modalBgFilter', v ? 'blur(4px)' : 'none');
|
|
|
|
}, { immediate: true });
|
2020-07-12 12:14:59 +03:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
let reloadDialogShowing = false;
|
|
|
|
stream.on('_disconnected_', async () => {
|
|
|
|
if (store.state.device.serverDisconnectedBehavior === 'reload') {
|
|
|
|
location.reload();
|
|
|
|
} else if (store.state.device.serverDisconnectedBehavior === 'dialog') {
|
|
|
|
if (reloadDialogShowing) return;
|
|
|
|
reloadDialogShowing = true;
|
|
|
|
const { canceled } = await dialog({
|
|
|
|
type: 'warning',
|
|
|
|
title: i18n.global.t('disconnectedFromServer'),
|
|
|
|
text: i18n.global.t('reloadConfirm'),
|
|
|
|
showCancelButton: true
|
|
|
|
});
|
|
|
|
reloadDialogShowing = false;
|
|
|
|
if (!canceled) {
|
2020-08-19 15:47:18 +03:00
|
|
|
location.reload();
|
|
|
|
}
|
2020-10-17 14:12:00 +03:00
|
|
|
}
|
|
|
|
});
|
2020-04-02 16:17:17 +03:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
stream.on('emojiAdded', data => {
|
|
|
|
// TODO
|
|
|
|
//store.commit('instance/set', );
|
|
|
|
});
|
2020-07-11 18:38:55 +03:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
for (const plugin of store.state.deviceUser.plugins.filter(p => p.active)) {
|
|
|
|
import('./plugin').then(({ install }) => {
|
|
|
|
install(plugin);
|
|
|
|
});
|
|
|
|
}
|
2020-07-11 18:38:55 +03:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
if (store.getters.isSignedIn) {
|
|
|
|
if ('Notification' in window) {
|
|
|
|
// 許可を得ていなかったらリクエスト
|
|
|
|
if (Notification.permission === 'default') {
|
|
|
|
Notification.requestPermission();
|
|
|
|
}
|
2020-07-11 18:38:55 +03:00
|
|
|
}
|
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
const main = stream.useSharedConnection('main');
|
2020-07-12 12:14:59 +03:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
// 自分の情報が更新されたとき
|
|
|
|
main.on('meUpdated', i => {
|
|
|
|
store.dispatch('mergeMe', i);
|
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('readAllNotifications', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadNotification: false
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('unreadNotification', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadNotification: true
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('unreadMention', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadMentions: true
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('readAllUnreadMentions', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadMentions: false
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('unreadSpecifiedNote', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadSpecifiedNotes: true
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('readAllUnreadSpecifiedNotes', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadSpecifiedNotes: false
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('readAllMessagingMessages', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadMessagingMessage: false
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('unreadMessagingMessage', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadMessagingMessage: true
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
sound('chatBg');
|
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('readAllAntennas', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadAntenna: false
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('unreadAntenna', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadAntenna: true
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
sound('antenna');
|
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('readAllAnnouncements', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadAnnouncement: false
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('readAllChannels', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadChannel: false
|
2020-08-18 16:44:21 +03:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-08-18 16:44:21 +03:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('unreadChannel', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadChannel: true
|
2020-08-18 16:44:21 +03:00
|
|
|
});
|
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
sound('channel');
|
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('readAllAnnouncements', () => {
|
|
|
|
store.dispatch('mergeMe', {
|
|
|
|
hasUnreadAnnouncement: false
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
2020-02-19 23:08:49 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
main.on('clientSettingUpdated', x => {
|
|
|
|
store.commit('settings/set', {
|
|
|
|
key: x.key,
|
|
|
|
value: x.value
|
2020-02-19 23:08:49 +02:00
|
|
|
});
|
2020-10-17 14:12:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// トークンが再生成されたとき
|
|
|
|
// このままではMisskeyが利用できないので強制的にサインアウトさせる
|
|
|
|
main.on('myTokenRegenerated', () => {
|
|
|
|
signout();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|