2022-02-27 03:07:39 +01:00
|
|
|
import Bull from 'bull';
|
|
|
|
import * as fs from 'node:fs';
|
2019-02-06 12:56:48 +01:00
|
|
|
|
2022-02-27 03:07:39 +01:00
|
|
|
import { queueLogger } from '../../logger.js';
|
|
|
|
import { addFile } from '@/services/drive/add-file.js';
|
2022-02-03 17:56:33 +01:00
|
|
|
import { format as dateFormat } from 'date-fns';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { getFullApAccount } from '@/misc/convert-host.js';
|
2022-05-25 09:50:22 +02:00
|
|
|
import { createTemp } from '@/misc/create-temp.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { Users, Blockings } from '@/models/index.js';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { MoreThan } from 'typeorm';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { DbUserJobData } from '@/queue/types.js';
|
2019-02-06 12:56:48 +01:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('export-blocking');
|
|
|
|
|
2021-05-08 11:56:21 +02:00
|
|
|
export async function exportBlocking(job: Bull.Job<DbUserJobData>, done: any): Promise<void> {
|
2019-04-07 14:50:36 +02:00
|
|
|
logger.info(`Exporting blocking of ${job.data.user.id} ...`);
|
2019-02-06 12:56:48 +01:00
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const user = await Users.findOneBy({ id: job.data.user.id });
|
2019-04-12 18:43:22 +02:00
|
|
|
if (user == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
2019-02-06 12:56:48 +01:00
|
|
|
|
|
|
|
// Create temp file
|
2022-05-25 09:50:22 +02:00
|
|
|
const [path, cleanup] = await createTemp();
|
2019-02-06 12:56:48 +01:00
|
|
|
|
|
|
|
logger.info(`Temp file is ${path}`);
|
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
try {
|
|
|
|
const stream = fs.createWriteStream(path, { flags: 'a' });
|
|
|
|
|
|
|
|
let exportedCount = 0;
|
|
|
|
let cursor: any = null;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const blockings = await Blockings.find({
|
|
|
|
where: {
|
|
|
|
blockerId: user.id,
|
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {}),
|
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
});
|
2019-02-06 12:56:48 +01:00
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
if (blockings.length === 0) {
|
|
|
|
job.progress(100);
|
|
|
|
break;
|
2019-04-12 18:43:22 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
cursor = blockings[blockings.length - 1].id;
|
|
|
|
|
|
|
|
for (const block of blockings) {
|
|
|
|
const u = await Users.findOneBy({ id: block.blockeeId });
|
|
|
|
if (u == null) {
|
|
|
|
exportedCount++; continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = getFullApAccount(u.username, u.host);
|
|
|
|
await new Promise<void>((res, rej) => {
|
|
|
|
stream.write(content + '\n', err => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
2019-02-06 12:56:48 +01:00
|
|
|
});
|
2022-05-25 09:50:22 +02:00
|
|
|
exportedCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const total = await Blockings.countBy({
|
|
|
|
blockerId: user.id,
|
2019-02-06 12:56:48 +01:00
|
|
|
});
|
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
job.progress(exportedCount / total);
|
|
|
|
}
|
2019-02-06 12:56:48 +01:00
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
stream.end();
|
|
|
|
logger.succ(`Exported to: ${path}`);
|
2019-02-06 12:56:48 +01:00
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
const fileName = 'blocking-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.csv';
|
|
|
|
const driveFile = await addFile({ user, path, name: fileName, force: true });
|
2019-02-06 12:56:48 +01:00
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
logger.succ(`Exported to: ${driveFile.id}`);
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
2019-02-06 12:56:48 +01:00
|
|
|
|
|
|
|
done();
|
|
|
|
}
|