期限切れミュートを掃除する機能を実装

This commit is contained in:
samunohito 2024-06-12 06:53:26 +09:00
parent fa8d905484
commit ae485ed568
2 changed files with 47 additions and 4 deletions

View file

@ -5,8 +5,9 @@
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import Redis from 'ioredis'; import Redis from 'ioredis';
import { In } from 'typeorm';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import type { ChannelMutingRepository, ChannelsRepository, MiChannel, MiUser } from '@/models/_.js'; import type { ChannelMutingRepository, ChannelsRepository, MiChannel, MiChannelMuting, MiUser } from '@/models/_.js';
import { IdService } from '@/core/IdService.js'; import { IdService } from '@/core/IdService.js';
import { GlobalEvents, GlobalEventService } from '@/core/GlobalEventService.js'; import { GlobalEvents, GlobalEventService } from '@/core/GlobalEventService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
@ -64,7 +65,7 @@ export class ChannelMutingService {
.where('channel_muting.userId = :userId', { userId: params.requestUserId }) .where('channel_muting.userId = :userId', { userId: params.requestUserId })
.andWhere(qb => { .andWhere(qb => {
qb.where('channel_muting.expiresAt IS NULL') qb.where('channel_muting.expiresAt IS NULL')
.orWhere('channel_muting.expiresAt > :now:', { now: new Date() }); .orWhere('channel_muting.expiresAt > :now', { now: new Date() });
}); });
if (opts?.joinUser) { if (opts?.joinUser) {
@ -78,6 +79,32 @@ export class ChannelMutingService {
return q.getMany(); return q.getMany();
} }
/**
* .
*
* @param [opts]
* @param {(boolean|undefined)} [opts.joinUser=undefined] JOINするかどうか(falseまたは省略時はJOINしない).
* @param {(boolean|undefined)} [opts.joinChannel=undefined] JOINするかどうか(falseまたは省略時はJOINしない).
*/
public async findExpiredMutings(opts?: {
joinUser?: boolean;
joinChannel?: boolean;
}): Promise<MiChannelMuting[]> {
const now = new Date();
const q = this.channelMutingRepository.createQueryBuilder('channel_muting')
.where('channel_muting.expiresAt < :now', { now });
if (opts?.joinUser) {
q.innerJoinAndSelect('channel_muting.user', 'user');
}
if (opts?.joinChannel) {
q.leftJoinAndSelect('channel_muting.channel', 'channel');
}
return q.getMany();
}
/** /**
* . * .
* @param params * @param params
@ -136,6 +163,20 @@ export class ChannelMutingService {
}); });
} }
/**
* .
*/
@bindThis
public async eraseExpiredMutings(): Promise<void> {
const expiredMutings = await this.findExpiredMutings();
await this.channelMutingRepository.delete({ id: In(expiredMutings.map(x => x.id)) });
const userIds = [...new Set(expiredMutings.map(x => x.userId))];
for (const userId of userIds) {
this.userMutingChannelsCache.refresh(userId).then();
}
}
@bindThis @bindThis
private async onMessage(_: string, data: string): Promise<void> { private async onMessage(_: string, data: string): Promise<void> {
const obj = JSON.parse(data); const obj = JSON.parse(data);

View file

@ -4,14 +4,13 @@
*/ */
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { In } from 'typeorm';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import type { MutingsRepository } from '@/models/_.js'; import type { MutingsRepository } from '@/models/_.js';
import type Logger from '@/logger.js'; import type Logger from '@/logger.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { UserMutingService } from '@/core/UserMutingService.js'; import { UserMutingService } from '@/core/UserMutingService.js';
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
import { QueueLoggerService } from '../QueueLoggerService.js'; import { QueueLoggerService } from '../QueueLoggerService.js';
import type * as Bull from 'bullmq';
@Injectable() @Injectable()
export class CheckExpiredMutingsProcessorService { export class CheckExpiredMutingsProcessorService {
@ -22,6 +21,7 @@ export class CheckExpiredMutingsProcessorService {
private mutingsRepository: MutingsRepository, private mutingsRepository: MutingsRepository,
private userMutingService: UserMutingService, private userMutingService: UserMutingService,
private channelMutingService: ChannelMutingService,
private queueLoggerService: QueueLoggerService, private queueLoggerService: QueueLoggerService,
) { ) {
this.logger = this.queueLoggerService.logger.createSubLogger('check-expired-mutings'); this.logger = this.queueLoggerService.logger.createSubLogger('check-expired-mutings');
@ -41,6 +41,8 @@ export class CheckExpiredMutingsProcessorService {
await this.userMutingService.unmute(expired); await this.userMutingService.unmute(expired);
} }
await this.channelMutingService.eraseExpiredMutings();
this.logger.succ('All expired mutings checked.'); this.logger.succ('All expired mutings checked.');
} }
} }