Skip to content

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

core: fix trash cleanup deleting only one expired item (#10513)

Abdullah Attaa9b3d564bfb9
diff --git a/packages/core/__tests__/trash.test.ts b/packages/core/__tests__/trash.test.ts
index 90caa3b63..f308ff2d9 100644
--- a/packages/core/__tests__/trash.test.ts
+++ b/packages/core/__tests__/trash.test.ts
@@ -167,6 +167,30 @@ test("permanently delete items older than 7 days", () =>
expect(await db.trash.all()).toHaveLength(0);
}));
+test("trash cleanup should delete all expired items", () =>
+ databaseTest().then(async (db) => {
+ const noteIds: string[] = [];
+ const notebookIds: string[] = [];
+ for (let i = 0; i < 5; i++) noteIds.push(await db.notes.add(TEST_NOTE));
+ for (let i = 0; i < 3; i++)
+ notebookIds.push((await db.notebooks.add(TEST_NOTEBOOK))!);
+
+ await db.notes.moveToTrash(...noteIds);
+ await db.notebooks.moveToTrash(...notebookIds);
+
+ const expired = dayjs().subtract(30, "days").toDate().getTime();
+ await db.notes.collection.update(noteIds, { dateDeleted: expired });
+ await db.notebooks.collection.update(notebookIds, {
+ dateDeleted: expired
+ });
+
+ expect(await db.trash.all()).toHaveLength(8);
+
+ await db.trash.cleanup();
+
+ expect(await db.trash.all()).toHaveLength(0);
+ }));
+
test("trash cleanup should not delete items newer than 7 days", () =>
databaseTest().then(async (db) => {
const noteId = await db.notes.add(TEST_NOTE);
diff --git a/packages/core/src/collections/trash.ts b/packages/core/src/collections/trash.ts
index 087fcfa6d..08cafc618 100644
--- a/packages/core/src/collections/trash.ts
+++ b/packages/core/src/collections/trash.ts
@@ -96,32 +96,24 @@ export default class Trash {
.subtract(duration, "days")
.toDate()
.getTime();
- const expiredItems = await this.db
+ const noteIds = await this.db
.sql()
- .selectNoFrom((eb) => [
- eb
- .selectFrom("notes")
- .where("type", "==", "trash")
- .where("dateDeleted", "<=", maxMs)
- .select("id")
- .as("noteId"),
- eb
- .selectFrom("notebooks")
- .where("type", "==", "trash")
- .where("dateDeleted", "<=", maxMs)
- .select("id")
- .as("notebookId")
- ])
+ .selectFrom("notes")
+ .where("type", "==", "trash")
+ .where("dateDeleted", "<=", maxMs)
+ .select("id")
.execute();
- const { noteIds, notebookIds } = expiredItems.reduce(
- (ids, item) => {
- if (item.noteId) ids.noteIds.push(item.noteId);
- if (item.notebookId) ids.notebookIds.push(item.notebookId);
- return ids;
- },
- { noteIds: [] as string[], notebookIds: [] as string[] }
+ const notebookIds = await this.db
+ .sql()
+ .selectFrom("notebooks")
+ .where("type", "==", "trash")
+ .where("dateDeleted", "<=", maxMs)
+ .select("id")
+ .execute();
+ await this._delete(
+ noteIds.map((n) => n.id),
+ notebookIds.map((n) => n.id)
);
- await this._delete(noteIds, notebookIds);
await this.buildCache();
}