Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: fix attachment locked note check never firing (#10522)

Abdullah Attaf5a5b93ce949
diff --git a/apps/mobile/app/components/attachments/actions.tsx b/apps/mobile/app/components/attachments/actions.tsx
index 7acba6ee1..9ca343570 100644
--- a/apps/mobile/app/components/attachments/actions.tsx
+++ b/apps/mobile/app/components/attachments/actions.tsx
@@ -245,6 +245,11 @@ const Actions = ({
});
return true;
} catch (e) {
+ ToastManager.error(
+ e as Error,
+ strings.failedToDelete(),
+ "local"
+ );
return false;
}
}
diff --git a/apps/web/src/common/multi-select.ts b/apps/web/src/common/multi-select.ts
index 35af47dba..d55c7472e 100644
--- a/apps/web/src/common/multi-select.ts
+++ b/apps/web/src/common/multi-select.ts
@@ -112,6 +112,7 @@ async function deleteAttachments(ids: string[]) {
)
return;
+ let deleted = 0;
await TaskManager.startTask({
type: "status",
id: "deleteAttachments",
@@ -127,11 +128,12 @@ async function deleteAttachments(ids: string[]) {
current: i,
total: ids.length
});
- await attachmentStore.permanentDelete(attachment);
+ if (await attachmentStore.permanentDelete(attachment)) deleted++;
}
}
});
- showToast("success", strings.actions.deleted.attachment(ids.length));
+ if (deleted > 0)
+ showToast("success", strings.actions.deleted.attachment(deleted));
}
async function moveRemindersToTrash(ids: string[]) {
diff --git a/apps/web/src/stores/attachment-store.ts b/apps/web/src/stores/attachment-store.ts
index 9eea48638..f8ca5e635 100644
--- a/apps/web/src/stores/attachment-store.ts
+++ b/apps/web/src/stores/attachment-store.ts
@@ -152,11 +152,14 @@ class AttachmentStore extends BaseStore<AttachmentStore> {
silent: true
});
}
+ return true;
}
+ return false;
} catch (e) {
console.error(e);
if (e instanceof Error)
showToast("error", `${strings.failedToDelete()}: ${e.message}`);
+ return false;
} finally {
this._changeWorkingStatus(attachment.hash);
}
diff --git a/packages/core/__tests__/attachments.test.ts b/packages/core/__tests__/attachments.test.ts
index a7c413b6b..3a96ead5f 100644
--- a/packages/core/__tests__/attachments.test.ts
+++ b/packages/core/__tests__/attachments.test.ts
@@ -58,3 +58,29 @@ test("remove orphaned attachments should remove only orphaned attachments", () =
expect(await db.attachments.exists(hash1)).toBe(true);
expect(await db.attachments.orphaned.count()).toBe(0);
}));
+
+test("remove should throw if attachment is inside a locked note", () =>
+ databaseTest().then(async (db) => {
+ await loginFakeUser(db);
+
+ const hash = await db.attachments.save(
+ "iVBORw0KGgoAAAANSUhEUgAAAAEEAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
+ "image/png",
+ "test.png"
+ );
+ if (!hash) throw new Error("Failed to create attachment");
+ const attachment = await db.attachments.attachment(hash);
+ if (!attachment) throw new Error("Failed to find attachment");
+
+ const noteId = await db.notes.add({ title: "Test Note" });
+ await db.relations.add(
+ { id: noteId, type: "note" },
+ { id: attachment.id, type: "attachment" }
+ );
+ await db.vault.create("password");
+ await db.vault.add(noteId);
+
+ await expect(db.attachments.remove(hash, false)).rejects.toThrow(
+ "locked note"
+ );
+ }));
diff --git a/packages/core/src/collections/attachments.ts b/packages/core/src/collections/attachments.ts
index 7e94fb1cf..cb93d2628 100644
--- a/packages/core/src/collections/attachments.ts
+++ b/packages/core/src/collections/attachments.ts
@@ -280,7 +280,7 @@ export class Attachments implements ICollection {
const linkedNotes = await this.db.relations.to(attachment, "note").get();
return (
(await this.db.relations
- .to({ ids: linkedNotes.map((n) => n.toId), type: "note" }, "vault")
+ .to({ ids: linkedNotes.map((n) => n.fromId), type: "note" }, "vault")
.count()) <= 0
);
}