2019-03-07 15:07:21 +01:00
|
|
|
import * as Bull from 'bull';
|
2019-02-06 12:56:48 +01:00
|
|
|
import * as tmp from 'tmp';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
2021-08-19 14:55:45 +02:00
|
|
|
import { queueLogger } from '../../logger';
|
2022-01-19 18:43:13 +01:00
|
|
|
import { addFile } from '@/services/drive/add-file';
|
2022-02-03 13:09:15 +01:00
|
|
|
import dateFormat from 'dateformat';
|
2021-08-19 14:55:45 +02:00
|
|
|
import { getFullApAccount } from '@/misc/convert-host';
|
|
|
|
import { Users, Blockings } from '@/models/index';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { MoreThan } from 'typeorm';
|
2021-08-19 14:55:45 +02:00
|
|
|
import { DbUserJobData } from '@/queue/types';
|
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
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
const user = await Users.findOne(job.data.user.id);
|
|
|
|
if (user == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
2019-02-06 12:56:48 +01:00
|
|
|
|
|
|
|
// Create temp file
|
|
|
|
const [path, cleanup] = await new Promise<[string, any]>((res, rej) => {
|
|
|
|
tmp.file((e, path, fd, cleanup) => {
|
|
|
|
if (e) return rej(e);
|
|
|
|
res([path, cleanup]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.info(`Temp file is ${path}`);
|
|
|
|
|
|
|
|
const stream = fs.createWriteStream(path, { flags: 'a' });
|
|
|
|
|
|
|
|
let exportedCount = 0;
|
|
|
|
let cursor: any = null;
|
|
|
|
|
2019-04-14 10:18:17 +02:00
|
|
|
while (true) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const blockings = await Blockings.find({
|
|
|
|
where: {
|
|
|
|
blockerId: user.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {}),
|
2019-04-07 14:50:36 +02:00
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 1,
|
|
|
|
},
|
2019-02-06 12:56:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (blockings.length === 0) {
|
2019-03-07 15:07:21 +01:00
|
|
|
job.progress(100);
|
2019-02-06 12:56:48 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
cursor = blockings[blockings.length - 1].id;
|
2019-02-06 12:56:48 +01:00
|
|
|
|
|
|
|
for (const block of blockings) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const u = await Users.findOne({ id: block.blockeeId });
|
2019-04-12 18:43:22 +02:00
|
|
|
if (u == null) {
|
|
|
|
exportedCount++; continue;
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:38:11 +01:00
|
|
|
const content = getFullApAccount(u.username, u.host);
|
2021-05-08 11:56:21 +02:00
|
|
|
await new Promise<void>((res, rej) => {
|
2019-02-06 12:56:48 +01:00
|
|
|
stream.write(content + '\n', err => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
exportedCount++;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const total = await Blockings.count({
|
|
|
|
blockerId: user.id,
|
2019-02-06 12:56:48 +01:00
|
|
|
});
|
|
|
|
|
2019-03-07 15:07:21 +01:00
|
|
|
job.progress(exportedCount / total);
|
2019-02-06 12:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stream.end();
|
|
|
|
logger.succ(`Exported to: ${path}`);
|
|
|
|
|
2019-02-06 13:10:12 +01:00
|
|
|
const fileName = 'blocking-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv';
|
2022-01-23 14:52:35 +01:00
|
|
|
const driveFile = await addFile({ user, path, name: fileName, force: true });
|
2019-02-06 12:56:48 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
logger.succ(`Exported to: ${driveFile.id}`);
|
2019-02-06 12:56:48 +01:00
|
|
|
cleanup();
|
|
|
|
done();
|
|
|
|
}
|