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';
|
2023-03-01 03:20:03 +02:00
|
|
|
import { In, LessThan } from 'typeorm';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-04-04 08:06:57 +03:00
|
|
|
import type { AntennasRepository, MutedNotesRepository, RoleAssignmentsRepository, UserIpsRepository } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type Logger from '@/logger.js';
|
2022-12-22 01:17:13 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-12-22 01:29:18 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-09-07 10:20:28 +03:00
|
|
|
import type { Config } from '@/config.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
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CleanProcessorService {
|
2022-09-18 21:11:50 +03:00
|
|
|
private logger: Logger;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
constructor(
|
2023-09-07 10:20:28 +03:00
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
@Inject(DI.userIpsRepository)
|
|
|
|
private userIpsRepository: UserIpsRepository,
|
|
|
|
|
2022-12-22 01:29:18 +02:00
|
|
|
@Inject(DI.mutedNotesRepository)
|
|
|
|
private mutedNotesRepository: MutedNotesRepository,
|
|
|
|
|
2023-03-20 13:12:38 +02:00
|
|
|
@Inject(DI.antennasRepository)
|
|
|
|
private antennasRepository: AntennasRepository,
|
|
|
|
|
2023-03-01 03:20:03 +02:00
|
|
|
@Inject(DI.roleAssignmentsRepository)
|
|
|
|
private roleAssignmentsRepository: RoleAssignmentsRepository,
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
private queueLoggerService: QueueLoggerService,
|
2022-12-22 01:29:18 +02:00
|
|
|
private idService: IdService,
|
2022-09-17 21:27:08 +03:00
|
|
|
) {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('clean');
|
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(): Promise<void> {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.info('Cleaning...');
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
this.userIpsRepository.delete({
|
2022-12-22 01:17:13 +02:00
|
|
|
createdAt: LessThan(new Date(Date.now() - (1000 * 60 * 60 * 24 * 90))),
|
|
|
|
});
|
|
|
|
|
2022-12-22 01:29:18 +02:00
|
|
|
this.mutedNotesRepository.delete({
|
|
|
|
id: LessThan(this.idService.genId(new Date(Date.now() - (1000 * 60 * 60 * 24 * 90)))),
|
|
|
|
reason: 'word',
|
|
|
|
});
|
|
|
|
|
2023-03-20 13:12:38 +02:00
|
|
|
this.mutedNotesRepository.delete({
|
2022-12-22 01:32:01 +02:00
|
|
|
id: LessThan(this.idService.genId(new Date(Date.now() - (1000 * 60 * 60 * 24 * 90)))),
|
2023-03-20 13:12:38 +02:00
|
|
|
reason: 'word',
|
|
|
|
});
|
|
|
|
|
2023-09-07 10:20:28 +03:00
|
|
|
// 使われてないアンテナを停止
|
|
|
|
if (this.config.deactivateAntennaThreshold > 0) {
|
|
|
|
this.antennasRepository.update({
|
|
|
|
lastUsedAt: LessThan(new Date(Date.now() - this.config.deactivateAntennaThreshold)),
|
|
|
|
}, {
|
|
|
|
isActive: false,
|
|
|
|
});
|
|
|
|
}
|
2022-12-22 01:32:01 +02:00
|
|
|
|
2023-03-01 03:20:03 +02:00
|
|
|
const expiredRoleAssignments = await this.roleAssignmentsRepository.createQueryBuilder('assign')
|
|
|
|
.where('assign.expiresAt IS NOT NULL')
|
|
|
|
.andWhere('assign.expiresAt < :now', { now: new Date() })
|
|
|
|
.getMany();
|
|
|
|
|
|
|
|
if (expiredRoleAssignments.length > 0) {
|
|
|
|
await this.roleAssignmentsRepository.delete({
|
|
|
|
id: In(expiredRoleAssignments.map(x => x.id)),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.succ('Cleaned.');
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
}
|