2021-08-19 21:55:45 +09:00
|
|
|
import { Antenna } from '@/models/entities/antenna';
|
|
|
|
import { Note } from '@/models/entities/note';
|
|
|
|
import { AntennaNotes, Mutings, Notes } from '@/models/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
|
|
|
import { isMutedUserRelated } from '@/misc/is-muted-user-related';
|
|
|
|
import { publishAntennaStream, publishMainStream } from '@/services/stream';
|
|
|
|
import { User } from '@/models/entities/user';
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2021-03-24 11:05:37 +09:00
|
|
|
export async function addNoteToAntenna(antenna: Antenna, note: Note, noteUser: { id: User['id']; }) {
|
2020-01-30 04:37:25 +09:00
|
|
|
// 通知しない設定になっているか、自分自身の投稿なら既読にする
|
|
|
|
const read = !antenna.notify || (antenna.userId === noteUser.id);
|
|
|
|
|
2021-03-21 21:27:09 +09:00
|
|
|
AntennaNotes.insert({
|
2020-01-30 04:37:25 +09:00
|
|
|
id: genId(),
|
|
|
|
antennaId: antenna.id,
|
|
|
|
noteId: note.id,
|
|
|
|
read: read,
|
|
|
|
});
|
|
|
|
|
|
|
|
publishAntennaStream(antenna.id, 'note', note);
|
|
|
|
|
|
|
|
if (!read) {
|
|
|
|
const mutings = await Mutings.find({
|
|
|
|
where: {
|
2021-12-09 23:58:30 +09:00
|
|
|
muterId: antenna.userId,
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
select: ['muteeId'],
|
2020-01-30 04:37:25 +09:00
|
|
|
});
|
|
|
|
|
2020-08-18 22:44:21 +09:00
|
|
|
// Copy
|
2020-01-30 04:37:25 +09:00
|
|
|
const _note: Note = {
|
2021-12-09 23:58:30 +09:00
|
|
|
...note,
|
2020-01-30 04:37:25 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
if (note.replyId != null) {
|
2021-02-13 15:33:38 +09:00
|
|
|
_note.reply = await Notes.findOneOrFail(note.replyId);
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
|
|
|
if (note.renoteId != null) {
|
2021-02-13 15:33:38 +09:00
|
|
|
_note.renote = await Notes.findOneOrFail(note.renoteId);
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
2020-03-04 11:45:33 +09:00
|
|
|
|
2021-01-30 11:09:46 +09:00
|
|
|
if (isMutedUserRelated(_note, new Set<string>(mutings.map(x => x.muteeId)))) {
|
2020-01-30 04:37:25 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2秒経っても既読にならなかったら通知
|
|
|
|
setTimeout(async () => {
|
|
|
|
const unread = await AntennaNotes.findOne({ antennaId: antenna.id, read: false });
|
|
|
|
if (unread) {
|
|
|
|
publishMainStream(antenna.userId, 'unreadAntenna', antenna);
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
}
|