2020-01-29 20:37:25 +01:00
|
|
|
import { Antenna } from '../models/entities/antenna';
|
|
|
|
import { Note } from '../models/entities/note';
|
|
|
|
import { AntennaNotes, Mutings, Notes } from '../models';
|
|
|
|
import { genId } from '../misc/gen-id';
|
2020-07-28 02:36:43 +02:00
|
|
|
import { isMutedUserRelated } from '../misc/is-muted-user-related';
|
2020-01-29 20:37:25 +01:00
|
|
|
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);
|
|
|
|
|
2021-03-21 13:27:09 +01:00
|
|
|
AntennaNotes.insert({
|
2020-01-29 20:37:25 +01:00
|
|
|
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']
|
|
|
|
});
|
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
// Copy
|
2020-01-29 20:37:25 +01:00
|
|
|
const _note: Note = {
|
|
|
|
...note
|
|
|
|
};
|
|
|
|
|
|
|
|
if (note.replyId != null) {
|
2021-02-13 07:33:38 +01:00
|
|
|
_note.reply = await Notes.findOneOrFail(note.replyId);
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
if (note.renoteId != null) {
|
2021-02-13 07:33:38 +01:00
|
|
|
_note.renote = await Notes.findOneOrFail(note.renoteId);
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
2020-03-04 03:45:33 +01:00
|
|
|
|
2021-01-30 03:09:46 +01:00
|
|
|
if (isMutedUserRelated(_note, new Set<string>(mutings.map(x => x.muteeId)))) {
|
2020-01-29 20:37:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2秒経っても既読にならなかったら通知
|
|
|
|
setTimeout(async () => {
|
|
|
|
const unread = await AntennaNotes.findOne({ antennaId: antenna.id, read: false });
|
|
|
|
if (unread) {
|
|
|
|
publishMainStream(antenna.userId, 'unreadAntenna', antenna);
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
}
|