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 * as fs from 'node:fs';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { IsNull, MoreThan } from 'typeorm';
|
|
|
|
import { format as dateFormat } from 'date-fns';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-08-05 04:33:00 +03:00
|
|
|
import type { MutingsRepository, UsersRepository, Muting } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
|
|
|
import { createTemp } from '@/misc/create-temp.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2023-05-29 05:54:49 +03:00
|
|
|
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';
|
2023-04-12 03:13:58 +03:00
|
|
|
import type { DbJobDataWithUser } from '../types.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ExportMutingProcessorService {
|
2022-09-18 21:11:50 +03:00
|
|
|
private logger: Logger;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.mutingsRepository)
|
|
|
|
private mutingsRepository: MutingsRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private driveService: DriveService,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('export-muting');
|
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<DbJobDataWithUser>): Promise<void> {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.info(`Exporting muting of ${job.data.user.id} ...`);
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
const user = await this.usersRepository.findOneBy({ id: job.data.user.id });
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create temp file
|
|
|
|
const [path, cleanup] = await createTemp();
|
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.info(`Temp file is ${path}`);
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
const stream = fs.createWriteStream(path, { flags: 'a' });
|
|
|
|
|
|
|
|
let exportedCount = 0;
|
2023-07-14 04:45:01 +03:00
|
|
|
let cursor: Muting['id'] | null = null;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const mutes = await this.mutingsRepository.find({
|
|
|
|
where: {
|
|
|
|
muterId: user.id,
|
|
|
|
expiresAt: IsNull(),
|
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {}),
|
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (mutes.length === 0) {
|
2023-05-29 05:54:49 +03:00
|
|
|
job.updateProgress(100);
|
2022-09-17 21:27:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-14 04:45:01 +03:00
|
|
|
cursor = mutes.at(-1)?.id ?? null;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
for (const mute of mutes) {
|
|
|
|
const u = await this.usersRepository.findOneBy({ id: mute.muteeId });
|
|
|
|
if (u == null) {
|
|
|
|
exportedCount++; continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = this.utilityService.getFullApAccount(u.username, u.host);
|
|
|
|
await new Promise<void>((res, rej) => {
|
|
|
|
stream.write(content + '\n', err => {
|
|
|
|
if (err) {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.error(err);
|
2022-09-17 21:27:08 +03:00
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
exportedCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const total = await this.mutingsRepository.countBy({
|
|
|
|
muterId: user.id,
|
|
|
|
});
|
|
|
|
|
2023-05-29 05:54:49 +03:00
|
|
|
job.updateProgress(exportedCount / total);
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stream.end();
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.succ(`Exported to: ${path}`);
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
const fileName = 'mute-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.csv';
|
2023-04-14 08:35:38 +03:00
|
|
|
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true, ext: 'csv' });
|
2022-09-17 21:27:08 +03:00
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.succ(`Exported to: ${driveFile.id}`);
|
2022-09-17 21:27:08 +03:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|