2023-07-27 00:31:52 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-05-28 23:32:19 -05:00
|
|
|
import * as WebSocket from 'ws';
|
2023-09-19 21:33:36 -05:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiAccessToken } from '@/models/AccessToken.js';
|
2023-03-09 23:22:37 -06:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import type { NoteReadService } from '@/core/NoteReadService.js';
|
|
|
|
import type { NotificationService } from '@/core/NotificationService.js';
|
2022-12-04 00:03:09 -06:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-04-04 20:21:10 -05:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2023-10-03 06:26:11 -05:00
|
|
|
import { MiFollowing, MiUserProfile } from '@/models/_.js';
|
2023-09-28 21:29:54 -05:00
|
|
|
import type { StreamEventEmitter, GlobalEvents } from '@/core/GlobalEventService.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import type { ChannelsService } from './ChannelsService.js';
|
|
|
|
import type { EventEmitter } from 'events';
|
|
|
|
import type Channel from './channel.js';
|
2020-03-27 21:24:37 -05:00
|
|
|
|
2018-10-06 21:06:17 -05:00
|
|
|
/**
|
|
|
|
* Main stream connection
|
|
|
|
*/
|
2023-08-17 07:20:58 -05:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2018-10-06 21:06:17 -05:00
|
|
|
export default class Connection {
|
2023-08-16 03:51:28 -05:00
|
|
|
public user?: MiUser;
|
|
|
|
public token?: MiAccessToken;
|
2023-05-28 23:32:19 -05:00
|
|
|
private wsConnection: WebSocket.WebSocket;
|
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'>[] = [];
|
2023-08-16 03:51:28 -05:00
|
|
|
public userProfile: MiUserProfile | null = null;
|
2023-10-03 06:26:11 -05:00
|
|
|
public following: Record<string, Pick<MiFollowing, 'withReplies'> | undefined> = {};
|
2023-04-04 20:21:10 -05:00
|
|
|
public followingChannels: Set<string> = new Set();
|
|
|
|
public userIdsWhoMeMuting: Set<string> = new Set();
|
|
|
|
public userIdsWhoBlockingMe: Set<string> = new Set();
|
|
|
|
public userIdsWhoMeMutingRenotes: Set<string> = new Set();
|
2023-09-03 23:33:38 -05:00
|
|
|
private fetchIntervalId: NodeJS.Timeout | null = null;
|
2018-10-06 21:06:17 -05:00
|
|
|
|
|
|
|
constructor(
|
2022-09-17 13:27:08 -05:00
|
|
|
private channelsService: ChannelsService,
|
|
|
|
private noteReadService: NoteReadService,
|
|
|
|
private notificationService: NotificationService,
|
2023-04-04 20:21:10 -05:00
|
|
|
private cacheService: CacheService,
|
2022-09-17 13:27:08 -05:00
|
|
|
|
2023-08-16 03:51:28 -05:00
|
|
|
user: MiUser | null | undefined,
|
|
|
|
token: MiAccessToken | null | undefined,
|
2018-10-06 21:06:17 -05:00
|
|
|
) {
|
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;
|
2023-04-04 20:21:10 -05:00
|
|
|
}
|
2018-10-06 21:06:17 -05:00
|
|
|
|
2023-04-04 20:21:10 -05:00
|
|
|
@bindThis
|
|
|
|
public async fetch() {
|
|
|
|
if (this.user == null) return;
|
|
|
|
const [userProfile, following, followingChannels, userIdsWhoMeMuting, userIdsWhoBlockingMe, userIdsWhoMeMutingRenotes] = await Promise.all([
|
|
|
|
this.cacheService.userProfileCache.fetch(this.user.id),
|
|
|
|
this.cacheService.userFollowingsCache.fetch(this.user.id),
|
|
|
|
this.cacheService.userFollowingChannelsCache.fetch(this.user.id),
|
|
|
|
this.cacheService.userMutingsCache.fetch(this.user.id),
|
|
|
|
this.cacheService.userBlockedCache.fetch(this.user.id),
|
|
|
|
this.cacheService.renoteMutingsCache.fetch(this.user.id),
|
|
|
|
]);
|
|
|
|
this.userProfile = userProfile;
|
|
|
|
this.following = following;
|
|
|
|
this.followingChannels = followingChannels;
|
|
|
|
this.userIdsWhoMeMuting = userIdsWhoMeMuting;
|
|
|
|
this.userIdsWhoBlockingMe = userIdsWhoBlockingMe;
|
|
|
|
this.userIdsWhoMeMutingRenotes = userIdsWhoMeMutingRenotes;
|
|
|
|
}
|
2020-04-02 08:17:17 -05:00
|
|
|
|
2023-04-04 20:21:10 -05:00
|
|
|
@bindThis
|
|
|
|
public async init() {
|
|
|
|
if (this.user != null) {
|
|
|
|
await this.fetch();
|
2021-03-21 01:14:03 -05:00
|
|
|
|
2023-05-28 23:32:19 -05:00
|
|
|
if (!this.fetchIntervalId) {
|
|
|
|
this.fetchIntervalId = setInterval(this.fetch, 1000 * 10);
|
|
|
|
}
|
2021-03-21 01:14:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2023-05-28 23:32:19 -05:00
|
|
|
public async listen(subscriber: EventEmitter, wsConnection: WebSocket.WebSocket) {
|
|
|
|
this.subscriber = subscriber;
|
|
|
|
|
2023-04-04 20:21:10 -05:00
|
|
|
this.wsConnection = wsConnection;
|
|
|
|
this.wsConnection.on('message', this.onWsConnectionMessage);
|
|
|
|
|
|
|
|
this.subscriber.on('broadcast', data => {
|
|
|
|
this.onBroadcastMessage(data);
|
|
|
|
});
|
2018-10-06 21:06:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* クライアントからメッセージ受信時
|
|
|
|
*/
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2023-05-28 23:32:19 -05:00
|
|
|
private async onWsConnectionMessage(data: WebSocket.RawData) {
|
2020-08-18 08:52:54 -05:00
|
|
|
let obj: Record<string, any>;
|
|
|
|
|
|
|
|
try {
|
2023-05-28 23:32:19 -05:00
|
|
|
obj = JSON.parse(data.toString());
|
2020-08-18 08:52:54 -05:00
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { type, body } = obj;
|
2018-10-06 21:06:17 -05:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
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
|
2018-10-06 21:06:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2023-09-28 21:29:54 -05:00
|
|
|
private onBroadcastMessage(data: GlobalEvents['broadcast']['payload']) {
|
2021-10-20 11:04:10 -05:00
|
|
|
this.sendMessageToWs(data.type, data.body);
|
2020-04-02 08:17:17 -05:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
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
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
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)) {
|
2023-04-04 17:52:49 -05:00
|
|
|
this.noteReadService.read(this.user.id, [note]);
|
2021-03-21 03:38:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2018-10-06 21:06:17 -05:00
|
|
|
private onReadNotification(payload: any) {
|
2023-04-04 00:06:57 -05:00
|
|
|
this.notificationService.readAllNotification(this.user!.id);
|
2018-10-06 21:06:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 投稿購読要求時
|
|
|
|
*/
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 投稿購読解除要求時
|
|
|
|
*/
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2018-10-06 21:06:17 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2023-09-28 21:29:54 -05:00
|
|
|
private async onNoteStreamMessage(data: GlobalEvents['note']['payload']) {
|
2018-10-06 21:06:17 -05:00
|
|
|
this.sendMessageToWs('noteUpdated', {
|
|
|
|
id: data.body.id,
|
|
|
|
type: data.type,
|
|
|
|
body: data.body.body,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネル接続要求時
|
|
|
|
*/
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2018-10-06 21:06:17 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネル切断要求時
|
|
|
|
*/
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2018-10-06 21:06:17 -05:00
|
|
|
private onChannelDisconnectRequested(payload: any) {
|
|
|
|
const { id } = payload;
|
|
|
|
this.disconnectChannel(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* クライアントにメッセージ送信
|
|
|
|
*/
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2018-10-06 21:06:17 -05:00
|
|
|
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
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* チャンネルに接続
|
|
|
|
*/
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2018-10-13 05:14:05 -05:00
|
|
|
public connectChannel(id: string, params: any, channel: string, pong = false) {
|
2022-09-17 13:27:08 -05:00
|
|
|
const channelService = this.channelsService.getChannelService(channel);
|
|
|
|
|
|
|
|
if (channelService.requireCredential && this.user == null) {
|
2018-11-10 11:22:34 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-11 09:01:57 -05:00
|
|
|
// 共有可能チャンネルに接続しようとしていて、かつそのチャンネルに既に接続していたら無意味なので無視
|
2022-09-17 13:27:08 -05:00
|
|
|
if (channelService.shouldShare && this.channels.some(c => c.chName === channel)) {
|
2018-10-11 09:01:57 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
const ch: Channel = channelService.create(id, this);
|
2018-10-11 09:01:57 -05:00
|
|
|
this.channels.push(ch);
|
2023-05-16 21:10:31 -05:00
|
|
|
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
|
|
|
*/
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
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 メッセージ
|
|
|
|
*/
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ストリームが切れたとき
|
|
|
|
*/
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2018-10-06 21:06:17 -05:00
|
|
|
public dispose() {
|
2023-04-04 20:21:10 -05:00
|
|
|
if (this.fetchIntervalId) clearInterval(this.fetchIntervalId);
|
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
|
|
|
}
|
|
|
|
}
|