Skip to content

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

feat: detach att. from note content on perm delete

thecodrr24daeaae56ab
diff --git a/packages/core/collections/attachments.js b/packages/core/collections/attachments.js
index bbeeca016..29b40e293 100644
--- a/packages/core/collections/attachments.js
+++ b/packages/core/collections/attachments.js
@@ -143,12 +143,28 @@ export default class Attachments extends Collection {
const attachment = this.all.find((a) => a.metadata.hash === hash);
if (!attachment) return false;
if (await this._db.fs.deleteFile(hash, localOnly)) {
- await this._collection.deleteItem(attachment.id);
+ if (!localOnly) {
+ await this.detach(hash);
+ }
+ await this._collection.removeItem(attachment.id);
return true;
}
return false;
}
+ async detach(hashOrId) {
+ const attachment = this.attachment(hashOrId);
+ if (!attachment) return;
+
+ await this._db.notes.init();
+ for (let noteId of attachment.noteIds) {
+ const note = this._db.notes.note(noteId);
+ if (!note) continue;
+ const contentId = note.data.contentId;
+ await this._db.content.removeAttachments(contentId, [hash]);
+ }
+ }
+
/**
* Get specified type of attachments of a note
* @param {string} noteId
diff --git a/packages/core/collections/content.js b/packages/core/collections/content.js
index 70ee76e87..8245bbaa6 100644
--- a/packages/core/collections/content.js
+++ b/packages/core/collections/content.js
@@ -118,6 +118,19 @@ export default class Content extends Collection {
return contentItem;
}
+ /**
+ *
+ * @param {string} id
+ * @param {string[]} hashes
+ * @returns {Promise<any>}
+ */
+ async removeAttachments(id, hashes) {
+ const contentItem = await this.raw(id);
+ const content = getContentFromData(contentItem.type, contentItem.data);
+ contentItem.data = content.removeAttachments(hashes);
+ await this.add(contentItem);
+ }
+
/**
*
* @param {any} contentItem
diff --git a/packages/core/content-types/tiny.js b/packages/core/content-types/tiny.js
index 688c47dc6..274602c4c 100644
--- a/packages/core/content-types/tiny.js
+++ b/packages/core/content-types/tiny.js
@@ -69,6 +69,18 @@ class Tiny {
return this.document.outerHTML || this.document.body.innerHTML;
}
+ removeAttachments(hashes) {
+ const query = hashes.map((h) => `[data-hash="${h}"]`).join(",");
+ const attachmentElements = this.document.querySelectorAll(query);
+
+ for (var i = 0; i < attachmentElements.length; ++i) {
+ const attachment = attachmentElements[i];
+ attachment.remove();
+ }
+
+ return this.document.outerHTML || this.document.body.innerHTML;
+ }
+
async extractAttachments(store) {
const attachments = [];
const attachmentElements = this.document.querySelectorAll("img,span");