2018-10-07 04:06:17 +02:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import Channel from '../channel';
|
2020-01-26 21:36:59 +01:00
|
|
|
import { Notes, UserListJoinings, UserLists } from '../../../../models';
|
2019-04-07 14:50:36 +02:00
|
|
|
import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
|
|
|
|
import { User } from '../../../../models/entities/user';
|
2019-04-23 15:35:26 +02:00
|
|
|
import { PackedNote } from '../../../../models/repositories/note';
|
2018-10-07 04:06:17 +02:00
|
|
|
|
|
|
|
export default class extends Channel {
|
2018-10-11 16:01:57 +02:00
|
|
|
public readonly chName = 'userList';
|
2018-10-11 16:07:20 +02:00
|
|
|
public static shouldShare = false;
|
2018-11-10 18:22:34 +01:00
|
|
|
public static requireCredential = false;
|
2019-04-07 14:50:36 +02:00
|
|
|
private listId: string;
|
|
|
|
public listUsers: User['id'][] = [];
|
|
|
|
private listUsersClock: NodeJS.Timer;
|
2018-10-11 16:01:57 +02:00
|
|
|
|
2018-10-07 04:06:17 +02:00
|
|
|
@autobind
|
|
|
|
public async init(params: any) {
|
2019-04-07 14:50:36 +02:00
|
|
|
this.listId = params.listId as string;
|
2018-10-07 04:06:17 +02:00
|
|
|
|
2020-01-26 21:36:59 +01:00
|
|
|
// Check existence and owner
|
|
|
|
const list = await UserLists.findOne({
|
|
|
|
id: this.listId,
|
|
|
|
userId: this.user!.id
|
|
|
|
});
|
|
|
|
if (!list) return;
|
|
|
|
|
2018-10-07 04:06:17 +02:00
|
|
|
// Subscribe stream
|
2019-04-07 14:50:36 +02:00
|
|
|
this.subscriber.on(`userListStream:${this.listId}`, this.send);
|
|
|
|
|
|
|
|
this.subscriber.on('notesStream', this.onNote);
|
|
|
|
|
|
|
|
this.updateListUsers();
|
|
|
|
this.listUsersClock = setInterval(this.updateListUsers, 5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
private async updateListUsers() {
|
|
|
|
const users = await UserListJoinings.find({
|
|
|
|
where: {
|
|
|
|
userListId: this.listId,
|
|
|
|
},
|
|
|
|
select: ['userId']
|
|
|
|
});
|
|
|
|
|
|
|
|
this.listUsers = users.map(x => x.userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
2019-04-23 15:35:26 +02:00
|
|
|
private async onNote(note: PackedNote) {
|
2019-04-07 14:50:36 +02:00
|
|
|
if (!this.listUsers.includes(note.userId)) return;
|
|
|
|
|
|
|
|
if (['followers', 'specified'].includes(note.visibility)) {
|
|
|
|
note = await Notes.pack(note.id, this.user, {
|
2019-01-20 16:32:54 +01:00
|
|
|
detail: true
|
|
|
|
});
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
if (note.isHidden) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// リプライなら再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 (shouldMuteThisNote(note, this.muting)) return;
|
|
|
|
|
|
|
|
this.send('note', note);
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public dispose() {
|
|
|
|
// Unsubscribe events
|
|
|
|
this.subscriber.off(`userListStream:${this.listId}`, this.send);
|
|
|
|
this.subscriber.off('notesStream', this.onNote);
|
|
|
|
|
|
|
|
clearInterval(this.listUsersClock);
|
2018-10-07 04:06:17 +02:00
|
|
|
}
|
|
|
|
}
|