2019-03-07 23:07:21 +09:00
|
|
|
import * as Bull from 'bull';
|
2019-02-06 20:56:48 +09:00
|
|
|
import * as tmp from 'tmp';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
2021-08-19 21:55:45 +09:00
|
|
|
import { queueLogger } from '../../logger';
|
|
|
|
import addFile from '@/services/drive/add-file';
|
2021-08-19 18:33:41 +09:00
|
|
|
import dateFormat from 'dateformat';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { getFullApAccount } from '@/misc/convert-host';
|
|
|
|
import { Users, Mutings } from '@/models/index';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { MoreThan } from 'typeorm';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { DbUserJobData } from '@/queue/types';
|
2019-02-06 20:56:48 +09:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('export-mute');
|
|
|
|
|
2021-05-08 18:56:21 +09:00
|
|
|
export async function exportMute(job: Bull.Job<DbUserJobData>, done: any): Promise<void> {
|
2019-04-07 21:50:36 +09:00
|
|
|
logger.info(`Exporting mute of ${job.data.user.id} ...`);
|
2019-02-06 20:56:48 +09:00
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
const user = await Users.findOne(job.data.user.id);
|
|
|
|
if (user == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
2019-02-06 20:56:48 +09: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 17:18:17 +09:00
|
|
|
while (true) {
|
2019-04-07 21:50:36 +09:00
|
|
|
const mutes = await Mutings.find({
|
|
|
|
where: {
|
|
|
|
muterId: user.id,
|
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {})
|
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
|
|
|
id: 1
|
2019-02-06 20:56:48 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (mutes.length === 0) {
|
2019-03-07 23:07:21 +09:00
|
|
|
job.progress(100);
|
2019-02-06 20:56:48 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
cursor = mutes[mutes.length - 1].id;
|
2019-02-06 20:56:48 +09:00
|
|
|
|
|
|
|
for (const mute of mutes) {
|
2019-04-07 21:50:36 +09:00
|
|
|
const u = await Users.findOne({ id: mute.muteeId });
|
2019-04-13 01:43:22 +09:00
|
|
|
if (u == null) {
|
|
|
|
exportedCount++; continue;
|
|
|
|
}
|
|
|
|
|
2019-03-12 23:38:11 +09:00
|
|
|
const content = getFullApAccount(u.username, u.host);
|
2021-05-08 18:56:21 +09:00
|
|
|
await new Promise<void>((res, rej) => {
|
2019-02-06 20:56:48 +09:00
|
|
|
stream.write(content + '\n', err => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
exportedCount++;
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const total = await Mutings.count({
|
|
|
|
muterId: user.id,
|
2019-02-06 20:56:48 +09:00
|
|
|
});
|
|
|
|
|
2019-03-07 23:07:21 +09:00
|
|
|
job.progress(exportedCount / total);
|
2019-02-06 20:56:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
stream.end();
|
|
|
|
logger.succ(`Exported to: ${path}`);
|
|
|
|
|
2019-02-06 21:10:12 +09:00
|
|
|
const fileName = 'mute-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv';
|
2019-05-03 18:33:25 +09:00
|
|
|
const driveFile = await addFile(user, path, fileName, null, null, true);
|
2019-02-06 20:56:48 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
logger.succ(`Exported to: ${driveFile.id}`);
|
2019-02-06 20:56:48 +09:00
|
|
|
cleanup();
|
|
|
|
done();
|
|
|
|
}
|