rudeshark.net/packages/backend/src/services/add-note-to-antenna.ts

62 lines
1.6 KiB
TypeScript
Raw Normal View History

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