2018-10-07 04:06:17 +02:00
|
|
|
import autobind from 'autobind-decorator';
|
2018-02-21 21:05:19 +01:00
|
|
|
import Vue from 'vue';
|
2017-11-15 19:06:52 +01:00
|
|
|
import { EventEmitter } from 'eventemitter3';
|
2018-02-24 16:18:09 +01:00
|
|
|
|
2020-02-19 22:08:49 +01:00
|
|
|
import { apiUrl, version } from './config';
|
2020-01-29 20:37:25 +01:00
|
|
|
import Progress from './scripts/loading';
|
2017-11-15 19:06:52 +01:00
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
import Stream from './scripts/stream';
|
2020-03-31 02:11:43 +02:00
|
|
|
import store from './store';
|
2018-03-03 06:42:25 +01:00
|
|
|
|
2017-11-15 19:06:52 +01:00
|
|
|
/**
|
|
|
|
* Misskey Operating System
|
|
|
|
*/
|
|
|
|
export default class MiOS extends EventEmitter {
|
2018-02-22 15:53:07 +01:00
|
|
|
public app: Vue;
|
|
|
|
|
2020-03-31 02:11:43 +02:00
|
|
|
public store: ReturnType<typeof store>;
|
2018-04-29 10:17:15 +02:00
|
|
|
|
2017-11-15 19:06:52 +01:00
|
|
|
/**
|
2017-11-16 17:24:44 +01:00
|
|
|
* A connection manager of home stream
|
2017-11-15 19:06:52 +01:00
|
|
|
*/
|
2018-10-07 04:06:17 +02:00
|
|
|
public stream: Stream;
|
2018-02-09 10:28:06 +01:00
|
|
|
|
2017-11-20 19:40:09 +01:00
|
|
|
/**
|
|
|
|
* A registration of service worker
|
|
|
|
*/
|
|
|
|
private swRegistration: ServiceWorkerRegistration = null;
|
|
|
|
|
2020-03-31 02:11:43 +02:00
|
|
|
constructor(vuex: MiOS['store']) {
|
|
|
|
super();
|
|
|
|
this.store = vuex;
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:06:17 +02:00
|
|
|
@autobind
|
2018-02-26 11:23:53 +01:00
|
|
|
public signout() {
|
2018-05-27 06:49:09 +02:00
|
|
|
this.store.dispatch('logout');
|
2018-02-26 11:23:53 +01:00
|
|
|
location.href = '/';
|
|
|
|
}
|
|
|
|
|
2017-11-15 19:06:52 +01:00
|
|
|
/**
|
|
|
|
* Initialize MiOS (boot)
|
|
|
|
* @param callback A function that call when initialized
|
|
|
|
*/
|
2018-10-07 04:06:17 +02:00
|
|
|
@autobind
|
2020-02-12 21:08:01 +01:00
|
|
|
public async init(callback) {
|
|
|
|
const finish = () => {
|
|
|
|
callback();
|
2020-02-11 12:05:47 +01:00
|
|
|
|
|
|
|
this.store.dispatch('instance/fetch').then(() => {
|
|
|
|
// Init service worker
|
|
|
|
if (this.store.state.instance.meta.swPublickey) this.registerSw(this.store.state.instance.meta.swPublickey);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-15 19:06:52 +01:00
|
|
|
// ユーザーをフェッチしてコールバックする
|
|
|
|
const fetchme = (token, cb) => {
|
|
|
|
let me = null;
|
|
|
|
|
|
|
|
// Return when not signed in
|
2020-02-14 15:59:54 +01:00
|
|
|
if (token == null || token === 'null') {
|
2017-11-15 19:06:52 +01:00
|
|
|
return done();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch user
|
2018-02-24 16:18:09 +01:00
|
|
|
fetch(`${apiUrl}/i`, {
|
2017-11-15 19:06:52 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
i: token
|
|
|
|
})
|
2017-11-16 17:24:44 +01:00
|
|
|
})
|
|
|
|
// When success
|
|
|
|
.then(res => {
|
2017-11-15 19:06:52 +01:00
|
|
|
// When failed to authenticate user
|
2018-09-25 14:23:26 +02:00
|
|
|
if (res.status !== 200 && res.status < 500) {
|
2018-02-26 11:23:53 +01:00
|
|
|
return this.signout();
|
2017-11-15 19:06:52 +01:00
|
|
|
}
|
|
|
|
|
2017-11-16 17:24:44 +01:00
|
|
|
// Parse response
|
2017-11-15 19:06:52 +01:00
|
|
|
res.json().then(i => {
|
|
|
|
me = i;
|
2018-04-07 20:58:11 +02:00
|
|
|
me.token = token;
|
2017-11-15 19:06:52 +01:00
|
|
|
done();
|
|
|
|
});
|
2017-11-16 17:24:44 +01:00
|
|
|
})
|
|
|
|
// When failure
|
|
|
|
.catch(() => {
|
2017-11-15 19:06:52 +01:00
|
|
|
// Render the error screen
|
2020-02-10 15:17:42 +01:00
|
|
|
document.body.innerHTML = '<div id="err">Oops!</div>';
|
2017-11-16 17:24:44 +01:00
|
|
|
|
2017-11-15 19:06:52 +01:00
|
|
|
Progress.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (cb) cb(me);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// フェッチが完了したとき
|
2018-05-27 06:49:09 +02:00
|
|
|
const fetched = () => {
|
2018-02-09 10:28:06 +01:00
|
|
|
this.emit('signedin');
|
2017-11-15 19:06:52 +01:00
|
|
|
|
2018-10-09 08:08:31 +02:00
|
|
|
this.initStream();
|
2018-10-07 04:06:17 +02:00
|
|
|
|
2017-11-15 19:06:52 +01:00
|
|
|
// Finish init
|
2020-02-12 21:08:01 +01:00
|
|
|
finish();
|
2017-11-15 19:06:52 +01:00
|
|
|
};
|
|
|
|
|
2017-11-20 23:06:36 +01:00
|
|
|
// キャッシュがあったとき
|
2018-05-27 06:49:09 +02:00
|
|
|
if (this.store.state.i != null) {
|
|
|
|
if (this.store.state.i.token == null) {
|
2018-04-07 21:44:59 +02:00
|
|
|
this.signout();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:06:36 +01:00
|
|
|
// とりあえずキャッシュされたデータでお茶を濁して(?)おいて、
|
2018-05-27 06:49:09 +02:00
|
|
|
fetched();
|
2017-11-15 19:06:52 +01:00
|
|
|
|
|
|
|
// 後から新鮮なデータをフェッチ
|
2018-05-27 06:49:09 +02:00
|
|
|
fetchme(this.store.state.i.token, freshData => {
|
|
|
|
this.store.dispatch('mergeMe', freshData);
|
2017-11-15 19:06:52 +01:00
|
|
|
});
|
|
|
|
} else {
|
2020-01-29 20:37:25 +01:00
|
|
|
// Get token from localStorage
|
2020-03-20 05:56:22 +01:00
|
|
|
let i = localStorage.getItem('i');
|
|
|
|
|
|
|
|
// 連携ログインの場合用にCookieを参照する
|
|
|
|
if (i == null || i === 'null') {
|
|
|
|
i = (document.cookie.match(/igi=(\w+)/) || [null, null])[1];
|
|
|
|
}
|
2020-03-04 03:45:33 +01:00
|
|
|
|
2018-04-29 10:17:15 +02:00
|
|
|
fetchme(i, me => {
|
|
|
|
if (me) {
|
2018-05-27 06:49:09 +02:00
|
|
|
this.store.dispatch('login', me);
|
|
|
|
fetched();
|
2018-04-29 10:17:15 +02:00
|
|
|
} else {
|
2018-10-20 04:24:02 +02:00
|
|
|
this.initStream();
|
|
|
|
|
2018-04-29 10:17:15 +02:00
|
|
|
// Finish init
|
2020-02-12 21:08:01 +01:00
|
|
|
finish();
|
2018-04-29 10:17:15 +02:00
|
|
|
}
|
|
|
|
});
|
2017-11-15 19:06:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 08:08:31 +02:00
|
|
|
@autobind
|
|
|
|
private initStream() {
|
|
|
|
this.stream = new Stream(this);
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:06:36 +01:00
|
|
|
/**
|
|
|
|
* Register service worker
|
|
|
|
*/
|
2018-10-07 04:06:17 +02:00
|
|
|
@autobind
|
2019-07-23 19:16:41 +02:00
|
|
|
private registerSw(swPublickey: string) {
|
2017-11-20 23:06:36 +01: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-27 06:49:09 +02:00
|
|
|
if (!this.store.getters.isSignedIn) return;
|
2017-11-20 23:06:36 +01:00
|
|
|
|
|
|
|
// When service worker activated
|
|
|
|
navigator.serviceWorker.ready.then(registration => {
|
|
|
|
this.swRegistration = registration;
|
|
|
|
|
|
|
|
// Options of pushManager.subscribe
|
2017-11-22 21:43:00 +01:00
|
|
|
// SEE: https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe#Parameters
|
2017-11-20 23:06:36 +01: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 21:43:00 +01:00
|
|
|
userVisibleOnly: true,
|
|
|
|
|
|
|
|
// A public key your push server will use to send
|
|
|
|
// messages to client apps via a push server.
|
2019-03-06 01:24:16 +01:00
|
|
|
applicationServerKey: urlBase64ToUint8Array(swPublickey)
|
2017-11-20 23:06:36 +01: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
|
2020-03-31 02:11:43 +02:00
|
|
|
this.store.dispatch('api', {
|
|
|
|
endpoint: 'sw/register',
|
|
|
|
data: {
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
auth: encode(subscription.getKey('auth')),
|
|
|
|
publickey: encode(subscription.getKey('p256dh'))
|
|
|
|
}
|
2017-11-20 23:06:36 +01:00
|
|
|
});
|
2017-11-23 01:52:45 +01:00
|
|
|
})
|
|
|
|
// When subscribe failed
|
|
|
|
.catch(async (err: Error) => {
|
|
|
|
// 通知が許可されていなかったとき
|
2020-04-04 01:46:54 +02:00
|
|
|
if (err.name === 'NotAllowedError') {
|
2017-11-23 01:52:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 22:26:22 +01:00
|
|
|
// 違うapplicationServerKey (または gcm_sender_id)のサブスクリプションが
|
|
|
|
// 既に存在していることが原因でエラーになった可能性があるので、
|
|
|
|
// そのサブスクリプションを解除しておく
|
|
|
|
const subscription = await this.swRegistration.pushManager.getSubscription();
|
|
|
|
if (subscription) subscription.unsubscribe();
|
2017-11-20 19:40:09 +01:00
|
|
|
});
|
2017-11-20 23:06:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// The path of service worker script
|
2018-11-14 08:35:00 +01:00
|
|
|
const sw = `/sw.${version}.js`;
|
2017-11-20 23:06:36 +01:00
|
|
|
|
|
|
|
// Register service worker
|
2020-01-29 20:37:25 +01:00
|
|
|
navigator.serviceWorker.register(sw);
|
2017-11-20 19:40:09 +01:00
|
|
|
}
|
2017-11-15 19:06:52 +01:00
|
|
|
}
|
2017-11-22 21:43:00 +01: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-05 02:27:55 +02:00
|
|
|
.replace(/-/g, '+')
|
2017-11-22 21:43:00 +01: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;
|
|
|
|
}
|