FoundKey/src/services/add-note-to-antenna.ts
syuilo f6154dc0af
v12 (#5712)
Co-authored-by: MeiMei <30769358+mei23@users.noreply.github.com>
Co-authored-by: Satsuki Yanagi <17376330+u1-liquid@users.noreply.github.com>
2020-01-30 04:37:25 +09:00

54 lines
1.5 KiB
TypeScript

import { Antenna } from '../models/entities/antenna';
import { Note } from '../models/entities/note';
import { AntennaNotes, Mutings, Notes } from '../models';
import { genId } from '../misc/gen-id';
import shouldMuteThisNote from '../misc/should-mute-this-note';
import { ensure } from '../prelude/ensure';
import { publishAntennaStream, publishMainStream } from './stream';
import { User } from '../models/entities/user';
export async function addNoteToAntenna(antenna: Antenna, note: Note, noteUser: User) {
// 通知しない設定になっているか、自分自身の投稿なら既読にする
const read = !antenna.notify || (antenna.userId === noteUser.id);
AntennaNotes.save({
id: genId(),
antennaId: antenna.id,
noteId: note.id,
read: read,
});
publishAntennaStream(antenna.id, 'note', note);
if (!read) {
const mutings = await Mutings.find({
where: {
muterId: antenna.userId
},
select: ['muteeId']
});
const _note: Note = {
...note
};
if (note.replyId != null) {
_note.reply = await Notes.findOne(note.replyId).then(ensure);
}
if (note.renoteId != null) {
_note.renote = await Notes.findOne(note.renoteId).then(ensure);
}
if (shouldMuteThisNote(_note, mutings.map(x => x.muteeId))) {
return;
}
// 2秒経っても既読にならなかったら通知
setTimeout(async () => {
const unread = await AntennaNotes.findOne({ antennaId: antenna.id, read: false });
if (unread) {
publishMainStream(antenna.userId, 'unreadAntenna', antenna);
}
}, 2000);
}
}