mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
feat: remove trash collection
This commit is contained in:
@@ -0,0 +1 @@
|
||||
{}
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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" }] }),
|
||||
];
|
||||
|
||||
|
||||
@@ -15,10 +15,6 @@ const emptyServerResponse = {
|
||||
notes: [],
|
||||
notebooks: [],
|
||||
content: [],
|
||||
text: [],
|
||||
tags: [],
|
||||
colors: [],
|
||||
trash: [],
|
||||
settings: [],
|
||||
};
|
||||
|
||||
|
||||
@@ -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()),
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
get raw() {
|
||||
return this._collection.getRaw();
|
||||
}
|
||||
// async cleanup() {
|
||||
// const sevenDayPreviousTimestamp = Date.now() - get7DayTimestamp();
|
||||
// this.all.forEach(async (item) => {
|
||||
// if (item.dateDeleted < sevenDayPreviousTimestamp) {
|
||||
// await this.delete(item.id);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
get all() {
|
||||
return this._collection.getItems((u) => u.dateDeleted);
|
||||
let trashItems = [];
|
||||
for (let key in this.collections) {
|
||||
const collection = this.collections[key];
|
||||
trashItems.push(...collection.deleted);
|
||||
}
|
||||
return trashItems;
|
||||
}
|
||||
|
||||
_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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user