2022-09-17 18:27:08 +00:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-02-16 14:09:41 +00:00
|
|
|
import { In } from 'typeorm';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { format as dateFormat } from 'date-fns';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { UserListJoiningsRepository, UserListsRepository, UsersRepository } from '@/models/index.js';
|
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 18:27:08 +00: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';
|
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
|
|
|
import type Bull from 'bull';
|
2023-04-12 00:13:58 +00:00
|
|
|
import type { DbJobDataWithUser } from '../types.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ExportUserListsProcessorService {
|
2022-09-18 18:11:50 +00:00
|
|
|
private logger: Logger;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userListsRepository)
|
|
|
|
private userListsRepository: UserListsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userListJoiningsRepository)
|
|
|
|
private userListJoiningsRepository: UserListJoiningsRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private driveService: DriveService,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('export-user-lists');
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-04-12 00:13:58 +00:00
|
|
|
public async process(job: Bull.Job<DbJobDataWithUser>, done: () => void): Promise<void> {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.info(`Exporting user lists of ${job.data.user.id} ...`);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
const user = await this.usersRepository.findOneBy({ id: job.data.user.id });
|
|
|
|
if (user == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const lists = await this.userListsRepository.findBy({
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create temp file
|
|
|
|
const [path, cleanup] = await createTemp();
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.info(`Temp file is ${path}`);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const stream = fs.createWriteStream(path, { flags: 'a' });
|
|
|
|
|
|
|
|
for (const list of lists) {
|
|
|
|
const joinings = await this.userListJoiningsRepository.findBy({ userListId: list.id });
|
|
|
|
const users = await this.usersRepository.findBy({
|
|
|
|
id: In(joinings.map(j => j.userId)),
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const u of users) {
|
|
|
|
const acct = this.utilityService.getFullApAccount(u.username, u.host);
|
|
|
|
const content = `${list.name},${acct}`;
|
|
|
|
await new Promise<void>((res, rej) => {
|
|
|
|
stream.write(content + '\n', err => {
|
|
|
|
if (err) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.error(err);
|
2022-09-17 18:27:08 +00:00
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.end();
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.succ(`Exported to: ${path}`);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
const fileName = 'user-lists-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.csv';
|
|
|
|
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true });
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.succ(`Exported to: ${driveFile.id}`);
|
2022-09-17 18:27:08 +00:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|