2023-01-13 05:40:33 +01:00
|
|
|
import type Bull from "bull";
|
|
|
|
import { In } from "typeorm";
|
|
|
|
import { Notes, Polls, PollVotes } from "@/models/index.js";
|
|
|
|
import { queueLogger } from "../logger.js";
|
|
|
|
import type { EndedPollNotificationJobData } from "@/queue/types.js";
|
|
|
|
import { createNotification } from "@/services/create-notification.js";
|
2022-03-06 08:06:27 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const logger = queueLogger.createSubLogger("ended-poll-notification");
|
2022-03-06 08:06:27 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export async function endedPollNotification(
|
|
|
|
job: Bull.Job<EndedPollNotificationJobData>,
|
|
|
|
done: any,
|
|
|
|
): Promise<void> {
|
2022-03-26 07:34:00 +01:00
|
|
|
const note = await Notes.findOneBy({ id: job.data.noteId });
|
2022-03-06 08:06:27 +01:00
|
|
|
if (note == null || !note.hasPoll) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const votes = await PollVotes.createQueryBuilder("vote")
|
|
|
|
.select("vote.userId")
|
|
|
|
.where("vote.noteId = :noteId", { noteId: note.id })
|
|
|
|
.innerJoinAndSelect("vote.user", "user")
|
|
|
|
.andWhere("user.host IS NULL")
|
2022-03-06 08:06:27 +01:00
|
|
|
.getMany();
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const userIds = [...new Set([note.userId, ...votes.map((v) => v.userId)])];
|
2022-03-06 08:06:27 +01:00
|
|
|
|
|
|
|
for (const userId of userIds) {
|
2023-01-13 05:40:33 +01:00
|
|
|
createNotification(userId, "pollEnded", {
|
2022-03-06 08:06:27 +01:00
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|