Skip to content

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

feat: remove trash collection

thecodrrb5a0b9e6495c
diff --git a/packages/core/.watchmanconfig b/packages/core/.watchmanconfig
index e69de29bb..0967ef424 100644
--- a/packages/core/.watchmanconfig
+++ b/packages/core/.watchmanconfig
@@ -0,0 +1 @@
+{}
diff --git a/packages/core/__tests__/backup.test.js b/packages/core/__tests__/backup.test.js
index 94ef30def..9a70421b1 100644
--- a/packages/core/__tests__/backup.test.js
+++ b/packages/core/__tests__/backup.test.js
@@ -110,7 +110,7 @@ describe.each([
verifyIndex(data, db, "notebooks", "notebooks");
verifyIndex(data, db, "delta", "content");
verifyIndex(data, db, "content", "content");
- verifyIndex(data, db, "trash", "trash");
+ // verifyIndex(data, db, "trash", "trash");
});
});
});
diff --git a/packages/core/__tests__/trash.test.js b/packages/core/__tests__/trash.test.js
index 8e352f839..a28265148 100644
--- a/packages/core/__tests__/trash.test.js
+++ b/packages/core/__tests__/trash.test.js
@@ -78,7 +78,7 @@ test("restore a deleted note that's in a deleted notebook", () =>
await db.notes.delete(id);
await db.notebooks.delete(nbId);
const deletedNote = db.trash.all.find(
- (v) => v.itemId.includes(id) && v.itemType === "note"
+ (v) => v.id === id && v.itemType === "note"
);
await db.trash.restore(deletedNote.id);
let note = db.notes.note(id);
@@ -100,7 +100,7 @@ test("restore a deleted notebook", () =>
let noteId = await db.notes.add(TEST_NOTE);
await db.notebooks.notebook(id).topics.topic("General").add(noteId);
await db.notebooks.delete(id);
- await db.trash.restore(db.trash.all[0].id);
+ await db.trash.restore(id);
let notebook = db.notebooks.notebook(id);
expect(notebook).toBeDefined();
let note = db.notes.note(noteId);
@@ -118,7 +118,7 @@ test("restore a notebook that has deleted notes", () =>
await db.notebooks.delete(id);
await db.notes.delete(noteId);
const deletedNotebook = db.trash.all.find(
- (v) => v.itemId.includes(id) && v.itemType === "notebook"
+ (v) => v.id === id && v.itemType === "notebook"
);
await db.trash.restore(deletedNotebook.id);
let notebook = db.notebooks.notebook(id);
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 3eca2fd2e..7303c3e7b 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -80,8 +80,8 @@ class Database {
this.colors = await Tags.new(this, "colors");
/** @type {Content} */
this.content = await Content.new(this, "content", false);
- /** @type {Trash} */
- this.trash = await Trash.new(this, "trash");
+
+ this.trash = new Trash(this);
await this.settings.init();
await this.outbox.init();
diff --git a/packages/core/api/sync/__tests__/collector.test.js b/packages/core/api/sync/__tests__/collector.test.js
index 45374c212..eac8dfa4e 100644
--- a/packages/core/api/sync/__tests__/collector.test.js
+++ b/packages/core/api/sync/__tests__/collector.test.js
@@ -44,11 +44,6 @@ const MAX_ITEMS = 5;
const tests = [
getMainCollectionParams("notes", TEST_NOTE),
getMainCollectionParams("notebooks", TEST_NOTEBOOK),
- getMainCollectionParams("trash", {
- id: 2141,
- type: "note",
- title: "someTitle",
- }),
getMainCollectionParams("content", { ops: [{ insert: "true" }] }),
];
diff --git a/packages/core/api/sync/__tests__/merger.test.js b/packages/core/api/sync/__tests__/merger.test.js
index 4480d18b8..67c6af5af 100644
--- a/packages/core/api/sync/__tests__/merger.test.js
+++ b/packages/core/api/sync/__tests__/merger.test.js
@@ -15,10 +15,6 @@ const emptyServerResponse = {
notes: [],
notebooks: [],
content: [],
- text: [],
- tags: [],
- colors: [],
- trash: [],
settings: [],
};
diff --git a/packages/core/api/sync/collector.js b/packages/core/api/sync/collector.js
index 97acdcd3c..8d2890c6b 100644
--- a/packages/core/api/sync/collector.js
+++ b/packages/core/api/sync/collector.js
@@ -40,7 +40,7 @@ class Collector {
notes: await this._collect(this._db.notes.raw),
notebooks: await this._collect(this._db.notebooks.raw),
content: await this._collect(await this._db.content.all()),
- trash: await this._collect(this._db.trash.raw),
+ // trash: await this._collect(this._db.trash.raw),
settings: await this._collect([this._db.settings.raw]),
vaultKey: await this._serialize(await this._db.vault._getKey()),
};
diff --git a/packages/core/api/sync/merger.js b/packages/core/api/sync/merger.js
index 6e549f412..7fbd31d02 100644
--- a/packages/core/api/sync/merger.js
+++ b/packages/core/api/sync/merger.js
@@ -130,18 +130,6 @@ class Merger {
}
);
- // await this._mergeArray(
- // tags,
- // (id) => this._db.tags.tag(id),
- // (item) => this._db.tags.merge(item)
- // );
-
- // await this._mergeArray(
- // colors,
- // (id) => this._db.colors.tag(id),
- // (item) => this._db.colors.merge(item)
- // );
-
await this._mergeArray(
trash,
() => undefined,
diff --git a/packages/core/collections/notebooks.js b/packages/core/collections/notebooks.js
index 4da798922..f6b667424 100644
--- a/packages/core/collections/notebooks.js
+++ b/packages/core/collections/notebooks.js
@@ -75,6 +75,10 @@ export default class Notebooks extends Collection {
return tfun.filter(".pinned === true")(this.all);
}
+ get deleted() {
+ return tfun.filter(".dateDeleted > 0")(this.raw);
+ }
+
/**
*
* @param {string} id The id of the notebook
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index 7cbef57c2..bae4edfbf 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -19,6 +19,10 @@ if (!tfun) {
export default class Notes extends Collection {
async add(noteArg) {
if (!noteArg) return;
+ if (noteArg.deleted) {
+ await this._collection.addItem(noteArg);
+ return;
+ }
let id = noteArg.id || getId();
let oldNote = this._collection.getItem(id);
@@ -141,6 +145,10 @@ export default class Notes extends Collection {
return tfun.filter(".favorite === true")(this.all);
}
+ get deleted() {
+ return tfun.filter(".dateDeleted > 0")(this.raw);
+ }
+
tagged(tagId) {
const tag = this._db.tags.tag(tagId);
if (!tag || tag.noteIds.length <= 0) return [];
@@ -241,8 +249,9 @@ export default class Notes extends Collection {
if (item.data.color) {
await this._db.colors.remove(item.data.color, id);
}
- await this._collection.removeItem(id);
+ // await this._collection.removeItem(id);
if (moveToTrash) await this._db.trash.add(itemData);
+ else await this._collection.removeItem(id);
}
}
diff --git a/packages/core/collections/trash.js b/packages/core/collections/trash.js
index 666e72fb2..0f33acbb7 100644
--- a/packages/core/collections/trash.js
+++ b/packages/core/collections/trash.js
@@ -2,70 +2,92 @@ import Collection from "./collection";
import getId from "../utils/id";
import { get7DayTimestamp } from "../utils/date";
-export default class Trash extends Collection {
- async init() {
- await super.init();
- await this.cleanup();
+export default class Trash {
+ /**
+ *
+ * @param {import("../api").default} db
+ */
+ constructor(db) {
+ this._db = db;
+ this.collections = {
+ notes: db.notes,
+ notebooks: db.notebooks,
+ };
}
+ // async init() {
+ // await super.init();
+ // await this.cleanup();
+ // }
- async cleanup() {
- const sevenDayPreviousTimestamp = Date.now() - get7DayTimestamp();
- this.all.forEach(async (item) => {
- if (item.dateDeleted < sevenDayPreviousTimestamp) {
- await this.delete(item.id);
- }
- });
- }
+ // async cleanup() {
+ // const sevenDayPreviousTimestamp = Date.now() - get7DayTimestamp();
+ // this.all.forEach(async (item) => {
+ // if (item.dateDeleted < sevenDayPreviousTimestamp) {
+ // await this.delete(item.id);
+ // }
+ // });
+ // }
- get raw() {
- return this._collection.getRaw();
+ get all() {
+ let trashItems = [];
+ for (let key in this.collections) {
+ const collection = this.collections[key];
+ trashItems.push(...collection.deleted);
+ }
+ return trashItems;
}
- get all() {
- return this._collection.getItems((u) => u.dateDeleted);
+ _getItem(id) {
+ for (let key in this.collections) {
+ const collection = this.collections[key]._collection;
+ if (collection.has(id)) return [collection.getItem(id), collection];
+ }
+ return [];
}
async add(item) {
- if (!item && !item.type) return;
- if (item.dateDeleted || item.deleted || item.migrated) {
- return await this._collection.addItem(item);
- }
- await this._collection.addItem({
+ const collection = collectionNameFromItem(item);
+ console.log("Adding to trash.", item, collection);
+ if (!item || !item.type || !collection) return;
+ // if (item.dateDeleted || item.deleted || item.migrated) {
+ // return await this._collection.addItem(item);
+ // }
+ await this.collections[collection]._collection.updateItem({
...item,
+ id: item.itemId || item.id,
type: "trash",
- itemType: item.type,
- id: getId(),
- itemId: item.id,
- dateDeleted: Date.now(),
+ itemType: item.itemType || item.type,
+ dateDeleted: item.dateDeleted || Date.now(),
+ deleted: true,
});
}
async delete(...ids) {
for (let id of ids) {
if (!id) continue;
- let item = this._collection.getItem(id);
+ let [item, collection] = this._getItem(id);
if (!item) continue;
if (item.itemType === "note") {
await this._db.content.remove(item.contentId);
}
- await this._collection.removeItem(id);
+ await collection.removeItem(id);
}
}
async restore(...ids) {
for (let id of ids) {
- let item = { ...this._collection.getItem(id) };
+ let [item] = this._getItem(id);
if (!item) continue;
+ item = { ...item };
delete item.dateDeleted;
- delete item.id;
- item.id = item.itemId;
+ delete item.deleted;
item.type = item.itemType;
delete item.itemType;
- delete item.itemId;
+
if (item.type === "note") {
let { notebooks } = item;
item.notebooks = undefined;
- await this._db.notes.add(item);
+ await this.collections.notes.add(item);
if (notebooks) {
for (let nb of notebooks) {
@@ -91,7 +113,7 @@ export default class Trash extends Collection {
} else if (item.type === "notebook") {
const { topics } = item;
item.topics = [];
- await this._db.notebooks.add(item);
+ await this.collections.notebooks.add(item);
let notebook = this._db.notebooks.notebook(item.id);
for (let topic of topics) {
await notebook.topics.add(topic.title);
@@ -100,12 +122,26 @@ export default class Trash extends Collection {
if (topic.notes) await t.add(...topic.notes);
}
}
- await this._collection.removeItem(id);
}
}
async clear() {
- let indices = await this._collection.indexer.getIndices();
- return this.delete(...indices);
+ for (let item of this.all) {
+ const collectionName = collectionNameFromItem(item);
+ await this.collections[collectionName]._collection.removeItem(item.id);
+ }
+ }
+}
+
+function collectionNameFromItem(item) {
+ const { type, itemType } = item;
+ let typeToCompare = type === "trash" ? itemType : type;
+ switch (typeToCompare) {
+ case "note":
+ return "notes";
+ case "notebook":
+ return "notebooks";
+ default:
+ return null;
}
}
diff --git a/packages/core/database/cached-collection.js b/packages/core/database/cached-collection.js
index 17d90cd7c..683052d27 100644
--- a/packages/core/database/cached-collection.js
+++ b/packages/core/database/cached-collection.js
@@ -30,6 +30,10 @@ export default class CachedCollection extends IndexedCollection {
return this.map.has(id) && !this.map.get(id).deleted;
}
+ has(id) {
+ return this.map.has(id);
+ }
+
getItem(id) {
return this.map.get(id);
}
diff --git a/packages/core/database/indexed-collection.js b/packages/core/database/indexed-collection.js
index 3e268a5c6..2ad0fcf5e 100644
--- a/packages/core/database/indexed-collection.js
+++ b/packages/core/database/indexed-collection.js
@@ -37,13 +37,13 @@ export default class IndexedCollection {
return this.updateItem({
id,
deleted: true,
- dateCreated: Date.now(),
dateEdited: Date.now(),
});
}
- deleteItem(id) {
- return this.indexer.remove(id);
+ async deleteItem(id) {
+ await this.indexer.deindex(id);
+ return await this.indexer.remove(id);
}
exists(id) {
diff --git a/packages/core/database/migrator.js b/packages/core/database/migrator.js
index 76df21270..5bfbadc99 100644
--- a/packages/core/database/migrator.js
+++ b/packages/core/database/migrator.js
@@ -12,8 +12,8 @@ class Migrator {
let item = get(id);
if (!item) return;
- if (item.deleted)
- return await collection.dbCollection._collection.addItem(item);
+ if (item.deleted && !item.type)
+ return await collection.dbCollection?._collection?.addItem(item);
const migrate = migrations[version][item.type || collection.type];
if (migrate) item = migrate(item);