2022-02-27 03:07:39 +01:00
|
|
|
import { Note } from '@/models/entities/note.js';
|
|
|
|
import { publishMainStream } from '@/services/stream.js';
|
|
|
|
import { 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
|
|
|
|
2021-10-31 07:30:22 +01:00
|
|
|
export async function insertNoteUnread(userId: User['id'], note: Note, params: {
|
2020-08-18 15:44:21 +02:00
|
|
|
// 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にミュートは適用されないのでよしなにケアする
|
2019-04-07 14:50:36 +02:00
|
|
|
const mute = await Mutings.find({
|
2021-12-09 15:58:30 +01:00
|
|
|
muterId: userId,
|
2018-09-21 01:37:26 +02:00
|
|
|
});
|
2019-04-07 14:50:36 +02: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
|
|
|
// スレッドミュート
|
|
|
|
const threadMute = await NoteThreadMutings.findOne({
|
|
|
|
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 () => {
|
2019-04-07 14:50:36 +02:00
|
|
|
const exist = await NoteUnreads.findOne(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) {
|
|
|
|
publishMainStream(userId, 'unreadMention', note.id);
|
|
|
|
}
|
|
|
|
if (params.isSpecified) {
|
|
|
|
publishMainStream(userId, 'unreadSpecifiedNote', note.id);
|
|
|
|
}
|
|
|
|
if (note.channelId) {
|
|
|
|
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
|
|
|
}
|