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';
|
|
|
|
|
2019-03-07 15:07:21 +01:00
|
|
|
import { queueLogger } from '../../logger';
|
|
|
|
import addFile from '../../../services/drive/add-file';
|
2019-02-06 12:56:48 +01:00
|
|
|
import dateFormat = require('dateformat');
|
2019-03-12 15:38:11 +01:00
|
|
|
import { getFullApAccount } from '../../../misc/convert-host';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Users, Mutings } from '../../../models';
|
|
|
|
import { MoreThan } from 'typeorm';
|
2019-02-06 12:56:48 +01:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('export-mute');
|
|
|
|
|
2019-03-07 15:07:21 +01:00
|
|
|
export async function exportMute(job: Bull.Job, done: any): Promise<void> {
|
2019-04-07 14:50:36 +02:00
|
|
|
logger.info(`Exporting mute 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 mutes = await Mutings.find({
|
|
|
|
where: {
|
|
|
|
muterId: user.id,
|
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {})
|
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
|
|
|
id: 1
|
2019-02-06 12:56:48 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (mutes.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 = mutes[mutes.length - 1].id;
|
2019-02-06 12:56:48 +01:00
|
|
|
|
|
|
|
for (const mute of mutes) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const u = await Users.findOne({ id: mute.muteeId });
|
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);
|
2019-02-06 12:56:48 +01:00
|
|
|
await new Promise((res, rej) => {
|
|
|
|
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 Mutings.count({
|
|
|
|
muterId: 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 = 'mute-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv';
|
2019-05-03 11:33:25 +02:00
|
|
|
const driveFile = await addFile(user, path, fileName, null, null, 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();
|
|
|
|
}
|