2020-08-18 15:44:21 +02:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import Channel from '../channel';
|
2021-02-20 12:20:05 +01:00
|
|
|
import { Notes, Users } from '../../../../models';
|
2020-08-18 15:44:21 +02:00
|
|
|
import { isMutedUserRelated } from '../../../../misc/is-muted-user-related';
|
|
|
|
import { PackedNote } from '../../../../models/repositories/note';
|
2021-02-20 12:20:05 +01:00
|
|
|
import { User } from '../../../../models/entities/user';
|
2020-08-18 15:44:21 +02:00
|
|
|
|
|
|
|
export default class extends Channel {
|
|
|
|
public readonly chName = 'channel';
|
|
|
|
public static shouldShare = false;
|
|
|
|
public static requireCredential = false;
|
|
|
|
private channelId: string;
|
2021-02-20 12:20:05 +01:00
|
|
|
private typers: Record<User['id'], Date> = {};
|
|
|
|
private emitTypersIntervalId: ReturnType<typeof setInterval>;
|
2020-08-18 15:44:21 +02:00
|
|
|
|
|
|
|
@autobind
|
|
|
|
public async init(params: any) {
|
|
|
|
this.channelId = params.channelId as string;
|
|
|
|
|
|
|
|
// Subscribe stream
|
|
|
|
this.subscriber.on('notesStream', this.onNote);
|
2021-02-20 12:20:05 +01:00
|
|
|
this.subscriber.on(`channelStream:${this.channelId}`, this.onEvent);
|
|
|
|
this.emitTypersIntervalId = setInterval(this.emitTypers, 5000);
|
2020-08-18 15:44:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
private async onNote(note: PackedNote) {
|
|
|
|
if (note.channelId !== this.channelId) return;
|
|
|
|
|
|
|
|
// リプライなら再pack
|
|
|
|
if (note.replyId != null) {
|
|
|
|
note.reply = await Notes.pack(note.replyId, this.user, {
|
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Renoteなら再pack
|
|
|
|
if (note.renoteId != null) {
|
|
|
|
note.renote = await Notes.pack(note.renoteId, this.user, {
|
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する
|
|
|
|
if (isMutedUserRelated(note, this.muting)) return;
|
|
|
|
|
2021-03-21 09:38:09 +01:00
|
|
|
this.connection.cacheNote(note);
|
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
this.send('note', note);
|
|
|
|
}
|
|
|
|
|
2021-02-20 12:20:05 +01:00
|
|
|
@autobind
|
|
|
|
private onEvent(data: any) {
|
|
|
|
if (data.type === 'typing') {
|
|
|
|
const id = data.body;
|
|
|
|
const begin = this.typers[id] == null;
|
|
|
|
this.typers[id] = new Date();
|
|
|
|
if (begin) {
|
|
|
|
this.emitTypers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
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 15:44:21 +02:00
|
|
|
@autobind
|
|
|
|
public dispose() {
|
|
|
|
// Unsubscribe events
|
|
|
|
this.subscriber.off('notesStream', this.onNote);
|
2021-02-20 12:20:05 +01:00
|
|
|
this.subscriber.off(`channelStream:${this.channelId}`, this.onEvent);
|
|
|
|
|
|
|
|
clearInterval(this.emitTypersIntervalId);
|
2020-08-18 15:44:21 +02:00
|
|
|
}
|
|
|
|
}
|