2019-03-07 16:07:21 +02:00
|
|
|
import * as Bull from 'bull';
|
2019-02-05 12:50:14 +02:00
|
|
|
import * as tmp from 'tmp';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
2019-03-07 16:07:21 +02:00
|
|
|
import { queueLogger } from '../../logger';
|
|
|
|
import addFile from '../../../services/drive/add-file';
|
2019-02-05 12:50:14 +02:00
|
|
|
import dateFormat = require('dateformat');
|
2019-04-07 15:50:36 +03:00
|
|
|
import { Users, Notes, Polls } from '../../../models';
|
|
|
|
import { MoreThan } from 'typeorm';
|
|
|
|
import { Note } from '../../../models/entities/note';
|
|
|
|
import { Poll } from '../../../models/entities/poll';
|
2019-02-05 12:50:14 +02:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('export-notes');
|
|
|
|
|
2019-03-07 16:07:21 +02:00
|
|
|
export async function exportNotes(job: Bull.Job, done: any): Promise<void> {
|
2019-04-07 15:50:36 +03:00
|
|
|
logger.info(`Exporting notes of ${job.data.user.id} ...`);
|
2019-02-05 12:50:14 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
const user = await Users.findOne({
|
|
|
|
id: job.data.user.id
|
2019-02-05 12:50:14 +02: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' });
|
|
|
|
|
|
|
|
await new Promise((res, rej) => {
|
|
|
|
stream.write('[', err => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let exportedNotesCount = 0;
|
|
|
|
let ended = false;
|
|
|
|
let cursor: any = null;
|
|
|
|
|
|
|
|
while (!ended) {
|
2019-04-07 15:50:36 +03:00
|
|
|
const notes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {})
|
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
|
|
|
id: 1
|
2019-02-05 12:50:14 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (notes.length === 0) {
|
|
|
|
ended = true;
|
2019-03-07 16:07:21 +02:00
|
|
|
job.progress(100);
|
2019-02-05 12:50:14 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
cursor = notes[notes.length - 1].id;
|
2019-02-05 12:50:14 +02:00
|
|
|
|
|
|
|
for (const note of notes) {
|
2019-04-07 15:50:36 +03:00
|
|
|
let poll: Poll;
|
|
|
|
if (note.hasPoll) {
|
|
|
|
poll = await Polls.findOne({ noteId: note.id });
|
|
|
|
}
|
|
|
|
const content = JSON.stringify(serialize(note, poll));
|
2019-02-05 12:50:14 +02:00
|
|
|
await new Promise((res, rej) => {
|
|
|
|
stream.write(exportedNotesCount === 0 ? content : ',\n' + content, err => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
exportedNotesCount++;
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
const total = await Notes.count({
|
|
|
|
userId: user.id,
|
2019-02-05 12:50:14 +02:00
|
|
|
});
|
|
|
|
|
2019-03-07 16:07:21 +02:00
|
|
|
job.progress(exportedNotesCount / total);
|
2019-02-05 12:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await new Promise((res, rej) => {
|
|
|
|
stream.write(']', err => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.end();
|
|
|
|
logger.succ(`Exported to: ${path}`);
|
|
|
|
|
2019-02-06 14:10:37 +02:00
|
|
|
const fileName = 'notes-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.json';
|
2019-02-05 12:50:14 +02:00
|
|
|
const driveFile = await addFile(user, path, fileName);
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
logger.succ(`Exported to: ${driveFile.id}`);
|
2019-02-05 12:50:14 +02:00
|
|
|
cleanup();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
function serialize(note: Note, poll: Poll): any {
|
2019-02-05 12:50:14 +02:00
|
|
|
return {
|
2019-04-07 15:50:36 +03:00
|
|
|
id: note.id,
|
2019-02-05 12:50:14 +02:00
|
|
|
text: note.text,
|
|
|
|
createdAt: note.createdAt,
|
|
|
|
fileIds: note.fileIds,
|
|
|
|
replyId: note.replyId,
|
|
|
|
renoteId: note.renoteId,
|
2019-04-07 15:50:36 +03:00
|
|
|
poll: poll,
|
2019-02-05 12:50:14 +02:00
|
|
|
cw: note.cw,
|
|
|
|
viaMobile: note.viaMobile,
|
|
|
|
visibility: note.visibility,
|
|
|
|
visibleUserIds: note.visibleUserIds,
|
|
|
|
appId: note.appId,
|
|
|
|
geo: note.geo,
|
|
|
|
localOnly: note.localOnly
|
|
|
|
};
|
|
|
|
}
|