Sharkey/packages/backend/src/queue/processors/CheckExpiredMutingsProcessorService.ts

53 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-09-17 21:27:08 +03:00
import { Inject, Injectable } from '@nestjs/common';
import { In } from 'typeorm';
2022-09-17 21:27:08 +03:00
import { DI } from '@/di-symbols.js';
2022-09-20 23:33:11 +03:00
import type { MutingsRepository } from '@/models/index.js';
import type { Config } from '@/config.js';
2022-09-17 21:27:08 +03:00
import type Logger from '@/logger.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { QueueLoggerService } from '../QueueLoggerService.js';
import type Bull from 'bull';
import { bindThis } from '@/decorators.js';
2022-09-17 21:27:08 +03:00
@Injectable()
export class CheckExpiredMutingsProcessorService {
2022-09-18 21:11:50 +03:00
private logger: Logger;
2022-09-17 21:27:08 +03:00
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.mutingsRepository)
private mutingsRepository: MutingsRepository,
private globalEventService: GlobalEventService,
private queueLoggerService: QueueLoggerService,
) {
2022-09-18 21:11:50 +03:00
this.logger = this.queueLoggerService.logger.createSubLogger('check-expired-mutings');
2022-09-17 21:27:08 +03:00
}
@bindThis
2022-09-17 21:27:08 +03:00
public async process(job: Bull.Job<Record<string, unknown>>, done: () => void): Promise<void> {
2022-09-18 21:11:50 +03:00
this.logger.info('Checking expired mutings...');
2022-09-17 21:27:08 +03:00
const expired = await this.mutingsRepository.createQueryBuilder('muting')
.where('muting.expiresAt IS NOT NULL')
.andWhere('muting.expiresAt < :now', { now: new Date() })
.innerJoinAndSelect('muting.mutee', 'mutee')
.getMany();
if (expired.length > 0) {
await this.mutingsRepository.delete({
id: In(expired.map(m => m.id)),
});
for (const m of expired) {
this.globalEventService.publishUserEvent(m.muterId, 'unmute', m.mutee!);
}
}
2022-09-18 21:11:50 +03:00
this.logger.succ('All expired mutings checked.');
2022-09-17 21:27:08 +03:00
done();
}
}