Files
notesnook/packages/core/collections/trash.js

151 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-04-16 02:14:53 +05:00
import Collection from "./collection";
2020-03-23 13:22:28 +05:00
import getId from "../utils/id";
import { get7DayTimestamp } from "../utils/date";
2021-02-16 16:56:06 +05:00
export default class Trash {
/**
*
* @param {import("../api").default} db
*/
constructor(db) {
this._db = db;
this.collections = {
notes: db.notes,
notebooks: db.notebooks,
};
}
2021-02-16 16:56:06 +05:00
// async init() {
// await super.init();
// await this.cleanup();
// }
async 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);
}
});
}
2020-02-06 16:46:23 +05:00
2021-02-16 16:56:06 +05:00
get all() {
let trashItems = [];
for (let key in this.collections) {
const collection = this.collections[key];
trashItems.push(...collection.deleted);
}
return trashItems;
2020-03-23 15:06:12 +05:00
}
2021-02-16 16:56:06 +05:00
_getItem(id) {
for (let key in this.collections) {
const collection = this.collections[key]._collection;
if (collection.has(id)) return [collection.getItem(id), collection];
}
return [];
2020-02-06 16:46:23 +05:00
}
async add(item) {
2021-02-16 16:56:06 +05:00
const collection = collectionNameFromItem(item);
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({
2020-02-22 22:03:09 +05:00
...item,
2021-02-16 16:56:06 +05:00
id: item.itemId || item.id,
type: "trash",
2021-02-16 16:56:06 +05:00
itemType: item.itemType || item.type,
dateDeleted: item.dateDeleted || Date.now(),
deleted: true,
2020-02-22 22:03:09 +05:00
});
2020-02-06 16:46:23 +05:00
}
async delete(...ids) {
for (let id of ids) {
2020-03-19 11:30:05 +05:00
if (!id) continue;
2021-02-16 16:56:06 +05:00
let [item, collection] = this._getItem(id);
2020-03-19 11:30:05 +05:00
if (!item) continue;
if (item.itemType === "note") {
await this._db.content.remove(item.contentId);
2020-03-19 11:30:05 +05:00
}
2021-02-16 16:56:06 +05:00
await collection.removeItem(id);
2020-02-06 16:46:23 +05:00
}
}
async restore(...ids) {
for (let id of ids) {
2021-02-16 16:56:06 +05:00
let [item] = this._getItem(id);
2020-02-06 16:46:23 +05:00
if (!item) continue;
2021-02-16 16:56:06 +05:00
item = { ...item };
2020-02-06 16:46:23 +05:00
delete item.dateDeleted;
2021-02-16 16:56:06 +05:00
delete item.deleted;
item.type = item.itemType;
delete item.itemType;
2021-02-16 16:56:06 +05:00
2020-02-06 16:46:23 +05:00
if (item.type === "note") {
2020-12-07 11:19:36 +05:00
let { notebooks } = item;
item.notebooks = undefined;
2021-02-16 16:56:06 +05:00
await this.collections.notes.add(item);
2020-02-06 16:46:23 +05:00
2020-12-07 11:19:36 +05:00
if (notebooks) {
for (let nb of notebooks) {
const { id, topics } = nb;
for (let topic of topics) {
// if the notebook or topic has been deleted
if (
!this._db.notebooks._collection.exists(id) ||
!this._db.notebooks.notebook(id).topics.has(topic)
) {
notebooks = undefined;
continue;
}
2020-02-06 16:46:23 +05:00
2020-12-07 11:19:36 +05:00
// restore the note to the topic it was in before deletion
await this._db.notebooks
.notebook(id)
.topics.topic(topic)
.add(item.id);
}
2020-02-06 16:46:23 +05:00
}
}
} else if (item.type === "notebook") {
2020-02-06 16:46:23 +05:00
const { topics } = item;
item.topics = [];
2021-02-16 16:56:06 +05:00
await this.collections.notebooks.add(item);
2020-04-16 02:14:53 +05:00
let notebook = this._db.notebooks.notebook(item.id);
2020-02-06 16:46:23 +05:00
for (let topic of topics) {
2020-03-23 13:22:28 +05:00
await notebook.topics.add(topic.title);
let t = notebook.topics.topic(topic.title);
2020-02-06 18:47:42 +05:00
if (!t) continue;
if (topic.notes) await t.add(...topic.notes);
2020-02-06 16:46:23 +05:00
}
}
}
}
async clear() {
2021-02-16 16:56:06 +05:00
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;
2020-02-06 16:46:23 +05:00
}
}