2023-07-27 00:31:52 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-03-02 20:13:12 -06:00
|
|
|
import { setTimeout } from 'node:timers/promises';
|
|
|
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
2023-04-04 17:52:49 -05:00
|
|
|
import { In } from 'typeorm';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-19 21:33:36 -05:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
2023-03-09 23:22:37 -06:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2023-09-19 21:33:36 -05:00
|
|
|
import type { MiNote } from '@/models/Note.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2023-09-15 00:28:29 -05:00
|
|
|
import type { NoteUnreadsRepository, MutingsRepository, NoteThreadMutingsRepository } from '@/models/_.js';
|
2023-02-01 02:29:28 -06:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
@Injectable()
|
2023-03-02 20:13:12 -06:00
|
|
|
export class NoteReadService implements OnApplicationShutdown {
|
|
|
|
#shutdownController = new AbortController();
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.noteUnreadsRepository)
|
|
|
|
private noteUnreadsRepository: NoteUnreadsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.mutingsRepository)
|
|
|
|
private mutingsRepository: MutingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.noteThreadMutingsRepository)
|
|
|
|
private noteThreadMutingsRepository: NoteThreadMutingsRepository,
|
|
|
|
|
|
|
|
private idService: IdService,
|
2023-02-03 19:02:03 -06:00
|
|
|
private globalEventService: GlobalEventService,
|
2022-09-17 13:27:08 -05:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2023-08-16 03:51:28 -05:00
|
|
|
public async insertNoteUnread(userId: MiUser['id'], note: MiNote, params: {
|
2022-09-17 13:27:08 -05:00
|
|
|
// NOTE: isSpecifiedがtrueならisMentionedは必ずfalse
|
|
|
|
isSpecified: boolean;
|
|
|
|
isMentioned: boolean;
|
|
|
|
}): Promise<void> {
|
|
|
|
//#region ミュートしているなら無視
|
|
|
|
const mute = await this.mutingsRepository.findBy({
|
|
|
|
muterId: userId,
|
|
|
|
});
|
|
|
|
if (mute.map(m => m.muteeId).includes(note.userId)) return;
|
|
|
|
//#endregion
|
2023-03-02 20:13:12 -06:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
// スレッドミュート
|
2023-07-11 00:58:58 -05:00
|
|
|
const isThreadMuted = await this.noteThreadMutingsRepository.exist({
|
|
|
|
where: {
|
|
|
|
userId: userId,
|
|
|
|
threadId: note.threadId ?? note.id,
|
|
|
|
},
|
2022-09-17 13:27:08 -05:00
|
|
|
});
|
2023-07-11 00:58:58 -05:00
|
|
|
if (isThreadMuted) return;
|
2023-03-02 20:13:12 -06:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
const unread = {
|
2023-10-15 20:45:22 -05:00
|
|
|
id: this.idService.gen(),
|
2022-09-17 13:27:08 -05:00
|
|
|
noteId: note.id,
|
|
|
|
userId: userId,
|
|
|
|
isSpecified: params.isSpecified,
|
|
|
|
isMentioned: params.isMentioned,
|
|
|
|
noteUserId: note.userId,
|
|
|
|
};
|
2023-03-02 20:13:12 -06:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
await this.noteUnreadsRepository.insert(unread);
|
2023-03-02 20:13:12 -06:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
// 2秒経っても既読にならなかったら「未読の投稿がありますよ」イベントを発行する
|
2023-03-02 20:13:12 -06:00
|
|
|
setTimeout(2000, 'unread note', { signal: this.#shutdownController.signal }).then(async () => {
|
2023-07-11 00:58:58 -05:00
|
|
|
const exist = await this.noteUnreadsRepository.exist({ where: { id: unread.id } });
|
2023-03-02 20:13:12 -06:00
|
|
|
|
2023-07-11 00:58:58 -05:00
|
|
|
if (!exist) return;
|
2023-03-02 20:13:12 -06:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
if (params.isMentioned) {
|
2023-02-03 19:02:03 -06:00
|
|
|
this.globalEventService.publishMainStream(userId, 'unreadMention', note.id);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
if (params.isSpecified) {
|
2023-02-03 19:02:03 -06:00
|
|
|
this.globalEventService.publishMainStream(userId, 'unreadSpecifiedNote', note.id);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
2023-03-02 20:13:12 -06:00
|
|
|
}, () => { /* aborted, ignore it */ });
|
|
|
|
}
|
2022-09-17 13:27:08 -05:00
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-17 13:27:08 -05:00
|
|
|
public async read(
|
2023-08-16 03:51:28 -05:00
|
|
|
userId: MiUser['id'],
|
|
|
|
notes: (MiNote | Packed<'Note'>)[],
|
2022-09-17 13:27:08 -05:00
|
|
|
): Promise<void> {
|
2023-08-16 03:51:28 -05:00
|
|
|
const readMentions: (MiNote | Packed<'Note'>)[] = [];
|
|
|
|
const readSpecifiedNotes: (MiNote | Packed<'Note'>)[] = [];
|
2023-03-02 20:13:12 -06:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
for (const note of notes) {
|
|
|
|
if (note.mentions && note.mentions.includes(userId)) {
|
|
|
|
readMentions.push(note);
|
|
|
|
} else if (note.visibleUserIds && note.visibleUserIds.includes(userId)) {
|
|
|
|
readSpecifiedNotes.push(note);
|
|
|
|
}
|
|
|
|
}
|
2023-03-02 20:13:12 -06:00
|
|
|
|
2023-04-04 17:52:49 -05:00
|
|
|
if ((readMentions.length > 0) || (readSpecifiedNotes.length > 0)) {
|
2022-09-17 13:27:08 -05:00
|
|
|
// Remove the record
|
|
|
|
await this.noteUnreadsRepository.delete({
|
|
|
|
userId: userId,
|
2023-04-04 17:52:49 -05:00
|
|
|
noteId: In([...readMentions.map(n => n.id), ...readSpecifiedNotes.map(n => n.id)]),
|
2022-09-17 13:27:08 -05:00
|
|
|
});
|
2023-03-02 20:13:12 -06:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
// TODO: ↓まとめてクエリしたい
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
this.noteUnreadsRepository.countBy({
|
|
|
|
userId: userId,
|
|
|
|
isMentioned: true,
|
|
|
|
}).then(mentionsCount => {
|
|
|
|
if (mentionsCount === 0) {
|
|
|
|
// 全て既読になったイベントを発行
|
2023-02-03 19:02:03 -06:00
|
|
|
this.globalEventService.publishMainStream(userId, 'readAllUnreadMentions');
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
});
|
2023-07-07 17:08:16 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
this.noteUnreadsRepository.countBy({
|
|
|
|
userId: userId,
|
|
|
|
isSpecified: true,
|
|
|
|
}).then(specifiedCount => {
|
|
|
|
if (specifiedCount === 0) {
|
|
|
|
// 全て既読になったイベントを発行
|
2023-02-03 19:02:03 -06:00
|
|
|
this.globalEventService.publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-03-02 20:13:12 -06:00
|
|
|
|
2023-05-28 23:21:26 -05:00
|
|
|
@bindThis
|
|
|
|
public dispose(): void {
|
2023-03-02 20:13:12 -06:00
|
|
|
this.#shutdownController.abort();
|
|
|
|
}
|
2023-05-28 23:21:26 -05:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|