2021-02-27 11:55:13 +05:00
|
|
|
import sort from "fast-sort";
|
2021-07-16 18:46:16 +05:00
|
|
|
import dayjs from "dayjs";
|
2020-03-11 12:11:59 +05:00
|
|
|
|
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,
|
|
|
|
|
};
|
2020-03-11 12:11:59 +05:00
|
|
|
}
|
|
|
|
|
|
2021-02-27 11:54:52 +05:00
|
|
|
async init() {
|
|
|
|
|
await this.cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async cleanup() {
|
2021-09-29 09:53:50 +05:00
|
|
|
const now = dayjs().unix();
|
|
|
|
|
for (const item of this.all) {
|
2021-12-15 15:24:43 +05:00
|
|
|
if (dayjs(item.dateDeleted).add(7, "days").unix() > now) continue;
|
2021-09-29 09:53:50 +05:00
|
|
|
await this.delete(item.id);
|
|
|
|
|
}
|
2021-02-27 11:54:52 +05:00
|
|
|
}
|
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);
|
|
|
|
|
}
|
2021-02-27 11:55:13 +05:00
|
|
|
return sort(trashItems).desc((item) => item.dateDeleted);
|
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,
|
2020-09-24 10:04:08 +05:00
|
|
|
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;
|
2020-09-24 10:04:08 +05:00
|
|
|
if (item.itemType === "note") {
|
2020-11-04 10:17:37 +05:00
|
|
|
await this._db.content.remove(item.contentId);
|
2022-01-07 09:17:27 +05:00
|
|
|
await this._db.noteHistory.clearSessions(id);
|
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;
|
2020-09-24 10:04:08 +05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2020-03-21 12:25:15 +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) {
|
2022-01-07 09:17:27 +05:00
|
|
|
await this.delete(item.id);
|
2021-02-16 16:56:06 +05:00
|
|
|
}
|
|
|
|
|
}
|
2022-04-01 14:29:58 +05:00
|
|
|
|
|
|
|
|
synced(id) {
|
|
|
|
|
let [item] = this._getItem(id);
|
|
|
|
|
if (item.itemType === "note") {
|
|
|
|
|
const { contentId } = item;
|
|
|
|
|
return this._db.content.exists(contentId);
|
|
|
|
|
} else return true;
|
|
|
|
|
}
|
2021-02-16 16:56:06 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|