2017-01-01 04:23:09 +02:00
|
|
|
/**
|
|
|
|
* boot
|
|
|
|
*/
|
2016-12-31 13:26:22 +02:00
|
|
|
|
|
|
|
const riot = require('riot');
|
2017-01-13 18:16:30 +02:00
|
|
|
require('velocity-animate');
|
2017-02-13 00:39:54 +02:00
|
|
|
const api = require('./common/scripts/api');
|
2017-02-18 09:37:22 +02:00
|
|
|
const signout = require('./common/scripts/signout');
|
|
|
|
const generateDefaultUserdata = require('./common/scripts/generate-default-userdata');
|
2017-02-18 09:55:18 +02:00
|
|
|
const mixins = require('./common/mixins');
|
2017-02-02 22:22:12 +02:00
|
|
|
require('./common/tags');
|
2016-12-31 13:26:22 +02:00
|
|
|
|
2017-01-01 04:23:09 +02:00
|
|
|
/**
|
2017-02-27 10:35:56 +02:00
|
|
|
* MISSKEY ENTRY POINT!
|
2017-01-01 04:23:09 +02:00
|
|
|
*/
|
2016-12-31 13:26:22 +02:00
|
|
|
|
2017-02-20 13:21:28 +02:00
|
|
|
"use strict";
|
|
|
|
|
2017-02-21 21:19:53 +02:00
|
|
|
const CONFIG = require('./common/scripts/config');
|
|
|
|
|
2016-12-31 13:26:22 +02:00
|
|
|
document.domain = CONFIG.host;
|
|
|
|
|
2017-02-27 10:36:30 +02:00
|
|
|
// Set global configuration
|
2017-02-19 08:32:10 +02:00
|
|
|
riot.mixin({
|
2017-02-21 21:19:53 +02:00
|
|
|
CONFIG
|
2017-02-19 08:32:10 +02:00
|
|
|
});
|
|
|
|
|
2016-12-31 13:26:22 +02:00
|
|
|
// ↓ iOS待ちPolyfill (SEE: http://caniuse.com/#feat=fetch)
|
2017-01-13 18:16:30 +02:00
|
|
|
require('whatwg-fetch');
|
2016-12-31 13:26:22 +02:00
|
|
|
|
2017-02-23 16:31:28 +02:00
|
|
|
// ↓ NodeList、HTMLCollection、FileList、DataTransferItemListで forEach を使えるようにする
|
2016-12-31 13:26:22 +02:00
|
|
|
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
|
|
|
}
|
2017-02-21 02:49:35 +02:00
|
|
|
if (FileList.prototype.forEach === undefined) {
|
|
|
|
FileList.prototype.forEach = Array.prototype.forEach;
|
|
|
|
}
|
2017-02-24 04:25:36 +02:00
|
|
|
if (window.DataTransferItemList && DataTransferItemList.prototype.forEach === undefined) {
|
2017-02-23 16:31:28 +02:00
|
|
|
DataTransferItemList.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
|
|
|
}
|
|
|
|
|
|
|
|
// ユーザーをフェッチしてコールバックする
|
|
|
|
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-02-18 03:28:04 +02:00
|
|
|
if (cachedMe && cachedMe.data && cachedMe.data.cache) {
|
2017-01-01 04:17:17 +02:00
|
|
|
fetched(cachedMe);
|
2016-12-31 13:26:22 +02:00
|
|
|
|
|
|
|
// 後から新鮮なデータをフェッチ
|
2017-02-06 09:40:42 +02:00
|
|
|
fetchme(cachedMe.token, freshData => {
|
2017-01-01 04:17:17 +02:00
|
|
|
Object.assign(cachedMe, freshData);
|
|
|
|
cachedMe.trigger('updated');
|
|
|
|
});
|
|
|
|
} else {
|
2016-12-31 13:26:22 +02:00
|
|
|
// キャッシュ無効なのにキャッシュが残ってたら掃除
|
2017-02-18 03:28:04 +02:00
|
|
|
if (cachedMe) {
|
2017-01-01 04:17:17 +02:00
|
|
|
localStorage.removeItem('me');
|
|
|
|
}
|
2017-01-01 04:23:09 +02:00
|
|
|
|
2017-02-06 09:40:42 +02:00
|
|
|
// Get token from cookie
|
|
|
|
const i = (document.cookie.match(/i=(!\w+)/) || [null, null])[1];
|
|
|
|
|
|
|
|
fetchme(i, fetched);
|
2017-01-01 04:17:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function fetched(me) {
|
2017-02-18 03:28:04 +02:00
|
|
|
if (me) {
|
2017-01-01 04:17:17 +02:00
|
|
|
riot.observable(me);
|
2017-01-01 04:23:09 +02:00
|
|
|
|
2017-01-21 14:08:40 +02:00
|
|
|
me.update = data => {
|
|
|
|
if (data) Object.assign(me, data);
|
|
|
|
me.trigger('updated');
|
|
|
|
};
|
|
|
|
|
2017-01-01 04:17:17 +02:00
|
|
|
if (me.data.cache) {
|
|
|
|
localStorage.setItem('me', JSON.stringify(me));
|
2017-01-01 04:23:09 +02:00
|
|
|
|
2017-01-01 04:17:17 +02:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-01-01 04:23:09 +02:00
|
|
|
|
2017-01-01 04:17:17 +02:00
|
|
|
mixins(me);
|
2017-01-01 04:23:09 +02:00
|
|
|
|
2017-02-21 20:27:38 +02:00
|
|
|
const ini = document.getElementById('ini');
|
|
|
|
ini.parentNode.removeChild(ini);
|
2017-01-01 04:23:09 +02:00
|
|
|
|
2017-01-01 04:17:17 +02:00
|
|
|
const app = document.createElement('div');
|
|
|
|
app.setAttribute('id', 'app');
|
|
|
|
document.body.appendChild(app);
|
2017-01-01 04:23:09 +02:00
|
|
|
|
2017-01-01 04:17:17 +02:00
|
|
|
try {
|
|
|
|
callback(me);
|
|
|
|
} catch (e) {
|
|
|
|
panic(e);
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 13:26:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// ユーザーをフェッチしてコールバックする
|
2017-02-06 09:40:42 +02:00
|
|
|
function fetchme(token, 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-02-19 08:32:10 +02:00
|
|
|
fetch(CONFIG.apiUrl + '/i', {
|
2017-01-01 04:17:17 +02:00
|
|
|
method: 'POST',
|
2017-02-11 19:19:19 +02:00
|
|
|
body: JSON.stringify({
|
|
|
|
i: token
|
|
|
|
})
|
2017-01-01 04:17:17 +02:00
|
|
|
}).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) {
|
2017-01-01 04:19:42 +02:00
|
|
|
return signout();
|
2017-01-01 04:17:17 +02:00
|
|
|
}
|
2017-01-01 04:23:09 +02:00
|
|
|
|
2017-01-01 04:17:17 +02:00
|
|
|
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-02-18 03:28:04 +02:00
|
|
|
me.data ? done() : init();
|
2017-01-01 04:17:17 +02:00
|
|
|
});
|
2017-02-06 09:40:42 +02:00
|
|
|
}, () => {
|
2017-02-18 03:28:04 +02:00
|
|
|
riot.mount(document.body.appendChild(document.createElement('mk-core-error')), {
|
2017-02-06 09:40:42 +02:00
|
|
|
retry: () => {
|
|
|
|
fetchme(token, cb);
|
|
|
|
}
|
|
|
|
});
|
2017-01-01 04:17:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
2017-02-18 03:28:04 +02:00
|
|
|
if (cb) cb(me);
|
2017-01-01 04:17:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
2017-01-01 04:19:32 +02:00
|
|
|
const data = generateDefaultUserdata();
|
2017-01-01 04:17:17 +02:00
|
|
|
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>';
|
2017-02-18 03:28:04 +02:00
|
|
|
// TODO: Report the bug
|
2016-12-31 13:26:22 +02:00
|
|
|
}
|