2022-03-06 07:06:27 +00:00
|
|
|
import Bull from 'bull';
|
|
|
|
import { In } from 'typeorm';
|
|
|
|
import { Notes, Polls, PollVotes } from '@/models/index.js';
|
|
|
|
import { queueLogger } from '../logger.js';
|
|
|
|
import { EndedPollNotificationJobData } from '@/queue/types.js';
|
|
|
|
import { createNotification } from '@/services/create-notification.js';
|
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('ended-poll-notification');
|
|
|
|
|
|
|
|
export async function endedPollNotification(job: Bull.Job<EndedPollNotificationJobData>, done: any): Promise<void> {
|
2022-03-26 06:34:00 +00:00
|
|
|
const note = await Notes.findOneBy({ id: job.data.noteId });
|
2022-03-06 07:06:27 +00:00
|
|
|
if (note == null || !note.hasPoll) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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')
|
|
|
|
.getMany();
|
|
|
|
|
|
|
|
const userIds = [...new Set([note.userId, ...votes.map(v => v.userId)])];
|
|
|
|
|
|
|
|
for (const userId of userIds) {
|
|
|
|
createNotification(userId, 'pollEnded', {
|
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|