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';
|
|
|
|
|
2021-08-19 14:55:45 +02:00
|
|
|
import { queueLogger } from '../../logger';
|
2022-01-19 18:43:13 +01:00
|
|
|
import { addFile } from '@/services/drive/add-file';
|
2022-02-03 17:56:33 +01:00
|
|
|
import { format as dateFormat } from 'date-fns';
|
2021-08-19 14:55:45 +02:00
|
|
|
import { Users, Notes, Polls } from '@/models/index';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { MoreThan } from 'typeorm';
|
2021-08-19 14:55:45 +02:00
|
|
|
import { Note } from '@/models/entities/note';
|
|
|
|
import { Poll } from '@/models/entities/poll';
|
|
|
|
import { DbUserJobData } from '@/queue/types';
|
2019-02-05 11:50:14 +01:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('export-notes');
|
|
|
|
|
2021-05-08 11:56:21 +02:00
|
|
|
export async function exportNotes(job: Bull.Job<DbUserJobData>, done: any): Promise<void> {
|
2019-04-07 14:50:36 +02:00
|
|
|
logger.info(`Exporting notes of ${job.data.user.id} ...`);
|
2019-02-05 11:50:14 +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-05 11:50:14 +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' });
|
|
|
|
|
2021-12-11 22:21:48 +01:00
|
|
|
const write = (text: string): Promise<void> => {
|
|
|
|
return new Promise<void>((res, rej) => {
|
|
|
|
stream.write(text, err => {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
rej(err);
|
|
|
|
} else {
|
|
|
|
res();
|
|
|
|
}
|
|
|
|
});
|
2019-02-05 11:50:14 +01:00
|
|
|
});
|
2021-12-11 22:21:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
await write('[');
|
2019-02-05 11:50:14 +01:00
|
|
|
|
|
|
|
let exportedNotesCount = 0;
|
2021-11-12 07:08:36 +01:00
|
|
|
let cursor: Note['id'] | null = null;
|
2019-02-05 11:50:14 +01:00
|
|
|
|
2019-04-14 10:18:17 +02:00
|
|
|
while (true) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const notes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
2021-11-12 07:08:36 +01:00
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {}),
|
2019-04-07 14:50:36 +02:00
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
2021-11-12 07:08:36 +01:00
|
|
|
id: 1,
|
|
|
|
},
|
2019-02-05 11:50:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (notes.length === 0) {
|
2019-03-07 15:07:21 +01:00
|
|
|
job.progress(100);
|
2019-02-05 11:50:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
cursor = notes[notes.length - 1].id;
|
2019-02-05 11:50:14 +01:00
|
|
|
|
|
|
|
for (const note of notes) {
|
2019-04-12 18:43:22 +02:00
|
|
|
let poll: Poll | undefined;
|
2019-04-07 14:50:36 +02:00
|
|
|
if (note.hasPoll) {
|
2021-02-13 07:33:38 +01:00
|
|
|
poll = await Polls.findOneOrFail({ noteId: note.id });
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
const content = JSON.stringify(serialize(note, poll));
|
2021-12-11 22:21:48 +01:00
|
|
|
const isFirst = exportedNotesCount === 0;
|
|
|
|
await write(isFirst ? content : ',\n' + content);
|
2019-02-05 11:50:14 +01:00
|
|
|
exportedNotesCount++;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const total = await Notes.count({
|
|
|
|
userId: user.id,
|
2019-02-05 11:50:14 +01:00
|
|
|
});
|
|
|
|
|
2019-03-07 15:07:21 +01:00
|
|
|
job.progress(exportedNotesCount / total);
|
2019-02-05 11:50:14 +01:00
|
|
|
}
|
|
|
|
|
2021-12-11 22:21:48 +01:00
|
|
|
await write(']');
|
2019-02-05 11:50:14 +01:00
|
|
|
|
|
|
|
stream.end();
|
|
|
|
logger.succ(`Exported to: ${path}`);
|
|
|
|
|
2022-02-03 17:56:33 +01:00
|
|
|
const fileName = 'notes-' + dateFormat(new Date(), 'yyyy-MM-dd-HH-mm-ss') + '.json';
|
2022-01-23 14:52:35 +01:00
|
|
|
const driveFile = await addFile({ user, path, name: fileName, force: true });
|
2019-02-05 11:50:14 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
logger.succ(`Exported to: ${driveFile.id}`);
|
2019-02-05 11:50:14 +01:00
|
|
|
cleanup();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2021-11-12 07:08:36 +01:00
|
|
|
function serialize(note: Note, poll: Poll | null = null): Record<string, unknown> {
|
2019-02-05 11:50:14 +01:00
|
|
|
return {
|
2019-04-07 14:50:36 +02:00
|
|
|
id: note.id,
|
2019-02-05 11:50:14 +01:00
|
|
|
text: note.text,
|
|
|
|
createdAt: note.createdAt,
|
|
|
|
fileIds: note.fileIds,
|
|
|
|
replyId: note.replyId,
|
|
|
|
renoteId: note.renoteId,
|
2019-04-07 14:50:36 +02:00
|
|
|
poll: poll,
|
2019-02-05 11:50:14 +01:00
|
|
|
cw: note.cw,
|
|
|
|
visibility: note.visibility,
|
|
|
|
visibleUserIds: note.visibleUserIds,
|
2021-11-12 07:08:36 +01:00
|
|
|
localOnly: note.localOnly,
|
2019-02-05 11:50:14 +01:00
|
|
|
};
|
|
|
|
}
|