2020-02-06 16:46:23 +05:00
|
|
|
import CachedCollection from "../database/cached-collection";
|
2020-03-23 13:22:28 +05:00
|
|
|
import getId from "../utils/id";
|
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");
|
2020-02-06 16:46:23 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2020-04-12 11:04:30 +05:00
|
|
|
* @param {import('./notes').default} notes
|
|
|
|
|
* @param {import('./notebooks').default} notebooks
|
|
|
|
|
* @param {import('./content').default} delta
|
|
|
|
|
* @param {import('./content').default} text
|
2020-02-06 16:46:23 +05:00
|
|
|
*/
|
2020-03-21 10:45:53 +05:00
|
|
|
async init(notes, notebooks, delta, text) {
|
2020-02-22 21:53:56 +05:00
|
|
|
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;
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._collection.init();
|
2020-03-11 12:11:59 +05:00
|
|
|
await this.cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async cleanup() {
|
|
|
|
|
const sevenDayPreviousTimestamp = Date.now() - get7DayTimestamp();
|
2020-04-12 11:04:30 +05:00
|
|
|
this.all.forEach(async (item) => {
|
2020-03-11 12:11:59 +05:00
|
|
|
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() {
|
2020-04-12 11:04:30 +05:00
|
|
|
return this._collection.getAllItems((u) => u.dateDeleted);
|
2020-02-06 16:46:23 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async add(item) {
|
2020-03-24 13:02:16 +05:00
|
|
|
if (item.dateDeleted || item.deleted) {
|
2020-03-23 15:40:22 +05:00
|
|
|
return await this._collection.addItem(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-04-12 11:04:30 +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
|
|
|
}
|
2020-02-22 21:53:56 +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 = {};
|
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;
|
|
|
|
|
|
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) {
|
2020-04-12 11:04:30 +05:00
|
|
|
await this._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 = [];
|
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-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() {
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|