2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-02-16 14:09:41 +00:00
|
|
|
import type { UserListJoiningsRepository, UserListsRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { User } from '@/models/entities/User.js';
|
2022-06-27 12:48:10 +00:00
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
2023-03-10 05:22:37 +00:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import Channel from '../channel.js';
|
2018-10-07 02:06:17 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
class UserListChannel extends Channel {
|
2018-10-11 14:01:57 +00:00
|
|
|
public readonly chName = 'userList';
|
2018-10-11 14:07:20 +00:00
|
|
|
public static shouldShare = false;
|
2018-11-10 17:22:34 +00:00
|
|
|
public static requireCredential = false;
|
2019-04-07 12:50:36 +00:00
|
|
|
private listId: string;
|
|
|
|
public listUsers: User['id'][] = [];
|
|
|
|
private listUsersClock: NodeJS.Timer;
|
2018-10-11 14:01:57 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
constructor(
|
|
|
|
private userListsRepository: UserListsRepository,
|
|
|
|
private userListJoiningsRepository: UserListJoiningsRepository,
|
|
|
|
private noteEntityService: NoteEntityService,
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
id: string,
|
|
|
|
connection: Channel['connection'],
|
|
|
|
) {
|
2022-02-27 02:07:39 +00:00
|
|
|
super(id, connection);
|
2022-12-04 06:03:09 +00:00
|
|
|
//this.updateListUsers = this.updateListUsers.bind(this);
|
|
|
|
//this.onNote = this.onNote.bind(this);
|
2022-02-27 02:07:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2018-10-07 02:06:17 +00:00
|
|
|
public async init(params: any) {
|
2019-04-07 12:50:36 +00:00
|
|
|
this.listId = params.listId as string;
|
2018-10-07 02:06:17 +00:00
|
|
|
|
2020-01-26 20:36:59 +00:00
|
|
|
// Check existence and owner
|
2022-09-17 18:27:08 +00:00
|
|
|
const list = await this.userListsRepository.findOneBy({
|
2020-01-26 20:36:59 +00:00
|
|
|
id: this.listId,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: this.user!.id,
|
2020-01-26 20:36:59 +00:00
|
|
|
});
|
|
|
|
if (!list) return;
|
|
|
|
|
2018-10-07 02:06:17 +00:00
|
|
|
// Subscribe stream
|
2019-04-07 12:50:36 +00:00
|
|
|
this.subscriber.on(`userListStream:${this.listId}`, this.send);
|
|
|
|
|
|
|
|
this.subscriber.on('notesStream', this.onNote);
|
|
|
|
|
|
|
|
this.updateListUsers();
|
|
|
|
this.listUsersClock = setInterval(this.updateListUsers, 5000);
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2019-04-07 12:50:36 +00:00
|
|
|
private async updateListUsers() {
|
2022-09-17 18:27:08 +00:00
|
|
|
const users = await this.userListJoiningsRepository.find({
|
2019-04-07 12:50:36 +00:00
|
|
|
where: {
|
|
|
|
userListId: this.listId,
|
|
|
|
},
|
2021-12-09 14:58:30 +00:00
|
|
|
select: ['userId'],
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.listUsers = users.map(x => x.userId);
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2021-09-22 13:35:55 +00:00
|
|
|
private async onNote(note: Packed<'Note'>) {
|
2019-04-07 12:50:36 +00:00
|
|
|
if (!this.listUsers.includes(note.userId)) return;
|
|
|
|
|
|
|
|
if (['followers', 'specified'].includes(note.visibility)) {
|
2022-09-17 18:27:08 +00:00
|
|
|
note = await this.noteEntityService.pack(note.id, this.user, {
|
2021-12-09 14:58:30 +00:00
|
|
|
detail: true,
|
2019-01-20 15:32:54 +00:00
|
|
|
});
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
if (note.isHidden) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// リプライなら再pack
|
|
|
|
if (note.replyId != null) {
|
2022-09-17 18:27:08 +00:00
|
|
|
note.reply = await this.noteEntityService.pack(note.replyId, this.user, {
|
2021-12-09 14:58:30 +00:00
|
|
|
detail: true,
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
// Renoteなら再pack
|
|
|
|
if (note.renoteId != null) {
|
2022-09-17 18:27:08 +00:00
|
|
|
note.renote = await this.noteEntityService.pack(note.renoteId, this.user, {
|
2021-12-09 14:58:30 +00:00
|
|
|
detail: true,
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する
|
2023-04-05 01:21:10 +00:00
|
|
|
if (isUserRelated(note, this.userIdsWhoMeMuting)) return;
|
2021-08-17 12:48:59 +00:00
|
|
|
// 流れてきたNoteがブロックされているユーザーが関わるものだったら無視する
|
2023-04-05 01:21:10 +00:00
|
|
|
if (isUserRelated(note, this.userIdsWhoBlockingMe)) return;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2023-04-05 01:21:10 +00:00
|
|
|
if (note.renote && !note.text && isUserRelated(note, this.userIdsWhoMeMutingRenotes)) return;
|
2023-03-07 23:56:09 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
this.send('note', note);
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2019-04-07 12:50:36 +00:00
|
|
|
public dispose() {
|
|
|
|
// Unsubscribe events
|
|
|
|
this.subscriber.off(`userListStream:${this.listId}`, this.send);
|
|
|
|
this.subscriber.off('notesStream', this.onNote);
|
|
|
|
|
|
|
|
clearInterval(this.listUsersClock);
|
2018-10-07 02:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class UserListChannelService {
|
|
|
|
public readonly shouldShare = UserListChannel.shouldShare;
|
|
|
|
public readonly requireCredential = UserListChannel.requireCredential;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.userListsRepository)
|
|
|
|
private userListsRepository: UserListsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userListJoiningsRepository)
|
|
|
|
private userListJoiningsRepository: UserListJoiningsRepository,
|
|
|
|
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public create(id: string, connection: Channel['connection']): UserListChannel {
|
|
|
|
return new UserListChannel(
|
|
|
|
this.userListsRepository,
|
|
|
|
this.userListJoiningsRepository,
|
|
|
|
this.noteEntityService,
|
|
|
|
id,
|
|
|
|
connection,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|