2020-02-06 16:46:23 +05:00
|
|
|
import CachedCollection from "../database/cached-collection";
|
|
|
|
|
import Notes from "./notes";
|
|
|
|
|
import Notebooks from "./notebooks";
|
|
|
|
|
import Storage from "../database/storage";
|
2020-03-11 12:11:59 +05:00
|
|
|
import { get7DayTimestamp } from "../utils/date";
|
|
|
|
|
|
2020-02-06 16:46:23 +05:00
|
|
|
export default class Trash {
|
|
|
|
|
constructor(context) {
|
2020-02-22 21:53:56 +05:00
|
|
|
this._collection = new CachedCollection(context, "trash");
|
|
|
|
|
this._deltaStorage = new Storage(context);
|
2020-02-06 16:46:23 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {Notes} notes
|
|
|
|
|
* @param {Notebooks} notebooks
|
|
|
|
|
*/
|
|
|
|
|
async init(notes, notebooks) {
|
2020-02-22 21:53:56 +05:00
|
|
|
this._notes = notes;
|
|
|
|
|
this._notebooks = notebooks;
|
|
|
|
|
await this._collection.init();
|
2020-03-11 12:11:59 +05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get all() {
|
2020-03-01 11:42:30 +05:00
|
|
|
return this._collection.getAllItems(u => u.dateDeleted);
|
2020-02-06 16:46:23 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async add(item) {
|
2020-02-22 21:53:56 +05:00
|
|
|
if (this._collection.exists(item.id + "_deleted"))
|
2020-02-06 16:46:23 +05:00
|
|
|
throw new Error("This item has already been deleted.");
|
2020-02-22 22:03:09 +05:00
|
|
|
await this._collection.addItem({
|
|
|
|
|
...item,
|
|
|
|
|
dateDeleted: Date.now(),
|
|
|
|
|
id: item.id + "_deleted"
|
|
|
|
|
});
|
2020-02-06 16:46:23 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(...ids) {
|
|
|
|
|
for (let id of ids) {
|
2020-02-22 21:53:56 +05:00
|
|
|
if (!this._collection.exists(id)) return;
|
2020-02-06 16:46:23 +05:00
|
|
|
if (id.indexOf("note") > -1)
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._deltaStorage.remove(id.replace("_deleted", "") + "_delta");
|
|
|
|
|
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;
|
|
|
|
|
item.id = item.id.replace("_deleted", "");
|
|
|
|
|
if (item.type === "note") {
|
|
|
|
|
let { notebook } = item;
|
|
|
|
|
item.notebook = {};
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._notes.add(item);
|
2020-02-06 16:46:23 +05:00
|
|
|
|
|
|
|
|
if (notebook && notebook.id && notebook.topic) {
|
|
|
|
|
const { id, topic } = notebook;
|
|
|
|
|
|
|
|
|
|
// if the notebook has been deleted
|
2020-02-22 21:53:56 +05:00
|
|
|
if (!this._notebooks._collection.exists(id)) {
|
2020-02-06 16:46:23 +05:00
|
|
|
notebook = {};
|
|
|
|
|
} else {
|
|
|
|
|
// if the topic has been deleted
|
2020-02-22 21:53:56 +05:00
|
|
|
if (!this._notebooks.notebook(id).topics.has(topic)) {
|
2020-02-06 16:46:23 +05:00
|
|
|
notebook = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// restore the note to the topic it was in before deletion
|
|
|
|
|
if (notebook.id && notebook.topic) {
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._notebooks
|
2020-02-06 16:46:23 +05:00
|
|
|
.notebook(id)
|
|
|
|
|
.topics.topic(topic)
|
|
|
|
|
.add(item.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const { topics } = item;
|
|
|
|
|
item.topics = [];
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._notebooks.add(item);
|
|
|
|
|
let notebook = this._notebooks.notebook(item.id);
|
2020-02-06 16:46:23 +05:00
|
|
|
for (let topic of topics) {
|
2020-02-06 18:47:42 +05:00
|
|
|
await notebook.topics.add(topic.title || topic);
|
|
|
|
|
let t = notebook.topics.topic(topic.title || topic);
|
|
|
|
|
if (!t) continue;
|
|
|
|
|
if (topic.notes) await t.add(...topic.notes);
|
2020-02-06 16:46:23 +05:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._collection.removeItem(id);
|
2020-02-06 16:46:23 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async clear() {
|
2020-02-22 21:53:56 +05:00
|
|
|
let indices = await this._collection.indexer.getIndices();
|
2020-02-06 16:46:23 +05:00
|
|
|
return this.delete(...indices);
|
|
|
|
|
}
|
|
|
|
|
}
|