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';
|
|
|
|
|
2019-03-07 23:07:21 +09:00
|
|
|
import { queueLogger } from '../../logger';
|
|
|
|
import addFile from '../../../services/drive/add-file';
|
2019-02-06 20:56:48 +09:00
|
|
|
import dateFormat = require('dateformat');
|
2019-03-12 23:38:11 +09:00
|
|
|
import { getFullApAccount } from '../../../misc/convert-host';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { Users, Mutings } from '../../../models';
|
|
|
|
import { MoreThan } from 'typeorm';
|
2019-02-06 20:56:48 +09:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('export-mute');
|
|
|
|
|
2019-03-07 23:07:21 +09:00
|
|
|
export async function exportMute(job: Bull.Job, 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);
|
2019-02-06 20:56:48 +09:00
|
|
|
await new Promise((res, rej) => {
|
|
|
|
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();
|
|
|
|
}
|