2023-01-12 21:40:33 -07: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"] },
|
|
|
|
) {
|
2020-01-29 12:37:25 -07:00
|
|
|
// 通知しない設定になっているか、自分自身の投稿なら既読にする
|
2023-01-12 21:40:33 -07:00
|
|
|
const read = !antenna.notify || antenna.userId === noteUser.id;
|
2020-01-29 12:37:25 -07:00
|
|
|
|
2021-03-21 06:27:09 -06:00
|
|
|
AntennaNotes.insert({
|
2020-01-29 12:37:25 -07:00
|
|
|
id: genId(),
|
|
|
|
antennaId: antenna.id,
|
|
|
|
noteId: note.id,
|
|
|
|
read: read,
|
|
|
|
});
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
publishAntennaStream(antenna.id, "note", note);
|
2020-01-29 12:37:25 -07:00
|
|
|
|
|
|
|
if (!read) {
|
|
|
|
const mutings = await Mutings.find({
|
|
|
|
where: {
|
2021-12-09 07:58:30 -07:00
|
|
|
muterId: antenna.userId,
|
2020-01-29 12:37:25 -07:00
|
|
|
},
|
2023-01-12 21:40:33 -07:00
|
|
|
select: ["muteeId"],
|
2020-01-29 12:37:25 -07:00
|
|
|
});
|
|
|
|
|
2020-08-18 07:44:21 -06:00
|
|
|
// Copy
|
2020-01-29 12:37:25 -07:00
|
|
|
const _note: Note = {
|
2021-12-09 07:58:30 -07:00
|
|
|
...note,
|
2020-01-29 12:37:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (note.replyId != null) {
|
2022-03-26 00:34:00 -06:00
|
|
|
_note.reply = await Notes.findOneByOrFail({ id: note.replyId });
|
2020-01-29 12:37:25 -07:00
|
|
|
}
|
|
|
|
if (note.renoteId != null) {
|
2022-03-26 00:34:00 -06:00
|
|
|
_note.renote = await Notes.findOneByOrFail({ id: note.renoteId });
|
2020-01-29 12:37:25 -07:00
|
|
|
}
|
2020-03-03 19:45:33 -07:00
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
if (isUserRelated(_note, new Set<string>(mutings.map((x) => x.muteeId)))) {
|
2020-01-29 12:37:25 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2秒経っても既読にならなかったら通知
|
|
|
|
setTimeout(async () => {
|
2023-01-12 21:40:33 -07:00
|
|
|
const unread = await AntennaNotes.findOneBy({
|
|
|
|
antennaId: antenna.id,
|
|
|
|
read: false,
|
|
|
|
});
|
2020-01-29 12:37:25 -07:00
|
|
|
if (unread) {
|
2023-01-12 21:40:33 -07:00
|
|
|
publishMainStream(antenna.userId, "unreadAntenna", antenna);
|
2020-01-29 12:37:25 -07:00
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
}
|