mirror of
https://github.com/misskey-dev/misskey.git
synced 2026-05-04 07:16:14 +02:00
This commit is contained in:
181
src/web/app/common/mios.ts
Normal file
181
src/web/app/common/mios.ts
Normal file
@@ -0,0 +1,181 @@
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import * as riot from 'riot';
|
||||
import signout from './scripts/signout';
|
||||
import Progress from './scripts/loading';
|
||||
import Connection from './scripts/home-stream';
|
||||
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
|
||||
*/
|
||||
public i: any;
|
||||
|
||||
/**
|
||||
* Whether signed in
|
||||
*/
|
||||
public get isSignedin() {
|
||||
return this.i != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A connection of home stream
|
||||
*/
|
||||
public stream: Connection;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
//#region BIND
|
||||
this.init = this.init.bind(this);
|
||||
this.api = this.api.bind(this);
|
||||
this.getMeta = this.getMeta.bind(this);
|
||||
//#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
|
||||
})
|
||||
}).then(res => { // When success
|
||||
// When failed to authenticate user
|
||||
if (res.status !== 200) {
|
||||
return signout();
|
||||
}
|
||||
|
||||
res.json().then(i => {
|
||||
me = i;
|
||||
me.token = token;
|
||||
done();
|
||||
});
|
||||
}, () => { // When failure
|
||||
// Render the error screen
|
||||
document.body.innerHTML = '<mk-error />';
|
||||
riot.mount('*');
|
||||
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));
|
||||
|
||||
me.on('updated', () => {
|
||||
// キャッシュ更新
|
||||
localStorage.setItem('me', JSON.stringify(me));
|
||||
});
|
||||
}
|
||||
|
||||
this.i = me;
|
||||
|
||||
// Init home stream connection
|
||||
this.stream = this.i ? new Connection(this.i) : null;
|
||||
|
||||
// Finish init
|
||||
callback();
|
||||
};
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
39
src/web/app/common/mixins.ts
Normal file
39
src/web/app/common/mixins.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import * as riot from 'riot';
|
||||
|
||||
import MiOS from './mios';
|
||||
import ServerStreamManager from './scripts/server-stream-manager';
|
||||
import RequestsStreamManager from './scripts/requests-stream-manager';
|
||||
import MessagingIndexStream from './scripts/messaging-index-stream-manager';
|
||||
|
||||
export default (mios: MiOS) => {
|
||||
(riot as any).mixin('os', {
|
||||
mios: mios
|
||||
});
|
||||
|
||||
(riot as any).mixin('i', {
|
||||
init: function() {
|
||||
this.I = mios.i;
|
||||
this.SIGNIN = mios.isSignedin;
|
||||
|
||||
if (this.SIGNIN) {
|
||||
this.on('mount', () => {
|
||||
mios.i.on('updated', this.update);
|
||||
});
|
||||
this.on('unmount', () => {
|
||||
mios.i.off('updated', this.update);
|
||||
});
|
||||
}
|
||||
},
|
||||
me: mios.i
|
||||
});
|
||||
|
||||
(riot as any).mixin('api', {
|
||||
api: mios.api
|
||||
});
|
||||
|
||||
(riot as any).mixin('stream', { stream: mios.stream });
|
||||
|
||||
(riot as any).mixin('server-stream', { serverStream: new ServerStreamManager() });
|
||||
(riot as any).mixin('requests-stream', { requestsStream: new RequestsStreamManager() });
|
||||
(riot as any).mixin('messaging-index-stream', { messagingIndexStream: new MessagingIndexStream(mios.i) });
|
||||
};
|
||||
@@ -1,8 +0,0 @@
|
||||
import * as riot from 'riot';
|
||||
import api from '../scripts/api';
|
||||
|
||||
export default me => {
|
||||
(riot as any).mixin('api', {
|
||||
api: api.bind(null, me ? me.token : null)
|
||||
});
|
||||
};
|
||||
@@ -1,20 +0,0 @@
|
||||
import * as riot from 'riot';
|
||||
|
||||
export default me => {
|
||||
(riot as any).mixin('i', {
|
||||
init: function() {
|
||||
this.I = me;
|
||||
this.SIGNIN = me != null;
|
||||
|
||||
if (this.SIGNIN) {
|
||||
this.on('mount', () => {
|
||||
me.on('updated', this.update);
|
||||
});
|
||||
this.on('unmount', () => {
|
||||
me.off('updated', this.update);
|
||||
});
|
||||
}
|
||||
},
|
||||
me: me
|
||||
});
|
||||
};
|
||||
@@ -1,18 +0,0 @@
|
||||
import * as riot from 'riot';
|
||||
|
||||
import activateMe from './i';
|
||||
import activateApi from './api';
|
||||
import ServerStreamManager from '../scripts/server-stream-manager';
|
||||
import RequestsStreamManager from '../scripts/requests-stream-manager';
|
||||
import MessagingIndexStream from '../scripts/messaging-index-stream-manager';
|
||||
|
||||
export default (me, stream) => {
|
||||
activateMe(me);
|
||||
activateApi(me);
|
||||
|
||||
(riot as any).mixin('stream', { stream });
|
||||
|
||||
(riot as any).mixin('server-stream', { serverStream: new ServerStreamManager() });
|
||||
(riot as any).mixin('requests-stream', { requestsStream: new RequestsStreamManager() });
|
||||
(riot as any).mixin('messaging-index-stream', { messagingIndexStream: new MessagingIndexStream(me) });
|
||||
};
|
||||
@@ -14,7 +14,7 @@ let pending = 0;
|
||||
* @param {any} [data={}] Data
|
||||
* @return {Promise<any>} Response
|
||||
*/
|
||||
export default (i, endpoint, data = {}): Promise<any> => {
|
||||
export default (i, endpoint, data = {}): Promise<{ [x: string]: any }> => {
|
||||
if (++pending === 1) {
|
||||
spinner = document.createElement('div');
|
||||
spinner.setAttribute('id', 'wait');
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
import CONFIG from './config';
|
||||
import MiOS from '../mios';
|
||||
|
||||
declare var VERSION: string;
|
||||
|
||||
export default function() {
|
||||
fetch(CONFIG.apiUrl + '/meta', {
|
||||
method: 'POST'
|
||||
}).then(res => {
|
||||
res.json().then(meta => {
|
||||
if (meta.version != VERSION) {
|
||||
localStorage.setItem('should-refresh', 'true');
|
||||
alert('%i18n:common.update-available%'.replace('{newer}', meta.version).replace('{current}', VERSION));
|
||||
}
|
||||
});
|
||||
});
|
||||
export default async function(mios: MiOS) {
|
||||
const meta = await mios.getMeta();
|
||||
|
||||
if (meta.version != VERSION) {
|
||||
localStorage.setItem('should-refresh', 'true');
|
||||
alert('%i18n:common.update-available%'.replace('{newer}', meta.version).replace('{current}', VERSION));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user