2016-12-31 13:26:22 +02:00
|
|
|
/*
|
|
|
|
MISSKEY BOOT LOADER
|
|
|
|
|
|
|
|
Misskeyを起動します。
|
|
|
|
1. 初期化
|
|
|
|
2. ユーザー取得(ログインしていれば)
|
|
|
|
3. アプリケーションをマウント
|
|
|
|
*/
|
|
|
|
|
|
|
|
// LOAD DEPENDENCIES
|
|
|
|
|
|
|
|
const riot = require('riot');
|
|
|
|
require('velocity');
|
|
|
|
const log = require('./common/scripts/log.ls');
|
|
|
|
const api = require('./common/scripts/api.ls');
|
|
|
|
const signout = require('./common/scripts/signout.ls');
|
|
|
|
const generateDefaultUserdata = require('./common/scripts/generate-default-userdata.ls');
|
|
|
|
const mixins = require('./common/mixins.ls');
|
|
|
|
const checkForUpdate = require('./common/scripts/check-for-update.ls');
|
|
|
|
require('./common/tags.ls');
|
|
|
|
|
|
|
|
// MISSKEY ENTORY POINT
|
|
|
|
|
|
|
|
document.domain = CONFIG.host;
|
|
|
|
|
|
|
|
// ↓ iOS待ちPolyfill (SEE: http://caniuse.com/#feat=fetch)
|
|
|
|
require('fetch');
|
|
|
|
|
|
|
|
// ↓ NodeList、HTMLCollectionで forEach を使えるようにする
|
|
|
|
if (NodeList.prototype.forEach === undefined) {
|
2017-01-01 04:17:17 +02:00
|
|
|
NodeList.prototype.forEach = Array.prototype.forEach;
|
2016-12-31 13:26:22 +02:00
|
|
|
}
|
|
|
|
if (HTMLCollection.prototype.forEach === undefined) {
|
2017-01-01 04:17:17 +02:00
|
|
|
HTMLCollection.prototype.forEach = Array.prototype.forEach;
|
2016-12-31 13:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ↓ iOSでプライベートモードだとlocalStorageが使えないので既存のメソッドを上書きする
|
|
|
|
try {
|
2017-01-01 04:17:17 +02:00
|
|
|
localStorage.setItem('kyoppie', 'yuppie');
|
2016-12-31 13:26:22 +02:00
|
|
|
} catch (e) {
|
2017-01-01 04:17:17 +02:00
|
|
|
Storage.prototype.setItem = () => { }; // noop
|
2016-12-31 13:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MAIN PROCESS
|
|
|
|
|
|
|
|
log("Misskey (aoi) v:" + VERSION);
|
|
|
|
|
|
|
|
// Check for Update
|
|
|
|
checkForUpdate();
|
|
|
|
|
|
|
|
// Get token from cookie
|
|
|
|
const i = (document.cookie.match(/i=(\w+)/) || [null, null])[1];
|
|
|
|
|
|
|
|
if (i != null) {
|
2017-01-01 04:17:17 +02:00
|
|
|
log("ME: " + i);
|
2016-12-31 13:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ユーザーをフェッチしてコールバックする
|
|
|
|
module.exports = callback => {
|
|
|
|
// Get cached account data
|
2017-01-01 04:17:17 +02:00
|
|
|
let cachedMe = JSON.parse(localStorage.getItem('me'));
|
2016-12-31 13:26:22 +02:00
|
|
|
|
2017-01-01 04:17:17 +02:00
|
|
|
if (cachedMe != null && cachedMe.data != null && cachedMe.data.cache) {
|
|
|
|
fetched(cachedMe);
|
2016-12-31 13:26:22 +02:00
|
|
|
|
|
|
|
// 後から新鮮なデータをフェッチ
|
2017-01-01 04:17:17 +02:00
|
|
|
fetchme(i, true, freshData => {
|
|
|
|
Object.assign(cachedMe, freshData);
|
|
|
|
cachedMe.trigger('updated');
|
|
|
|
});
|
|
|
|
} else {
|
2016-12-31 13:26:22 +02:00
|
|
|
// キャッシュ無効なのにキャッシュが残ってたら掃除
|
2017-01-01 04:17:17 +02:00
|
|
|
if (cachedMe != null) {
|
|
|
|
localStorage.removeItem('me');
|
|
|
|
}
|
|
|
|
fetchme(i, false, fetched);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetched(me) {
|
|
|
|
if (me != null) {
|
|
|
|
riot.observable(me);
|
|
|
|
if (me.data.cache) {
|
|
|
|
localStorage.setItem('me', JSON.stringify(me));
|
|
|
|
me.on('updated', () => {
|
2016-12-31 13:26:22 +02:00
|
|
|
// キャッシュ更新
|
2017-01-01 04:17:17 +02:00
|
|
|
localStorage.setItem('me', JSON.stringify(me));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
log("Fetched! Hello " + me.username + ".");
|
|
|
|
}
|
|
|
|
mixins(me);
|
|
|
|
const init = document.getElementById('init');
|
|
|
|
init.parentNode.removeChild(init);
|
|
|
|
const app = document.createElement('div');
|
|
|
|
app.setAttribute('id', 'app');
|
|
|
|
document.body.appendChild(app);
|
|
|
|
try {
|
|
|
|
callback(me);
|
|
|
|
} catch (e) {
|
|
|
|
panic(e);
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 13:26:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// ユーザーをフェッチしてコールバックする
|
|
|
|
function fetchme(token, silent, cb) {
|
2017-01-01 04:17:17 +02:00
|
|
|
let me = null;
|
2016-12-31 13:26:22 +02:00
|
|
|
|
|
|
|
// Return when not signed in
|
2017-01-01 04:17:17 +02:00
|
|
|
if (token == null) {
|
|
|
|
return done();
|
|
|
|
}
|
2016-12-31 13:26:22 +02:00
|
|
|
|
|
|
|
// Fetch user
|
2017-01-01 04:17:17 +02:00
|
|
|
fetch(CONFIG.api.url + "/i", {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
|
|
|
|
},
|
|
|
|
body: "i=" + token
|
|
|
|
}).then(res => {
|
2016-12-31 13:26:22 +02:00
|
|
|
// When failed to authenticate user
|
2017-01-01 04:17:17 +02:00
|
|
|
if (res.status !== 200) {
|
|
|
|
signout();
|
|
|
|
}
|
|
|
|
res.json().then(i => {
|
|
|
|
me = i;
|
|
|
|
me.token = token;
|
2016-12-31 13:26:22 +02:00
|
|
|
|
|
|
|
// initialize it if user data is empty
|
2017-01-01 04:17:17 +02:00
|
|
|
if (me.data != null) {
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(() => {
|
|
|
|
if (!silent) {
|
|
|
|
const info = document.body.appendChild(document.createElement('mk-core-error'));
|
|
|
|
riot.mount(info, {
|
|
|
|
retry: () => {
|
|
|
|
fetchme(token, false, cb);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (cb != null) {
|
|
|
|
cb(me);
|
2016-12-31 13:26:22 +02:00
|
|
|
}
|
2017-01-01 04:17:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
var data, this$ = this;
|
|
|
|
data = generateDefaultUserdata();
|
|
|
|
api(token, 'i/appdata/set', {
|
|
|
|
data: JSON.stringify(data)
|
|
|
|
}).then(() => {
|
|
|
|
me.data = data;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2016-12-31 13:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function panic(e) {
|
2017-01-01 04:17:17 +02:00
|
|
|
console.error(e);
|
|
|
|
document.body.innerHTML = '<div id="error"><p>致命的な問題が発生しました。</p></div>';
|
2016-12-31 13:26:22 +02:00
|
|
|
}
|