2021-08-19 21:55:45 +09: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 14:18:34 +09:00
|
|
|
|
2020-08-18 22:44:21 +09:00
|
|
|
export default async function(userId: User['id'], note: Note, params: {
|
|
|
|
// NOTE: isSpecifiedがtrueならisMentionedは必ずfalse
|
|
|
|
isSpecified: boolean;
|
|
|
|
isMentioned: boolean;
|
|
|
|
}) {
|
2018-09-21 08:37:26 +09:00
|
|
|
//#region ミュートしているなら無視
|
2020-08-18 22:44:21 +09:00
|
|
|
// TODO: 現在の仕様ではChannelにミュートは適用されないのでよしなにケアする
|
2019-04-07 21:50:36 +09:00
|
|
|
const mute = await Mutings.find({
|
2020-08-18 22:44:21 +09:00
|
|
|
muterId: userId
|
2018-09-21 08:37:26 +09:00
|
|
|
});
|
2019-04-07 21:50:36 +09:00
|
|
|
if (mute.map(m => m.muteeId).includes(note.userId)) return;
|
2018-09-21 08:37:26 +09:00
|
|
|
//#endregion
|
|
|
|
|
2021-03-21 21:27:09 +09:00
|
|
|
const unread = {
|
2019-04-07 21:50:36 +09:00
|
|
|
id: genId(),
|
|
|
|
noteId: note.id,
|
2020-08-18 22:44:21 +09:00
|
|
|
userId: userId,
|
|
|
|
isSpecified: params.isSpecified,
|
|
|
|
isMentioned: params.isMentioned,
|
|
|
|
noteChannelId: note.channelId,
|
|
|
|
noteUserId: note.userId,
|
2021-03-21 21:27:09 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
await NoteUnreads.insert(unread);
|
2018-09-19 14:18:34 +09:00
|
|
|
|
2018-10-08 02:10:46 +09:00
|
|
|
// 2秒経っても既読にならなかったら「未読の投稿がありますよ」イベントを発行する
|
2018-09-19 14:18:34 +09:00
|
|
|
setTimeout(async () => {
|
2019-04-07 21:50:36 +09:00
|
|
|
const exist = await NoteUnreads.findOne(unread.id);
|
2018-09-19 14:18:34 +09:00
|
|
|
|
2020-08-18 22:44:21 +09:00
|
|
|
if (exist == null) return;
|
2018-09-19 14:18:34 +09:00
|
|
|
|
2020-08-18 22:44:21 +09: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 14:18:34 +09:00
|
|
|
}
|
2018-10-08 02:10:46 +09:00
|
|
|
}, 2000);
|
2018-09-19 14:18:34 +09:00
|
|
|
}
|