feat: remove trash collection

This commit is contained in:
thecodrr
2021-02-16 16:56:06 +05:00
parent 53be480b67
commit b5a0b9e649
14 changed files with 104 additions and 71 deletions

View File

@@ -0,0 +1 @@
{}

View File

@@ -110,7 +110,7 @@ describe.each([
verifyIndex(data, db, "notebooks", "notebooks"); verifyIndex(data, db, "notebooks", "notebooks");
verifyIndex(data, db, "delta", "content"); verifyIndex(data, db, "delta", "content");
verifyIndex(data, db, "content", "content"); verifyIndex(data, db, "content", "content");
verifyIndex(data, db, "trash", "trash"); // verifyIndex(data, db, "trash", "trash");
}); });
}); });
}); });

View File

@@ -78,7 +78,7 @@ test("restore a deleted note that's in a deleted notebook", () =>
await db.notes.delete(id); await db.notes.delete(id);
await db.notebooks.delete(nbId); await db.notebooks.delete(nbId);
const deletedNote = db.trash.all.find( 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); await db.trash.restore(deletedNote.id);
let note = db.notes.note(id); let note = db.notes.note(id);
@@ -100,7 +100,7 @@ test("restore a deleted notebook", () =>
let noteId = await db.notes.add(TEST_NOTE); let noteId = await db.notes.add(TEST_NOTE);
await db.notebooks.notebook(id).topics.topic("General").add(noteId); await db.notebooks.notebook(id).topics.topic("General").add(noteId);
await db.notebooks.delete(id); 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); let notebook = db.notebooks.notebook(id);
expect(notebook).toBeDefined(); expect(notebook).toBeDefined();
let note = db.notes.note(noteId); 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.notebooks.delete(id);
await db.notes.delete(noteId); await db.notes.delete(noteId);
const deletedNotebook = db.trash.all.find( 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); await db.trash.restore(deletedNotebook.id);
let notebook = db.notebooks.notebook(id); let notebook = db.notebooks.notebook(id);

View File

@@ -80,8 +80,8 @@ class Database {
this.colors = await Tags.new(this, "colors"); this.colors = await Tags.new(this, "colors");
/** @type {Content} */ /** @type {Content} */
this.content = await Content.new(this, "content", false); 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.settings.init();
await this.outbox.init(); await this.outbox.init();

View File

@@ -44,11 +44,6 @@ const MAX_ITEMS = 5;
const tests = [ const tests = [
getMainCollectionParams("notes", TEST_NOTE), getMainCollectionParams("notes", TEST_NOTE),
getMainCollectionParams("notebooks", TEST_NOTEBOOK), getMainCollectionParams("notebooks", TEST_NOTEBOOK),
getMainCollectionParams("trash", {
id: 2141,
type: "note",
title: "someTitle",
}),
getMainCollectionParams("content", { ops: [{ insert: "true" }] }), getMainCollectionParams("content", { ops: [{ insert: "true" }] }),
]; ];

View File

@@ -15,10 +15,6 @@ const emptyServerResponse = {
notes: [], notes: [],
notebooks: [], notebooks: [],
content: [], content: [],
text: [],
tags: [],
colors: [],
trash: [],
settings: [], settings: [],
}; };

View File

@@ -40,7 +40,7 @@ class Collector {
notes: await this._collect(this._db.notes.raw), notes: await this._collect(this._db.notes.raw),
notebooks: await this._collect(this._db.notebooks.raw), notebooks: await this._collect(this._db.notebooks.raw),
content: await this._collect(await this._db.content.all()), 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]), settings: await this._collect([this._db.settings.raw]),
vaultKey: await this._serialize(await this._db.vault._getKey()), vaultKey: await this._serialize(await this._db.vault._getKey()),
}; };

View File

