2022-02-27 02:07:39 +00:00
|
|
|
import Channel from '../channel.js';
|
2022-05-24 08:00:46 +00:00
|
|
|
import { Users } from '@/models/index.js';
|
2022-06-27 12:48:10 +00:00
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { StreamMessages } from '../types.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
2020-08-18 13:44:21 +00:00
|
|
|
|
|
|
|
export default class extends Channel {
|
|
|
|
public readonly chName = 'channel';
|
|
|
|
public static shouldShare = false;
|
|
|
|
public static requireCredential = false;
|
|
|
|
private channelId: string;
|
2021-02-20 11:20:05 +00:00
|
|
|
private typers: Record<User['id'], Date> = {};
|
|
|
|
private emitTypersIntervalId: ReturnType<typeof setInterval>;
|
2020-08-18 13:44:21 +00:00
|
|
|
|
2022-02-27 02:07:39 +00:00
|
|
|
constructor(id: string, connection: Channel['connection']) {
|
|
|
|
super(id, connection);
|
2022-05-24 08:00:46 +00:00
|
|
|
this.onNote = this.withPackedNote(this.onNote.bind(this));
|
2022-02-27 02:07:39 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:44:21 +00:00
|
|
|
public async init(params: any) {
|
|
|
|
this.channelId = params.channelId as string;
|
|
|
|
|
|
|
|
// Subscribe stream
|
|
|
|
this.subscriber.on('notesStream', this.onNote);
|
2021-02-20 11:20:05 +00:00
|
|
|
this.subscriber.on(`channelStream:${this.channelId}`, this.onEvent);
|
|
|
|
this.emitTypersIntervalId = setInterval(this.emitTypers, 5000);
|
2020-08-18 13:44:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:35:55 +00:00
|
|
|
private async onNote(note: Packed<'Note'>) {
|
2020-08-18 13:44:21 +00:00
|
|
|
if (note.channelId !== this.channelId) return;
|
|
|
|
|
|
|
|
// 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する
|
2022-06-27 12:48:10 +00:00
|
|
|
if (isUserRelated(note, this.muting)) return;
|
2021-08-17 12:48:59 +00:00
|
|
|
// 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する
|
2022-06-27 12:48:10 +00:00
|
|
|
if (isUserRelated(note, this.blocking)) return;
|
2020-08-18 13:44:21 +00:00
|
|
|
|
2021-03-21 08:38:09 +00:00
|
|
|
this.connection.cacheNote(note);
|
|
|
|
|
2020-08-18 13:44:21 +00:00
|
|
|
this.send('note', note);
|
|
|
|
}
|
|
|
|
|
2021-10-20 16:04:10 +00:00
|
|
|
private onEvent(data: StreamMessages['channel']['payload']) {
|
2021-02-20 11:20:05 +00: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 13:44:21 +00:00
|
|
|
public dispose() {
|
|
|
|
// Unsubscribe events
|
|
|
|
this.subscriber.off('notesStream', this.onNote);
|
2021-02-20 11:20:05 +00:00
|
|
|
this.subscriber.off(`channelStream:${this.channelId}`, this.onEvent);
|
|
|
|
|
|
|
|
clearInterval(this.emitTypersIntervalId);
|
2020-08-18 13:44:21 +00:00
|
|
|
}
|
|
|
|
}
|