2022-02-27 11:07:39 +09:00
|
|
|
import Channel from '../channel.js';
|
|
|
|
import { Notes, Users } from '@/models/index.js';
|
2022-06-27 21:48:10 +09:00
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { StreamMessages } from '../types.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
2020-08-18 22:44:21 +09:00
|
|
|
|
|
|
|
export default class extends Channel {
|
|
|
|
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-02-27 11:07:39 +09:00
|
|
|
constructor(id: string, connection: Channel['connection']) {
|
|
|
|
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) {
|
|
|
|
note.reply = await Notes.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) {
|
|
|
|
note.renote = await Notes.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];
|
|
|
|
}
|
|
|
|
|
|
|
|
const users = await Users.packMany(Object.keys(this.typers), null, { detail: false });
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|