2018-02-21 20:05:19 +00:00
|
|
|
import Vue from 'vue';
|
2017-11-15 18:06:52 +00:00
|
|
|
import { EventEmitter } from 'eventemitter3';
|
2018-03-05 23:35:25 +00:00
|
|
|
import * as merge from 'object-assign-deep';
|
2018-02-24 15:18:09 +00:00
|
|
|
|
2018-03-05 04:40:21 +00:00
|
|
|
import { host, apiUrl, swPublickey, version, lang, googleMapsApiKey } from '../config';
|
2017-11-15 18:06:52 +00:00
|
|
|
import Progress from './scripts/loading';
|
2018-03-07 08:48:32 +00:00
|
|
|
import { HomeStreamManager } from './scripts/streaming/home';
|
|
|
|
import { DriveStreamManager } from './scripts/streaming/drive';
|
|
|
|
import { ServerStreamManager } from './scripts/streaming/server';
|
|
|
|
import { RequestsStreamManager } from './scripts/streaming/requests';
|
|
|
|
import { MessagingIndexStreamManager } from './scripts/streaming/messaging-index';
|
|
|
|
import { OthelloStreamManager } from './scripts/streaming/othello';
|
2017-11-15 18:06:52 +00:00
|
|
|
|
2018-02-21 20:05:19 +00:00
|
|
|
import Err from '../common/views/components/connect-failed.vue';
|
|
|
|
|
2018-03-03 05:42:25 +00:00
|
|
|
//#region api requests
|
|
|
|
let spinner = null;
|
|
|
|
let pending = 0;
|
|
|
|
//#endregion
|
|
|
|
|
2018-02-20 17:53:34 +00:00
|
|
|
export type API = {
|
|
|
|
chooseDriveFile: (opts: {
|
|
|
|
title?: string;
|
|
|
|
currentFolder?: any;
|
|
|
|
multiple?: boolean;
|
|
|
|
}) => Promise<any>;
|
|
|
|
|
|
|
|
chooseDriveFolder: (opts: {
|
|
|
|
title?: string;
|
|
|
|
currentFolder?: any;
|
|
|
|
}) => Promise<any>;
|
|
|
|
|
|
|
|
dialog: (opts: {
|
|
|
|
title: string;
|
|
|
|
text: string;
|
2018-03-02 09:46:08 +00:00
|
|
|
actions?: Array<{
|
2018-02-20 17:53:34 +00:00
|
|
|
text: string;
|
|
|
|
id?: string;
|
|
|
|
}>;
|
|
|
|
}) => Promise<string>;
|
|
|
|
|
|
|
|
input: (opts: {
|
|
|
|
title: string;
|
|
|
|
placeholder?: string;
|
|
|
|
default?: string;
|
|
|
|
}) => Promise<string>;
|
|
|
|
|
2018-02-21 17:15:46 +00:00
|
|
|
post: (opts?: {
|
|
|
|
reply?: any;
|
|
|
|
repost?: any;
|
|
|
|
}) => void;
|
2018-02-20 17:53:34 +00:00
|
|
|
|
|
|
|
notify: (message: string) => void;
|
|
|
|
};
|
|
|
|
|
2017-11-15 18:06:52 +00: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 14:53:07 +00:00
|
|
|
public app: Vue;
|
|
|
|
|
|
|
|
public new(vm, props) {
|
|
|
|
const w = new vm({
|
|
|
|
parent: this.app,
|
|
|
|
propsData: props
|
|
|
|
}).$mount();
|
|
|
|
document.body.appendChild(w.$el);
|
|
|
|
}
|
|
|
|
|
2017-11-15 18:06:52 +00:00
|
|
|
/**
|
|
|
|
* A signing user
|
|
|
|
*/
|
2017-11-16 00:00:24 +00:00
|
|
|
public i: { [x: string]: any };
|
2017-11-15 18:06:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether signed in
|
|
|
|
*/
|
2018-02-10 05:56:33 +00:00
|
|
|
public get isSignedIn() {
|
2017-11-15 18:06:52 +00:00
|
|
|
return this.i != null;
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:06:36 +00:00
|
|
|
/**
|
|
|
|
* Whether is debug mode
|
|
|
|
*/
|
|
|
|
public get debug() {
|
|
|
|
return localStorage.getItem('debug') == 'true';
|
|
|
|
}
|
|
|
|
|
2018-03-04 09:50:30 +00:00
|
|
|
/**
|
|
|
|
* Whether enable sounds
|
|
|
|
*/
|
|
|
|
public get isEnableSounds() {
|
|
|
|
return localStorage.getItem('enableSounds') == 'true';
|
|
|
|
}
|
|
|
|
|
2018-02-20 17:53:34 +00:00
|
|
|
public apis: API;
|
|
|
|
|
2017-11-15 18:06:52 +00:00
|
|
|
/**
|
2017-11-16 16:24:44 +00:00
|
|
|
* A connection manager of home stream
|
2017-11-15 18:06:52 +00:00
|
|
|
*/
|
2017-11-16 16:24:44 +00:00
|
|
|
public stream: HomeStreamManager;
|
2017-11-15 18:06:52 +00:00
|
|
|
|
2018-02-09 09:28:06 +00:00
|
|
|
/**
|
|
|
|
* Connection managers
|
|
|
|
*/
|
|
|
|
public streams: {
|
|
|
|
driveStream: DriveStreamManager;
|
|
|
|
serverStream: ServerStreamManager;
|
|
|
|
requestsStream: RequestsStreamManager;
|
|
|
|
messagingIndexStream: MessagingIndexStreamManager;
|
2018-03-07 08:48:32 +00:00
|
|
|
othelloStream: OthelloStreamManager;
|
2018-02-10 01:52:26 +00:00
|
|
|
} = {
|
|
|
|
driveStream: null,
|
|
|
|
serverStream: null,
|
|
|
|
requestsStream: null,
|
2018-03-07 08:48:32 +00:00
|
|
|
messagingIndexStream: null,
|
|
|
|
othelloStream: null
|
2018-02-09 09:28:06 +00:00
|
|
|
};
|
|
|
|
|
2017-11-20 18:40:09 +00:00
|
|
|
/**
|
|
|
|
* A registration of service worker
|
|
|
|
*/
|
|
|
|
private swRegistration: ServiceWorkerRegistration = null;
|
|
|
|
|
2017-11-21 01:01:00 +00:00
|
|
|
/**
|
|
|
|
* Whether should register ServiceWorker
|
|
|
|
*/
|
|
|
|
private shouldRegisterSw: boolean;
|
|
|
|
|
2018-02-11 13:04:08 +00:00
|
|
|
/**
|
|
|
|
* ウィンドウシステム
|
|
|
|
*/
|
|
|
|
public windows = new WindowSystem();
|
|
|
|
|
2017-11-21 01:01:00 +00:00
|
|
|
/**
|
|
|
|
* MiOSインスタンスを作成します
|
|
|
|
* @param shouldRegisterSw ServiceWorkerを登録するかどうか
|
|
|
|
*/
|
|
|
|
constructor(shouldRegisterSw = false) {
|
2017-11-15 18:06:52 +00:00
|
|
|
super();
|
|
|
|
|
2017-11-21 01:01:00 +00:00
|
|
|
this.shouldRegisterSw = shouldRegisterSw;
|
|
|
|
|
2018-02-09 09:28:06 +00:00
|
|
|
this.streams.serverStream = new ServerStreamManager();
|
|
|
|
this.streams.requestsStream = new RequestsStreamManager();
|
|
|
|
|
2017-11-15 18:06:52 +00:00
|
|
|
//#region BIND
|
2017-11-20 22:06:36 +00:00
|
|
|
this.log = this.log.bind(this);
|
|
|
|
this.logInfo = this.logInfo.bind(this);
|
|
|
|
this.logWarn = this.logWarn.bind(this);
|
|
|
|
this.logError = this.logError.bind(this);
|
2017-11-15 18:06:52 +00:00
|
|
|
this.init = this.init.bind(this);
|
|
|
|
this.api = this.api.bind(this);
|
|
|
|
this.getMeta = this.getMeta.bind(this);
|
2017-11-20 22:06:36 +00:00
|
|
|
this.registerSw = this.registerSw.bind(this);
|
2017-11-15 18:06:52 +00:00
|
|
|
//#endregion
|
2018-02-09 09:28:06 +00:00
|
|
|
|
|
|
|
this.once('signedin', () => {
|
|
|
|
// Init home stream manager
|
2018-02-26 10:23:53 +00:00
|
|
|
this.stream = new HomeStreamManager(this, this.i);
|
2018-02-09 09:28:06 +00:00
|
|
|
|
|
|
|
// Init other stream manager
|
|
|
|
this.streams.driveStream = new DriveStreamManager(this.i);
|
|
|
|
this.streams.messagingIndexStream = new MessagingIndexStreamManager(this.i);
|
2018-03-07 08:48:32 +00:00
|
|
|
this.streams.othelloStream = new OthelloStreamManager(this.i);
|
2018-02-09 09:28:06 +00:00
|
|
|
});
|
2018-02-13 05:05:41 +00:00
|
|
|
|
2018-03-05 04:40:21 +00:00
|
|
|
if (this.debug) {
|
|
|
|
(window as any).os = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:39:47 +00:00
|
|
|
private googleMapsIniting = false;
|
|
|
|
|
2018-03-05 04:40:21 +00:00
|
|
|
public getGoogleMaps() {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
if ((window as any).google && (window as any).google.maps) {
|
|
|
|
res((window as any).google.maps);
|
|
|
|
} else {
|
|
|
|
this.once('init-google-maps', () => {
|
|
|
|
res((window as any).google.maps);
|
|
|
|
});
|
2018-03-05 22:39:47 +00:00
|
|
|
|
|
|
|
//#region load google maps api
|
|
|
|
if (!this.googleMapsIniting) {
|
|
|
|
this.googleMapsIniting = true;
|
|
|
|
(window as any).initGoogleMaps = () => {
|
|
|
|
this.emit('init-google-maps');
|
|
|
|
};
|
|
|
|
const head = document.getElementsByTagName('head')[0];
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.setAttribute('src', `https://maps.googleapis.com/maps/api/js?key=${googleMapsApiKey}&callback=initGoogleMaps`);
|
|
|
|
script.setAttribute('async', 'true');
|
|
|
|
script.setAttribute('defer', 'true');
|
|
|
|
head.appendChild(script);
|
|
|
|
}
|
|
|
|
//#endregion
|
2018-03-05 04:40:21 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-15 18:06:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 22:06:36 +00:00
|
|
|
public log(...args) {
|
|
|
|
if (!this.debug) return;
|
|
|
|
console.log.apply(null, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
public logInfo(...args) {
|
|
|
|
if (!this.debug) return;
|
|
|
|
console.info.apply(null, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
public logWarn(...args) {
|
|
|
|
if (!this.debug) return;
|
|
|
|
console.warn.apply(null, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
public logError(...args) {
|
|
|
|
if (!this.debug) return;
|
|
|
|
console.error.apply(null, args);
|
|
|
|
}
|
|
|
|
|
2018-02-26 10:23:53 +00:00
|
|
|
public signout() {
|
|
|
|
localStorage.removeItem('me');
|
|
|
|
document.cookie = `i=; domain=.${host}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
|
|
|
|
location.href = '/';
|
|
|
|
}
|
|
|
|
|
2017-11-15 18:06:52 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2018-02-24 15:18:09 +00:00
|
|
|
fetch(`${apiUrl}/i`, {
|
2017-11-15 18:06:52 +00:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
i: token
|
|
|
|
})
|
2017-11-16 16:24:44 +00:00
|
|
|
})
|
|
|
|
// When success
|
|
|
|
.then(res => {
|
2017-11-15 18:06:52 +00:00
|
|
|
// When failed to authenticate user
|
|
|
|
if (res.status !== 200) {
|
2018-02-26 10:23:53 +00:00
|
|
|
return this.signout();
|
2017-11-15 18:06:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 16:24:44 +00:00
|
|
|
// Parse response
|
2017-11-15 18:06:52 +00:00
|
|
|
res.json().then(i => {
|
|
|
|
me = i;
|
|
|
|
me.token = token;
|
|
|
|
done();
|
|
|
|
});
|
2017-11-16 16:24:44 +00:00
|
|
|
})
|
|
|
|
// When failure
|
|
|
|
.catch(() => {
|
2017-11-15 18:06:52 +00:00
|
|
|
// Render the error screen
|
2018-02-21 20:05:19 +00:00
|
|
|
document.body.innerHTML = '<div id="err"></div>';
|
|
|
|
new Vue({
|
|
|
|
render: createEl => createEl(Err)
|
|
|
|
}).$mount('#err');
|
2017-11-16 16:24:44 +00:00
|
|
|
|
2017-11-15 18:06:52 +00:00
|
|
|
Progress.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (cb) cb(me);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// フェッチが完了したとき
|
|
|
|
const fetched = me => {
|
|
|
|
if (me) {
|
2018-03-05 23:35:25 +00:00
|
|
|
// デフォルトの設定をマージ
|
|
|
|
me.client_settings = Object.assign({
|
|
|
|
fetchOnScroll: true,
|
|
|
|
showMaps: true,
|
2018-03-06 05:33:01 +00:00
|
|
|
showPostFormOnTopOfTl: false,
|
|
|
|
gradientWindowHeader: false
|
2018-03-05 23:35:25 +00:00
|
|
|
}, me.client_settings);
|
|
|
|
|
2017-11-15 18:06:52 +00:00
|
|
|
// ローカルストレージにキャッシュ
|
|
|
|
localStorage.setItem('me', JSON.stringify(me));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.i = me;
|
|
|
|
|
2018-02-09 09:28:06 +00:00
|
|
|
this.emit('signedin');
|
2017-11-15 18:06:52 +00:00
|
|
|
|
|
|
|
// Finish init
|
|
|
|
callback();
|
2017-11-20 18:40:09 +00:00
|
|
|
|
2017-11-20 22:06:36 +00:00
|
|
|
//#region Post
|
|
|
|
|
|
|
|
// Init service worker
|
2017-11-21 01:01:00 +00:00
|
|
|
if (this.shouldRegisterSw) this.registerSw();
|
2017-11-20 22:06:36 +00:00
|
|
|
|
2017-11-20 18:40:09 +00:00
|
|
|
//#endregion
|
2017-11-15 18:06:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Get cached account data
|
|
|
|
const cachedMe = JSON.parse(localStorage.getItem('me'));
|
|
|
|
|
2017-11-20 22:06:36 +00:00
|
|
|
// キャッシュがあったとき
|
2017-11-15 18:06:52 +00:00
|
|
|
if (cachedMe) {
|
2017-11-20 22:06:36 +00:00
|
|
|
// とりあえずキャッシュされたデータでお茶を濁して(?)おいて、
|
2017-11-15 18:06:52 +00:00
|
|
|
fetched(cachedMe);
|
|
|
|
|
|
|
|
// 後から新鮮なデータをフェッチ
|
|
|
|
fetchme(cachedMe.token, freshData => {
|
2018-03-05 23:35:25 +00:00
|
|
|
merge(cachedMe, freshData);
|
2017-11-15 18:06:52 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Get token from cookie
|
|
|
|
const i = (document.cookie.match(/i=(!\w+)/) || [null, null])[1];
|
|
|
|
|
|
|
|
fetchme(i, fetched);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:06:36 +00:00
|
|
|
/**
|
|
|
|
* Register service worker
|
|
|
|
*/
|
|
|
|
private registerSw() {
|
|
|
|
// 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-02-10 05:56:33 +00:00
|
|
|
if (!this.isSignedIn) return;
|
2017-11-20 22:06:36 +00:00
|
|
|
|
|
|
|
// When service worker activated
|
|
|
|
navigator.serviceWorker.ready.then(registration => {
|
|
|
|
this.log('[sw] ready: ', registration);
|
|
|
|
|
|
|
|
this.swRegistration = registration;
|
|
|
|
|
|
|
|
// Options of pushManager.subscribe
|
2017-11-22 20:43:00 +00:00
|
|
|
// SEE: https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe#Parameters
|
2017-11-20 22:06:36 +00: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 20:43:00 +00:00
|
|
|
userVisibleOnly: true,
|
|
|
|
|
|
|
|
// A public key your push server will use to send
|
|
|
|
// messages to client apps via a push server.
|
2018-02-24 15:18:09 +00:00
|
|
|
applicationServerKey: urlBase64ToUint8Array(swPublickey)
|
2017-11-20 22:06:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Subscribe push notification
|
|
|
|
this.swRegistration.pushManager.subscribe(opts).then(subscription => {
|
|
|
|
this.log('[sw] Subscribe OK:', 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-23 00:52:45 +00:00
|
|
|
})
|
|
|
|
// When subscribe failed
|
|
|
|
.catch(async (err: Error) => {
|
2017-11-20 22:06:36 +00:00
|
|
|
this.logError('[sw] Subscribe Error:', err);
|
2017-11-22 21:26:22 +00:00
|
|
|
|
2017-11-23 00:52:45 +00:00
|
|
|
// 通知が許可されていなかったとき
|
|
|
|
if (err.name == 'NotAllowedError') {
|
|
|
|
this.logError('[sw] Subscribe failed due to notification not allowed');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 21:26:22 +00:00
|
|
|
// 違うapplicationServerKey (または gcm_sender_id)のサブスクリプションが
|
|
|
|
// 既に存在していることが原因でエラーになった可能性があるので、
|
|
|
|
// そのサブスクリプションを解除しておく
|
|
|
|
const subscription = await this.swRegistration.pushManager.getSubscription();
|
|
|
|
if (subscription) subscription.unsubscribe();
|
2017-11-20 18:40:09 +00:00
|
|
|
});
|
2017-11-20 22:06:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// The path of service worker script
|
2018-02-24 15:18:09 +00:00
|
|
|
const sw = `/sw.${version}.${lang}.js`;
|
2017-11-20 22:06:36 +00:00
|
|
|
|
|
|
|
// Register service worker
|
|
|
|
navigator.serviceWorker.register(sw).then(registration => {
|
|
|
|
// 登録成功
|
|
|
|
this.logInfo('[sw] Registration successful with scope: ', registration.scope);
|
2017-11-20 18:40:09 +00:00
|
|
|
}).catch(err => {
|
2017-11-20 22:06:36 +00:00
|
|
|
// 登録失敗 :(
|
|
|
|
this.logError('[sw] Registration failed: ', err);
|
2017-11-20 18:40:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-15 18:06:52 +00:00
|
|
|
/**
|
|
|
|
* Misskey APIにリクエストします
|
|
|
|
* @param endpoint エンドポイント名
|
|
|
|
* @param data パラメータ
|
|
|
|
*/
|
2018-03-03 05:42:25 +00:00
|
|
|
public api(endpoint: string, data: { [x: string]: any } = {}): Promise<{ [x: string]: any }> {
|
|
|
|
if (++pending === 1) {
|
|
|
|
spinner = document.createElement('div');
|
|
|
|
spinner.setAttribute('id', 'wait');
|
|
|
|
document.body.appendChild(spinner);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append a credential
|
|
|
|
if (this.isSignedIn) (data as any).i = this.i.token;
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
//const viaStream = localStorage.getItem('enableExperimental') == 'true';
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
/*if (viaStream) {
|
|
|
|
const stream = this.stream.borrow();
|
|
|
|
const id = Math.random().toString();
|
|
|
|
stream.once(`api-res:${id}`, res => {
|
|
|
|
resolve(res);
|
|
|
|
});
|
|
|
|
stream.send({
|
|
|
|
type: 'api',
|
|
|
|
id,
|
|
|
|
endpoint,
|
|
|
|
data
|
|
|
|
});
|
|
|
|
} else {*/
|
|
|
|
// Send request
|
|
|
|
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
credentials: endpoint === 'signin' ? 'include' : 'omit',
|
|
|
|
cache: 'no-cache'
|
|
|
|
}).then(res => {
|
|
|
|
if (--pending === 0) spinner.parentNode.removeChild(spinner);
|
|
|
|
if (res.status === 200) {
|
|
|
|
res.json().then(resolve);
|
|
|
|
} else if (res.status === 204) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
res.json().then(err => {
|
|
|
|
reject(err.error);
|
|
|
|
}, reject);
|
|
|
|
}
|
|
|
|
}).catch(reject);
|
|
|
|
/*}*/
|
|
|
|
});
|
2017-11-15 18:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 20:43:00 +00:00
|
|
|
|
2018-02-11 13:04:08 +00:00
|
|
|
class WindowSystem {
|
|
|
|
private windows = new Set();
|
|
|
|
|
|
|
|
public add(window) {
|
|
|
|
this.windows.add(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
public remove(window) {
|
|
|
|
this.windows.delete(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getAll() {
|
|
|
|
return this.windows;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 20:43:00 +00: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)
|
|
|
|
.replace(/\-/g, '+')
|
|
|
|
.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;
|
|
|
|
}
|