paricafe/src/web/app/boot.js

165 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-12-31 20:23:09 -06:00
/**
* boot
*/
2016-12-31 05:26:22 -06:00
const riot = require('riot');
2017-01-13 10:16:30 -06:00
require('velocity-animate');
2016-12-31 05:26:22 -06:00
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');
2016-12-31 05:26:22 -06:00
2016-12-31 20:23:09 -06:00
/**
* MISSKEY ENTORY POINT!
*/
2016-12-31 05:26:22 -06:00
document.domain = CONFIG.host;
// ↓ iOS待ちPolyfill (SEE: http://caniuse.com/#feat=fetch)
2017-01-13 10:16:30 -06:00
require('whatwg-fetch');
2016-12-31 05:26:22 -06:00
// ↓ NodeList、HTMLCollectionで forEach を使えるようにする
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
}
// ↓ iOSでプライベートモードだとlocalStorageが使えないので既存のメソッドを上書きする
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
}
// Check for Update
checkForUpdate();
// ユーザーをフェッチしてコールバックする
module.exports = callback => {
// 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
2016-12-31 20:17:17 -06:00
if (cachedMe != null && cachedMe.data != null && cachedMe.data.cache) {
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 {
2016-12-31 05:26:22 -06:00
// キャッシュ無効なのにキャッシュが残ってたら掃除
2016-12-31 20:17:17 -06:00
if (cachedMe != null) {
localStorage.removeItem('me');
}
2016-12-31 20:23:09 -06:00
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
}
function fetched(me) {
if (me != null) {
riot.observable(me);
2016-12-31 20:23:09 -06:00
2017-01-21 06:08:40 -06:00
me.update = data => {
if (data) Object.assign(me, data);
me.trigger('updated');
};
2016-12-31 20:17:17 -06:00
if (me.data.cache) {
localStorage.setItem('me', JSON.stringify(me));
2016-12-31 20:23:09 -06:00
2016-12-31 20:17:17 -06:00
me.on('updated', () => {
2016-12-31 05:26:22 -06:00
// キャッシュ更新
2016-12-31 20:17:17 -06:00
localStorage.setItem('me', JSON.stringify(me));
});
}
}
2016-12-31 20:23:09 -06:00
2016-12-31 20:17:17 -06:00
mixins(me);
2016-12-31 20:23:09 -06:00
2016-12-31 20:17:17 -06:00
const init = document.getElementById('init');
init.parentNode.removeChild(init);
2016-12-31 20:23:09 -06: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
2016-12-31 20:17:17 -06: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 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
2016-12-31 20:17:17 -06:00
if (me.data != null) {
done();
} else {
init();
}
});
2017-02-06 01:40:42 -06:00
}, () => {
const info = document.body.appendChild(document.createElement('mk-core-error'));
riot.mount(info, {
retry: () => {
fetchme(token, cb);
}
});
2016-12-31 20:17:17 -06:00
});
function done() {
if (cb != null) {
cb(me);
2016-12-31 05:26:22 -06:00
}
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', {
data: JSON.stringify(data)
}).then(() => {
me.data = data;
done();
});
}
2016-12-31 05:26:22 -06:00
}
function panic(e) {
2016-12-31 20:17:17 -06:00
console.error(e);
document.body.innerHTML = '<div id="error"><p>致命的な問題が発生しました。</p></div>';
2016-12-31 05:26:22 -06:00
}