summaryrefslogtreecommitdiff
path: root/src/web/app/common/mios.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/web/app/common/mios.ts')
-rw-r--r--src/web/app/common/mios.ts110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/web/app/common/mios.ts b/src/web/app/common/mios.ts
index 9704e92af8..cf7841d848 100644
--- a/src/web/app/common/mios.ts
+++ b/src/web/app/common/mios.ts
@@ -6,6 +6,9 @@ import HomeStreamManager from './scripts/streaming/home-stream-manager';
import CONFIG from './scripts/config';
import api from './scripts/api';
+declare var VERSION: string;
+declare var LANG: string;
+
/**
* Misskey Operating System
*/
@@ -33,20 +36,57 @@ export default class MiOS extends EventEmitter {
}
/**
+ * Whether is debug mode
+ */
+ public get debug() {
+ return localStorage.getItem('debug') == 'true';
+ }
+
+ /**
* A connection manager of home stream
*/
public stream: HomeStreamManager;
+ /**
+ * A registration of service worker
+ */
+ private swRegistration: ServiceWorkerRegistration = null;
+
constructor() {
super();
//#region BIND
+ this.log = this.log.bind(this);
+ this.logInfo = this.logInfo.bind(this);
+ this.logWarn = this.logWarn.bind(this);
+ this.logError = this.logError.bind(this);
this.init = this.init.bind(this);
this.api = this.api.bind(this);
this.getMeta = this.getMeta.bind(this);
+ this.registerSw = this.registerSw.bind(this);
//#endregion
}
+ 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);
+ }
+
/**
* Initialize MiOS (boot)
* @param callback A function that call when initialized
@@ -126,12 +166,21 @@ export default class MiOS extends EventEmitter {
// Finish init
callback();
+
+ //#region Post
+
+ // Init service worker
+ this.registerSw();
+
+ //#endregion
};
// Get cached account data
const cachedMe = JSON.parse(localStorage.getItem('me'));
+ // キャッシュがあったとき
if (cachedMe) {
+ // とりあえずキャッシュされたデータでお茶を濁して(?)おいて、
fetched(cachedMe);
// 後から新鮮なデータをフェッチ
@@ -148,6 +197,67 @@ export default class MiOS extends EventEmitter {
}
/**
+ * 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
+ if (!this.isSignedin) return;
+
+ // When service worker activated
+ navigator.serviceWorker.ready.then(registration => {
+ this.log('[sw] ready: ', registration);
+
+ this.swRegistration = registration;
+
+ // Options of pushManager.subscribe
+ const opts = {
+ // A boolean indicating that the returned push subscription
+ // will only be used for messages whose effect is made visible to the user.
+ userVisibleOnly: true
+ };
+
+ // 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'))
+ });
+ }).then(() => {
+ this.logInfo('[sw] Server Stored Subscription.');
+ }).catch(err => {
+ this.logError('[sw] Subscribe Error:', err);
+ });
+ });
+
+ // The path of service worker script
+ const sw = `/sw.${VERSION}.${LANG}.js`;
+
+ // Register service worker
+ navigator.serviceWorker.register(sw).then(registration => {
+ // 登録成功
+ this.logInfo('[sw] Registration successful with scope: ', registration.scope);
+ }).catch(err => {
+ // 登録失敗 :(
+ this.logError('[sw] Registration failed: ', err);
+ });
+ }
+
+ /**
* Misskey APIにリクエストします
* @param endpoint エンドポイント名
* @param data パラメータ