misskey/src/web/app/common/mios.ts

240 lines
5.6 KiB
TypeScript
Raw Normal View History

2017-11-15 10:06:52 -08:00
import { EventEmitter } from 'eventemitter3';
import * as riot from 'riot';
import signout from './scripts/signout';
import Progress from './scripts/loading';
2017-11-16 08:24:44 -08:00
import HomeStreamManager from './scripts/streaming/home-stream-manager';
2017-11-15 10:06:52 -08:00
import CONFIG from './scripts/config';
import api from './scripts/api';
/**
* Misskey Operating System
*/
export default class MiOS extends EventEmitter {
/**
* Misskeyの /meta
*/
private meta: {
data: { [x: string]: any };
chachedAt: Date;
};
private isMetaFetching = false;
/**
* A signing user
*/
2017-11-15 16:00:24 -08:00
public i: { [x: string]: any };
2017-11-15 10:06:52 -08:00
/**
* Whether signed in
*/
public get isSignedin() {
return this.i != null;
}
/**
2017-11-16 08:24:44 -08:00
* A connection manager of home stream
2017-11-15 10:06:52 -08:00
*/
2017-11-16 08:24:44 -08:00
public stream: HomeStreamManager;
2017-11-15 10:06:52 -08:00
2017-11-20 10:40:09 -08:00
/**
* A registration of service worker
*/
private swRegistration: ServiceWorkerRegistration = null;
2017-11-15 10:06:52 -08:00
constructor() {
super();
//#region BIND
this.init = this.init.bind(this);
this.api = this.api.bind(this);
this.getMeta = this.getMeta.bind(this);
2017-11-20 10:40:09 -08:00
this.swSubscribe = this.swSubscribe.bind(this);
2017-11-15 10:06:52 -08:00
//#endregion
}
/**
* Initialize MiOS (boot)
* @param callback A function that call when initialized
*/
public async init(callback) {
// ユーザーをフェッチしてコールバックする
const fetchme = (token, cb) => {
let me = null;
// Return when not signed in
if (token == null) {
return done();
}
// Fetch user
fetch(`${CONFIG.apiUrl}/i`, {
method: 'POST',
body: JSON.stringify({
i: token
})
2017-11-16 08:24:44 -08:00
})
// When success
.then(res => {
2017-11-15 10:06:52 -08:00
// When failed to authenticate user
if (res.status !== 200) {
return signout();
}
2017-11-16 08:24:44 -08:00
// Parse response
2017-11-15 10:06:52 -08:00
res.json().then(i => {
me = i;
me.token = token;
done();
});
2017-11-16 08:24:44 -08:00
})
// When failure
.catch(() => {
2017-11-15 10:06:52 -08:00
// Render the error screen
document.body.innerHTML = '<mk-error />';
riot.mount('*');
2017-11-16 08:24:44 -08:00
2017-11-15 10:06:52 -08:00
Progress.done();
});
function done() {
if (cb) cb(me);
}
};
// フェッチが完了したとき
const fetched = me => {
if (me) {
riot.observable(me);
// この me オブジェクトを更新するメソッド
me.update = data => {
if (data) Object.assign(me, data);
me.trigger('updated');
};
// ローカルストレージにキャッシュ
localStorage.setItem('me', JSON.stringify(me));
2017-11-16 08:24:44 -08:00
// 自分の情報が更新されたとき
2017-11-15 10:06:52 -08:00
me.on('updated', () => {
// キャッシュ更新
localStorage.setItem('me', JSON.stringify(me));
});
}
this.i = me;
2017-11-16 08:24:44 -08:00
// Init home stream manager
this.stream = this.isSignedin
? new HomeStreamManager(this.i)
: null;
2017-11-15 10:06:52 -08:00
// Finish init
callback();
2017-11-20 10:40:09 -08:00
//#region Service worker
const isSwSupported =
('serviceWorker' in navigator) && ('PushManager' in window);
if (isSwSupported && this.isSignedin) {
// When service worker activated
navigator.serviceWorker.ready.then(this.swSubscribe);
// Register service worker
navigator.serviceWorker.register('/sw.js').then(registration => {
// 登録成功
console.info('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(err => {
// 登録失敗 :(
console.error('ServiceWorker registration failed: ', err);
});
}
//#endregion
2017-11-15 10:06:52 -08:00
};
// Get cached account data
const cachedMe = JSON.parse(localStorage.getItem('me'));
if (cachedMe) {
fetched(cachedMe);
// 後から新鮮なデータをフェッチ
fetchme(cachedMe.token, freshData => {
Object.assign(cachedMe, freshData);
cachedMe.trigger('updated');
});
} else {
// Get token from cookie
const i = (document.cookie.match(/i=(!\w+)/) || [null, null])[1];
fetchme(i, fetched);
}
}
2017-11-20 10:40:09 -08:00
private async swSubscribe(swRegistration: ServiceWorkerRegistration) {
this.swRegistration = swRegistration;
// Subscribe
this.swRegistration.pushManager.subscribe({
// A boolean indicating that the returned push subscription
// will only be used for messages whose effect is made visible to the user.
userVisibleOnly: true
}).then(subscription => {
console.log('Subscribe OK:', subscription);
// Register
this.api('sw/register', {
endpoint: subscription.endpoint,
auth: subscription.getKey('auth') ? btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth')))) : '',
publickey: subscription.getKey('p256dh') ? btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh')))) : ''
});
}).then(() => {
console.log('Server Stored Subscription.');
}).catch(err => {
console.error('Subscribe Error:', err);
});
}
2017-11-15 10:06:52 -08:00
/**
* Misskey APIにリクエストします
* @param endpoint
* @param data
*/
public api(endpoint: string, data?: { [x: string]: any }) {
return api(this.i, endpoint, data);
}
/**
* Misskeyのメタ情報を取得します
* @param force
*/
public getMeta(force = false) {
return new Promise<{ [x: string]: any }>(async (res, rej) => {
if (this.isMetaFetching) {
this.once('_meta_fetched_', () => {
res(this.meta.data);
});
return;
}
const expire = 1000 * 60; // 1min
// forceが有効, meta情報を保持していない or 期限切れ
if (force || this.meta == null || Date.now() - this.meta.chachedAt.getTime() > expire) {
this.isMetaFetching = true;
const meta = await this.api('meta');
this.meta = {
data: meta,
chachedAt: new Date()
};
this.isMetaFetching = false;
this.emit('_meta_fetched_');
res(meta);
} else {
res(this.meta.data);
}
});
}
}