From ecc7ca1aa6d266d4955a5fb6d2cc242f3606a462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E5=91=A8=E9=83=A8=E8=90=BD?= Date: Thu, 25 Apr 2024 21:33:10 +0800 Subject: [PATCH] fix: questionable if statements in note import --- .../backend/src/misc/process-masto-notes.ts | 6 +++++- .../queue/processors/db/import-firefish-post.ts | 17 +++++++++++++---- .../queue/processors/db/import-masto-post.ts | 17 +++++++++++++---- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/misc/process-masto-notes.ts b/packages/backend/src/misc/process-masto-notes.ts index 03968944ec..01c438f97d 100644 --- a/packages/backend/src/misc/process-masto-notes.ts +++ b/packages/backend/src/misc/process-masto-notes.ts @@ -53,7 +53,11 @@ function processMastoFile(fn: string, path: string, dir: string, uid: string) { continue; } for (const attachment of note.object.attachment) { - const url = attachment.url.replaceAll("..", ""); + // The url in some Mastodon import files do not start with /media_attachments/. + // If this is not handled properly, these users not be able to import images in their posts. + const url = attachment.url + .replaceAll("..", "") + .replaceAll(/.*\/media_attachments\//g, "/media_attachments/"); if (url.indexOf("\0") !== -1) { logger.error(`Found Poison Null Bytes Attack: ${url}`); reject(); diff --git a/packages/backend/src/queue/processors/db/import-firefish-post.ts b/packages/backend/src/queue/processors/db/import-firefish-post.ts index 88356c2458..34a27c4c7a 100644 --- a/packages/backend/src/queue/processors/db/import-firefish-post.ts +++ b/packages/backend/src/queue/processors/db/import-firefish-post.ts @@ -59,18 +59,27 @@ export async function importCkPost( userId: user.id, }); - // FIXME: What is this condition? - if (note != null && (note.fileIds?.length || 0) < files.length) { + // If an import is completely successful at once, the order should not be out of order. + // If it takes multiple imports to complete, the order is not guaranteed to be consistent. + if (note != null && files.length > 0) { + const addFiles: DriveFile[] = []; + for (const file of files) { + if (!note.fileIds.includes(file.id)) { + addFiles.push(file); + } + } + const update: Partial = {}; - update.fileIds = files.map((x) => x.id); + update.fileIds = addFiles.map((x) => x.id); if (update.fileIds != null) { - await NoteFiles.delete({ noteId: note.id }); await NoteFiles.insert( update.fileIds.map((fileId) => ({ noteId: note?.id, fileId })), ); } + update.fileIds = note.fileIds.concat(update.fileIds); + await Notes.update(note.id, update); await NoteEdits.insert({ id: genId(), diff --git a/packages/backend/src/queue/processors/db/import-masto-post.ts b/packages/backend/src/queue/processors/db/import-masto-post.ts index 6a39290741..532c288dba 100644 --- a/packages/backend/src/queue/processors/db/import-masto-post.ts +++ b/packages/backend/src/queue/processors/db/import-masto-post.ts @@ -85,18 +85,27 @@ export async function importMastoPost( userId: user.id, }); - // FIXME: What is this condition? - if (note != null && (note.fileIds?.length || 0) < files.length) { + // If an import is completely successful at once, the order should not be out of order. + // If it takes multiple imports to complete, the order is not guaranteed to be consistent. + if (note != null && files.length > 0) { + const addFiles: DriveFile[] = []; + for (const file of files) { + if (!note.fileIds.includes(file.id)) { + addFiles.push(file); + } + } + const update: Partial = {}; - update.fileIds = files.map((x) => x.id); + update.fileIds = addFiles.map((x) => x.id); if (update.fileIds != null) { - await NoteFiles.delete({ noteId: note.id }); await NoteFiles.insert( update.fileIds.map((fileId) => ({ noteId: note?.id, fileId })), ); } + update.fileIds = note.fileIds.concat(update.fileIds); + await Notes.update(note.id, update); await NoteEdits.insert({ id: genId(),