misskey/src/web/app/boot.js

173 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-12-31 18:23:09 -08:00
/**
* boot
*/
2016-12-31 03:26:22 -08:00
2017-03-18 04:05:11 -07:00
import * as riot from 'riot';
2017-01-13 08:16:30 -08:00
require('velocity-animate');
2017-03-18 04:05:11 -07:00
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';
require('./common/tags');
2016-12-31 03:26:22 -08:00
2016-12-31 18:23:09 -08:00
/**
2017-02-27 00:35:56 -08:00
* MISSKEY ENTRY POINT!
2016-12-31 18:23:09 -08:00
*/
2016-12-31 03:26:22 -08:00
2017-02-20 03:21:28 -08:00
"use strict";
2017-03-18 04:05:11 -07:00
console.info(`Misskey v${VERSION}`);
2017-02-21 11:19:53 -08:00
2016-12-31 03:26:22 -08:00
document.domain = CONFIG.host;
// Set global configuration
2017-02-18 22:32:10 -08:00
riot.mixin({
2017-02-21 11:19:53 -08:00
CONFIG
2017-02-18 22:32:10 -08:00
});
2016-12-31 03:26:22 -08:00
// ↓ iOS待ちPolyfill (SEE: http://caniuse.com/#feat=fetch)
2017-01-13 08:16:30 -08:00
require('whatwg-fetch');
2016-12-31 03:26:22 -08:00
2017-02-23 06:31:28 -08:00
// ↓ NodeList、HTMLCollection、FileList、DataTransferItemListで forEach を使えるようにする
2016-12-31 03:26:22 -08:00
if (NodeList.prototype.forEach === undefined) {
2016-12-31 18:17:17 -08:00
NodeList.prototype.forEach = Array.prototype.forEach;
2016-12-31 03:26:22 -08:00
}
if (HTMLCollection.prototype.forEach === undefined) {
2016-12-31 18:17:17 -08:00
HTMLCollection.prototype.forEach = Array.prototype.forEach;
2016-12-31 03:26:22 -08:00
}
2017-02-20 16:49:35 -08:00
if (FileList.prototype.forEach === undefined) {
FileList.prototype.forEach = Array.prototype.forEach;
}
2017-02-23 18:25:36 -08:00
if (window.DataTransferItemList && DataTransferItemList.prototype.forEach === undefined) {
2017-02-23 06:31:28 -08:00
DataTransferItemList.prototype.forEach = Array.prototype.forEach;
}
2016-12-31 03:26:22 -08:00
// ↓ iOSでプライベートモードだとlocalStorageが使えないので既存のメソッドを上書きする
try {
2016-12-31 18:17:17 -08:00
localStorage.setItem('kyoppie', 'yuppie');
2016-12-31 03:26:22 -08:00
} catch (e) {
2016-12-31 18:17:17 -08:00
Storage.prototype.setItem = () => { }; // noop
2016-12-31 03:26:22 -08:00
}
2017-03-17 08:02:41 -07:00
// クライアントを更新すべきならする
if (localStorage.getItem('should-refresh') == 'true') {
localStorage.removeItem('should-refresh');
location.reload(true);
}
// 更新チェック
2017-03-18 04:05:11 -07:00
setTimeout(checkForUpdate, 3000);
2017-03-17 08:02:41 -07:00
2016-12-31 03:26:22 -08:00
// ユーザーをフェッチしてコールバックする
2017-03-18 04:05:11 -07:00
export default callback => {
2016-12-31 03:26:22 -08:00
// Get cached account data
2016-12-31 18:17:17 -08:00
let cachedMe = JSON.parse(localStorage.getItem('me'));
2016-12-31 03:26:22 -08:00
if (cachedMe) {
2016-12-31 18:17:17 -08:00
fetched(cachedMe);
2016-12-31 03:26:22 -08:00
// 後から新鮮なデータをフェッチ
2017-02-05 23:40:42 -08:00
fetchme(cachedMe.token, freshData => {
2016-12-31 18:17:17 -08:00
Object.assign(cachedMe, freshData);
cachedMe.trigger('updated');
});
} else {
2017-02-05 23:40:42 -08:00
// Get token from cookie
const i = (document.cookie.match(/i=(!\w+)/) || [null, null])[1];
fetchme(i, fetched);
2016-12-31 18:17:17 -08:00
}
function fetched(me) {
2017-02-17 17:28:04 -08:00
if (me) {
2016-12-31 18:17:17 -08:00
riot.observable(me);
2016-12-31 18:23:09 -08:00
2017-01-21 04:08:40 -08:00
me.update = data => {
if (data) Object.assign(me, data);
me.trigger('updated');
};
localStorage.setItem('me', JSON.stringify(me));
2016-12-31 18:23:09 -08:00
me.on('updated', () => {
// キャッシュ更新
localStorage.setItem('me', JSON.stringify(me));
});
2016-12-31 18:17:17 -08:00
}
2016-12-31 18:23:09 -08:00
2017-03-18 04:05:11 -07:00
mixin(me);
2016-12-31 18:23:09 -08:00
2017-02-21 10:27:38 -08:00
const ini = document.getElementById('ini');
ini.parentNode.removeChild(ini);
2016-12-31 18:23:09 -08:00
2016-12-31 18:17:17 -08:00
const app = document.createElement('div');
app.setAttribute('id', 'app');
document.body.appendChild(app);
2016-12-31 18:23:09 -08:00
2016-12-31 18:17:17 -08:00
try {
callback(me);
} catch (e) {
panic(e);
}
}
2016-12-31 03:26:22 -08:00
};
// ユーザーをフェッチしてコールバックする
2017-02-05 23:40:42 -08:00
function fetchme(token, cb) {
2016-12-31 18:17:17 -08:00
let me = null;
2016-12-31 03:26:22 -08:00
// Return when not signed in
2016-12-31 18:17:17 -08:00
if (token == null) {
return done();
}
2016-12-31 03:26:22 -08:00
// Fetch user
2017-02-18 22:32:10 -08:00
fetch(CONFIG.apiUrl + '/i', {
2016-12-31 18:17:17 -08:00
method: 'POST',
2017-02-11 09:19:19 -08:00
body: JSON.stringify({
i: token
})
2016-12-31 18:17:17 -08:00
}).then(res => {
2016-12-31 03:26:22 -08:00
// When failed to authenticate user
2016-12-31 18:17:17 -08:00
if (res.status !== 200) {
2016-12-31 18:19:42 -08:00
return signout();
2016-12-31 18:17:17 -08:00
}
2016-12-31 18:23:09 -08:00
2016-12-31 18:17:17 -08:00
res.json().then(i => {
me = i;
me.token = token;
2016-12-31 03:26:22 -08:00
// initialize it if user data is empty
2017-02-17 17:28:04 -08:00
me.data ? done() : init();
2016-12-31 18:17:17 -08:00
});
2017-02-05 23:40:42 -08:00
}, () => {
2017-02-17 17:28:04 -08:00
riot.mount(document.body.appendChild(document.createElement('mk-core-error')), {
2017-02-05 23:40:42 -08:00
retry: () => {
fetchme(token, cb);
}
});
2016-12-31 18:17:17 -08:00
});
function done() {
2017-02-17 17:28:04 -08:00
if (cb) cb(me);
2016-12-31 18:17:17 -08:00
}
function init() {
2016-12-31 18:19:32 -08:00
const data = generateDefaultUserdata();
2016-12-31 18:17:17 -08:00
api(token, 'i/appdata/set', {
set: data
2016-12-31 18:17:17 -08:00
}).then(() => {
me.data = data;
done();
});
}
2016-12-31 03:26:22 -08:00
}
function panic(e) {
2016-12-31 18:17:17 -08:00
console.error(e);
document.body.innerHTML = '<div id="error"><p>致命的な問題が発生しました。</p></div>';
2017-02-17 17:28:04 -08:00
// TODO: Report the bug
2016-12-31 03:26:22 -08:00
}