2018-10-07 11:06:17 +09:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import Channel from '../channel';
|
2020-01-27 05:36:59 +09:00
|
|
|
import { Notes, UserListJoinings, UserLists } from '../../../../models';
|
2019-04-07 21:50:36 +09:00
|
|
|
import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
|
|
|
|
import { User } from '../../../../models/entities/user';
|
2019-04-23 22:35:26 +09:00
|
|
|
import { PackedNote } from '../../../../models/repositories/note';
|
2018-10-07 11:06:17 +09:00
|
|
|
|
|
|
|
export default class extends Channel {
|
2018-10-11 23:01:57 +09:00
|
|
|
public readonly chName = 'userList';
|
2018-10-11 23:07:20 +09:00
|
|
|
public static shouldShare = false;
|
2018-11-11 02:22:34 +09:00
|
|
|
public static requireCredential = false;
|
2019-04-07 21:50:36 +09:00
|
|
|
private listId: string;
|
|
|
|
public listUsers: User['id'][] = [];
|
|
|
|
private listUsersClock: NodeJS.Timer;
|
2018-10-11 23:01:57 +09:00
|
|
|
|
2018-10-07 11:06:17 +09:00
|
|
|
@autobind
|
|
|
|
public async init(params: any) {
|
2019-04-07 21:50:36 +09:00
|
|
|
this.listId = params.listId as string;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
2020-01-27 05:36:59 +09:00
|
|
|
// Check existence and owner
|
|
|
|
const list = await UserLists.findOne({
|
|
|
|
id: this.listId,
|
|
|
|
userId: this.user!.id
|
|
|
|
});
|
|
|
|
if (!list) return;
|
|
|
|
|
2018-10-07 11:06:17 +09:00
|
|
|
// Subscribe stream
|
2019-04-07 21:50:36 +09: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 22:35:26 +09:00
|
|
|
private async onNote(note: PackedNote) {
|
2019-04-07 21:50:36 +09:00
|
|
|
if (!this.listUsers.includes(note.userId)) return;
|
|
|
|
|
|
|
|
if (['followers', 'specified'].includes(note.visibility)) {
|
|
|
|
note = await Notes.pack(note.id, this.user, {
|
2019-01-21 00:32:54 +09:00
|
|
|
detail: true
|
|
|
|
});
|
2019-04-07 21:50:36 +09: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 11:06:17 +09:00
|
|
|
}
|
|
|
|
}
|