2018-10-07 11:06:17 +09:00
|
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
|
import * as websocket from 'websocket';
|
2019-04-07 21:50:36 +09:00
|
|
|
|
import { readNotification } from '../common/read-notification';
|
2018-10-07 11:06:17 +09:00
|
|
|
|
import call from '../call';
|
2021-03-21 15:35:02 +09:00
|
|
|
|
import readNote from '../../../services/note/read';
|
2018-10-07 11:06:17 +09:00
|
|
|
|
import Channel from './channel';
|
|
|
|
|
import channels from './channels';
|
2018-10-11 18:09:41 +09:00
|
|
|
|
import { EventEmitter } from 'events';
|
2019-04-07 21:50:36 +09:00
|
|
|
|
import { User } from '../../../models/entities/user';
|
2020-08-18 22:44:21 +09:00
|
|
|
|
import { Channel as ChannelModel } from '../../../models/entities/channel';
|
|
|
|
|
import { Users, Followings, Mutings, UserProfiles, ChannelFollowings } from '../../../models';
|
2020-01-30 04:37:25 +09:00
|
|
|
|
import { ApiError } from '../error';
|
2020-03-28 18:07:41 +09:00
|
|
|
|
import { AccessToken } from '../../../models/entities/access-token';
|
2020-07-27 13:34:20 +09:00
|
|
|
|
import { UserProfile } from '../../../models/entities/user-profile';
|
2021-02-21 12:26:49 +09:00
|
|
|
|
import { publishChannelStream, publishGroupMessagingStream, publishMessagingStream } from '../../../services/stream';
|
|
|
|
|
import { UserGroup } from '../../../models/entities/user-group';
|
2020-03-28 11:24:37 +09:00
|
|
|
|
|
2018-10-07 11:06:17 +09:00
|
|
|
|
/**
|
|
|
|
|
* Main stream connection
|
|
|
|
|
*/
|
|
|
|
|
export default class Connection {
|
2019-04-07 21:50:36 +09:00
|
|
|
|
public user?: User;
|
2020-07-27 13:34:20 +09:00
|
|
|
|
public userProfile?: UserProfile;
|
2021-01-30 11:09:46 +09:00
|
|
|
|
public following: Set<User['id']> = new Set();
|
|
|
|
|
public muting: Set<User['id']> = new Set();
|
|
|
|
|
public followingChannels: Set<ChannelModel['id']> = new Set();
|
2020-03-29 11:28:55 +09:00
|
|
|
|
public token?: AccessToken;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
private wsConnection: websocket.connection;
|
2018-10-11 18:09:41 +09:00
|
|
|
|
public subscriber: EventEmitter;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
private channels: Channel[] = [];
|
|
|
|
|
private subscribingNotes: any = {};
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
wsConnection: websocket.connection,
|
2018-10-11 18:09:41 +09:00
|
|
|
|
subscriber: EventEmitter,
|
2019-04-13 01:43:22 +09:00
|
|
|
|
user: User | null | undefined,
|
2020-03-28 18:07:41 +09:00
|
|
|
|
token: AccessToken | null | undefined
|
2018-10-07 11:06:17 +09:00
|
|
|
|
) {
|
|
|
|
|
this.wsConnection = wsConnection;
|
|
|
|
|
this.subscriber = subscriber;
|
2019-04-13 01:43:22 +09:00
|
|
|
|
if (user) this.user = user;
|
2020-03-28 18:07:41 +09:00
|
|
|
|
if (token) this.token = token;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
|
|
|
|
|
this.wsConnection.on('message', this.onWsConnectionMessage);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
2020-04-02 22:17:17 +09:00
|
|
|
|
this.subscriber.on('broadcast', async ({ type, body }) => {
|
|
|
|
|
this.onBroadcastMessage(type, body);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
|
if (this.user) {
|
|
|
|
|
this.updateFollowing();
|
|
|
|
|
this.updateMuting();
|
2020-08-18 22:44:21 +09:00
|
|
|
|
this.updateFollowingChannels();
|
2020-07-27 13:34:20 +09:00
|
|
|
|
this.updateUserProfile();
|
2021-03-21 15:14:03 +09:00
|
|
|
|
|
|
|
|
|
this.subscriber.on(`user:${this.user.id}`, ({ type, body }) => {
|
|
|
|
|
this.onUserEvent(type, body);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
|
private onUserEvent(type: string, body: any) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'follow':
|
|
|
|
|
this.following.add(body.id);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'unfollow':
|
|
|
|
|
this.following.delete(body.id);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'mute':
|
|
|
|
|
this.muting.add(body.id);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'unmute':
|
|
|
|
|
this.muting.delete(body.id);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'followChannel':
|
|
|
|
|
this.followingChannels.add(body.id);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'unfollowChannel':
|
|
|
|
|
this.followingChannels.delete(body.id);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'updateUserProfile':
|
|
|
|
|
this.userProfile = body;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2019-04-07 21:50:36 +09:00
|
|
|
|
}
|
2018-10-07 11:06:17 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* クライアントからメッセージ受信時
|
|
|
|
|
*/
|
|
|
|
|
@autobind
|
|
|
|
|
private async onWsConnectionMessage(data: websocket.IMessage) {
|
2019-04-13 01:43:22 +09:00
|
|
|
|
if (data.utf8Data == null) return;
|
|
|
|
|
|
2020-08-18 22:52:54 +09:00
|
|
|
|
let obj: Record<string, any>;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
obj = JSON.parse(data.utf8Data);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { type, body } = obj;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'api': this.onApiRequest(body); break;
|
|
|
|
|
case 'readNotification': this.onReadNotification(body); break;
|
2021-03-21 15:35:02 +09:00
|
|
|
|
case 'subNote': this.onSubscribeNote(body, true); break;
|
|
|
|
|
case 'sn': this.onSubscribeNote(body, true); break; // alias
|
|
|
|
|
case 's': this.onSubscribeNote(body, false); break;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
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-10 03:24:09 +09:00
|
|
|
|
case 'ch': this.onChannelMessageRequested(body); break; // alias
|
2021-02-21 12:26:49 +09:00
|
|
|
|
|
|
|
|
|
// 個々のチャンネルではなくルートレベルでこれらのメッセージを受け取る理由は、
|
|
|
|
|
// クライアントの事情を考慮したとき、入力フォームはノートチャンネルやメッセージのメインコンポーネントとは別
|
|
|
|
|
// なこともあるため、それらのコンポーネントがそれぞれ各チャンネルに接続するようにするのは面倒なため。
|
2021-02-20 20:20:05 +09:00
|
|
|
|
case 'typingOnChannel': this.typingOnChannel(body.channel); break;
|
2021-02-21 12:26:49 +09:00
|
|
|
|
case 'typingOnMessaging': this.typingOnMessaging(body); break;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 22:17:17 +09:00
|
|
|
|
@autobind
|
|
|
|
|
private onBroadcastMessage(type: string, body: any) {
|
|
|
|
|
this.sendMessageToWs(type, body);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 11:06:17 +09:00
|
|
|
|
/**
|
|
|
|
|
* APIリクエスト要求時
|
|
|
|
|
*/
|
|
|
|
|
@autobind
|
|
|
|
|
private async onApiRequest(payload: any) {
|
|
|
|
|
// 新鮮なデータを利用するためにユーザーをフェッチ
|
2019-04-07 21:50:36 +09:00
|
|
|
|
const user = this.user ? await Users.findOne(this.user.id) : null;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
|
|
|
|
|
const endpoint = payload.endpoint || payload.ep; // alias
|
|
|
|
|
|
|
|
|
|
// 呼び出し
|
2020-03-28 18:07:41 +09:00
|
|
|
|
call(endpoint, user, this.token, payload.data).then(res => {
|
2018-10-07 11:06:17 +09:00
|
|
|
|
this.sendMessageToWs(`api:${payload.id}`, { res });
|
2020-01-30 04:37:25 +09: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-07 11:06:17 +09:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
|
private onReadNotification(payload: any) {
|
|
|
|
|
if (!payload.id) return;
|
2019-04-13 01:43:22 +09:00
|
|
|
|
readNotification(this.user!.id, [payload.id]);
|
2018-10-07 11:06:17 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 投稿購読要求時
|
|
|
|
|
*/
|
|
|
|
|
@autobind
|
2021-03-21 15:35:02 +09:00
|
|
|
|
private onSubscribeNote(payload: any, read: boolean) {
|
2018-10-07 11:06:17 +09:00
|
|
|
|
if (!payload.id) return;
|
|
|
|
|
|
|
|
|
|
if (this.subscribingNotes[payload.id] == null) {
|
|
|
|
|
this.subscribingNotes[payload.id] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.subscribingNotes[payload.id]++;
|
|
|
|
|
|
2020-03-29 10:39:36 +09:00
|
|
|
|
if (this.subscribingNotes[payload.id] === 1) {
|
2018-10-07 11:06:17 +09:00
|
|
|
|
this.subscriber.on(`noteStream:${payload.id}`, this.onNoteStreamMessage);
|
|
|
|
|
}
|
2021-03-21 15:35:02 +09:00
|
|
|
|
|
|
|
|
|
if (this.user && read) {
|
|
|
|
|
// TODO: クライアントでタイムライン読み込みなどすると、一度に大量のreadNoteが発生しクエリ数がすごいことになるので、ある程度まとめてreadNoteするようにする
|
|
|
|
|
// 具体的には、この箇所ではキュー的な配列にread予定ノートを溜めておくに留めて、別の箇所で定期的にキューにあるノートを配列でreadNoteに渡すような実装にする
|
|
|
|
|
readNote(this.user.id, payload.id);
|
|
|
|
|
}
|
2018-10-07 11:06:17 +09: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 19:14:05 +09:00
|
|
|
|
const { channel, id, params, pong } = payload;
|
|
|
|
|
this.connectChannel(id, params, channel, pong);
|
2018-10-07 11:06:17 +09: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 19:14:05 +09:00
|
|
|
|
public connectChannel(id: string, params: any, channel: string, pong = false) {
|
2018-11-11 02:22:34 +09:00
|
|
|
|
if ((channels as any)[channel].requireCredential && this.user == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 23:01:57 +09: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 19:14:05 +09:00
|
|
|
|
|
|
|
|
|
if (pong) {
|
|
|
|
|
this.sendMessageToWs('connected', {
|
|
|
|
|
id: id
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-10-07 11:06:17 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* チャンネルから切断
|
2018-10-07 17:06:28 +09:00
|
|
|
|
* @param id チャンネルコネクションID
|
2018-10-07 11:06:17 +09:00
|
|
|
|
*/
|
|
|
|
|
@autobind
|
2018-10-07 17:19:52 +09:00
|
|
|
|
public disconnectChannel(id: string) {
|
2018-10-07 11:06:17 +09: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 17:06:28 +09:00
|
|
|
|
/**
|
|
|
|
|
* チャンネルへメッセージ送信要求時
|
|
|
|
|
* @param data メッセージ
|
|
|
|
|
*/
|
2018-10-07 11:06:17 +09: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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 20:20:05 +09:00
|
|
|
|
@autobind
|
|
|
|
|
private typingOnChannel(channel: ChannelModel['id']) {
|
|
|
|
|
if (this.user) {
|
|
|
|
|
publishChannelStream(channel, 'typing', this.user.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 12:26:49 +09:00
|
|
|
|
@autobind
|
|
|
|
|
private typingOnMessaging(param: { partner?: User['id']; group?: UserGroup['id']; }) {
|
|
|
|
|
if (this.user) {
|
|
|
|
|
if (param.partner) {
|
|
|
|
|
publishMessagingStream(param.partner, this.user.id, 'typing', this.user.id);
|
|
|
|
|
} else if (param.group) {
|
|
|
|
|
publishGroupMessagingStream(param.group, 'typing', this.user.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
|
@autobind
|
|
|
|
|
private async updateFollowing() {
|
|
|
|
|
const followings = await Followings.find({
|
|
|
|
|
where: {
|
2019-04-13 01:43:22 +09:00
|
|
|
|
followerId: this.user!.id
|
2019-04-07 21:50:36 +09:00
|
|
|
|
},
|
|
|
|
|
select: ['followeeId']
|
|
|
|
|
});
|
|
|
|
|
|
2021-01-30 11:09:46 +09:00
|
|
|
|
this.following = new Set<string>(followings.map(x => x.followeeId));
|
2019-04-07 21:50:36 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
|
private async updateMuting() {
|
|
|
|
|
const mutings = await Mutings.find({
|
|
|
|
|
where: {
|
2019-04-13 01:43:22 +09:00
|
|
|
|
muterId: this.user!.id
|
2019-04-07 21:50:36 +09:00
|
|
|
|
},
|
|
|
|
|
select: ['muteeId']
|
|
|
|
|
});
|
|
|
|
|
|
2021-01-30 11:09:46 +09:00
|
|
|
|
this.muting = new Set<string>(mutings.map(x => x.muteeId));
|
2019-04-07 21:50:36 +09:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 22:44:21 +09:00
|
|
|
|
@autobind
|
|
|
|
|
private async updateFollowingChannels() {
|
|
|
|
|
const followings = await ChannelFollowings.find({
|
|
|
|
|
where: {
|
|
|
|
|
followerId: this.user!.id
|
|
|
|
|
},
|
|
|
|
|
select: ['followeeId']
|
|
|
|
|
});
|
|
|
|
|
|
2021-01-30 11:09:46 +09:00
|
|
|
|
this.followingChannels = new Set<string>(followings.map(x => x.followeeId));
|
2020-08-18 22:44:21 +09:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 13:34:20 +09:00
|
|
|
|
@autobind
|
|
|
|
|
private async updateUserProfile() {
|
|
|
|
|
this.userProfile = await UserProfiles.findOne({
|
|
|
|
|
userId: this.user!.id
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 11:06:17 +09:00
|
|
|
|
/**
|
|
|
|
|
* ストリームが切れたとき
|
|
|
|
|
*/
|
|
|
|
|
@autobind
|
|
|
|
|
public dispose() {
|
2018-12-11 20:36:55 +09:00
|
|
|
|
for (const c of this.channels.filter(c => c.dispose)) {
|
2019-04-13 01:43:22 +09:00
|
|
|
|
if (c.dispose) c.dispose();
|
2018-12-11 20:36:55 +09:00
|
|
|
|
}
|
2018-10-07 11:06:17 +09:00
|
|
|
|
}
|
|
|
|
|
}
|