From c0cec22e10b65a46be1ccdb120d668dceb16e287 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Fri, 1 Mar 2024 16:57:48 +0500 Subject: [PATCH] core: only get attachment id when processing content --- packages/core/src/collections/content.ts | 42 ++++++------------------ 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/packages/core/src/collections/content.ts b/packages/core/src/collections/content.ts index 504a9d243..f95e50b56 100644 --- a/packages/core/src/collections/content.ts +++ b/packages/core/src/collections/content.ts @@ -320,10 +320,6 @@ export class Content implements ICollection { return hashes.every((hash) => hash !== attachment.hash); }); - const toAdd = hashes.filter((hash) => { - return hash && noteAttachments.every((a) => hash !== a.hash); - }); - for (const attachment of toDelete) { await this.db.relations.unlink( { @@ -334,44 +330,26 @@ export class Content implements ICollection { ); } - for (const hash of toAdd) { - // TODO: only get id instead of the whole object - const attachment = await this.db.attachments.attachment(hash); - if (!attachment) continue; + const toAdd = hashes.filter((hash) => { + return hash && noteAttachments.every((a) => hash !== a.hash); + }); + + const attachments = await this.db.attachments.all + .fields(["attachments.id"]) + .where((eb) => eb("hash", "in", toAdd)) + .items(); + for (const attachment of attachments) { await this.db.relations.add( { id: contentItem.noteId, type: "note" }, - attachment + { id: attachment.id, type: "attachment" } ); } - // if (toAdd.length > 0) { - // contentItem.dateModified = Date.now(); - // } - // contentItem.data = data; return data; } - - // async cleanup() { - // const indices = this.collection.indexer.indices; - // await this.db.notes.init(); - // const notes = this.db.notes.all; - // if (!notes.length && indices.length > 0) return []; - // const ids = []; - // for (const contentId of indices) { - // const noteIndex = notes.findIndex((note) => note.contentId === contentId); - // const isOrphaned = noteIndex === -1; - // if (isOrphaned) { - // ids.push(contentId); - // await this.collection.deleteItem(contentId); - // } else if (notes[noteIndex].localOnly) { - // ids.push(contentId); - // } - // } - // return ids; - // } } export function isUnencryptedContent(