2018-10-06 21:06:17 -05:00
|
|
|
import * as websocket from 'websocket';
|
2022-02-26 20:07:39 -06:00
|
|
|
import { readNotification } from '../common/read-notification.js';
|
|
|
|
import call from '../call.js';
|
|
|
|
import readNote from '@/services/note/read.js';
|
|
|
|
import Channel from './channel.js';
|
|
|
|
import channels from './channels/index.js';
|
2018-10-11 04:09:41 -05:00
|
|
|
import { EventEmitter } from 'events';
|
2022-02-26 20:07:39 -06:00
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Channel as ChannelModel } from '@/models/entities/channel.js';
|
|
|
|
import { Users, Followings, Mutings, UserProfiles, ChannelFollowings, Blockings } from '@/models/index.js';
|
|
|
|
import { ApiError } from '../error.js';
|
|
|
|
import { AccessToken } from '@/models/entities/access-token.js';
|
|
|
|
import { UserProfile } from '@/models/entities/user-profile.js';
|
|
|
|
import { publishChannelStream, publishGroupMessagingStream, publishMessagingStream } from '@/services/stream.js';
|
|
|
|
import { UserGroup } from '@/models/entities/user-group.js';
|
|
|
|
import { StreamEventEmitter, StreamMessages } from './types.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
2020-03-27 21:24:37 -05:00
|
|
|
|
2018-10-06 21:06:17 -05:00
|
|
|
/**
|
|
|
|
* Main stream connection
|
|
|
|
*/
|
|
|
|
export default class Connection {
|
2019-04-07 07:50:36 -05:00
|
|
|
public user?: User;
|
2020-07-26 23:34:20 -05:00
|
|
|
public userProfile?: UserProfile;
|
2021-01-29 20:09:46 -06:00
|
|
|
public following: Set<User['id']> = new Set();
|
|
|
|
public muting: Set<User['id']> = new Set();
|
2021-08-17 07:48:59 -05:00
|
|
|
public blocking: Set<User['id']> = new Set(); // "被"blocking
|
2021-01-29 20:09:46 -06:00
|
|
|
public followingChannels: Set<ChannelModel['id']> = new Set();
|
2020-03-28 21:28:55 -05:00
|
|
|
public token?: AccessToken;
|
2018-10-06 21:06:17 -05:00
|
|
|
private wsConnection: websocket.connection;
|
2021-10-20 11:04:10 -05:00
|
|
|
public subscriber: StreamEventEmitter;
|
2018-10-06 21:06:17 -05:00
|
|
|
private channels: Channel[] = [];
|
|
|
|
private subscribingNotes: any = {};
|
2021-09-22 08:35:55 -05:00
|
|
|
private cachedNotes: Packed<'Note'>[] = [];
|
2018-10-06 21:06:17 -05:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
wsConnection: websocket.connection,
|
2018-10-11 04:09:41 -05:00
|
|
|
subscriber: EventEmitter,
|
2019-04-12 11:43:22 -05:00
|
|
|
user: User | null | undefined,
|
2022-02-26 20:07:39 -06:00
|
|
|
token: AccessToken | null | undefined,
|
2018-10-06 21:06:17 -05:00
|
|
|
) {
|
|
|
|
this.wsConnection = wsConnection;
|
|
|
|
this.subscriber = subscriber;
|
2019-04-12 11:43:22 -05:00
|
|
|
if (user) this.user = user;
|
2020-03-28 04:07:41 -05:00
|
|
|
if (token) this.token = token;
|
2018-10-06 21:06:17 -05:00
|
|
|
|
2022-02-26 20:07:39 -06:00
|
|
|
this.onWsConnectionMessage = this.onWsConnectionMessage.bind(this);
|
|
|
|
this.onUserEvent = this.onUserEvent.bind(this);
|
|
|
|
this.onNoteStreamMessage = this.onNoteStreamMessage.bind(this);
|
|
|
|
this.onBroadcastMessage = this.onBroadcastMessage.bind(this);
|
|
|
|
|
2018-10-06 21:06:17 -05:00
|
|
|
this.wsConnection.on('message', this.onWsConnectionMessage);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
2021-10-20 11:04:10 -05:00
|
|
|
this.subscriber.on('broadcast', data => {
|
|
|
|
this.onBroadcastMessage(data);
|
2020-04-02 08:17:17 -05:00
|
|
|
});
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (this.user) {
|
|
|
|
this.updateFollowing();
|
|
|
|
this.updateMuting();
|
2021-08-17 07:48:59 -05:00
|
|
|
this.updateBlocking();
|
2020-08-18 08:44:21 -05:00
|
|
|
this.updateFollowingChannels();
|
2020-07-26 23:34:20 -05:00
|
|
|
this.updateUserProfile();
|
2021-03-21 01:14:03 -05:00
|
|
|
|
2021-10-20 11:04:10 -05:00
|
|
|
this.subscriber.on(`user:${this.user.id}`, this.onUserEvent);
|
2021-03-21 01:14:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 11:04:10 -05:00
|
|
|
private onUserEvent(data: StreamMessages['user']['payload']) { // { type, body }と展開するとそれぞれ型が分離してしまう
|
|
|
|
switch (data.type) {
|
2021-03-21 01:14:03 -05:00
|
|
|
case 'follow':
|
2021-10-20 11:04:10 -05:00
|
|
|
this.following.add(data.body.id);
|
2021-03-21 01:14:03 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'unfollow':
|
2021-10-20 11:04:10 -05:00
|
|
|
this.following.delete(data.body.id);
|
2021-03-21 01:14:03 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mute':
|
2021-10-20 11:04:10 -05:00
|
|
|
this.muting.add(data.body.id);
|
2021-03-21 01:14:03 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'unmute':
|
2021-10-20 11:04:10 -05:00
|
|
|
this.muting.delete(data.body.id);
|
2021-03-21 01:14:03 -05:00
|
|
|
break;
|
|
|
|
|
2021-08-17 07:48:59 -05:00
|
|
|
// TODO: block events
|
|
|
|
|
2021-03-21 01:14:03 -05:00
|
|
|
case 'followChannel':
|
2021-10-20 11:04:10 -05:00
|
|
|
this.followingChannels.add(data.body.id);
|
2021-03-21 01:14:03 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'unfollowChannel':
|
2021-10-20 11:04:10 -05:00
|
|
|
this.followingChannels.delete(data.body.id);
|
2021-03-21 01:14:03 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'updateUserProfile':
|
2021-10-20 11:04:10 -05:00
|
|
|
this.userProfile = data.body;
|
2021-03-21 01:14:03 -05:00
|
|
|
break;
|
|
|
|
|
2021-07-18 05:57:53 -05:00
|
|
|
case 'terminate':
|
|
|
|
this.wsConnection.close();
|
|
|
|
this.dispose();
|
|
|
|
break;
|
|
|
|
|
2021-03-21 01:14:03 -05:00
|
|
|
default:
|
|
|
|
break;
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
2018-10-06 21:06:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* クライアントからメッセージ受信時
|
|
|
|
*/
|
2022-02-18 23:30:42 -06:00
|
|
|
private async onWsConnectionMessage(data: websocket.Message) {
|
|
|
|
if (data.type !== 'utf8') return;
|
2019-04-12 11:43:22 -05:00
|
|
|
if (data.utf8Data == null) return;
|
|
|
|
|
2020-08-18 08:52:54 -05:00
|
|
|
let obj: Record<string, any>;
|
|
|
|
|
|
|
|
try {
|
|
|
|
obj = JSON.parse(data.utf8Data);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { type, body } = obj;
|
2018-10-06 21:06:17 -05:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'api': this.onApiRequest(body); break;
|
|
|
|
case 'readNotification': this.onReadNotification(body); break;
|
2021-03-21 03:38:09 -05:00
|
|
|
case 'subNote': this.onSubscribeNote(body); break;
|
|
|
|
case 's': this.onSubscribeNote(body); break; // alias
|
|
|
|
case 'sr': this.onSubscribeNote(body); this.readNote(body); break;
|
2018-10-06 21:06:17 -05: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-09 13:24:09 -05:00
|
|
|
case 'ch': this.onChannelMessageRequested(body); break; // alias
|
2021-02-20 21:26:49 -06:00
|
|
|
|
|
|
|
// 個々のチャンネルではなくルートレベルでこれらのメッセージを受け取る理由は、
|
|
|
|
// クライアントの事情を考慮したとき、入力フォームはノートチャンネルやメッセージのメインコンポーネントとは別
|
|
|
|
// なこともあるため、それらのコンポーネントがそれぞれ各チャンネルに接続するようにするのは面倒なため。
|
2021-02-20 05:20:05 -06:00
|
|
|
case 'typingOnChannel': this.typingOnChannel(body.channel); break;
|
2021-02-20 21:26:49 -06:00
|
|
|
case 'typingOnMessaging': this.typingOnMessaging(body); break;
|
2018-10-06 21:06:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 11:04:10 -05:00
|
|
|
private onBroadcastMessage(data: StreamMessages['broadcast']['payload']) {
|
|
|
|
this.sendMessageToWs(data.type, data.body);
|
2020-04-02 08:17:17 -05:00
|
|
|
}
|
|
|
|
|
2021-09-22 08:35:55 -05:00
|
|
|
public cacheNote(note: Packed<'Note'>) {
|
|
|
|
const add = (note: Packed<'Note'>) => {
|
2021-03-21 03:38:09 -05:00
|
|
|
const existIndex = this.cachedNotes.findIndex(n => n.id === note.id);
|
|
|
|
if (existIndex > -1) {
|
|
|
|
this.cachedNotes[existIndex] = note;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cachedNotes.unshift(note);
|
|
|
|
if (this.cachedNotes.length > 32) {
|
|
|
|
this.cachedNotes.splice(32);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
add(note);
|
2021-09-11 11:12:23 -05:00
|
|
|
if (note.reply) add(note.reply);
|
|
|
|
if (note.renote) add(note.renote);
|
2021-03-21 03:38:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private readNote(body: any) {
|
|
|
|
const id = body.id;
|
|
|
|
|
|
|
|
const note = this.cachedNotes.find(n => n.id === id);
|
|
|
|
if (note == null) return;
|
|
|
|
|
|
|
|
if (this.user && (note.userId !== this.user.id)) {
|
2021-03-23 01:06:56 -05:00
|
|
|
readNote(this.user.id, [note], {
|
|
|
|
following: this.following,
|
|
|
|
followingChannels: this.followingChannels,
|
|
|
|
});
|
2021-03-21 03:38:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 21:06:17 -05:00
|
|
|
/**
|
|
|
|
* APIリクエスト要求時
|
|
|
|
*/
|
|
|
|
private async onApiRequest(payload: any) {
|
|
|
|
// 新鮮なデータを利用するためにユーザーをフェッチ
|
2019-04-07 07:50:36 -05:00
|
|
|
const user = this.user ? await Users.findOne(this.user.id) : null;
|
2018-10-06 21:06:17 -05:00
|
|
|
|
|
|
|
const endpoint = payload.endpoint || payload.ep; // alias
|
|
|
|
|
|
|
|
// 呼び出し
|
2020-03-28 04:07:41 -05:00
|
|
|
call(endpoint, user, this.token, payload.data).then(res => {
|
2018-10-06 21:06:17 -05:00
|
|
|
this.sendMessageToWs(`api:${payload.id}`, { res });
|
2020-01-29 13:37:25 -06:00
|
|
|
}).catch((e: ApiError) => {
|
|
|
|
this.sendMessageToWs(`api:${payload.id}`, {
|
|
|
|
error: {
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
id: e.id,
|
|
|
|
kind: e.kind,
|
2021-12-09 08:58:30 -06:00
|
|
|
...(e.info ? { info: e.info } : {}),
|
|
|
|
},
|
2020-01-29 13:37:25 -06:00
|
|
|
});
|
2018-10-06 21:06:17 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private onReadNotification(payload: any) {
|
|
|
|
if (!payload.id) return;
|
2019-04-12 11:43:22 -05:00
|
|
|
readNotification(this.user!.id, [payload.id]);
|
2018-10-06 21:06:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 投稿購読要求時
|
|
|
|
*/
|
2021-03-21 03:38:09 -05:00
|
|
|
private onSubscribeNote(payload: any) {
|
2018-10-06 21:06:17 -05:00
|
|
|
if (!payload.id) return;
|
|
|
|
|
|
|
|
if (this.subscribingNotes[payload.id] == null) {
|
|
|
|
this.subscribingNotes[payload.id] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.subscribingNotes[payload.id]++;
|
|
|
|
|
2020-03-28 20:39:36 -05:00
|
|
|
if (this.subscribingNotes[payload.id] === 1) {
|
2018-10-06 21:06:17 -05:00
|
|
|
this.subscriber.on(`noteStream:${payload.id}`, this.onNoteStreamMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 投稿購読解除要求時
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 11:04:10 -05:00
|
|
|
private async onNoteStreamMessage(data: StreamMessages['note']['payload']) {
|
2018-10-06 21:06:17 -05:00
|
|
|
this.sendMessageToWs('noteUpdated', {
|
|
|
|
id: data.body.id,
|
|
|
|
type: data.type,
|
|
|
|
body: data.body.body,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネル接続要求時
|
|
|
|
*/
|
|
|
|
private onChannelConnectRequested(payload: any) {
|
2018-10-13 05:14:05 -05:00
|
|
|
const { channel, id, params, pong } = payload;
|
|
|
|
this.connectChannel(id, params, channel, pong);
|
2018-10-06 21:06:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネル切断要求時
|
|
|
|
*/
|
|
|
|
private onChannelDisconnectRequested(payload: any) {
|
|
|
|
const { id } = payload;
|
|
|
|
this.disconnectChannel(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* クライアントにメッセージ送信
|
|
|
|
*/
|
|
|
|
public sendMessageToWs(type: string, payload: any) {
|
|
|
|
this.wsConnection.send(JSON.stringify({
|
|
|
|
type: type,
|
2021-12-09 08:58:30 -06:00
|
|
|
body: payload,
|
2018-10-06 21:06:17 -05:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネルに接続
|
|
|
|
*/
|
2018-10-13 05:14:05 -05:00
|
|
|
public connectChannel(id: string, params: any, channel: string, pong = false) {
|
2018-11-10 11:22:34 -06:00
|
|
|
if ((channels as any)[channel].requireCredential && this.user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-11 09:01:57 -05: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 05:14:05 -05:00
|
|
|
|
|
|
|
if (pong) {
|
|
|
|
this.sendMessageToWs('connected', {
|
2021-12-09 08:58:30 -06:00
|
|
|
id: id,
|
2018-10-13 05:14:05 -05:00
|
|
|
});
|
|
|
|
}
|
2018-10-06 21:06:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネルから切断
|
2018-10-07 03:06:28 -05:00
|
|
|
* @param id チャンネルコネクションID
|
2018-10-06 21:06:17 -05:00
|
|
|
*/
|
2018-10-07 03:19:52 -05:00
|
|
|
public disconnectChannel(id: string) {
|
2018-10-06 21:06:17 -05: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 03:06:28 -05:00
|
|
|
/**
|
|
|
|
* チャンネルへメッセージ送信要求時
|
|
|
|
* @param data メッセージ
|
|
|
|
*/
|
2018-10-06 21:06:17 -05:00
|
|
|
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 05:20:05 -06:00
|
|
|
private typingOnChannel(channel: ChannelModel['id']) {
|
|
|
|
if (this.user) {
|
|
|
|
publishChannelStream(channel, 'typing', this.user.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 21:26:49 -06:00
|
|
|
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 07:50:36 -05:00
|
|
|
private async updateFollowing() {
|
|
|
|
const followings = await Followings.find({
|
|
|
|
where: {
|
2021-12-09 08:58:30 -06:00
|
|
|
followerId: this.user!.id,
|
2019-04-07 07:50:36 -05:00
|
|
|
},
|
2021-12-09 08:58:30 -06:00
|
|
|
select: ['followeeId'],
|
2019-04-07 07:50:36 -05:00
|
|
|
});
|
|
|
|
|
2021-01-29 20:09:46 -06:00
|
|
|
this.following = new Set<string>(followings.map(x => x.followeeId));
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private async updateMuting() {
|
|
|
|
const mutings = await Mutings.find({
|
|
|
|
where: {
|
2021-12-09 08:58:30 -06:00
|
|
|
muterId: this.user!.id,
|
2019-04-07 07:50:36 -05:00
|
|
|
},
|
2021-12-09 08:58:30 -06:00
|
|
|
select: ['muteeId'],
|
2019-04-07 07:50:36 -05:00
|
|
|
});
|
|
|
|
|
2021-01-29 20:09:46 -06:00
|
|
|
this.muting = new Set<string>(mutings.map(x => x.muteeId));
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
|
|
|
|
2021-08-17 07:48:59 -05:00
|
|
|
private async updateBlocking() { // ここでいうBlockingは被Blockingの意
|
|
|
|
const blockings = await Blockings.find({
|
|
|
|
where: {
|
2021-12-09 08:58:30 -06:00
|
|
|
blockeeId: this.user!.id,
|
2021-08-17 07:48:59 -05:00
|
|
|
},
|
2021-12-09 08:58:30 -06:00
|
|
|
select: ['blockerId'],
|
2021-08-17 07:48:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.blocking = new Set<string>(blockings.map(x => x.blockerId));
|
|
|
|
}
|
|
|
|
|
2020-08-18 08:44:21 -05:00
|
|
|
private async updateFollowingChannels() {
|
|
|
|
const followings = await ChannelFollowings.find({
|
|
|
|
where: {
|
2021-12-09 08:58:30 -06:00
|
|
|
followerId: this.user!.id,
|
2020-08-18 08:44:21 -05:00
|
|
|
},
|
2021-12-09 08:58:30 -06:00
|
|
|
select: ['followeeId'],
|
2020-08-18 08:44:21 -05:00
|
|
|
});
|
|
|
|
|
2021-01-29 20:09:46 -06:00
|
|
|
this.followingChannels = new Set<string>(followings.map(x => x.followeeId));
|
2020-08-18 08:44:21 -05:00
|
|
|
}
|
|
|
|
|
2020-07-26 23:34:20 -05:00
|
|
|
private async updateUserProfile() {
|
|
|
|
this.userProfile = await UserProfiles.findOne({
|
2021-12-09 08:58:30 -06:00
|
|
|
userId: this.user!.id,
|
2020-07-26 23:34:20 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-06 21:06:17 -05:00
|
|
|
/**
|
|
|
|
* ストリームが切れたとき
|
|
|
|
*/
|
|
|
|
public dispose() {
|
2018-12-11 05:36:55 -06:00
|
|
|
for (const c of this.channels.filter(c => c.dispose)) {
|
2019-04-12 11:43:22 -05:00
|
|
|
if (c.dispose) c.dispose();
|
2018-12-11 05:36:55 -06:00
|
|
|
}
|
2018-10-06 21:06:17 -05:00
|
|
|
}
|
|
|
|
}
|