2016-12-31 20:23:09 -06:00
|
|
|
/**
|
2017-05-10 14:15:54 -05:00
|
|
|
* boot loader
|
2016-12-31 20:23:09 -06:00
|
|
|
*/
|
2016-12-31 05:26:22 -06:00
|
|
|
|
2017-05-10 14:15:54 -05:00
|
|
|
"use strict";
|
|
|
|
|
2017-03-18 06:05:11 -05: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';
|
|
|
|
import mixin from './common/mixins';
|
|
|
|
import generateDefaultUserdata from './common/scripts/generate-default-userdata';
|
|
|
|
import CONFIG from './common/scripts/config';
|
2017-02-02 14:22:12 -06:00
|
|
|
require('./common/tags');
|
2016-12-31 05:26:22 -06:00
|
|
|
|
2016-12-31 20:23:09 -06:00
|
|
|
/**
|
2017-02-27 02:35:56 -06:00
|
|
|
* MISSKEY ENTRY POINT!
|
2016-12-31 20:23:09 -06:00
|
|
|
*/
|
2016-12-31 05:26:22 -06:00
|
|
|
|
2017-03-18 06:05:11 -05:00
|
|
|
console.info(`Misskey v${VERSION}`);
|
2017-02-21 13:19:53 -06:00
|
|
|
|
2016-12-31 05:26:22 -06:00
|
|
|
document.domain = CONFIG.host;
|
|
|
|
|
2017-02-27 02:36:30 -06:00
|
|
|
// Set global configuration
|
2017-05-10 14:15:54 -05:00
|
|
|
riot.mixin({ CONFIG });
|
2017-02-19 00:32:10 -06:00
|
|
|
|
2017-02-23 08:31:28 -06:00
|
|
|
// ↓ NodeList、HTMLCollection、FileList、DataTransferItemListで forEach を使えるようにする
|
2016-12-31 05:26:22 -06:00
|
|
|
if (NodeList.prototype.forEach === undefined) {
|
2016-12-31 20:17:17 -06:00
|
|
|
NodeList.prototype.forEach = Array.prototype.forEach;
|
2016-12-31 05:26:22 -06:00
|
|
|
}
|
|
|
|
if (HTMLCollection.prototype.forEach === undefined) {
|
2016-12-31 20:17:17 -06:00
|
|
|
HTMLCollection.prototype.forEach = Array.prototype.forEach;
|
2016-12-31 05:26:22 -06:00
|
|
|
}
|
2017-02-20 18:49:35 -06:00
|
|
|
if (FileList.prototype.forEach === undefined) {
|
|
|
|
FileList.prototype.forEach = Array.prototype.forEach;
|
|
|
|
}
|
2017-02-23 20:25:36 -06:00
|
|
|
if (window.DataTransferItemList && DataTransferItemList.prototype.forEach === undefined) {
|
2017-02-23 08:31:28 -06:00
|
|
|
DataTransferItemList.prototype.forEach = Array.prototype.forEach;
|
|
|
|
}
|
2016-12-31 05:26:22 -06:00
|
|
|
|
2017-05-10 14:15:54 -05:00
|
|
|
// iOSでプライベートモードだとlocalStorageが使えないので既存のメソッドを上書きする
|
2016-12-31 05:26:22 -06:00
|
|
|
try {
|
2016-12-31 20:17:17 -06:00
|
|
|
localStorage.setItem('kyoppie', 'yuppie');
|
2016-12-31 05:26:22 -06:00
|
|
|
} catch (e) {
|
2016-12-31 20:17:17 -06:00
|
|
|
Storage.prototype.setItem = () => { }; // noop
|
2016-12-31 05:26:22 -06:00
|
|
|
}
|
|
|
|
|
2017-03-17 10:02:41 -05:00
|
|
|
// クライアントを更新すべきならする
|
|
|
|
if (localStorage.getItem('should-refresh') == 'true') {
|
|
|
|
localStorage.removeItem('should-refresh');
|
|
|
|
location.reload(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新チェック
|
2017-03-18 06:05:11 -05:00
|
|
|
setTimeout(checkForUpdate, 3000);
|
2017-03-17 10:02:41 -05:00
|
|
|
|
2016-12-31 05:26:22 -06:00
|
|
|
// ユーザーをフェッチしてコールバックする
|
2017-03-18 06:05:11 -05:00
|
|
|
export default callback => {
|
2016-12-31 05:26:22 -06:00
|
|
|
// Get cached account data
|
2016-12-31 20:17:17 -06:00
|
|
|
let cachedMe = JSON.parse(localStorage.getItem('me'));
|
2016-12-31 05:26:22 -06:00
|
|
|
|
2017-03-19 01:22:55 -05:00
|
|
|
if (cachedMe) {
|
2016-12-31 20:17:17 -06:00
|
|
|
fetched(cachedMe);
|
2016-12-31 05:26:22 -06:00
|
|
|
|
|
|
|
// 後から新鮮なデータをフェッチ
|
2017-02-06 01:40:42 -06:00
|
|
|
fetchme(cachedMe.token, freshData => {
|
2016-12-31 20:17:17 -06:00
|
|
|
Object.assign(cachedMe, freshData);
|
|
|
|
cachedMe.trigger('updated');
|
|
|
|
});
|
|
|
|
} else {
|
2017-02-06 01:40:42 -06:00
|
|
|
// Get token from cookie
|
|
|
|
const i = (document.cookie.match(/i=(!\w+)/) || [null, null])[1];
|
|
|
|
|
|
|
|
fetchme(i, fetched);
|
2016-12-31 20:17:17 -06:00
|
|
|
}
|
|
|
|
|
2017-05-10 14:15:54 -05:00
|
|
|
// フェッチが完了したとき
|
2016-12-31 20:17:17 -06:00
|
|
|
function fetched(me) {
|
2017-02-17 19:28:04 -06:00
|
|
|
if (me) {
|
2016-12-31 20:17:17 -06:00
|
|
|
riot.observable(me);
|
2016-12-31 20:23:09 -06:00
|
|
|
|
2017-05-10 14:15:54 -05:00
|
|
|
// この me オブジェクトを更新するメソッド
|
2017-01-21 06:08:40 -06:00
|
|
|
me.update = data => {
|
|
|
|
if (data) Object.assign(me, data);
|
|
|
|
me.trigger('updated');
|
|
|
|
};
|
|
|
|
|
2017-05-10 14:15:54 -05:00
|
|
|
// ローカルストレージにキャッシュ
|
2017-03-19 01:22:55 -05:00
|
|
|
localStorage.setItem('me', JSON.stringify(me));
|
2016-12-31 20:23:09 -06:00
|
|
|
|
2017-03-19 01:22:55 -05:00
|
|
|
me.on('updated', () => {
|
|
|
|
// キャッシュ更新
|
|
|
|
localStorage.setItem('me', JSON.stringify(me));
|
|
|
|
});
|
2016-12-31 20:17:17 -06:00
|
|
|
}
|
2016-12-31 20:23:09 -06:00
|
|
|
|
2017-05-10 14:15:54 -05:00
|
|
|
// ミックスイン初期化
|
2017-03-18 06:05:11 -05:00
|
|
|
mixin(me);
|
2016-12-31 20:23:09 -06:00
|
|
|
|
2017-05-10 14:15:54 -05:00
|
|
|
// ローディング画面クリア
|
2017-02-21 12:27:38 -06:00
|
|
|
const ini = document.getElementById('ini');
|
|
|
|
ini.parentNode.removeChild(ini);
|
2016-12-31 20:23:09 -06:00
|
|
|
|
2017-05-10 14:15:54 -05:00
|
|
|
// アプリ基底要素マウント
|
2016-12-31 20:17:17 -06:00
|
|
|
const app = document.createElement('div');
|
|
|
|
app.setAttribute('id', 'app');
|
|
|
|
document.body.appendChild(app);
|
2016-12-31 20:23:09 -06:00
|
|
|
|
2016-12-31 20:17:17 -06:00
|
|
|
try {
|
|
|
|
callback(me);
|
|
|
|
} catch (e) {
|
|
|
|
panic(e);
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 05:26:22 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// ユーザーをフェッチしてコールバックする
|
2017-02-06 01:40:42 -06:00
|
|
|
function fetchme(token, cb) {
|
2016-12-31 20:17:17 -06:00
|
|
|
let me = null;
|
2016-12-31 05:26:22 -06:00
|
|
|
|
|
|
|
// Return when not signed in
|
2016-12-31 20:17:17 -06:00
|
|
|
if (token == null) {
|
|
|
|
return done();
|
|
|
|
}
|
2016-12-31 05:26:22 -06:00
|
|
|
|
|
|
|
// Fetch user
|
2017-05-08 09:35:53 -05:00
|
|
|
fetch(`${CONFIG.apiUrl}/i`, {
|
2016-12-31 20:17:17 -06:00
|
|
|
method: 'POST',
|
2017-02-11 11:19:19 -06:00
|
|
|
body: JSON.stringify({
|
|
|
|
i: token
|
|
|
|
})
|
2017-05-10 14:15:54 -05:00
|
|
|
}).then(res => { // When success
|
2016-12-31 05:26:22 -06:00
|
|
|
// When failed to authenticate user
|
2016-12-31 20:17:17 -06:00
|
|
|
if (res.status !== 200) {
|
2016-12-31 20:19:42 -06:00
|
|
|
return signout();
|
2016-12-31 20:17:17 -06:00
|
|
|
}
|
2016-12-31 20:23:09 -06:00
|
|
|
|
2016-12-31 20:17:17 -06:00
|
|
|
res.json().then(i => {
|
|
|
|
me = i;
|
|
|
|
me.token = token;
|
2016-12-31 05:26:22 -06:00
|
|
|
|
|
|
|
// initialize it if user data is empty
|
2017-02-17 19:28:04 -06:00
|
|
|
me.data ? done() : init();
|
2016-12-31 20:17:17 -06:00
|
|
|
});
|
2017-05-10 14:15:54 -05:00
|
|
|
}, () => { // When failure
|
|
|
|
// Display error screen
|
|
|
|
riot.mount(document.body.appendChild(
|
|
|
|
document.createElement('mk-error')));
|
2016-12-31 20:17:17 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
2017-02-17 19:28:04 -06:00
|
|
|
if (cb) cb(me);
|
2016-12-31 20:17:17 -06:00
|
|
|
}
|
|
|
|
|
2017-05-10 14:15:54 -05:00
|
|
|
// Initialize user data
|
2016-12-31 20:17:17 -06:00
|
|
|
function init() {
|
2016-12-31 20:19:32 -06:00
|
|
|
const data = generateDefaultUserdata();
|
2016-12-31 20:17:17 -06:00
|
|
|
api(token, 'i/appdata/set', {
|
2017-03-19 01:47:36 -05:00
|
|
|
data
|
2016-12-31 20:17:17 -06:00
|
|
|
}).then(() => {
|
|
|
|
me.data = data;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2016-12-31 05:26:22 -06:00
|
|
|
}
|
|
|
|
|
2017-05-10 14:15:54 -05:00
|
|
|
// BSoD
|
2016-12-31 05:26:22 -06:00
|
|
|
function panic(e) {
|
2016-12-31 20:17:17 -06:00
|
|
|
console.error(e);
|
2017-05-10 14:15:54 -05:00
|
|
|
|
|
|
|
// Display blue screen
|
2017-05-08 09:35:53 -05:00
|
|
|
document.body.innerHTML =
|
|
|
|
`<div id="error">
|
|
|
|
<h1>:( 致命的な問題が発生しました。</h1>
|
|
|
|
<p>お使いのブラウザ(またはOS)のバージョンを更新すると解決する可能性があります。</p>
|
|
|
|
<hr>
|
|
|
|
<p>エラーコード: ${e.toString()}</p>
|
|
|
|
<p>ブラウザ バージョン: ${navigator.userAgent}</p>
|
|
|
|
<p>クライアント バージョン: ${VERSION}</p>
|
|
|
|
<hr>
|
2017-05-10 14:15:54 -05:00
|
|
|
<p>問題が解決しない場合は、上記の情報をお書き添えの上 syuilotan@yahoo.co.jp までご連絡ください。</p>
|
2017-05-08 09:35:53 -05:00
|
|
|
<p>Thank you for using Misskey.</p>
|
|
|
|
</div>`;
|
2017-05-10 14:15:54 -05:00
|
|
|
|
2017-02-17 19:28:04 -06:00
|
|
|
// TODO: Report the bug
|
2016-12-31 05:26:22 -06:00
|
|
|
}
|