2017-05-17 20:06:55 +00:00
|
|
|
/**
|
|
|
|
* App initializer
|
|
|
|
*/
|
|
|
|
|
2017-10-26 00:02:40 +00:00
|
|
|
'use strict';
|
2017-05-17 20:06:55 +00:00
|
|
|
|
|
|
|
import * as riot from 'riot';
|
|
|
|
import api from './common/scripts/api';
|
|
|
|
import signout from './common/scripts/signout';
|
|
|
|
import checkForUpdate from './common/scripts/check-for-update';
|
2017-06-08 16:03:54 +00:00
|
|
|
import Connection from './common/scripts/home-stream';
|
2017-11-12 18:47:06 +00:00
|
|
|
import ServerStreamManager from './common/scripts/server-stream-manager.ts';
|
2017-06-11 12:26:09 +00:00
|
|
|
import Progress from './common/scripts/loading';
|
2017-05-17 20:06:55 +00:00
|
|
|
import mixin from './common/mixins';
|
|
|
|
import CONFIG from './common/scripts/config';
|
|
|
|
require('./common/tags');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* APP ENTRY POINT!
|
|
|
|
*/
|
|
|
|
|
2017-10-25 11:48:52 +00:00
|
|
|
console.info(`Misskey v${VERSION} (葵 aoi)`);
|
2017-05-17 20:06:55 +00:00
|
|
|
|
2017-11-03 08:46:42 +00:00
|
|
|
{ // Set lang attr
|
|
|
|
const html = document.documentElement;
|
|
|
|
html.setAttribute('lang', LANG);
|
|
|
|
}
|
|
|
|
|
2017-10-26 00:02:40 +00:00
|
|
|
{ // Set description meta tag
|
|
|
|
const head = document.getElementsByTagName('head')[0];
|
|
|
|
const meta = document.createElement('meta');
|
|
|
|
meta.setAttribute('name', 'description');
|
|
|
|
meta.setAttribute('content', '%i18n:common.misskey%');
|
|
|
|
head.appendChild(meta);
|
|
|
|
}
|
|
|
|
|
2017-05-17 20:06:55 +00:00
|
|
|
document.domain = CONFIG.host;
|
|
|
|
|
|
|
|
// Set global configuration
|
|
|
|
riot.mixin({ CONFIG });
|
|
|
|
|
|
|
|
// ↓ NodeList、HTMLCollection、FileList、DataTransferItemListで forEach を使えるようにする
|
|
|
|
if (NodeList.prototype.forEach === undefined) {
|
|
|
|
NodeList.prototype.forEach = Array.prototype.forEach;
|
|
|
|
}
|
|
|
|
if (HTMLCollection.prototype.forEach === undefined) {
|
|
|
|
HTMLCollection.prototype.forEach = Array.prototype.forEach;
|
|
|
|
}
|
|
|
|
if (FileList.prototype.forEach === undefined) {
|
|
|
|
FileList.prototype.forEach = Array.prototype.forEach;
|
|
|
|
}
|
|
|
|
if (window.DataTransferItemList && DataTransferItemList.prototype.forEach === undefined) {
|
|
|
|
DataTransferItemList.prototype.forEach = Array.prototype.forEach;
|
|
|
|
}
|
|
|
|
|
|
|
|
// iOSでプライベートモードだとlocalStorageが使えないので既存のメソッドを上書きする
|
|
|
|
try {
|
|
|
|
localStorage.setItem('kyoppie', 'yuppie');
|
|
|
|
} catch (e) {
|
|
|
|
Storage.prototype.setItem = () => { }; // noop
|
|
|
|
}
|
|
|
|
|
|
|
|
// クライアントを更新すべきならする
|
|
|
|
if (localStorage.getItem('should-refresh') == 'true') {
|
|
|
|
localStorage.removeItem('should-refresh');
|
|
|
|
location.reload(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新チェック
|
|
|
|
setTimeout(checkForUpdate, 3000);
|
|
|
|
|
|
|
|
// ユーザーをフェッチしてコールバックする
|
|
|
|
export default callback => {
|
|
|
|
// Get cached account data
|
|
|
|
let cachedMe = JSON.parse(localStorage.getItem('me'));
|
|
|
|
|
|
|
|
if (cachedMe) {
|
|
|
|
fetched(cachedMe);
|
|
|
|
|
|
|
|
// 後から新鮮なデータをフェッチ
|
|
|
|
fetchme(cachedMe.token, freshData => {
|
|
|
|
Object.assign(cachedMe, freshData);
|
|
|
|
cachedMe.trigger('updated');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Get token from cookie
|
|
|
|
const i = (document.cookie.match(/i=(!\w+)/) || [null, null])[1];
|
|
|
|
|
|
|
|
fetchme(i, fetched);
|
|
|
|
}
|
|
|
|
|
|
|
|
// フェッチが完了したとき
|
|
|
|
function fetched(me) {
|
|
|
|
if (me) {
|
|
|
|
riot.observable(me);
|
|
|
|
|
|
|
|
// この me オブジェクトを更新するメソッド
|
|
|
|
me.update = data => {
|
|
|
|
if (data) Object.assign(me, data);
|
|
|
|
me.trigger('updated');
|
|
|
|
};
|
|
|
|
|
|
|
|
// ローカルストレージにキャッシュ
|
|
|
|
localStorage.setItem('me', JSON.stringify(me));
|
|
|
|
|
|
|
|
me.on('updated', () => {
|
|
|
|
// キャッシュ更新
|
|
|
|
localStorage.setItem('me', JSON.stringify(me));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-08 16:03:54 +00:00
|
|
|
// Init home stream connection
|
2017-06-06 15:04:28 +00:00
|
|
|
const stream = me ? new Connection(me) : null;
|
|
|
|
|
2017-11-12 18:47:06 +00:00
|
|
|
// Init server stream connection manager
|
|
|
|
const serverStreamManager = new ServerStreamManager();
|
|
|
|
|
2017-05-17 20:06:55 +00:00
|
|
|
// ミックスイン初期化
|
2017-11-12 18:47:06 +00:00
|
|
|
mixin(me, stream, serverStreamManager);
|
2017-05-17 20:06:55 +00:00
|
|
|
|
|
|
|
// ローディング画面クリア
|
|
|
|
const ini = document.getElementById('ini');
|
|
|
|
ini.parentNode.removeChild(ini);
|
|
|
|
|
|
|
|
// アプリ基底要素マウント
|
|
|
|
const app = document.createElement('div');
|
|
|
|
app.setAttribute('id', 'app');
|
|
|
|
document.body.appendChild(app);
|
|
|
|
|
|
|
|
try {
|
2017-06-06 15:04:28 +00:00
|
|
|
callback(me, stream);
|
2017-05-17 20:06:55 +00:00
|
|
|
} catch (e) {
|
|
|
|
panic(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ユーザーをフェッチしてコールバックする
|
|
|
|
function fetchme(token, cb) {
|
|
|
|
let me = null;
|
|
|
|
|
|
|
|
// Return when not signed in
|
|
|
|
if (token == null) {
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch user
|
|
|
|
fetch(`${CONFIG.apiUrl}/i`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
i: token
|
|
|
|
})
|
|
|
|
}).then(res => { // When success
|
|
|
|
// When failed to authenticate user
|
|
|
|
if (res.status !== 200) {
|
|
|
|
return signout();
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json().then(i => {
|
|
|
|
me = i;
|
|
|
|
me.token = token;
|
2017-11-08 14:43:47 +00:00
|
|
|
done();
|
2017-05-17 20:06:55 +00:00
|
|
|
});
|
|
|
|
}, () => { // When failure
|
2017-06-11 12:26:09 +00:00
|
|
|
// Render the error screen
|
|
|
|
document.body.innerHTML = '<mk-error />';
|
|
|
|
riot.mount('*');
|
|
|
|
Progress.done();
|
2017-05-17 20:06:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (cb) cb(me);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BSoD
|
|
|
|
function panic(e) {
|
|
|
|
console.error(e);
|
|
|
|
|
|
|
|
// Display blue screen
|
2017-06-12 18:20:43 +00:00
|
|
|
document.documentElement.style.background = '#1269e2';
|
2017-05-17 20:06:55 +00:00
|
|
|
document.body.innerHTML =
|
2017-05-17 20:18:32 +00:00
|
|
|
'<div id="error">'
|
|
|
|
+ '<h1>:( 致命的な問題が発生しました。</h1>'
|
|
|
|
+ '<p>お使いのブラウザ(またはOS)のバージョンを更新すると解決する可能性があります。</p>'
|
|
|
|
+ '<hr>'
|
|
|
|
+ `<p>エラーコード: ${e.toString()}</p>`
|
|
|
|
+ `<p>ブラウザ バージョン: ${navigator.userAgent}</p>`
|
|
|
|
+ `<p>クライアント バージョン: ${VERSION}</p>`
|
|
|
|
+ '<hr>'
|
|
|
|
+ '<p>問題が解決しない場合は、上記の情報をお書き添えの上 syuilotan@yahoo.co.jp までご連絡ください。</p>'
|
|
|
|
+ '<p>Thank you for using Misskey.</p>'
|
|
|
|
+ '</div>';
|
2017-05-17 20:06:55 +00:00
|
|
|
|
|
|
|
// TODO: Report the bug
|
|
|
|
}
|