2023-12-05 07:12:10 +00:00
|
|
|
import { publishMainStream } from "@/services/stream.js";
|
2023-01-13 04:40:33 +00:00
|
|
|
import type { Note } from "@/models/entities/note.js";
|
|
|
|
import type { User } from "@/models/entities/user.js";
|
2024-04-20 20:57:05 +00:00
|
|
|
import { NoteUnreads, Followings, ChannelFollowings } from "@/models/index.js";
|
2023-12-05 07:12:10 +00:00
|
|
|
import { Not, IsNull, In } from "typeorm";
|
|
|
|
import type { Channel } from "@/models/entities/channel.js";
|
2023-01-13 04:40:33 +00:00
|
|
|
import { readNotificationByQuery } from "@/server/api/common/read-notification.js";
|
2023-12-05 07:12:10 +00:00
|
|
|
import type { Packed } from "@/misc/schema.js";
|
2021-03-21 06:35:02 +00:00
|
|
|
|
|
|
|
/**
|
2021-03-21 08:38:09 +00:00
|
|
|
* Mark notes as read
|
2021-03-21 06:35:02 +00:00
|
|
|
*/
|
2023-01-13 04:40:33 +00:00
|
|
|
export default async function (
|
|
|
|
userId: User["id"],
|
|
|
|
notes: (Note | Packed<"Note">)[],
|
2021-03-23 06:12:47 +00:00
|
|
|
info?: {
|
2023-01-13 04:40:33 +00:00
|
|
|
following: Set<User["id"]>;
|
|
|
|
followingChannels: Set<Channel["id"]>;
|
|
|
|
},
|
2021-03-21 06:35:02 +00:00
|
|
|
) {
|
2023-01-13 04:40:33 +00:00
|
|
|
const following = info?.following
|
|
|
|
? info.following
|
|
|
|
: new Set<string>(
|
|
|
|
(
|
|
|
|
await Followings.find({
|
|
|
|
where: {
|
|
|
|
followerId: userId,
|
|
|
|
},
|
|
|
|
select: ["followeeId"],
|
|
|
|
})
|
|
|
|
).map((x) => x.followeeId),
|
2024-03-28 05:25:33 +00:00
|
|
|
);
|
2023-01-13 04:40:33 +00:00
|
|
|
const followingChannels = info?.followingChannels
|
|
|
|
? info.followingChannels
|
|
|
|
: new Set<string>(
|
|
|
|
(
|
|
|
|
await ChannelFollowings.find({
|
|
|
|
where: {
|
|
|
|
followerId: userId,
|
|
|
|
},
|
|
|
|
select: ["followeeId"],
|
|
|
|
})
|
|
|
|
).map((x) => x.followeeId),
|
2024-03-28 05:25:33 +00:00
|
|
|
);
|
2021-03-23 06:12:47 +00:00
|
|
|
|
2023-07-08 23:10:12 +00:00
|
|
|
// const myAntennas = (await getAntennas()).filter((a) => a.userId === userId);
|
2023-01-13 04:40:33 +00:00
|
|
|
const readMentions: (Note | Packed<"Note">)[] = [];
|
|
|
|
const readSpecifiedNotes: (Note | Packed<"Note">)[] = [];
|
|
|
|
const readChannelNotes: (Note | Packed<"Note">)[] = [];
|
2023-07-08 22:39:42 +00:00
|
|
|
// const readAntennaNotes: (Note | Packed<"Note">)[] = [];
|
2021-03-23 06:06:56 +00:00
|
|
|
|
|
|
|
for (const note of notes) {
|
2023-01-13 04:40:33 +00:00
|
|
|
if (note.mentions?.includes(userId)) {
|
2021-03-23 06:06:56 +00:00
|
|
|
readMentions.push(note);
|
2023-01-13 04:40:33 +00:00
|
|
|
} else if (note.visibleUserIds?.includes(userId)) {
|
2021-03-23 06:06:56 +00:00
|
|
|
readSpecifiedNotes.push(note);
|
|
|
|
}
|
|
|
|
|
2021-03-23 06:12:47 +00:00
|
|
|
if (note.channelId && followingChannels.has(note.channelId)) {
|
2021-03-23 06:06:56 +00:00
|
|
|
readChannelNotes.push(note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
if (
|
|
|
|
readMentions.length > 0 ||
|
|
|
|
readSpecifiedNotes.length > 0 ||
|
|
|
|
readChannelNotes.length > 0
|
|
|
|
) {
|
2021-03-21 06:35:02 +00:00
|
|
|
// Remove the record
|
|
|
|
await NoteUnreads.delete({
|
|
|
|
userId: userId,
|
2023-01-13 04:40:33 +00:00
|
|
|
noteId: In([
|
|
|
|
...readMentions.map((n) => n.id),
|
|
|
|
...readSpecifiedNotes.map((n) => n.id),
|
|
|
|
...readChannelNotes.map((n) => n.id),
|
|
|
|
]),
|
2021-03-21 06:35:02 +00:00
|
|
|
});
|
|
|
|
|
2021-03-23 06:06:56 +00:00
|
|
|
// TODO: ↓まとめてクエリしたい
|
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
NoteUnreads.countBy({
|
2021-03-21 08:38:09 +00:00
|
|
|
userId: userId,
|
2021-12-09 14:58:30 +00:00
|
|
|
isMentioned: true,
|
2023-01-13 04:40:33 +00:00
|
|
|
}).then((mentionsCount) => {
|
2021-03-21 08:38:09 +00:00
|
|
|
if (mentionsCount === 0) {
|
|
|
|
// 全て既読になったイベントを発行
|
2023-01-13 04:40:33 +00:00
|
|
|
publishMainStream(userId, "readAllUnreadMentions");
|
2021-03-21 08:38:09 +00:00
|
|
|
}
|
|
|
|
});
|
2021-03-21 06:35:02 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
NoteUnreads.countBy({
|
2021-03-21 08:38:09 +00:00
|
|
|
userId: userId,
|
2021-12-09 14:58:30 +00:00
|
|
|
isSpecified: true,
|
2023-01-13 04:40:33 +00:00
|
|
|
}).then((specifiedCount) => {
|
2021-03-21 08:38:09 +00:00
|
|
|
if (specifiedCount === 0) {
|
|
|
|
// 全て既読になったイベントを発行
|
2023-01-13 04:40:33 +00:00
|
|
|
publishMainStream(userId, "readAllUnreadSpecifiedNotes");
|
2021-03-21 08:38:09 +00:00
|
|
|
}
|
|
|
|
});
|
2021-03-21 06:35:02 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
NoteUnreads.countBy({
|
2021-03-21 08:38:09 +00:00
|
|
|
userId: userId,
|
2021-12-09 14:58:30 +00:00
|
|
|
noteChannelId: Not(IsNull()),
|
2023-01-13 04:40:33 +00:00
|
|
|
}).then((channelNoteCount) => {
|
2021-03-21 08:38:09 +00:00
|
|
|
if (channelNoteCount === 0) {
|
|
|
|
// 全て既読になったイベントを発行
|
2023-01-13 04:40:33 +00:00
|
|
|
publishMainStream(userId, "readAllChannels");
|
2021-03-21 08:38:09 +00:00
|
|
|
}
|
|
|
|
});
|
2021-07-08 16:07:55 +00:00
|
|
|
|
|
|
|
readNotificationByQuery(userId, {
|
2023-01-13 04:40:33 +00:00
|
|
|
noteId: In([
|
|
|
|
...readMentions.map((n) => n.id),
|
|
|
|
...readSpecifiedNotes.map((n) => n.id),
|
|
|
|
]),
|
2021-07-08 16:07:55 +00:00
|
|
|
});
|
2021-03-21 06:35:02 +00:00
|
|
|
}
|
|
|
|
}
|