2021-08-19 14:55:45 +02:00
|
|
|
import { Note } from '@/models/entities/note';
|
|
|
|
import { publishMainStream } from '@/services/stream';
|
|
|
|
import { User } from '@/models/entities/user';
|
|
|
|
import { Mutings, NoteUnreads } from '@/models/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
2018-09-19 07:18:34 +02:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
export default async function(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にミュートは適用されないのでよしなにケアする
|
2019-04-07 14:50:36 +02:00
|
|
|
const mute = await Mutings.find({
|
2020-08-18 15:44:21 +02: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-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
|
|
|
}
|