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

117 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-02-06 16:46:23 +05:00
import CachedCollection from "../database/cached-collection";
import Notes from "./notes";
import Notebooks from "./notebooks";
2020-03-21 10:45:53 +05:00
import Content from "./content";
2020-03-23 13:22:28 +05:00
import getId from "../utils/id";
import { get7DayTimestamp } from "../utils/date";
2020-02-06 16:46:23 +05:00
export default class Trash {
constructor(context) {
this._collection = new CachedCollection(context, "trash");
2020-02-06 16:46:23 +05:00
}
/**
*
* @param {Notes} notes
* @param {Notebooks} notebooks
2020-03-21 10:45:53 +05:00
* @param {Content} delta
* @param {Content} text
2020-02-06 16:46:23 +05:00
*/
2020-03-21 10:45:53 +05:00
async init(notes, notebooks, delta, text) {
this._notes = notes;
this._notebooks = notebooks;
2020-03-19 11:30:05 +05:00
this._deltaCollection = delta;
2020-03-21 10:45:53 +05:00
this._textCollection = text;
await this._collection.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
}
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 22:03:09 +05:00
await this._collection.addItem({
...item,
2020-03-23 13:22:28 +05:00
id: getId(),
itemId: item.id,
2020-03-19 11:30:05 +05:00
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.type === "note") {
await this._deltaCollection.remove(item.content.delta);
2020-03-21 10:45:53 +05:00
await this._textCollection.remove(item.content.text);
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;
delete item.itemId;
2020-02-06 16:46:23 +05:00
if (item.type === "note") {
let { notebook } = item;
item.notebook = {};
await this._notes.add(item);
2020-02-06 16:46:23 +05:00
if (notebook && notebook.id && notebook.topic) {
const { id, topic } = notebook;
2020-03-23 13:22:28 +05:00
// if the notebook or topic has been deleted
if (
!this._notebooks._collection.exists(id) ||
!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) {
await this._notebooks
2020-02-06 16:46:23 +05:00
.notebook(id)
.topics.topic(topic)
.add(item.id);
}
}
} else if (item.type === "notebook") {
2020-02-06 16:46:23 +05:00
const { topics } = item;
item.topics = [];
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-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);
}
}