2019-03-07 15:07:21 +01:00
|
|
|
import * as Bull from 'bull';
|
2019-02-05 11:50:14 +01:00
|
|
|
import * as tmp from 'tmp';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
|
2019-03-07 15:07:21 +01:00
|
|
|
import { queueLogger } from '../../logger';
|
|
|
|
import Note, { INote } from '../../../models/note';
|
|
|
|
import addFile from '../../../services/drive/add-file';
|
|
|
|
import User from '../../../models/user';
|
2019-02-05 11:50:14 +01:00
|
|
|
import dateFormat = require('dateformat');
|
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('export-notes');
|
|
|
|
|
2019-03-07 15:07:21 +01:00
|
|
|
export async function exportNotes(job: Bull.Job, done: any): Promise<void> {
|
2019-02-05 11:50:14 +01:00
|
|
|
logger.info(`Exporting notes of ${job.data.user._id} ...`);
|
|
|
|
|
|
|
|
const user = await User.findOne({
|
|
|
|
_id: new mongo.ObjectID(job.data.user._id.toString())
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
const notes = await Note.find({
|
|
|
|
userId: user._id,
|
|
|
|
...(cursor ? { _id: { $gt: cursor } } : {})
|
|
|
|
}, {
|
|
|
|
limit: 100,
|
|
|
|
sort: {
|
|
|
|
_id: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (notes.length === 0) {
|
|
|
|
ended = true;
|
2019-03-07 15:07:21 +01:00
|
|
|
job.progress(100);
|
2019-02-05 11:50:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = notes[notes.length - 1]._id;
|
|
|
|
|
|
|
|
for (const note of notes) {
|
|
|
|
const content = JSON.stringify(serialize(note));
|
|
|
|
await new Promise((res, rej) => {
|
|
|
|
stream.write(exportedNotesCount === 0 ? content : ',\n' + content, err => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
exportedNotesCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const total = await Note.count({
|
|
|
|
userId: user._id,
|
|
|
|
});
|
|
|
|
|
2019-03-07 15:07:21 +01:00
|
|
|
job.progress(exportedNotesCount / total);
|
2019-02-05 11:50:14 +01: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 13:10:37 +01:00
|
|
|
const fileName = 'notes-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.json';
|
2019-02-05 11:50:14 +01:00
|
|
|
const driveFile = await addFile(user, path, fileName);
|
|
|
|
|
|
|
|
logger.succ(`Exported to: ${driveFile._id}`);
|
|
|
|
cleanup();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
function serialize(note: INote): any {
|
|
|
|
return {
|
|
|
|
id: note._id,
|
|
|
|
text: note.text,
|
|
|
|
createdAt: note.createdAt,
|
|
|
|
fileIds: note.fileIds,
|
|
|
|
replyId: note.replyId,
|
|
|
|
renoteId: note.renoteId,
|
|
|
|
poll: note.poll,
|
|
|
|
cw: note.cw,
|
|
|
|
viaMobile: note.viaMobile,
|
|
|
|
visibility: note.visibility,
|
|
|
|
visibleUserIds: note.visibleUserIds,
|
|
|
|
appId: note.appId,
|
|
|
|
geo: note.geo,
|
|
|
|
localOnly: note.localOnly
|
|
|
|
};
|
|
|
|
}
|