Skip to content

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

feat: cleanup orphaned content items

thecodrrf0dbe822c7b7
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 14b0d6850..1a019c95b 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -96,6 +96,8 @@ class Database {
await this.migrations.migrate();
this.monographs.init();
+
+ await this.syncer.cleanup();
}
async connectSSE() {
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index c9b87b026..53d4ba8fd 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -133,4 +133,18 @@ export default class Sync {
return response.lastSynced;
}
+
+ async cleanup() {
+ try {
+ let token = await this._tokenManager.getAccessToken();
+ if (!token) return;
+ const contentIds = await this._db.content.cleanup();
+ if (contentIds.length <= 0) return;
+ await http.post.json(
+ `${Constants.API_HOST}/sync/delete`,
+ { collection: "content", ids: contentIds },
+ token
+ );
+ } catch (e) {}
+ }
}
diff --git a/packages/core/collections/content.js b/packages/core/collections/content.js
index 6ada294d0..42e7534ad 100644
--- a/packages/core/collections/content.js
+++ b/packages/core/collections/content.js
@@ -55,4 +55,17 @@ export default class Content extends Collection {
all() {
return this._collection.getItems(this._collection.indexer.indices);
}
+
+ async cleanup() {
+ const allContent = await this.all();
+ let ids = [];
+ for (let content of allContent) {
+ const { noteId } = content;
+ if (!this._db.notes.note(noteId)) {
+ ids.push(content.id);
+ await this._collection.deleteItem(content.id);
+ }
+ }
+ return ids;
+ }
}