2023-01-13 05:40:33 +01:00
|
|
|
import type Bull from "bull";
|
|
|
|
import * as fs from "node:fs";
|
|
|
|
|
|
|
|
import { queueLogger } from "../../logger.js";
|
|
|
|
import { addFile } from "@/services/drive/add-file.js";
|
|
|
|
import { format as dateFormat } from "date-fns";
|
|
|
|
import { getFullApAccount } from "@/misc/convert-host.js";
|
|
|
|
import { createTemp } from "@/misc/create-temp.js";
|
|
|
|
import { Users, Blockings } from "@/models/index.js";
|
|
|
|
import { MoreThan } from "typeorm";
|
|
|
|
import type { DbUserJobData } from "@/queue/types.js";
|
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger("export-blocking");
|
|
|
|
|
|
|
|
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 {
|
2023-01-13 05:40:33 +01:00
|
|
|
const stream = fs.createWriteStream(path, { flags: "a" });
|
2022-05-25 09:50:22 +02:00
|
|
|
|
|
|
|
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) {
|
2023-01-13 05:40:33 +01:00
|
|
|
exportedCount++;
|
|
|
|
continue;
|
2022-05-25 09:50:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const content = getFullApAccount(u.username, u.host);
|
|
|
|
await new Promise<void>((res, rej) => {
|
2023-01-13 05:40:33 +01:00
|
|
|
stream.write(content + "\n", (err) => {
|
2022-05-25 09:50:22 +02:00
|
|
|
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
|
|
|
|
2023-01-16 20:19:20 +01:00
|
|
|
const fileName = `blocking-${dateFormat(
|
|
|
|
new Date(),
|
|
|
|
"yyyy-MM-dd-HH-mm-ss",
|
|
|
|
)}.csv`;
|
2023-01-13 05:40:33 +01:00
|
|
|
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();
|
|
|
|
}
|