2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import type { NotesRepository, UsersRepository } from '@/models/index.js';
|
2022-06-27 21:48:10 +09:00
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { Packed } from '@/misc/schema.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import Channel from '../channel.js';
|
|
|
|
import type { StreamMessages } from '../types.js';
|
2020-08-18 22:44:21 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
class ChannelChannel extends Channel {
|
2020-08-18 22:44:21 +09:00
|
|
|
public readonly chName = 'channel';
|
|
|
|
public static shouldShare = false;
|
|
|
|
public static requireCredential = false;
|
|
|
|
private channelId: string;
|
2021-02-20 20:20:05 +09:00
|
|
|
private typers: Record<User['id'], Date> = {};
|
|
|
|
private emitTypersIntervalId: ReturnType<typeof setInterval>;
|
2020-08-18 22:44:21 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
|
|
|
|
id: string,
|
|
|
|
connection: Channel['connection'],
|
|
|
|
) {
|
2022-02-27 11:07:39 +09:00
|
|
|
super(id, connection);
|
|
|
|
this.onNote = this.onNote.bind(this);
|
2022-07-16 19:03:41 +09:00
|
|
|
this.emitTypers = this.emitTypers.bind(this);
|
2022-02-27 11:07:39 +09:00
|
|
|
}
|
|
|
|
|
2020-08-18 22:44:21 +09:00
|
|
|
public async init(params: any) {
|
|
|
|
this.channelId = params.channelId as string;
|
|
|
|
|
|
|
|
// Subscribe stream
|
|
|
|
this.subscriber.on('notesStream', this.onNote);
|
2021-02-20 20:20:05 +09:00
|
|
|
this.subscriber.on(`channelStream:${this.channelId}`, this.onEvent);
|
|
|
|
this.emitTypersIntervalId = setInterval(this.emitTypers, 5000);
|
2020-08-18 22:44:21 +09:00
|
|
|
}
|
|
|
|
|
2021-09-22 22:35:55 +09:00
|
|
|
private async onNote(note: Packed<'Note'>) {
|
2020-08-18 22:44:21 +09:00
|
|
|
if (note.channelId !== this.channelId) return;
|
|
|
|
|
|
|
|
// リプライなら再pack
|
|
|
|
if (note.replyId != null) {
|
2022-09-18 03:27:08 +09:00
|
|
|
note.reply = await this.noteEntityService.pack(note.replyId, this.user, {
|
2021-12-09 23:58:30 +09:00
|
|
|
detail: true,
|
2020-08-18 22:44:21 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
// Renoteなら再pack
|
|
|
|
if (note.renoteId != null) {
|
2022-09-18 03:27:08 +09:00
|
|
|
note.renote = await this.noteEntityService.pack(note.renoteId, this.user, {
|
2021-12-09 23:58:30 +09:00
|
|
|
detail: true,
|
2020-08-18 22:44:21 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する
|
2022-06-27 21:48:10 +09:00
|
|
|
if (isUserRelated(note, this.muting)) return;
|
2021-08-17 21:48:59 +09:00
|
|
|
// 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する
|
2022-06-27 21:48:10 +09:00
|
|
|
if (isUserRelated(note, this.blocking)) return;
|
2020-08-18 22:44:21 +09:00
|
|
|
|
2021-03-21 17:38:09 +09:00
|
|
|
this.connection.cacheNote(note);
|
|
|
|
|
2020-08-18 22:44:21 +09:00
|
|
|
this.send('note', note);
|
|
|
|
}
|
|
|
|
|
2021-10-21 01:04:10 +09:00
|
|
|
private onEvent(data: StreamMessages['channel']['payload']) {
|
2021-02-20 20:20:05 +09:00
|
|
|
if (data.type === 'typing') {
|
|
|
|
const id = data.body;
|
|
|
|
const begin = this.typers[id] == null;
|
|
|
|
this.typers[id] = new Date();
|
|
|
|
if (begin) {
|
|
|
|
this.emitTypers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async emitTypers() {
|
|
|
|
const now = new Date();
|
|
|
|
|
|
|
|
// Remove not typing users
|
|
|
|
for (const [userId, date] of Object.entries(this.typers)) {
|
|
|
|
if (now.getTime() - date.getTime() > 5000) delete this.typers[userId];
|
|
|
|
}
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const users = await this.userEntityService.packMany(Object.keys(this.typers), null, { detail: false });
|
2021-02-20 20:20:05 +09:00
|
|
|
|
|
|
|
this.send({
|
|
|
|
type: 'typers',
|
|
|
|
body: users,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-18 22:44:21 +09:00
|
|
|
public dispose() {
|
|
|
|
// Unsubscribe events
|
|
|
|
this.subscriber.off('notesStream', this.onNote);
|
2021-02-20 20:20:05 +09:00
|
|
|
this.subscriber.off(`channelStream:${this.channelId}`, this.onEvent);
|
|
|
|
|
|
|
|
clearInterval(this.emitTypersIntervalId);
|
2020-08-18 22:44:21 +09:00
|
|
|
}
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ChannelChannelService {
|
|
|
|
public readonly shouldShare = ChannelChannel.shouldShare;
|
|
|
|
public readonly requireCredential = ChannelChannel.requireCredential;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public create(id: string, connection: Channel['connection']): ChannelChannel {
|
|
|
|
return new ChannelChannel(
|
|
|
|
this.noteEntityService,
|
|
|
|
this.userEntityService,
|
|
|
|
id,
|
|
|
|
connection,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|