@@ -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( await this._mergeArray(
trash, trash,
() => undefined, () => undefined,

View File

@@ -75,6 +75,10 @@ export default class Notebooks extends Collection {
return tfun.filter(".pinned === true")(this.all); return tfun.filter(".pinned === true")(this.all);
} }
get deleted() {
return tfun.filter(".dateDeleted > 0")(this.raw);
}
/** /**
* *
* @param {string} id The id of the notebook * @param {string} id The id of the notebook

View File

@@ -19,6 +19,10 @@ if (!tfun) {
export default class Notes extends Collection { export default class Notes extends Collection {
async add(noteArg) { async add(noteArg) {
if (!noteArg) return; if (!noteArg) return;
if (noteArg.deleted) {
await this._collection.addItem(noteArg);
return;
}
let id = noteArg.id || getId(); let id = noteArg.id || getId();
let oldNote = this._collection.getItem(id); let oldNote = this._collection.getItem(id);
@@ -141,6 +145,10 @@ export default class Notes extends Collection {
return tfun.filter(".favorite === true")(this.all); return tfun.filter(".favorite === true")(this.all);
} }
get deleted() {
return tfun.filter(".dateDeleted > 0")(this.raw);
}
tagged(tagId) { tagged(tagId) {
const tag = this._db.tags.tag(tagId); const tag = this._db.tags.tag(tagId);
if (!tag || tag.noteIds.length <= 0) return []; if (!tag || tag.noteIds.length <= 0) return [];
@@ -241,8 +249,9 @@ export default class Notes extends Collection {
if (item.data.color) { if (item.data.color) {
await this._db.colors.remove(item.data.color, id); 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); if (moveToTrash) await this._db.trash.add(itemData);
else await this._collection.removeItem(id);
} }
} }

View File

@@ -2,70 +2,92 @@ import Collection from "./collection";
import getId from "../utils/id"; import getId from "../utils/id";
import { get7DayTimestamp } from "../utils/date"; import { get7DayTimestamp } from "../utils/date";
export default class Trash extends Collection { export default class Trash {
async init() { /**
await super.init(); *
await this.cleanup(); * @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() { // async cleanup() {
const sevenDayPreviousTimestamp = Date.now() - get7DayTimestamp(); // const sevenDayPreviousTimestamp = Date.now() - get7DayTimestamp();
this.all.forEach(async (item) => { // this.all.forEach(async (item) => {
if (item.dateDeleted < sevenDayPreviousTimestamp) { // if (item.dateDeleted < sevenDayPreviousTimestamp) {
await this.delete(item.id); // await this.delete(item.id);
} // }
}); // });
} // }
get raw() {
return this._collection.getRaw();
}
get all() { 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) { async add(item) {
if (!item && !item.type) return; const collection = collectionNameFromItem(item);
if (item.dateDeleted || item.deleted || item.migrated) { console.log("Adding to trash.", item, collection);
return await this._collection.addItem(item); if (!item || !item.type || !collection) return;
} // if (item.dateDeleted || item.deleted || item.migrated) {
await this._collection.addItem({ // return await this._collection.addItem(item);
// }
await this.collections[collection]._collection.updateItem({
...item, ...item,
id: item.itemId || item.id,
type: "trash", type: "trash",
itemType: item.type, itemType: item.itemType || item.type,
id: getId(), dateDeleted: item.dateDeleted || Date.now(),
itemId: item.id, deleted: true,
dateDeleted: Date.now(),
}); });
} }
async delete(...ids) { async delete(...ids) {
for (let id of ids) { for (let id of ids) {
if (!id) continue; if (!id) continue;
let item = this._collection.getItem(id); let [item, collection] = this._getItem(id);
if (!item) continue; if (!item) continue;
if (item.itemType === "note") { if (item.itemType === "note") {
await this._db.content.remove(item.contentId); await this._db.content.remove(item.contentId);
} }
await this._collection.removeItem(id); await collection.removeItem(id);
} }
} }
async restore(...ids) { async restore(...ids) {
for (let id of ids) { for (let id of ids) {
let item = { ...this._collection.getItem(id) }; let [item] = this._getItem(id);
if (!item) continue; if (!item) continue;
item = { ...item };
delete item.dateDeleted; delete item.dateDeleted;
delete item.id; delete item.deleted;
item.id = item.itemId;
item.type = item.itemType; item.type = item.itemType;
delete item.itemType; delete item.itemType;
delete item.itemId;
if (item.type === "note") { if (item.type === "note") {
let { notebooks } = item; let { notebooks } = item;
item.notebooks = undefined; item.notebooks = undefined;
await this._db.notes.add(item); await this.collections.notes.add(item);
if (notebooks) { if (notebooks) {
for (let nb of notebooks) { for (let nb of notebooks) {
@@ -91,7 +113,7 @@ export default class Trash extends Collection {
} else if (item.type === "notebook") { } else if (item.type === "notebook") {
const { topics } = item; const { topics } = item;
item.topics = []; item.topics = [];
await this._db.notebooks.add(item); await this.collections.notebooks.add(item);
let notebook = this._db.notebooks.notebook(item.id); let notebook = this._db.notebooks.notebook(item.id);
for (let topic of topics) { for (let topic of topics) {
await notebook.topics.add(topic.title); await notebook.topics.add(topic.title);
@@ -100,12 +122,26 @@ export default class Trash extends Collection {
if (topic.notes) await t.add(...topic.notes); if (topic.notes) await t.add(...topic.notes);
} }
} }
await this._collection.removeItem(id);
} }
} }
async clear() { async clear() {
let indices = await this._collection.indexer.getIndices(); for (let item of this.all) {
return this.delete(...indices); 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;
} }
} }

View File

@@ -30,6 +30,10 @@ export default class CachedCollection extends IndexedCollection {
return this.map.has(id) && !this.map.get(id).deleted; return this.map.has(id) && !this.map.get(id).deleted;
} }
has(id) {
return this.map.has(id);
}
getItem(id) { getItem(id) {
return this.map.get(id); return this.map.get(id);
} }

View File

@@ -37,13 +37,13 @@ export default class IndexedCollection {
return this.updateItem({ return this.updateItem({
id, id,
deleted: true, deleted: true,
dateCreated: Date.now(),
dateEdited: Date.now(), dateEdited: Date.now(),
}); });
} }
deleteItem(id) { async deleteItem(id) {
return this.indexer.remove(id); await this.indexer.deindex(id);
return await this.indexer.remove(id);
} }
exists(id) { exists(id) {

View File

@@ -12,8 +12,8 @@ class Migrator {
let item = get(id); let item = get(id);
if (!item) return; if (!item) return;
if (item.deleted) if (item.deleted && !item.type)
return await collection.dbCollection._collection.addItem(item); return await collection.dbCollection?._collection?.addItem(item);
const migrate = migrations[version][item.type || collection.type]; const migrate = migrations[version][item.type || collection.type];
if (migrate) item = migrate(item); if (migrate) item = migrate(item);