2022-09-17 20:27:08 +02:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { In, MoreThan, Not } from 'typeorm';
|
|
|
|
import { format as dateFormat } from 'date-fns';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-22 23:21:31 +02:00
|
|
|
import type { UsersRepository, FollowingsRepository, MutingsRepository } from '@/models/index.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
|
|
|
import { createTemp } from '@/misc/create-temp.js';
|
|
|
|
import type { Following } from '@/models/entities/Following.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2023-05-29 04:54:49 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
2023-05-29 04:54:49 +02:00
|
|
|
import type * as Bull from 'bullmq';
|
2023-04-12 02:13:58 +02:00
|
|
|
import type { DbExportFollowingData } from '../types.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ExportFollowingProcessorService {
|
2022-09-18 20:11:50 +02:00
|
|
|
private logger: Logger;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
private followingsRepository: FollowingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.mutingsRepository)
|
|
|
|
private mutingsRepository: MutingsRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private driveService: DriveService,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
2022-09-18 20:11:50 +02:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('export-following');
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-05-29 04:54:49 +02:00
|
|
|
public async process(job: Bull.Job<DbExportFollowingData>): Promise<void> {
|
2022-09-18 20:11:50 +02:00
|
|
|
this.logger.info(`Exporting following of ${job.data.user.id} ...`);
|
2022-09-17 20:27:08 +02: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 20:11:50 +02:00
|
|
|
this.logger.info(`Temp file is ${path}`);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const stream = fs.createWriteStream(path, { flags: 'a' });
|
|
|
|
|
|
|
|
let cursor: Following['id'] | null = null;
|
|
|
|
|
|
|
|
const mutings = job.data.excludeMuting ? await this.mutingsRepository.findBy({
|
|
|
|
muterId: user.id,
|
|
|
|
}) : [];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const followings = await this.followingsRepository.find({
|
|
|
|
where: {
|
|
|
|
followerId: user.id,
|
|
|
|
...(mutings.length > 0 ? { followeeId: Not(In(mutings.map(x => x.muteeId))) } : {}),
|
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {}),
|
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
}) as Following[];
|
|
|
|
|
|
|
|
if (followings.length === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-14 03:45:01 +02:00
|
|
|
cursor = followings.at(-1)?.id ?? null;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
for (const following of followings) {
|
|
|
|
const u = await this.usersRepository.findOneBy({ id: following.followeeId });
|
|
|
|
if (u == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (job.data.excludeInactive && u.updatedAt && (Date.now() - u.updatedAt.getTime() > 1000 * 60 * 60 * 24 * 90)) {
|
|
|
|
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 20:11:50 +02:00
|
|
|
this.logger.error(err);
|
2022-09-17 20:27:08 +02:00
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.end();
|
2022-09-18 20:11:50 +02:00
|
|
|
this.logger.succ(`Exported to: ${path}`);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
const fileName = 'following-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.csv';
|
2023-04-14 07:35:38 +02:00
|
|
|
const driveFile = await this.driveService.addFile({ user, path, name: fileName, force: true, ext: 'csv' });
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
this.logger.succ(`Exported to: ${driveFile.id}`);
|
2022-09-17 20:27:08 +02:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|