2023-07-27 08:31:52 +03:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { PollVotesRepository, NotesRepository } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type Logger from '@/logger.js';
|
2023-03-16 07:24:11 +02:00
|
|
|
import { NotificationService } from '@/core/NotificationService.js';
|
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
2023-05-29 05:54:49 +03:00
|
|
|
import type * as Bull from 'bullmq';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type { EndedPollNotificationJobData } from '../types.js';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class EndedPollNotificationProcessorService {
|
2022-09-18 21:11:50 +03:00
|
|
|
private logger: Logger;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pollVotesRepository)
|
|
|
|
private pollVotesRepository: PollVotesRepository,
|
|
|
|
|
2023-03-16 07:24:11 +02:00
|
|
|
private notificationService: NotificationService,
|
2022-09-17 21:27:08 +03:00
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('ended-poll-notification');
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2023-05-29 05:54:49 +03:00
|
|
|
public async process(job: Bull.Job<EndedPollNotificationJobData>): Promise<void> {
|
2022-09-17 21:27:08 +03:00
|
|
|
const note = await this.notesRepository.findOneBy({ id: job.data.noteId });
|
|
|
|
if (note == null || !note.hasPoll) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const votes = await this.pollVotesRepository.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) {
|
2023-03-16 07:24:11 +02:00
|
|
|
this.notificationService.createNotification(userId, 'pollEnded', {
|
2022-09-17 21:27:08 +03:00
|
|
|
noteId: note.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|