mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-24 07:29:30 +01:00
fix: remove note content on trash clear (#21)
* fix content not removed when clear is called in trash * add test to ensure content is deleted on clearing trash * tests: clearing trash should clear referenced note history * add cleanup function for content
This commit is contained in:
@@ -16,16 +16,24 @@ test("trash should be empty", () =>
|
||||
}));
|
||||
|
||||
test("permanently delete a note", () =>
|
||||
noteTest().then(async ({ db, id }) => {
|
||||
const note = db.notes.note(id);
|
||||
await db.notes.delete(id);
|
||||
databaseTest().then(async (db) => {
|
||||
const noteId = await db.notes.add({...TEST_NOTE,sessionId:Date.now()});
|
||||
const note = db.notes.note(noteId);
|
||||
|
||||
let sessions = await db.noteHistory.get(noteId);
|
||||
expect(sessions.length).toBe(1);
|
||||
|
||||
await db.notes.delete(noteId);
|
||||
expect(db.trash.all.length).toBe(1);
|
||||
expect(await note.content()).toBeDefined();
|
||||
await db.trash.delete(db.trash.all[0].id);
|
||||
expect(db.trash.all.length).toBe(0);
|
||||
const content = await db.content.raw(note.data.contentId);
|
||||
expect(content.deleted).toBe(true);
|
||||
}));
|
||||
|
||||
sessions = await db.noteHistory.get(noteId);
|
||||
expect(sessions.length).toBe(0);
|
||||
}));
|
||||
|
||||
test("restore a deleted note that was in a notebook", () =>
|
||||
noteTest().then(async ({ db, id }) => {
|
||||
@@ -169,3 +177,30 @@ test("trash cleanup should not delete items newer than 7 days", () =>
|
||||
|
||||
expect(db.trash.all.length).toBe(2);
|
||||
}));
|
||||
|
||||
test("clear trash should delete note content", () =>
|
||||
databaseTest().then(async (db) => {
|
||||
const noteId = await db.notes.add({ ...TEST_NOTE, sessionId: Date.now() });
|
||||
|
||||
const notebookId = await db.notebooks.add(TEST_NOTEBOOK);
|
||||
|
||||
let sessions = await db.noteHistory.get(noteId);
|
||||
expect(sessions.length).toBe(1);
|
||||
|
||||
let note = { ...db.notes.note(noteId).data };
|
||||
|
||||
await db.notebooks.delete(notebookId);
|
||||
await db.notes.delete(noteId);
|
||||
|
||||
expect(db.trash.all.length).toBe(2);
|
||||
|
||||
await db.trash.clear();
|
||||
|
||||
expect(db.trash.all.length).toBe(0);
|
||||
|
||||
const content = await db.content.raw(note.contentId);
|
||||
expect(content.deleted).toBe(true);
|
||||
|
||||
sessions = await db.noteHistory.get(note.id);
|
||||
expect(sessions.length).toBe(0);
|
||||
}));
|
||||
|
||||
@@ -156,4 +156,23 @@ export default class Content extends Collection {
|
||||
contentItem.data = data;
|
||||
return contentItem;
|
||||
}
|
||||
|
||||
async cleanup() {
|
||||
const indices = this._collection.indexer.indices;
|
||||
await this._db.notes.init();
|
||||
const notes = this._db.notes._collection.getRaw();
|
||||
if (!notes.length && indices.length > 0) return [];
|
||||
let ids = [];
|
||||
for (let contentId of indices) {
|
||||
const noteIndex = notes.findIndex((note) => note.contentId === contentId);
|
||||
const isOrphaned = noteIndex === -1;
|
||||
if (isOrphaned) {
|
||||
ids.push(contentId);
|
||||
await this._collection.deleteItem(contentId);
|
||||
} else if (notes[noteIndex].localOnly) {
|
||||
ids.push(contentId);
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { makeSessionContentId, makeSessionId } from "../utils/id";
|
||||
import { makeSessionContentId } from "../utils/id";
|
||||
import Collection from "./collection";
|
||||
import SessionContent from "./session-content";
|
||||
/**
|
||||
|
||||
@@ -66,6 +66,7 @@ export default class Trash {
|
||||
if (!item) continue;
|
||||
if (item.itemType === "note") {
|
||||
await this._db.content.remove(item.contentId);
|
||||
await this._db.noteHistory.clearSessions(id);
|
||||
}
|
||||
await collection.removeItem(id);
|
||||
}
|
||||
@@ -124,8 +125,7 @@ export default class Trash {
|
||||
|
||||
async clear() {
|
||||
for (let item of this.all) {
|
||||
const collectionName = collectionNameFromItem(item);
|
||||
await this.collections[collectionName]._collection.removeItem(item.id);
|
||||
await this.delete(item.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user