2023-08-21 13:32:06 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
|
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import dayjs from "dayjs";
|
|
|
|
|
import Database from "../api";
|
|
|
|
|
|
|
|
|
|
export default class Trash {
|
|
|
|
|
collections = ["notes", "notebooks"] as const;
|
2023-09-15 20:25:04 +05:00
|
|
|
cache: string[] = [];
|
2023-08-21 13:32:06 +05:00
|
|
|
constructor(private readonly db: Database) {}
|
|
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
|
await this.cleanup();
|
2023-10-02 09:40:10 +05:00
|
|
|
const result = await this.db
|
|
|
|
|
.sql()
|
|
|
|
|
.selectNoFrom((eb) => [
|
|
|
|
|
eb
|
|
|
|
|
.selectFrom("notes")
|
|
|
|
|
.where("type", "==", "trash")
|
|
|
|
|
.select("id")
|
|
|
|
|
.as("id"),
|
|
|
|
|
eb
|
|
|
|
|
.selectFrom("notebooks")
|
|
|
|
|
.where("type", "==", "trash")
|
|
|
|
|
.select("id")
|
|
|
|
|
.as("id")
|
|
|
|
|
])
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
|
|
this.cache = result.reduce((ids, item) => {
|
|
|
|
|
if (item.id) ids.push(item.id);
|
|
|
|
|
return ids;
|
|
|
|
|
}, [] as string[]);
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async cleanup() {
|
|
|
|
|
const duration = this.db.settings.getTrashCleanupInterval();
|
|
|
|
|
if (duration === -1 || !duration) return;
|
|
|
|
|
|
2023-10-02 09:40:10 +05:00
|
|
|
const maxMs = dayjs().subtract(duration, "days").toDate().getTime();
|
|
|
|
|
const expiredItems = await this.db
|
|
|
|
|
.sql()
|
|
|
|
|
.selectNoFrom((eb) => [
|
|
|
|
|
eb
|
|
|
|
|
.selectFrom("notes")
|
|
|
|
|
.where("type", "==", "trash")
|
|
|
|
|
.where("dateDeleted", "<=", maxMs)
|
|
|
|
|
.select("id")
|
|
|
|
|
.as("noteId"),
|
|
|
|
|
eb
|
|
|
|
|
.selectFrom("notebooks")
|
|
|
|
|
.where("type", "==", "trash")
|
|
|
|
|
.where("dateDeleted", "<=", maxMs)
|
|
|
|
|
.select("id")
|
|
|
|
|
.as("notebookId")
|
|
|
|
|
])
|
|
|
|
|
.execute();
|
|
|
|
|
const { noteIds, notebookIds } = expiredItems.reduce(
|
|
|
|
|
(ids, item) => {
|
|
|
|
|
if (item.noteId) ids.noteIds.push(item.noteId);
|
|
|
|
|
if (item.notebookId) ids.notebookIds.push(item.notebookId);
|
|
|
|
|
return ids;
|
|
|
|
|
},
|
|
|
|
|
{ noteIds: [] as string[], notebookIds: [] as string[] }
|
|
|
|
|
);
|
|
|
|
|
await this.delete("note", noteIds);
|
|
|
|
|
await this.delete("note", notebookIds);
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
|
2023-10-02 09:40:10 +05:00
|
|
|
async add(type: "note" | "notebook", ids: string[]) {
|
|
|
|
|
if (type === "note") {
|
|
|
|
|
await this.db.notes.collection.update(ids, {
|
|
|
|
|
type: "trash",
|
|
|
|
|
itemType: "note",
|
|
|
|
|
dateDeleted: Date.now()
|
|
|
|
|
});
|
|
|
|
|
} else if (type === "notebook") {
|
|
|
|
|
await this.db.notebooks.collection.update(ids, {
|
|
|
|
|
type: "trash",
|
|
|
|
|
itemType: "notebook",
|
|
|
|
|
dateDeleted: Date.now()
|
|
|
|
|
});
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
2023-10-02 09:40:10 +05:00
|
|
|
this.cache.push(...ids);
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
|
2023-10-02 09:40:10 +05:00
|
|
|
async delete(type: "note" | "notebook", ids: string[]) {
|
|
|
|
|
if (type === "note") {
|
|
|
|
|
await this.db.content.removeByNoteId(...ids);
|
|
|
|
|
await this.db.noteHistory.clearSessions(...ids);
|
|
|
|
|
await this.db.notes.delete(...ids);
|
|
|
|
|
} else if (type === "notebook") {
|
|
|
|
|
await this.db.relations.unlinkOfType("notebook", ids);
|
|
|
|
|
await this.db.notebooks.delete(...ids);
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
2023-10-02 09:40:10 +05:00
|
|
|
ids.forEach((id) => this.cache.splice(this.cache.indexOf(id), 1));
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
|
2023-10-02 09:40:10 +05:00
|
|
|
async restore(type: "note" | "notebook", ids: string[]) {
|
|
|
|
|
if (type === "note") {
|
|
|
|
|
await this.db.notes.collection.update(ids, {
|
|
|
|
|
type: "note",
|
|
|
|
|
dateDeleted: null,
|
|
|
|
|
itemType: null
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
await this.db.notebooks.collection.update(ids, {
|
|
|
|
|
type: "notebook",
|
|
|
|
|
dateDeleted: null,
|
|
|
|
|
itemType: null
|
|
|
|
|
});
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
2023-10-02 09:40:10 +05:00
|
|
|
ids.forEach((id) => this.cache.splice(this.cache.indexOf(id), 1));
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async clear() {
|
2023-10-02 09:40:10 +05:00
|
|
|
// for (const item of this.all) {
|
|
|
|
|
// await this.delete(item.id);
|
|
|
|
|
// }
|
2023-09-15 20:25:04 +05:00
|
|
|
this.cache = [];
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
|
2023-10-02 09:40:10 +05:00
|
|
|
// synced(id: string) {
|
|
|
|
|
// // const [item] = this.getItem(id);
|
|
|
|
|
// if (item && item.itemType === "note") {
|
|
|
|
|
// const { contentId } = item;
|
|
|
|
|
// return !contentId || this.db.content.exists(contentId);
|
|
|
|
|
// } else return true;
|
|
|
|
|
// }
|
2023-08-21 13:32:06 +05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id
|
|
|
|
|
*/
|
|
|
|
|
exists(id: string) {
|
2023-09-15 20:25:04 +05:00
|
|
|
return this.cache.includes(id);
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
}
|