diff options
Diffstat (limited to 'src/queue/processors/db/export-notes.ts')
| -rw-r--r-- | src/queue/processors/db/export-notes.ts | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/src/queue/processors/db/export-notes.ts b/src/queue/processors/db/export-notes.ts index 8f3cdc5b99..92867ad82e 100644 --- a/src/queue/processors/db/export-notes.ts +++ b/src/queue/processors/db/export-notes.ts @@ -1,21 +1,22 @@ import * as Bull from 'bull'; import * as tmp from 'tmp'; import * as fs from 'fs'; -import * as mongo from 'mongodb'; import { queueLogger } from '../../logger'; -import Note, { INote } from '../../../models/note'; import addFile from '../../../services/drive/add-file'; -import User from '../../../models/user'; import dateFormat = require('dateformat'); +import { Users, Notes, Polls } from '../../../models'; +import { MoreThan } from 'typeorm'; +import { Note } from '../../../models/entities/note'; +import { Poll } from '../../../models/entities/poll'; const logger = queueLogger.createSubLogger('export-notes'); export async function exportNotes(job: Bull.Job, done: any): Promise<void> { - logger.info(`Exporting notes of ${job.data.user._id} ...`); + logger.info(`Exporting notes of ${job.data.user.id} ...`); - const user = await User.findOne({ - _id: new mongo.ObjectID(job.data.user._id.toString()) + const user = await Users.findOne({ + id: job.data.user.id }); // Create temp file @@ -46,13 +47,14 @@ export async function exportNotes(job: Bull.Job, done: any): Promise<void> { let cursor: any = null; while (!ended) { - const notes = await Note.find({ - userId: user._id, - ...(cursor ? { _id: { $gt: cursor } } : {}) - }, { - limit: 100, - sort: { - _id: 1 + const notes = await Notes.find({ + where: { + userId: user.id, + ...(cursor ? { id: MoreThan(cursor) } : {}) + }, + take: 100, + order: { + id: 1 } }); @@ -62,10 +64,14 @@ export async function exportNotes(job: Bull.Job, done: any): Promise<void> { break; } - cursor = notes[notes.length - 1]._id; + cursor = notes[notes.length - 1].id; for (const note of notes) { - const content = JSON.stringify(serialize(note)); + let poll: Poll; + if (note.hasPoll) { + poll = await Polls.findOne({ noteId: note.id }); + } + const content = JSON.stringify(serialize(note, poll)); await new Promise((res, rej) => { stream.write(exportedNotesCount === 0 ? content : ',\n' + content, err => { if (err) { @@ -79,8 +85,8 @@ export async function exportNotes(job: Bull.Job, done: any): Promise<void> { exportedNotesCount++; } - const total = await Note.count({ - userId: user._id, + const total = await Notes.count({ + userId: user.id, }); job.progress(exportedNotesCount / total); @@ -103,20 +109,20 @@ export async function exportNotes(job: Bull.Job, done: any): Promise<void> { const fileName = 'notes-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.json'; const driveFile = await addFile(user, path, fileName); - logger.succ(`Exported to: ${driveFile._id}`); + logger.succ(`Exported to: ${driveFile.id}`); cleanup(); done(); } -function serialize(note: INote): any { +function serialize(note: Note, poll: Poll): any { return { - id: note._id, + id: note.id, text: note.text, createdAt: note.createdAt, fileIds: note.fileIds, replyId: note.replyId, renoteId: note.renoteId, - poll: note.poll, + poll: poll, cw: note.cw, viaMobile: note.viaMobile, visibility: note.visibility, |