2018-10-07 05:06:17 +03:00
|
|
|
import autobind from 'autobind-decorator';
|
2018-02-21 22:05:19 +02:00
|
|
|
import Vue from 'vue';
|
2017-11-15 20:06:52 +02:00
|
|
|
import { EventEmitter } from 'eventemitter3';
|
2018-03-15 12:53:46 +02:00
|
|
|
import * as uuid from 'uuid';
|
2018-02-24 17:18:09 +02:00
|
|
|
|
2018-04-29 15:41:48 +03:00
|
|
|
import initStore from './store';
|
2018-11-03 10:04:33 +02:00
|
|
|
import { apiUrl, clientVersion as version, lang } from './config';
|
2018-04-29 15:41:48 +03:00
|
|
|
import Progress from './common/scripts/loading';
|
2017-11-15 20:06:52 +02:00
|
|
|
|
2018-04-29 16:04:51 +03:00
|
|
|
import Err from './common/views/components/connect-failed.vue';
|
2018-10-07 05:06:17 +03:00
|
|
|
import Stream from './common/scripts/stream';
|
2018-02-21 22:05:19 +02:00
|
|
|
|
2018-03-03 07:42:25 +02:00
|
|
|
//#region api requests
|
|
|
|
let spinner = null;
|
|
|
|
let pending = 0;
|
|
|
|
//#endregion
|
|
|
|
|
2018-02-20 19:53:34 +02: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 11:46:08 +02:00
|
|
|
actions?: Array<{
|
2018-02-20 19:53:34 +02:00
|
|
|
text: string;
|
|
|
|
id?: string;
|
|
|
|
}>;
|
|
|
|
}) => Promise<string>;
|
|
|
|
|
|
|
|
input: (opts: {
|
|
|
|
title: string;
|
|
|
|
placeholder?: string;
|
|
|
|
default?: string;
|
|
|
|
}) => Promise<string>;
|
|
|
|
|
2018-02-21 19:15:46 +02:00
|
|
|
post: (opts?: {
|
|
|
|
reply?: any;
|
2018-04-07 20:30:37 +03:00
|
|
|
renote?: any;
|
2018-02-21 19:15:46 +02:00
|
|
|
}) => void;
|
2018-02-20 19:53:34 +02:00
|
|
|
|
|
|
|
notify: (message: string) => void;
|
|
|
|
};
|
|
|
|
|
2017-11-15 20:06:52 +02:00
|
|
|
/**
|
|
|
|
* Misskey Operating System
|
|
|
|
*/
|
|
|
|
export default class MiOS extends EventEmitter {
|
|
|
|
/**
|
|
|
|
* Misskeyの /meta で取得できるメタ情報
|
|
|
|
*/
|
|
|
|
private meta: {
|
|
|
|
data: { [x: string]: any };
|
|
|
|
chachedAt: Date;
|
|
|
|
};
|
|
|
|
|
2018-08-19 15:07:18 +03:00
|
|
|
public get instanceName() {
|
2018-09-08 17:19:11 +03:00
|
|
|
return this.meta ? this.meta.data.name : 'Misskey';
|
2018-08-19 15:07:18 +03:00
|
|
|
}
|
|
|
|
|
2017-11-15 20:06:52 +02:00
|
|
|
private isMetaFetching = false;
|
|
|
|
|
2018-02-22 16:53:07 +02:00
|
|
|
public app: Vue;
|
|
|
|
|
2018-11-08 20:44:35 +02:00
|
|
|
@autobind
|
2018-02-22 16:53:07 +02:00
|
|
|
public new(vm, props) {
|
2018-06-05 23:18:08 +03:00
|
|
|
const x = new vm({
|
2018-02-22 16:53:07 +02:00
|
|
|
parent: this.app,
|
|
|
|
propsData: props
|
|
|
|
}).$mount();
|
2018-06-05 23:18:08 +03:00
|
|
|
document.body.appendChild(x.$el);
|
|
|
|
return x;
|
2018-02-22 16:53:07 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 00:06:36 +02:00
|
|
|
/**
|
|
|
|
* Whether is debug mode
|
|
|
|
*/
|
|
|
|
public get debug() {
|
2018-05-20 20:13:39 +03:00
|
|
|
return this.store ? this.store.state.device.debug : false;
|
2018-03-04 11:50:30 +02:00
|
|
|
}
|
|
|
|
|
2018-04-29 11:17:15 +03:00
|
|
|
public store: ReturnType<typeof initStore>;
|
|
|
|
|
2018-02-20 19:53:34 +02:00
|
|
|
public apis: API;
|
|
|
|
|
2017-11-15 20:06:52 +02:00
|
|
|
/**
|
2017-11-16 18:24:44 +02:00
|
|
|
* A connection manager of home stream
|
2017-11-15 20:06:52 +02:00
|
|
|
*/
|
2018-10-07 05:06:17 +03:00
|
|
|
public stream: Stream;
|
2018-02-09 11:28:06 +02:00
|
|
|
|
2017-11-20 20:40:09 +02:00
|
|
|
/**
|
|
|
|
* A registration of service worker
|
|
|
|
*/
|
|
|
|
private swRegistration: ServiceWorkerRegistration = null;
|
|
|
|
|
2017-11-21 03:01:00 +02:00
|
|
|
/**
|
|
|
|
* Whether should register ServiceWorker
|
|
|
|
*/
|
|
|
|
private shouldRegisterSw: boolean;
|
|
|
|
|
2018-02-11 15:04:08 +02:00
|
|
|
/**
|
|
|
|
* ウィンドウシステム
|
|
|
|
*/
|
|
|
|
public windows = new WindowSystem();
|
|
|
|
|
2017-11-21 03:01:00 +02:00
|
|
|
/**
|
|
|
|
* MiOSインスタンスを作成します
|
|
|
|
* @param shouldRegisterSw ServiceWorkerを登録するかどうか
|
|
|
|
*/
|
|
|
|
constructor(shouldRegisterSw = false) {
|
2017-11-15 20:06:52 +02:00
|
|
|
super();
|
|
|
|
|
2017-11-21 03:01:00 +02:00
|
|
|
this.shouldRegisterSw = shouldRegisterSw;
|
|
|
|
|
2018-03-05 06:40:21 +02:00
|
|
|
if (this.debug) {
|
|
|
|
(window as any).os = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 05:06:17 +03:00
|
|
|
@autobind
|
2017-11-21 00:06:36 +02:00
|
|
|
public log(...args) {
|
|
|
|
if (!this.debug) return;
|
|
|
|
console.log.apply(null, args);
|
|
|
|
}
|
|
|
|
|
2018-10-07 05:06:17 +03:00
|
|
|
@autobind
|
2017-11-21 00:06:36 +02:00
|
|
|
public logInfo(...args) {
|
|
|
|
if (!this.debug) return;
|
|
|
|
console.info.apply(null, args);
|
|
|
|
}
|
|
|
|
|
2018-10-07 05:06:17 +03:00
|
|
|
@autobind
|
2017-11-21 00:06:36 +02:00
|
|
|
public logWarn(...args) {
|
|
|
|
if (!this.debug) return;
|
|
|
|
console.warn.apply(null, args);
|
|
|
|
}
|
|
|
|
|
2018-10-07 05:06:17 +03:00
|
|
|
@autobind
|
2017-11-21 00:06:36 +02:00
|
|
|
public logError(...args) {
|
|
|
|
if (!this.debug) return;
|
|
|
|
console.error.apply(null, args);
|
|
|
|
}
|
|
|
|
|
2018-10-07 05:06:17 +03:00
|
|
|
@autobind
|
2018-02-26 12:23:53 +02:00
|
|
|
public signout() {
|
2018-05-27 07:49:09 +03:00
|
|
|
this.store.dispatch('logout');
|
2018-02-26 12:23:53 +02:00
|
|
|
location.href = '/';
|
|
|
|
}
|
|
|
|
|
2017-11-15 20:06:52 +02:00
|
|
|
/**
|
|
|
|
* Initialize MiOS (boot)
|
|
|
|
* @param callback A function that call when initialized
|
|
|
|
*/
|
2018-10-07 05:06:17 +03:00
|
|
|
@autobind
|
2017-11-15 20:06:52 +02:00
|
|
|
public async init(callback) {
|
2018-04-29 11:17:15 +03:00
|
|
|
this.store = initStore(this);
|
|
|
|
|
2017-11-15 20:06:52 +02:00
|
|
|
// ユーザーをフェッチしてコールバックする
|
|
|
|
const fetchme = (token, cb) => {
|
|
|
|
let me = null;
|
|
|
|
|
|
|
|
// Return when not signed in
|
|
|
|
if (token == null) {
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch user
|
2018-02-24 17:18:09 +02:00
|
|
|
fetch(`${apiUrl}/i`, {
|
2017-11-15 20:06:52 +02:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
i: token
|
|
|
|
})
|
2017-11-16 18:24:44 +02:00
|
|
|
})
|
|
|
|
// When success
|
|
|
|
.then(res => {
|
2017-11-15 20:06:52 +02:00
|
|
|
// When failed to authenticate user
|
2018-09-25 15:23:26 +03:00
|
|
|
if (res.status !== 200 && res.status < 500) {
|
2018-02-26 12:23:53 +02:00
|
|
|
return this.signout();
|
2017-11-15 20:06:52 +02:00
|
|
|
}
|
|
|
|
|
2017-11-16 18:24:44 +02:00
|
|
|
// Parse response
|
2017-11-15 20:06:52 +02:00
|
|
|
res.json().then(i => {
|
|
|
|
me = i;
|
2018-04-07 21:58:11 +03:00
|
|
|
me.token = token;
|
2017-11-15 20:06:52 +02:00
|
|
|
done();
|
|
|
|
});
|
2017-11-16 18:24:44 +02:00
|
|
|
})
|
|
|
|
// When failure
|
|
|
|
.catch(() => {
|
2017-11-15 20:06:52 +02:00
|
|
|
// Render the error screen
|
2018-02-21 22:05:19 +02:00
|
|
|
document.body.innerHTML = '<div id="err"></div>';
|
|
|
|
new Vue({
|
|
|
|
render: createEl => createEl(Err)
|
|
|
|
}).$mount('#err');
|
2017-11-16 18:24:44 +02:00
|
|
|
|
2017-11-15 20:06:52 +02:00
|
|
|
Progress.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (cb) cb(me);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// フェッチが完了したとき
|
2018-05-27 07:49:09 +03:00
|
|
|
const fetched = () => {
|
2018-02-09 11:28:06 +02:00
|
|
|
this.emit('signedin');
|
2017-11-15 20:06:52 +02:00
|
|
|
|
2018-10-09 09:08:31 +03:00
|
|
|
this.initStream();
|
2018-10-07 05:06:17 +03:00
|
|
|
|
2017-11-15 20:06:52 +02:00
|
|
|
// Finish init
|
|
|
|
callback();
|
2017-11-20 20:40:09 +02:00
|
|
|
|
2017-11-21 00:06:36 +02:00
|
|
|
// Init service worker
|
2017-11-21 03:01:00 +02:00
|
|
|
if (this.shouldRegisterSw) this.registerSw();
|
2017-11-15 20:06:52 +02:00
|
|
|
};
|
|
|
|
|
2017-11-21 00:06:36 +02:00
|
|
|
// キャッシュがあったとき
|
2018-05-27 07:49:09 +03:00
|
|
|
if (this.store.state.i != null) {
|
|
|
|
if (this.store.state.i.token == null) {
|
2018-04-07 22:44:59 +03:00
|
|
|
this.signout();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:06:36 +02:00
|
|
|
// とりあえずキャッシュされたデータでお茶を濁して(?)おいて、
|
2018-05-27 07:49:09 +03:00
|
|
|
fetched();
|
2017-11-15 20:06:52 +02:00
|
|
|
|
|
|
|
// 後から新鮮なデータをフェッチ
|
2018-05-27 07:49:09 +03:00
|
|
|
fetchme(this.store.state.i.token, freshData => {
|
|
|
|
this.store.dispatch('mergeMe', freshData);
|
2017-11-15 20:06:52 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Get token from cookie
|
|
|
|
const i = (document.cookie.match(/i=(!\w+)/) || [null, null])[1];
|
|
|
|
|
2018-04-29 11:17:15 +03:00
|
|
|
fetchme(i, me => {
|
|
|
|
if (me) {
|
2018-05-27 07:49:09 +03:00
|
|
|
this.store.dispatch('login', me);
|
|
|
|
fetched();
|
2018-04-29 11:17:15 +03:00
|
|
|
} else {
|
2018-10-20 05:24:02 +03:00
|
|
|
this.initStream();
|
|
|
|
|
2018-04-29 11:17:15 +03:00
|
|
|
// Finish init
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
2017-11-15 20:06:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 09:08:31 +03: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('readAllMessagingMessages', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadMessagingMessage: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('unreadMessagingMessage', () => {
|
|
|
|
this.store.dispatch('mergeMe', {
|
|
|
|
hasUnreadMessagingMessage: 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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('clientSettingUpdated', x => {
|
|
|
|
this.store.commit('settings/set', {
|
|
|
|
key: x.key,
|
|
|
|
value: x.value
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('homeUpdated', x => {
|
|
|
|
this.store.commit('settings/setHome', x);
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('mobileHomeUpdated', x => {
|
|
|
|
this.store.commit('settings/setMobileHome', x);
|
|
|
|
});
|
|
|
|
|
|
|
|
main.on('widgetUpdated', x => {
|
|
|
|
this.store.commit('settings/setWidget', {
|
|
|
|
id: x.id,
|
|
|
|
data: x.data
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// トークンが再生成されたとき
|
|
|
|
// このままではMisskeyが利用できないので強制的にサインアウトさせる
|
|
|
|
main.on('myTokenRegenerated', () => {
|
|
|
|
alert('%i18n:common.my-token-regenerated%');
|
|
|
|
this.signout();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:06:36 +02:00
|
|
|
/**
|
|
|
|
* Register service worker
|
|
|
|
*/
|
2018-10-07 05:06:17 +03:00
|
|
|
@autobind
|
2017-11-21 00:06:36 +02:00
|
|
|
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-05-27 07:49:09 +03:00
|
|
|
if (!this.store.getters.isSignedIn) return;
|
2017-11-21 00:06:36 +02: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 22:43:00 +02:00
|
|
|
// SEE: https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe#Parameters
|
2017-11-21 00:06:36 +02: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 22:43:00 +02:00
|
|
|
userVisibleOnly: true,
|
|
|
|
|
|
|
|
// A public key your push server will use to send
|
|
|
|
// messages to client apps via a push server.
|
2018-09-04 06:59:34 +03:00
|
|
|
applicationServerKey: urlBase64ToUint8Array(this.meta.data.swPublickey)
|
2017-11-21 00:06:36 +02: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 02:52:45 +02:00
|
|
|
})
|
|
|
|
// When subscribe failed
|
|
|
|
.catch(async (err: Error) => {
|
2017-11-21 00:06:36 +02:00
|
|
|
this.logError('[sw] Subscribe Error:', err);
|
2017-11-22 23:26:22 +02:00
|
|
|
|
2017-11-23 02:52:45 +02:00
|
|
|
// 通知が許可されていなかったとき
|
|
|
|
if (err.name == 'NotAllowedError') {
|
|
|
|
this.logError('[sw] Subscribe failed due to notification not allowed');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 23:26:22 +02:00
|
|
|
// 違うapplicationServerKey (または gcm_sender_id)のサブスクリプションが
|
|
|
|
// 既に存在していることが原因でエラーになった可能性があるので、
|
|
|
|
// そのサブスクリプションを解除しておく
|
|
|
|
const subscription = await this.swRegistration.pushManager.getSubscription();
|
|
|
|
if (subscription) subscription.unsubscribe();
|
2017-11-20 20:40:09 +02:00
|
|
|
});
|
2017-11-21 00:06:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// The path of service worker script
|
2018-05-20 20:13:39 +03:00
|
|
|
const sw = `/sw.${version}.${lang}.js`;
|
2017-11-21 00:06:36 +02:00
|
|
|
|
|
|
|
// Register service worker
|
|
|
|
navigator.serviceWorker.register(sw).then(registration => {
|
|
|
|
// 登録成功
|
|
|
|
this.logInfo('[sw] Registration successful with scope: ', registration.scope);
|
2017-11-20 20:40:09 +02:00
|
|
|
}).catch(err => {
|
2017-11-21 00:06:36 +02:00
|
|
|
// 登録失敗 :(
|
|
|
|
this.logError('[sw] Registration failed: ', err);
|
2017-11-20 20:40:09 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-15 12:53:46 +02:00
|
|
|
public requests = [];
|
|
|
|
|
2017-11-15 20:06:52 +02:00
|
|
|
/**
|
|
|
|
* Misskey APIにリクエストします
|
|
|
|
* @param endpoint エンドポイント名
|
|
|
|
* @param data パラメータ
|
|
|
|
*/
|
2018-10-07 05:06:17 +03:00
|
|
|
@autobind
|
|
|
|
public api(endpoint: string, data: { [x: string]: any } = {}, forceFetch = false): Promise<{ [x: string]: any }> {
|
2018-03-03 07:42:25 +02:00
|
|
|
if (++pending === 1) {
|
|
|
|
spinner = document.createElement('div');
|
|
|
|
spinner.setAttribute('id', 'wait');
|
|
|
|
document.body.appendChild(spinner);
|
|
|
|
}
|
|
|
|
|
2018-04-13 21:40:12 +03:00
|
|
|
const onFinally = () => {
|
|
|
|
if (--pending === 0) spinner.parentNode.removeChild(spinner);
|
|
|
|
};
|
|
|
|
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
2018-10-13 23:26:36 +03:00
|
|
|
const viaStream = this.stream && this.stream.state == 'connected' && this.store.state.device.apiViaStream && !forceFetch;
|
2018-04-13 03:44:00 +03:00
|
|
|
|
2018-04-11 11:40:01 +03:00
|
|
|
if (viaStream) {
|
2018-10-13 13:16:47 +03:00
|
|
|
const id = Math.random().toString().substr(2, 8);
|
2018-04-11 11:40:01 +03:00
|
|
|
|
2018-10-07 05:06:17 +03:00
|
|
|
this.stream.once(`api:${id}`, res => {
|
2018-04-14 00:18:18 +03:00
|
|
|
if (res == null || Object.keys(res).length == 0) {
|
|
|
|
resolve(null);
|
|
|
|
} else if (res.res) {
|
2018-04-11 11:40:01 +03:00
|
|
|
resolve(res.res);
|
|
|
|
} else {
|
|
|
|
reject(res.e);
|
|
|
|
}
|
2018-03-03 07:42:25 +02:00
|
|
|
});
|
2018-04-11 11:40:01 +03:00
|
|
|
|
2018-10-07 05:06:17 +03:00
|
|
|
this.stream.send('api', {
|
|
|
|
id: id,
|
|
|
|
ep: endpoint,
|
|
|
|
data: data
|
2018-03-03 07:42:25 +02:00
|
|
|
});
|
2018-04-11 11:40:01 +03:00
|
|
|
} else {
|
2018-04-15 11:39:12 +03:00
|
|
|
// Append a credential
|
2018-05-27 07:49:09 +03:00
|
|
|
if (this.store.getters.isSignedIn) (data as any).i = this.store.state.i.token;
|
2018-04-15 11:39:12 +03:00
|
|
|
|
2018-03-15 12:53:46 +02:00
|
|
|
const req = {
|
|
|
|
id: uuid(),
|
|
|
|
date: new Date(),
|
|
|
|
name: endpoint,
|
|
|
|
data,
|
|
|
|
res: null,
|
|
|
|
status: null
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.debug) {
|
|
|
|
this.requests.push(req);
|
|
|
|
}
|
|
|
|
|
2018-03-03 07:42:25 +02:00
|
|
|
// Send request
|
|
|
|
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
credentials: endpoint === 'signin' ? 'include' : 'omit',
|
|
|
|
cache: 'no-cache'
|
2018-03-15 12:53:46 +02:00
|
|
|
}).then(async (res) => {
|
2018-03-15 23:02:18 +02:00
|
|
|
const body = res.status === 204 ? null : await res.json();
|
2018-03-15 12:53:46 +02:00
|
|
|
|
|
|
|
if (this.debug) {
|
|
|
|
req.status = res.status;
|
|
|
|
req.res = body;
|
|
|
|
}
|
|
|
|
|
2018-03-03 07:42:25 +02:00
|
|
|
if (res.status === 200) {
|
2018-03-15 12:53:46 +02:00
|
|
|
resolve(body);
|
2018-03-03 07:42:25 +02:00
|
|
|
} else if (res.status === 204) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
2018-03-15 12:53:46 +02:00
|
|
|
reject(body.error);
|
2018-03-03 07:42:25 +02:00
|
|
|
}
|
|
|
|
}).catch(reject);
|
2018-04-12 00:18:55 +03:00
|
|
|
}
|
2018-03-03 07:42:25 +02:00
|
|
|
});
|
2018-04-13 21:40:12 +03:00
|
|
|
|
|
|
|
promise.then(onFinally, onFinally);
|
|
|
|
|
|
|
|
return promise;
|
2017-11-15 20:06:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-01 04:51:49 +02:00
|
|
|
/**
|
|
|
|
* Misskeyのメタ情報を取得します
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
public getMetaSync() {
|
|
|
|
return this.meta ? this.meta.data : null;
|
|
|
|
}
|
|
|
|
|
2017-11-15 20:06:52 +02:00
|
|
|
/**
|
|
|
|
* Misskeyのメタ情報を取得します
|
|
|
|
* @param force キャッシュを無視するか否か
|
|
|
|
*/
|
2018-10-07 05:06:17 +03:00
|
|
|
@autobind
|
2017-11-15 20:06:52 +02: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 16:27:47 +02:00
|
|
|
const meta = await this.api('meta', {
|
|
|
|
detail: false
|
|
|
|
});
|
2017-11-15 20:06:52 +02: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 22:43:00 +02:00
|
|
|
|
2018-03-15 12:53:46 +02:00
|
|
|
class WindowSystem extends EventEmitter {
|
|
|
|
public windows = new Set();
|
2018-02-11 15:04:08 +02:00
|
|
|
|
|
|
|
public add(window) {
|
|
|
|
this.windows.add(window);
|
2018-03-15 12:53:46 +02:00
|
|
|
this.emit('added', window);
|
2018-02-11 15:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public remove(window) {
|
|
|
|
this.windows.delete(window);
|
2018-03-15 12:53:46 +02:00
|
|
|
this.emit('removed', window);
|
2018-02-11 15:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getAll() {
|
|
|
|
return this.windows;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 22:43:00 +02: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;
|
|
|
|
}
|