2018-10-06 21:06:17 -05:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import Channel from '../channel';
|
2020-01-26 14:36:59 -06:00
|
|
|
import { Notes, UserListJoinings, UserLists } from '../../../../models';
|
2020-07-27 19:36:43 -05:00
|
|
|
import { isMutedUserRelated } from '../../../../misc/is-muted-user-related';
|
2019-04-07 07:50:36 -05:00
|
|
|
import { User } from '../../../../models/entities/user';
|
2019-04-23 08:35:26 -05:00
|
|
|
import { PackedNote } from '../../../../models/repositories/note';
|
2018-10-06 21:06:17 -05:00
|
|
|
|
|
|
|
export default class extends Channel {
|
2018-10-11 09:01:57 -05:00
|
|
|
public readonly chName = 'userList';
|
2018-10-11 09:07:20 -05:00
|
|
|
public static shouldShare = false;
|
2018-11-10 11:22:34 -06:00
|
|
|
public static requireCredential = false;
|
2019-04-07 07:50:36 -05:00
|
|
|
private listId: string;
|
|
|
|
public listUsers: User['id'][] = [];
|
|
|
|
private listUsersClock: NodeJS.Timer;
|
2018-10-11 09:01:57 -05:00
|
|
|
|
2018-10-06 21:06:17 -05:00
|
|
|
@autobind
|
|
|
|
public async init(params: any) {
|
2019-04-07 07:50:36 -05:00
|
|
|
this.listId = params.listId as string;
|
2018-10-06 21:06:17 -05:00
|
|
|
|
2020-01-26 14:36:59 -06:00
|
|
|
// Check existence and owner
|
|
|
|
const list = await UserLists.findOne({
|
|
|
|
id: this.listId,
|
|
|
|
userId: this.user!.id
|
|
|
|
});
|
|
|
|
if (!list) return;
|
|
|
|
|
2018-10-06 21:06:17 -05:00
|
|
|
// Subscribe stream
|
2019-04-07 07:50:36 -05: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 08:35:26 -05:00
|
|
|
private async onNote(note: PackedNote) {
|
2019-04-07 07:50:36 -05: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 09:32:54 -06:00
|
|
|
detail: true
|
|
|
|
});
|
2019-04-07 07:50:36 -05: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がミュートしているユーザーが関わるものだったら無視する
|
2020-07-27 19:36:43 -05:00
|
|
|
if (isMutedUserRelated(note, this.muting)) return;
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
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-06 21:06:17 -05:00
|
|
|
}
|
|
|
|
}
|