2023-01-13 05:40:33 +01:00
|
|
|
import type { Note } from "@/models/entities/note.js";
|
|
|
|
import { publishMainStream } from "@/services/stream.js";
|
|
|
|
import type { User } from "@/models/entities/user.js";
|
|
|
|
import { Mutings, NoteThreadMutings, NoteUnreads } from "@/models/index.js";
|
|
|
|
import { genId } from "@/misc/gen-id.js";
|
2018-09-19 07:18:34 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export async function insertNoteUnread(
|
|
|
|
userId: User["id"],
|
|
|
|
note: Note,
|
|
|
|
params: {
|
|
|
|
// NOTE: isSpecifiedがtrueならisMentionedは必ずfalse
|
|
|
|
isSpecified: boolean;
|
|
|
|
isMentioned: boolean;
|
|
|
|
},
|
|
|
|
) {
|
2018-09-21 01:37:26 +02:00
|
|
|
//#region ミュートしているなら無視
|
2020-08-18 15:44:21 +02:00
|
|
|
// TODO: 現在の仕様ではChannelにミュートは適用されないのでよしなにケアする
|
2022-03-26 07:34:00 +01:00
|
|
|
const mute = await Mutings.findBy({
|
2021-12-09 15:58:30 +01:00
|
|
|
muterId: userId,
|
2018-09-21 01:37:26 +02:00
|
|
|
});
|
2023-01-13 05:40:33 +01:00
|
|
|
if (mute.map((m) => m.muteeId).includes(note.userId)) return;
|
2018-09-21 01:37:26 +02:00
|
|
|
//#endregion
|
|
|
|
|
2021-10-31 07:30:22 +01:00
|
|
|
// スレッドミュート
|
2022-03-26 07:34:00 +01:00
|
|
|
const threadMute = await NoteThreadMutings.findOneBy({
|
2021-10-31 07:30:22 +01:00
|
|
|
userId: userId,
|
|
|
|
threadId: note.threadId || note.id,
|
|
|
|
});
|
|
|
|
if (threadMute) return;
|
|
|
|
|
2021-03-21 13:27:09 +01:00
|
|
|
const unread = {
|
2019-04-07 14:50:36 +02:00
|
|
|
id: genId(),
|
|
|
|
noteId: note.id,
|
2020-08-18 15:44:21 +02:00
|
|
|
userId: userId,
|
|
|
|
isSpecified: params.isSpecified,
|
|
|
|
isMentioned: params.isMentioned,
|
|
|
|
noteChannelId: note.channelId,
|
|
|
|
noteUserId: note.userId,
|
2021-03-21 13:27:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
await NoteUnreads.insert(unread);
|
2018-09-19 07:18:34 +02:00
|
|
|
|
2018-10-07 19:10:46 +02:00
|
|
|
// 2秒経っても既読にならなかったら「未読の投稿がありますよ」イベントを発行する
|
2018-09-19 07:18:34 +02:00
|
|
|
setTimeout(async () => {
|
2022-03-26 07:34:00 +01:00
|
|
|
const exist = await NoteUnreads.findOneBy({ id: unread.id });
|
2018-09-19 07:18:34 +02:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
if (exist == null) return;
|
2018-09-19 07:18:34 +02:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
if (params.isMentioned) {
|
2023-01-13 05:40:33 +01:00
|
|
|
publishMainStream(userId, "unreadMention", note.id);
|
2020-08-18 15:44:21 +02:00
|
|
|
}
|
|
|
|
if (params.isSpecified) {
|
2023-01-13 05:40:33 +01:00
|
|
|
publishMainStream(userId, "unreadSpecifiedNote", note.id);
|
2020-08-18 15:44:21 +02:00
|
|
|
}
|
|
|
|
if (note.channelId) {
|
2023-01-13 05:40:33 +01:00
|
|
|
publishMainStream(userId, "unreadChannel", note.id);
|
2018-09-19 07:18:34 +02:00
|
|
|
}
|
2018-10-07 19:10:46 +02:00
|
|
|
}, 2000);
|
2018-09-19 07:18:34 +02:00
|
|
|
}
|