2018-10-06 19:06:17 -07:00
|
|
|
import autobind from 'autobind-decorator';
|
2018-02-21 12:05:19 -08:00
|
|
|
import Vue from 'vue';
|
2017-11-15 10:06:52 -08:00
|
|
|
import { EventEmitter } from 'eventemitter3';
|
2018-02-24 07:18:09 -08:00
|
|
|
|
2018-04-29 05:41:48 -07:00
|
|
|
import initStore from './store';
|
2019-04-15 21:05:10 -07:00
|
|
|
import { apiUrl, version, locale } from './config';
|
2020-01-29 11:37:25 -08:00
|
|
|
import Progress from './scripts/loading';
|
2017-11-15 10:06:52 -08:00
|
|
|
|
2020-01-29 11:37:25 -08:00
|
|
|
import Stream from './scripts/stream';
|
2018-02-21 12:05:19 -08:00
|
|
|
|
2018-03-02 21:42:25 -08:00
|
|
|
//#region api requests
|
|
|
|
let spinner = null;
|
|
|
|
let pending = 0;
|
|
|
|
//#endregion
|
|
|
|
|
2017-11-15 10:06:52 -08:00
|
|
|
/**
|
|
|
|
* Misskey Operating System
|
|
|
|
*/
|
|
|
|
export default class MiOS extends EventEmitter {
|
|
|
|
/**
|
|
|
|
* Misskeyの /meta で取得できるメタ情報
|
|
|
|
*/
|
|
|
|
private meta: {
|
|
|
|
data: { [x: string]: any };
|
|
|
|
chachedAt: Date;
|
|
|
|
};
|
|
|
|
|
|
|
|
private isMetaFetching = false;
|
|
|
|
|
2018-02-22 06:53:07 -08:00
|
|
|
public app: Vue;
|
|
|
|
|
2018-04-29 01:17:15 -07:00
|
|
|
public store: ReturnType<typeof initStore>;
|
|
|
|
|
2017-11-15 10:06:52 -08:00
|
|
|
/**
|
2017-11-16 08:24:44 -08:00
|
|
|
* A connection manager of home stream
|
2017-11-15 10:06:52 -08:00
|
|
|
*/
|
2018-10-06 19:06:17 -07:00
|
|
|
public stream: Stream;
|
2018-02-09 01:28:06 -08:00
|
|
|
|
2017-11-20 10:40:09 -08:00
|
|
|
/**
|
|
|
|
* A registration of service worker
|
|
|
|
*/
|
|
|
|
private swRegistration: ServiceWorkerRegistration = null;
|
|
|
|
|
2018-10-06 19:06:17 -07:00
|
|
|
@autobind
|
2018-02-26 02:23:53 -08:00
|
|
|
public signout() {
|
2018-05-26 21:49:09 -07:00
|
|
|
this.store.dispatch('logout');
|
2018-02-26 02:23:53 -08:00
|
|
|
location.href = '/';
|
|
|
|
}
|
|
|
|
|
2017-11-15 10:06:52 -08:00
|
|
|
/**
|
|
|
|
* Initialize MiOS (boot)
|
|
|
|
* @param callback A function that call when initialized
|
|
|
|
*/
|
2018-10-06 19:06:17 -07:00
|
|
|
@autobind
|
2017-11-15 10:06:52 -08:00
|
|
|
public async init(callback) {
|
2018-04-29 01:17:15 -07:00
|
|
|
this.store = initStore(this);
|
|
|
|
|
2017-11-15 10:06:52 -08:00
|
|
|
// ユーザーをフェッチしてコールバックする
|
|
|
|
const fetchme = (token, cb) => {
|
|
|
|
let me = null;
|
|
|
|
|
|
|
|
// Return when not signed in
|
|
|
|
if (token == null) {
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch user
|
2018-02-24 07:18:09 -08:00
|
|
|
fetch(`${apiUrl}/i`, {
|
2017-11-15 10:06:52 -08:00
|
|
|
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
|
2018-09-25 05:23:26 -07:00
|
|
|
if (res.status !== 200 && res.status < 500) {
|
2018-02-26 02:23:53 -08:00
|
|
|
return this.signout();
|
2017-11-15 10:06:52 -08:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:24:44 -08:00
|
|
|
// Parse response
|
2017-11-15 10:06:52 -08:00
|
|
|
res.json().then(i => {
|
|
|
|
me = i;
|
2018-04-07 11:58:11 -07:00
|
|
|
me.token = token;
|
2017-11-15 10:06:52 -08:00
|
|
|
done();
|
|
|
|
});
|
2017-11-16 08:24:44 -08:00
|
|
|
})
|
|
|
|
// When failure
|
|
|
|
.catch(() => {
|
2017-11-15 10:06:52 -08:00
|
|
|
// Render the error screen
|
2020-01-29 11:37:25 -08:00
|
|
|
document.body.innerHTML = '<div id="err">Error</div>';
|
2017-11-16 08:24:44 -08:00
|
|
|
|
2017-11-15 10:06:52 -08:00
|
|
|
Progress.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (cb) cb(me);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// フェッチが完了したとき
|
2018-05-26 21:49:09 -07:00
|
|
|
const fetched = () => {
|
2018-02-09 01:28:06 -08:00
|
|
|
this.emit('signedin');
|
2017-11-15 10:06:52 -08:00
|
|
|
|
2018-10-08 23:08:31 -07:00
|
|
|
this.initStream();
|
2018-10-06 19:06:17 -07:00
|
|
|
|
2017-11-15 10:06:52 -08:00
|
|
|
// Finish init
|
|
|
|
callback();
|
2017-11-20 10:40:09 -08:00
|
|
|
|
2017-11-20 14:06:36 -08:00
|
|
|
// Init service worker
|
2020-01-29 11:37:25 -08:00
|
|
|
this.getMeta().then(data => {
|
|
|
|
if (data.swPublickey) this.registerSw(data.swPublickey);
|
|
|
|
});
|
2017-11-15 10:06:52 -08:00
|
|
|
};
|
|
|
|
|
2017-11-20 14:06:36 -08:00
|
|
|
// キャッシュがあったとき
|
2018-05-26 21:49:09 -07:00
|
|
|
if (this.store.state.i != null) {
|
|
|
|
if (this.store.state.i.token == null) {
|
2018-04-07 12:44:59 -07:00
|
|
|
this.signout();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-20 14:06:36 -08:00
|
|
|
// とりあえずキャッシュされたデータでお茶を濁して(?)おいて、
|
2018-05-26 21:49:09 -07:00
|
|
|
fetched();
|
2017-11-15 10:06:52 -08:00
|
|
|
|
|
|
|
// 後から新鮮なデータをフェッチ
|
2018-05-26 21:49:09 -07:00
|
|
|
fetchme(this.store.state.i.token, freshData => {
|
|
|
|
this.store.dispatch('mergeMe', freshData);
|
2017-11-15 10:06:52 -08:00
|
|
|
});
|
|
|
|
} else {
|
2020-01-29 11:37:25 -08:00
|
|
|
// Get token from localStorage
|
|
|
|
const i = localStorage.getItem('i');
|
|
|
|
|
2018-04-29 01:17:15 -07:00
|
|
|
fetchme(i, me => {
|
|
|
|
if (me) {
|
2018-05-26 21:49:09 -07:00
|
|
|
this.store.dispatch('login', me);
|
|
|
|
fetched();
|
2018-04-29 01:17:15 -07:00
|
|
|
} else {
|
2018-10-19 19:24:02 -07:00
|
|
|
this.initStream();
|
|
|
|
|
2018-04-29 01:17:15 -07:00
|
|
|
// Finish init
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
2017-11-15 10:06:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 23:08:31 -07:00
|
|
|
@autobind
|
|
|
|
private initStream() {
|
|
|
|
this.stream = new Stream(this);
|
|
|
|
|
|
|
|
if (this.store.getters.isSignedIn) {
|
|
|
|
const main = this.stream.useSharedConnection('main');
|
|
|
|
|
|
|
|
// 自分の情報が更新されたとき
|
|
|
|
main.on('meUpdated', i => {
|
|
|
|
this.store.dispatch('mergeMe', i);
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('readAllNotifications', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadNotification: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('unreadNotification', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadNotification: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('unreadMention', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadMentions: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('readAllUnreadMentions', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadMentions: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('unreadSpecifiedNote', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadSpecifiedNotes: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('readAllUnreadSpecifiedNotes', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadSpecifiedNotes: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-29 11:37:25 -08:00
|
|
|
main.on('readAllMessagingMessages', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadMessagingMessage: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('unreadMessagingMessage', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadMessagingMessage: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('readAllAntennas', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadAntenna: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('unreadAntenna', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadAntenna: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('readAllAnnouncements', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadAnnouncement: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-08 23:08:31 -07:00
|
|
|
main.on('clientSettingUpdated', x => {
|
|
|
|
this.store.commit('settings/set', {
|
|
|
|
key: x.key,
|
|
|
|
value: x.value
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// トークンが再生成されたとき
|
|
|
|
// このままではMisskeyが利用できないので強制的にサインアウトさせる
|
|
|
|
main.on('myTokenRegenerated', () => {
|
2019-06-04 02:03:56 -07:00
|
|
|
alert(locale['common']['my-token-regenerated']);
|
2018-10-08 23:08:31 -07:00
|
|
|
this.signout();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 14:06:36 -08:00
|
|
|
/**
|
|
|
|
* Register service worker
|
|
|
|
*/
|
2018-10-06 19:06:17 -07:00
|
|
|
@autobind
|
2019-07-23 10:16:41 -07:00
|
|
|
private registerSw(swPublickey: string) {
|
2017-11-20 14:06:36 -08:00
|
|
|
// Check whether service worker and push manager supported
|
|
|
|
const isSwSupported =
|
|
|
|
('serviceWorker' in navigator) && ('PushManager' in window);
|
|
|
|
|
|
|
|
// Reject when browser not service worker supported
|
|
|
|
if (!isSwSupported) return;
|
|
|
|
|
|
|
|
// Reject when not signed in to Misskey
|
2018-05-26 21:49:09 -07:00
|
|
|
if (!this.store.getters.isSignedIn) return;
|
2017-11-20 14:06:36 -08:00
|
|
|
|
|
|
|
// When service worker activated
|
|
|
|
navigator.serviceWorker.ready.then(registration => {
|
|
|
|
this.swRegistration = registration;
|
|
|
|
|
|
|
|
// Options of pushManager.subscribe
|
2017-11-22 12:43:00 -08:00
|
|
|
// SEE: https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe#Parameters
|
2017-11-20 14:06:36 -08:00
|
|
|
const opts = {
|
|
|
|
// A boolean indicating that the returned push subscription
|
|
|
|
// will only be used for messages whose effect is made visible to the user.
|
2017-11-22 12:43:00 -08:00
|
|
|
userVisibleOnly: true,
|
|
|
|
|
|
|
|
// A public key your push server will use to send
|
|
|
|
// messages to client apps via a push server.
|
2019-03-05 16:24:16 -08:00
|
|
|
applicationServerKey: urlBase64ToUint8Array(swPublickey)
|
2017-11-20 14:06:36 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Subscribe push notification
|
|
|
|
this.swRegistration.pushManager.subscribe(opts).then(subscription => {
|
|
|
|
function encode(buffer: ArrayBuffer) {
|
|
|
|
return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register
|
|
|
|
this.api('sw/register', {
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
auth: encode(subscription.getKey('auth')),
|
|
|
|
publickey: encode(subscription.getKey('p256dh'))
|
|
|
|
});
|
2017-11-22 16:52:45 -08:00
|
|
|
})
|
|
|
|
// When subscribe failed
|
|
|
|
.catch(async (err: Error) => {
|
|
|
|
// 通知が許可されていなかったとき
|
|
|
|
if (err.name == 'NotAllowedError') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 13:26:22 -08:00
|
|
|
// 違うapplicationServerKey (または gcm_sender_id)のサブスクリプションが
|
|
|
|
// 既に存在していることが原因でエラーになった可能性があるので、
|
|
|
|
// そのサブスクリプションを解除しておく
|
|
|
|
const subscription = await this.swRegistration.pushManager.getSubscription();
|
|
|
|
if (subscription) subscription.unsubscribe();
|
2017-11-20 10:40:09 -08:00
|
|
|
});
|
2017-11-20 14:06:36 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
// The path of service worker script
|
2018-11-13 23:35:00 -08:00
|
|
|
const sw = `/sw.${version}.js`;
|
2017-11-20 14:06:36 -08:00
|
|
|
|
|
|
|
// Register service worker
|
2020-01-29 11:37:25 -08:00
|
|
|
navigator.serviceWorker.register(sw);
|
2017-11-20 10:40:09 -08:00
|
|
|
}
|
|
|
|
|
2017-11-15 10:06:52 -08:00
|
|
|
/**
|
|
|
|
* Misskey APIにリクエストします
|
|
|
|
* @param endpoint エンドポイント名
|
|
|
|
* @param data パラメータ
|
|
|
|
*/
|
2018-10-06 19:06:17 -07:00
|
|
|
@autobind
|
2020-01-29 11:37:25 -08:00
|
|
|
public api(endpoint: string, data: { [x: string]: any } = {}, token?): Promise<{ [x: string]: any }> {
|
|
|
|
if (++pending === 1) {
|
|
|
|
spinner = document.createElement('div');
|
|
|
|
spinner.setAttribute('id', 'wait');
|
|
|
|
document.body.appendChild(spinner);
|
2018-03-02 21:42:25 -08:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:40:12 -07:00
|
|
|
const onFinally = () => {
|
2020-01-29 11:37:25 -08:00
|
|
|
if (--pending === 0) spinner.parentNode.removeChild(spinner);
|
2018-04-13 11:40:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
2019-01-17 23:46:56 -08:00
|
|
|
// Append a credential
|
|
|
|
if (this.store.getters.isSignedIn) (data as any).i = this.store.state.i.token;
|
2020-01-29 11:37:25 -08:00
|
|
|
if (token) (data as any).i = token;
|
2019-01-17 23:46:56 -08:00
|
|
|
|
|
|
|
// Send request
|
|
|
|
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(data),
|
2020-01-29 11:37:25 -08:00
|
|
|
credentials: 'omit',
|
2019-01-17 23:46:56 -08:00
|
|
|
cache: 'no-cache'
|
|
|
|
}).then(async (res) => {
|
|
|
|
const body = res.status === 204 ? null : await res.json();
|
2018-03-15 03:53:46 -07:00
|
|
|
|
2019-01-17 23:46:56 -08:00
|
|
|
if (res.status === 200) {
|
|
|
|
resolve(body);
|
|
|
|
} else if (res.status === 204) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject(body.error);
|
|
|
|
}
|
|
|
|
}).catch(reject);
|
2018-03-02 21:42:25 -08:00
|
|
|
});
|
2018-04-13 11:40:12 -07:00
|
|
|
|
|
|
|
promise.then(onFinally, onFinally);
|
|
|
|
|
|
|
|
return promise;
|
2017-11-15 10:06:52 -08:00
|
|
|
}
|
|
|
|
|
2018-10-31 19:51:49 -07:00
|
|
|
/**
|
|
|
|
* Misskeyのメタ情報を取得します
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
public getMetaSync() {
|
|
|
|
return this.meta ? this.meta.data : null;
|
|
|
|
}
|
|
|
|
|
2017-11-15 10:06:52 -08:00
|
|
|
/**
|
|
|
|
* Misskeyのメタ情報を取得します
|
|
|
|
* @param force キャッシュを無視するか否か
|
|
|
|
*/
|
2018-10-06 19:06:17 -07:00
|
|
|
@autobind
|
2017-11-15 10:06:52 -08:00
|
|
|
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;
|
2018-11-02 07:27:47 -07:00
|
|
|
const meta = await this.api('meta', {
|
|
|
|
detail: false
|
|
|
|
});
|
2017-11-15 10:06:52 -08:00
|
|
|
this.meta = {
|
|
|
|
data: meta,
|
|
|
|
chachedAt: new Date()
|
|
|
|
};
|
|
|
|
this.isMetaFetching = false;
|
|
|
|
this.emit('_meta_fetched_');
|
|
|
|
res(meta);
|
|
|
|
} else {
|
|
|
|
res(this.meta.data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 12:43:00 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the URL safe base64 string to a Uint8Array
|
|
|
|
* @param base64String base64 string
|
|
|
|
*/
|
|
|
|
function urlBase64ToUint8Array(base64String: string): Uint8Array {
|
|
|
|
const padding = '='.repeat((4 - base64String.length % 4) % 4);
|
|
|
|
const base64 = (base64String + padding)
|
2019-05-04 17:27:55 -07:00
|
|
|
.replace(/-/g, '+')
|
2017-11-22 12:43:00 -08:00
|
|
|
.replace(/_/g, '/');
|
|
|
|
|
|
|
|
const rawData = window.atob(base64);
|
|
|
|
const outputArray = new Uint8Array(rawData.length);
|
|
|
|
|
|
|
|
for (let i = 0; i < rawData.length; ++i) {
|
|
|
|
outputArray[i] = rawData.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return outputArray;
|
|
|
|
}
|