2018-10-06 20:06:17 -06:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import * as websocket from 'websocket';
|
2019-04-07 06:50:36 -06:00
|
|
|
import { readNotification } from '../common/read-notification';
|
2018-10-06 20:06:17 -06:00
|
|
|
import call from '../call';
|
|
|
|
import readNote from '../../../services/note/read';
|
|
|
|
import Channel from './channel';
|
|
|
|
import channels from './channels';
|
2018-10-11 03:09:41 -06:00
|
|
|
import { EventEmitter } from 'events';
|
2019-04-07 06:50:36 -06:00
|
|
|
import { User } from '../../../models/entities/user';
|
|
|
|
import { App } from '../../../models/entities/app';
|
|
|
|
import { Users, Followings, Mutings } from '../../../models';
|
2020-01-29 12:37:25 -07:00
|
|
|
import { ApiError } from '../error';
|
2018-10-06 20:06:17 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Main stream connection
|
|
|
|
*/
|
|
|
|
export default class Connection {
|
2019-04-07 06:50:36 -06:00
|
|
|
public user?: User;
|
|
|
|
public following: User['id'][] = [];
|
|
|
|
public muting: User['id'][] = [];
|
|
|
|
public app: App;
|
2018-10-06 20:06:17 -06:00
|
|
|
private wsConnection: websocket.connection;
|
2018-10-11 03:09:41 -06:00
|
|
|
public subscriber: EventEmitter;
|
2018-10-06 20:06:17 -06:00
|
|
|
private channels: Channel[] = [];
|
|
|
|
private subscribingNotes: any = {};
|
2019-04-07 06:50:36 -06:00
|
|
|
private followingClock: NodeJS.Timer;
|
|
|
|
private mutingClock: NodeJS.Timer;
|
2018-10-06 20:06:17 -06:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
wsConnection: websocket.connection,
|
2018-10-11 03:09:41 -06:00
|
|
|
subscriber: EventEmitter,
|
2019-04-12 10:43:22 -06:00
|
|
|
user: User | null | undefined,
|
|
|
|
app: App | null | undefined
|
2018-10-06 20:06:17 -06:00
|
|
|
) {
|
|
|
|
this.wsConnection = wsConnection;
|
|
|
|
this.subscriber = subscriber;
|
2019-04-12 10:43:22 -06:00
|
|
|
if (user) this.user = user;
|
|
|
|
if (app) this.app = app;
|
2018-10-06 20:06:17 -06:00
|
|
|
|
|
|
|
this.wsConnection.on('message', this.onWsConnectionMessage);
|
2019-04-07 06:50:36 -06:00
|
|
|
|
|
|
|
if (this.user) {
|
|
|
|
this.updateFollowing();
|
|
|
|
this.followingClock = setInterval(this.updateFollowing, 5000);
|
|
|
|
|
|
|
|
this.updateMuting();
|
|
|
|
this.mutingClock = setInterval(this.updateMuting, 5000);
|
|
|
|
}
|
2018-10-06 20:06:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* クライアントからメッセージ受信時
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
private async onWsConnectionMessage(data: websocket.IMessage) {
|
2019-04-12 10:43:22 -06:00
|
|
|
if (data.utf8Data == null) return;
|
|
|
|
|
2018-10-06 20:06:17 -06:00
|
|
|
const { type, body } = JSON.parse(data.utf8Data);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'api': this.onApiRequest(body); break;
|
|
|
|
case 'readNotification': this.onReadNotification(body); break;
|
|
|
|
case 'subNote': this.onSubscribeNote(body); break;
|
|
|
|
case 'sn': this.onSubscribeNote(body); break; // alias
|
|
|
|
case 'unsubNote': this.onUnsubscribeNote(body); break;
|
|
|
|
case 'un': this.onUnsubscribeNote(body); break; // alias
|
|
|
|
case 'connect': this.onChannelConnectRequested(body); break;
|
|
|
|
case 'disconnect': this.onChannelDisconnectRequested(body); break;
|
|
|
|
case 'channel': this.onChannelMessageRequested(body); break;
|
2018-10-09 12:24:09 -06:00
|
|
|
case 'ch': this.onChannelMessageRequested(body); break; // alias
|
2018-10-06 20:06:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* APIリクエスト要求時
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
private async onApiRequest(payload: any) {
|
|
|
|
// 新鮮なデータを利用するためにユーザーをフェッチ
|
2019-04-07 06:50:36 -06:00
|
|
|
const user = this.user ? await Users.findOne(this.user.id) : null;
|
2018-10-06 20:06:17 -06:00
|
|
|
|
|
|
|
const endpoint = payload.endpoint || payload.ep; // alias
|
|
|
|
|
|
|
|
// 呼び出し
|
|
|
|
call(endpoint, user, this.app, payload.data).then(res => {
|
|
|
|
this.sendMessageToWs(`api:${payload.id}`, { res });
|
2020-01-29 12:37:25 -07:00
|
|
|
}).catch((e: ApiError) => {
|
|
|
|
this.sendMessageToWs(`api:${payload.id}`, {
|
|
|
|
error: {
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
id: e.id,
|
|
|
|
kind: e.kind,
|
|
|
|
...(e.info ? { info: e.info } : {})
|
|
|
|
}
|
|
|
|
});
|
2018-10-06 20:06:17 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
private onReadNotification(payload: any) {
|
|
|
|
if (!payload.id) return;
|
2019-04-12 10:43:22 -06:00
|
|
|
readNotification(this.user!.id, [payload.id]);
|
2018-10-06 20:06:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 投稿購読要求時
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
private onSubscribeNote(payload: any) {
|
|
|
|
if (!payload.id) return;
|
|
|
|
|
|
|
|
if (this.subscribingNotes[payload.id] == null) {
|
|
|
|
this.subscribingNotes[payload.id] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.subscribingNotes[payload.id]++;
|
|
|
|
|
|
|
|
if (this.subscribingNotes[payload.id] == 1) {
|
|
|
|
this.subscriber.on(`noteStream:${payload.id}`, this.onNoteStreamMessage);
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:37:25 -07:00
|
|
|
if (this.user) {
|
2019-04-07 06:50:36 -06:00
|
|
|
readNote(this.user.id, payload.id);
|
2018-10-06 20:06:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 投稿購読解除要求時
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
private onUnsubscribeNote(payload: any) {
|
|
|
|
if (!payload.id) return;
|
|
|
|
|
|
|
|
this.subscribingNotes[payload.id]--;
|
|
|
|
if (this.subscribingNotes[payload.id] <= 0) {
|
|
|
|
delete this.subscribingNotes[payload.id];
|
|
|
|
this.subscriber.off(`noteStream:${payload.id}`, this.onNoteStreamMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
private async onNoteStreamMessage(data: any) {
|
|
|
|
this.sendMessageToWs('noteUpdated', {
|
|
|
|
id: data.body.id,
|
|
|
|
type: data.type,
|
|
|
|
body: data.body.body,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネル接続要求時
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
private onChannelConnectRequested(payload: any) {
|
2018-10-13 04:14:05 -06:00
|
|
|
const { channel, id, params, pong } = payload;
|
|
|
|
this.connectChannel(id, params, channel, pong);
|
2018-10-06 20:06:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネル切断要求時
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
private onChannelDisconnectRequested(payload: any) {
|
|
|
|
const { id } = payload;
|
|
|
|
this.disconnectChannel(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* クライアントにメッセージ送信
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
public sendMessageToWs(type: string, payload: any) {
|
|
|
|
this.wsConnection.send(JSON.stringify({
|
|
|
|
type: type,
|
|
|
|
body: payload
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネルに接続
|
|
|
|
*/
|
|
|
|
@autobind
|
2018-10-13 04:14:05 -06:00
|
|
|
public connectChannel(id: string, params: any, channel: string, pong = false) {
|
2018-11-10 10:22:34 -07:00
|
|
|
if ((channels as any)[channel].requireCredential && this.user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-11 08:01:57 -06:00
|
|
|
// 共有可能チャンネルに接続しようとしていて、かつそのチャンネルに既に接続していたら無意味なので無視
|
|
|
|
if ((channels as any)[channel].shouldShare && this.channels.some(c => c.chName === channel)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ch: Channel = new (channels as any)[channel](id, this);
|
|
|
|
this.channels.push(ch);
|
|
|
|
ch.init(params);
|
2018-10-13 04:14:05 -06:00
|
|
|
|
|
|
|
if (pong) {
|
|
|
|
this.sendMessageToWs('connected', {
|
|
|
|
id: id
|
|
|
|
});
|
|
|
|
}
|
2018-10-06 20:06:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネルから切断
|
2018-10-07 02:06:28 -06:00
|
|
|
* @param id チャンネルコネクションID
|
2018-10-06 20:06:17 -06:00
|
|
|
*/
|
|
|
|
@autobind
|
2018-10-07 02:19:52 -06:00
|
|
|
public disconnectChannel(id: string) {
|
2018-10-06 20:06:17 -06:00
|
|
|
const channel = this.channels.find(c => c.id === id);
|
|
|
|
|
|
|
|
if (channel) {
|
|
|
|
if (channel.dispose) channel.dispose();
|
|
|
|
this.channels = this.channels.filter(c => c.id !== id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 02:06:28 -06:00
|
|
|
/**
|
|
|
|
* チャンネルへメッセージ送信要求時
|
|
|
|
* @param data メッセージ
|
|
|
|
*/
|
2018-10-06 20:06:17 -06:00
|
|
|
@autobind
|
|
|
|
private onChannelMessageRequested(data: any) {
|
|
|
|
const channel = this.channels.find(c => c.id === data.id);
|
|
|
|
if (channel != null && channel.onMessage != null) {
|
|
|
|
channel.onMessage(data.type, data.body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
@autobind
|
|
|
|
private async updateFollowing() {
|
|
|
|
const followings = await Followings.find({
|
|
|
|
where: {
|
2019-04-12 10:43:22 -06:00
|
|
|
followerId: this.user!.id
|
2019-04-07 06:50:36 -06:00
|
|
|
},
|
|
|
|
select: ['followeeId']
|
|
|
|
});
|
|
|
|
|
|
|
|
this.following = followings.map(x => x.followeeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
private async updateMuting() {
|
|
|
|
const mutings = await Mutings.find({
|
|
|
|
where: {
|
2019-04-12 10:43:22 -06:00
|
|
|
muterId: this.user!.id
|
2019-04-07 06:50:36 -06:00
|
|
|
},
|
|
|
|
select: ['muteeId']
|
|
|
|
});
|
|
|
|
|
|
|
|
this.muting = mutings.map(x => x.muteeId);
|
|
|
|
}
|
|
|
|
|
2018-10-06 20:06:17 -06:00
|
|
|
/**
|
|
|
|
* ストリームが切れたとき
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
public dispose() {
|
2018-12-11 04:36:55 -07:00
|
|
|
for (const c of this.channels.filter(c => c.dispose)) {
|
2019-04-12 10:43:22 -06:00
|
|
|
if (c.dispose) c.dispose();
|
2018-12-11 04:36:55 -07:00
|
|
|
}
|
2019-04-07 06:50:36 -06:00
|
|
|
|
|
|
|
if (this.followingClock) clearInterval(this.followingClock);
|
|
|
|
if (this.mutingClock) clearInterval(this.mutingClock);
|
2018-10-06 20:06:17 -06:00
|
|
|
}
|
|
|
|
}
|