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

112 lines
3.0 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";
2020-04-16 02:14:53 +05:00
export default class Trash extends Collection {
async init() {
2020-11-24 01:39:32 +05:00
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);
}
});
2020-02-06 16:46:23 +05:00
}
2020-03-23 15:06:12 +05:00
get raw() {
return this._collection.getRaw();
}
2020-02-06 16:46:23 +05:00
get all() {
return this._collection.getItems((u) => u.dateDeleted);
2020-02-06 16:46:23 +05:00
}
async add(item) {
2021-01-11 12:23:12 +05:00
if (!item && !item.type) return;
if (item.dateDeleted || item.deleted || item.migrated) {
return await this._collection.addItem(item);
}
2020-02-22 22:03:09 +05:00
await this._collection.addItem({
...item,
type: "trash",
itemType: item.type,
2020-03-23 13:22:28 +05:00
id: getId(),
itemId: item.id,
dateDeleted: Date.now(),
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;
let item = this._collection.getItem(id);
if (!item) continue;
if (item.itemType === "note") {
await this._db.content.remove(item.contentId);
2020-03-19 11:30:05 +05:00
}
await this._collection.removeItem(id);
2020-02-06 16:46:23 +05:00
}
}
async restore(...ids) {
for (let id of ids) {
2020-02-22 22:03:09 +05:00
let item = { ...this._collection.getItem(id) };
2020-02-06 16:46:23 +05:00
if (!item) continue;
delete item.dateDeleted;
2020-03-23 13:22:28 +05:00
delete item.id;
item.id = item.itemId;
item.type = item.itemType;
delete item.itemType;
2020-03-23 13:22:28 +05:00
delete item.itemId;
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;
2020-04-16 02:14:53 +05:00
await this._db.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 = [];
2020-04-16 02:14:53 +05:00
await this._db.notebooks.add(item);
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
}
}
2020-03-23 13:22:28 +05:00
await this._collection.removeItem(id);
2020-02-06 16:46:23 +05:00
}
}
async clear() {
let indices = await this._collection.indexer.getIndices();
2020-02-06 16:46:23 +05:00
return this.delete(...indices);
}
}