refactor: Refactor NoteReadService.read

This commit is contained in:
tamaina 2024-02-21 18:59:30 +00:00
parent b36e6b1a77
commit 9e662bc19f
1 changed files with 31 additions and 30 deletions

View File

@ -88,46 +88,47 @@ export class NoteReadService implements OnApplicationShutdown {
userId: MiUser['id'], userId: MiUser['id'],
notes: (MiNote | Packed<'Note'>)[], notes: (MiNote | Packed<'Note'>)[],
): Promise<void> { ): Promise<void> {
const readMentions: (MiNote | Packed<'Note'>)[] = []; if (notes.length === 0) return;
const readSpecifiedNotes: (MiNote | Packed<'Note'>)[] = [];
const noteIds = new Set<string>();
for (const note of notes) { for (const note of notes) {
if (note.mentions && note.mentions.includes(userId)) { if (note.mentions && note.mentions.includes(userId)) {
readMentions.push(note); noteIds.add(note.id);
} else if (note.visibleUserIds && note.visibleUserIds.includes(userId)) { } else if (note.visibleUserIds && note.visibleUserIds.includes(userId)) {
readSpecifiedNotes.push(note); noteIds.add(note.id);
} }
} }
if ((readMentions.length > 0) || (readSpecifiedNotes.length > 0)) { if (noteIds.size === 0) return;
// Remove the record
await this.noteUnreadsRepository.delete({
userId: userId,
noteId: In([...readMentions.map(n => n.id), ...readSpecifiedNotes.map(n => n.id)]),
});
// TODO: ↓まとめてクエリしたい // Remove the record
await this.noteUnreadsRepository.delete({
userId: userId,
noteId: In(Array.from(noteIds)),
});
trackPromise(this.noteUnreadsRepository.countBy({ // TODO: ↓まとめてクエリしたい
userId: userId,
isMentioned: true,
}).then(mentionsCount => {
if (mentionsCount === 0) {
// 全て既読になったイベントを発行
this.globalEventService.publishMainStream(userId, 'readAllUnreadMentions');
}
}));
trackPromise(this.noteUnreadsRepository.countBy({ trackPromise(this.noteUnreadsRepository.countBy({
userId: userId, userId: userId,
isSpecified: true, isMentioned: true,
}).then(specifiedCount => { }).then(mentionsCount => {
if (specifiedCount === 0) { if (mentionsCount === 0) {
// 全て既読になったイベントを発行 // 全て既読になったイベントを発行
this.globalEventService.publishMainStream(userId, 'readAllUnreadSpecifiedNotes'); this.globalEventService.publishMainStream(userId, 'readAllUnreadMentions');
} }
})); }));
}
trackPromise(this.noteUnreadsRepository.countBy({
userId: userId,
isSpecified: true,
}).then(specifiedCount => {
if (specifiedCount === 0) {
// 全て既読になったイベントを発行
this.globalEventService.publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
}
}));
} }
@bindThis @bindThis