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";
|
2023-10-07 13:26:03 +05:00
|
|
|
import { deleteItems } from "../utils/array";
|
2023-11-07 08:14:45 +05:00
|
|
|
import { GroupOptions, TrashItem } from "../types";
|
|
|
|
|
import { VirtualizedGrouping } from "../utils/virtualized-grouping";
|
2023-12-26 13:15:03 +05:00
|
|
|
import { getSortSelectors, groupArray } from "../utils/grouping";
|
|
|
|
|
import { sql } from "kysely";
|
2023-08-21 13:32:06 +05:00
|
|
|
|
|
|
|
|
export default class Trash {
|
|
|
|
|
collections = ["notes", "notebooks"] as const;
|
2023-10-07 13:26:03 +05:00
|
|
|
cache: {
|
|
|
|
|
notes: string[];
|
|
|
|
|
notebooks: string[];
|
|
|
|
|
} = {
|
|
|
|
|
notebooks: [],
|
|
|
|
|
notes: []
|
|
|
|
|
};
|
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()
|
2023-12-26 13:15:03 +05:00
|
|
|
.selectFrom("notes")
|
|
|
|
|
.where("type", "==", "trash")
|
|
|
|
|
.select(["id", sql`'note'`.as("itemType")])
|
|
|
|
|
.unionAll((eb) =>
|
2023-10-02 09:40:10 +05:00
|
|
|
eb
|
|
|
|
|
.selectFrom("notebooks")
|
|
|
|
|
.where("type", "==", "trash")
|
2023-12-26 13:15:03 +05:00
|
|
|
.select(["id", sql`'notebook'`.as("itemType")])
|
|
|
|
|
)
|
2023-10-02 09:40:10 +05:00
|
|
|
.execute();
|
|
|
|
|
|
2023-12-26 13:15:03 +05:00
|
|
|
for (const { id, itemType } of result) {
|
|
|
|
|
if (itemType === "note") this.cache.notes.push(id);
|
|
|
|
|
else if (itemType === "notebook") this.cache.notebooks.push(id);
|
2023-10-07 13:26:03 +05:00
|
|
|
}
|
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[] }
|
|
|
|
|
);
|
2023-10-07 13:26:03 +05:00
|
|
|
|
|
|
|
|
await this._delete(noteIds, notebookIds);
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
|
2024-02-05 16:14:33 +05:00
|
|
|
async add(
|
|
|
|
|
type: "note" | "notebook",
|
|
|
|
|
ids: string[],
|
|
|
|
|
deletedBy: TrashItem["deletedBy"] = "user"
|
|
|
|
|
) {
|
2023-10-02 09:40:10 +05:00
|
|
|
if (type === "note") {
|
|
|
|
|
await this.db.notes.collection.update(ids, {
|
|
|
|
|
type: "trash",
|
|
|
|
|
itemType: "note",
|
2024-02-05 16:14:33 +05:00
|
|
|
dateDeleted: Date.now(),
|
|
|
|
|
deletedBy
|
2023-10-02 09:40:10 +05:00
|
|
|
});
|
2023-10-07 13:26:03 +05:00
|
|
|
this.cache.notes.push(...ids);
|
2023-10-02 09:40:10 +05:00
|
|
|
} else if (type === "notebook") {
|
|
|
|
|
await this.db.notebooks.collection.update(ids, {
|
|
|
|
|
type: "trash",
|
|
|
|
|
itemType: "notebook",
|
2024-02-05 16:14:33 +05:00
|
|
|
dateDeleted: Date.now(),
|
|
|
|
|
deletedBy
|
2023-10-02 09:40:10 +05:00
|
|
|
});
|
2023-10-07 13:26:03 +05:00
|
|
|
this.cache.notebooks.push(...ids);
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 08:14:45 +05:00
|
|
|
async delete(...ids: string[]) {
|
|
|
|
|
if (ids.length <= 0) return;
|
2023-10-07 13:26:03 +05:00
|
|
|
|
|
|
|
|
const noteIds = [];
|
|
|
|
|
const notebookIds = [];
|
2023-11-07 08:14:45 +05:00
|
|
|
for (const id of ids) {
|
|
|
|
|
const isNote = this.cache.notes.includes(id);
|
|
|
|
|
if (isNote) {
|
|
|
|
|
noteIds.push(id);
|
|
|
|
|
this.cache.notes.splice(this.cache.notes.indexOf(id), 1);
|
|
|
|
|
} else if (!isNote) {
|
|
|
|
|
notebookIds.push(id);
|
|
|
|
|
this.cache.notebooks.splice(this.cache.notebooks.indexOf(id), 1);
|
2023-10-07 13:26:03 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this._delete(noteIds, notebookIds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async _delete(noteIds: string[], notebookIds: string[]) {
|
|
|
|
|
if (noteIds.length > 0) {
|
|
|
|
|
await this.db.content.removeByNoteId(...noteIds);
|
|
|
|
|
await this.db.noteHistory.clearSessions(...noteIds);
|
|
|
|
|
await this.db.notes.remove(...noteIds);
|
|
|
|
|
deleteItems(this.cache.notes, ...noteIds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (notebookIds.length > 0) {
|
2024-02-05 16:14:33 +05:00
|
|
|
const ids = [...notebookIds, ...(await this.subNotebooks(notebookIds))];
|
|
|
|
|
await this.db.notebooks.remove(...ids);
|
|
|
|
|
await this.db.relations.unlinkOfType("notebook", ids);
|
|
|
|
|
deleteItems(this.cache.notebooks, ...ids);
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 08:14:45 +05:00
|
|
|
async restore(...ids: string[]) {
|
|
|
|
|
if (ids.length <= 0) return;
|
2023-10-07 13:26:03 +05:00
|
|
|
|
|
|
|
|
const noteIds = [];
|
|
|
|
|
const notebookIds = [];
|
2023-11-07 08:14:45 +05:00
|
|
|
for (const id of ids) {
|
|
|
|
|
const isNote = this.cache.notes.includes(id);
|
|
|
|
|
if (isNote) {
|
|
|
|
|
noteIds.push(id);
|
2024-02-05 16:14:33 +05:00
|
|
|
// this.cache.notes.splice(this.cache.notes.indexOf(id), 1);
|
2023-11-07 08:14:45 +05:00
|
|
|
} else if (!isNote) {
|
|
|
|
|
notebookIds.push(id);
|
2024-02-05 16:14:33 +05:00
|
|
|
// this.cache.notebooks.splice(this.cache.notebooks.indexOf(id), 1);
|
2023-10-07 13:26:03 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (noteIds.length > 0) {
|
|
|
|
|
await this.db.notes.collection.update(noteIds, {
|
2023-10-02 09:40:10 +05:00
|
|
|
type: "note",
|
|
|
|
|
dateDeleted: null,
|
2024-02-05 16:14:33 +05:00
|
|
|
itemType: null,
|
|
|
|
|
deletedBy: null
|
2023-10-02 09:40:10 +05:00
|
|
|
});
|
2024-02-05 16:14:33 +05:00
|
|
|
deleteItems(this.cache.notes, ...noteIds);
|
2023-10-07 13:26:03 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (notebookIds.length > 0) {
|
2024-02-05 16:14:33 +05:00
|
|
|
const ids = [...notebookIds, ...(await this.subNotebooks(notebookIds))];
|
|
|
|
|
await this.db.notebooks.collection.update(ids, {
|
2023-10-02 09:40:10 +05:00
|
|
|
type: "notebook",
|
|
|
|
|
dateDeleted: null,
|
2024-02-05 16:14:33 +05:00
|
|
|
itemType: null,
|
|
|
|
|
deletedBy: null
|
2023-10-02 09:40:10 +05:00
|
|
|
});
|
2024-02-05 16:14:33 +05:00
|
|
|
deleteItems(this.cache.notebooks, ...ids);
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async clear() {
|
2023-10-07 13:26:03 +05:00
|
|
|
await this._delete(this.cache.notes, this.cache.notebooks);
|
|
|
|
|
this.cache = { notebooks: [], notes: [] };
|
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
|
|
|
|
2024-02-05 22:05:55 +05:00
|
|
|
async all(deletedBy?: TrashItem["deletedBy"]) {
|
2023-12-26 13:15:03 +05:00
|
|
|
return [
|
2024-02-05 22:05:55 +05:00
|
|
|
...(await this.trashedNotes(this.cache.notes, deletedBy)),
|
|
|
|
|
...(await this.trashedNotebooks(this.cache.notebooks, deletedBy))
|
2023-12-26 13:15:03 +05:00
|
|
|
] as TrashItem[];
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 22:05:55 +05:00
|
|
|
private async trashedNotes(
|
|
|
|
|
ids: string[],
|
|
|
|
|
deletedBy?: TrashItem["deletedBy"]
|
|
|
|
|
) {
|
2023-12-26 13:15:03 +05:00
|
|
|
return (await this.db
|
2023-10-07 13:26:03 +05:00
|
|
|
.sql()
|
|
|
|
|
.selectFrom("notes")
|
|
|
|
|
.where("type", "==", "trash")
|
2023-12-26 13:15:03 +05:00
|
|
|
.where("id", "in", ids)
|
2024-02-05 22:05:55 +05:00
|
|
|
.$if(!!deletedBy, (eb) => eb.where("deletedBy", "==", deletedBy))
|
2023-10-07 13:26:03 +05:00
|
|
|
.selectAll()
|
2023-12-26 13:15:03 +05:00
|
|
|
.execute()) as TrashItem[];
|
|
|
|
|
}
|
2023-10-07 13:26:03 +05:00
|
|
|
|
2024-02-05 22:05:55 +05:00
|
|
|
private async trashedNotebooks(
|
|
|
|
|
ids: string[],
|
|
|
|
|
deletedBy?: TrashItem["deletedBy"]
|
|
|
|
|
) {
|
2023-12-26 13:15:03 +05:00
|
|
|
return (await this.db
|
2023-10-07 13:26:03 +05:00
|
|
|
.sql()
|
|
|
|
|
.selectFrom("notebooks")
|
|
|
|
|
.where("type", "==", "trash")
|
2023-12-26 13:15:03 +05:00
|
|
|
.where("id", "in", ids)
|
2024-02-05 22:05:55 +05:00
|
|
|
.$if(!!deletedBy, (eb) => eb.where("deletedBy", "==", deletedBy))
|
2023-10-07 13:26:03 +05:00
|
|
|
.selectAll()
|
2023-12-26 13:15:03 +05:00
|
|
|
.execute()) as TrashItem[];
|
2023-10-07 13:26:03 +05:00
|
|
|
}
|
|
|
|
|
|
2023-11-07 08:14:45 +05:00
|
|
|
async grouped(options: GroupOptions) {
|
2023-12-26 13:15:03 +05:00
|
|
|
const ids = [...this.cache.notes, ...this.cache.notebooks];
|
|
|
|
|
const selector = getSortSelectors(options)[options.sortDirection];
|
2023-11-07 08:14:45 +05:00
|
|
|
return new VirtualizedGrouping<TrashItem>(
|
2023-12-26 13:15:03 +05:00
|
|
|
ids.length,
|
2023-12-05 15:34:18 +05:00
|
|
|
this.db.options.batchSize,
|
2023-12-26 13:15:03 +05:00
|
|
|
() => Promise.resolve(ids),
|
2023-12-05 15:34:18 +05:00
|
|
|
async (start, end) => {
|
2023-12-26 13:15:03 +05:00
|
|
|
const notesRange =
|
|
|
|
|
end < this.cache.notes.length
|
|
|
|
|
? [start, end]
|
|
|
|
|
: [start, this.cache.notes.length];
|
|
|
|
|
const notebooksRange =
|
|
|
|
|
start >= this.cache.notes.length
|
|
|
|
|
? [start, end]
|
|
|
|
|
: [0, Math.min(this.cache.notebooks.length, end)];
|
|
|
|
|
|
|
|
|
|
const items = [
|
|
|
|
|
...(await this.trashedNotes(
|
2024-02-05 22:05:55 +05:00
|
|
|
this.cache.notes.slice(notesRange[0], notesRange[1]),
|
|
|
|
|
"user"
|
2023-12-26 13:15:03 +05:00
|
|
|
)),
|
|
|
|
|
...(await this.trashedNotebooks(
|
2024-02-05 22:05:55 +05:00
|
|
|
this.cache.notebooks.slice(notebooksRange[0], notebooksRange[1]),
|
|
|
|
|
"user"
|
2023-12-26 13:15:03 +05:00
|
|
|
))
|
|
|
|
|
];
|
|
|
|
|
items.sort(selector);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
ids: ids.slice(start, end),
|
|
|
|
|
items
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
(items) => groupArray(items, options),
|
|
|
|
|
async () => {
|
|
|
|
|
const items = await this.all();
|
|
|
|
|
items.sort(selector);
|
|
|
|
|
return Array.from(groupArray(items, options).values());
|
2023-11-07 08:14:45 +05:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-21 13:32:06 +05:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id
|
|
|
|
|
*/
|
|
|
|
|
exists(id: string) {
|
2023-10-07 13:26:03 +05:00
|
|
|
return this.cache.notebooks.includes(id) || this.cache.notes.includes(id);
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|
2024-02-05 16:14:33 +05:00
|
|
|
|
|
|
|
|
private async subNotebooks(notebookIds: string[]) {
|
|
|
|
|
const ids = await this.db
|
|
|
|
|
.sql()
|
|
|
|
|
.withRecursive(`subNotebooks(id)`, (eb) =>
|
|
|
|
|
eb
|
|
|
|
|
.selectFrom((eb) =>
|
|
|
|
|
sql<{ id: string }>`(VALUES ${sql.join(
|
|
|
|
|
notebookIds.map((id) => eb.parens(sql`${id}`))
|
|
|
|
|
)})`.as("notebookIds")
|
|
|
|
|
)
|
|
|
|
|
.selectAll()
|
|
|
|
|
.unionAll((eb) =>
|
|
|
|
|
eb
|
|
|
|
|
.selectFrom(["relations", "subNotebooks", "notebooks"])
|
|
|
|
|
.select("relations.toId as id")
|
|
|
|
|
.where("toType", "==", "notebook")
|
|
|
|
|
.where("fromType", "==", "notebook")
|
|
|
|
|
.whereRef("fromId", "==", "subNotebooks.id")
|
|
|
|
|
.where(
|
|
|
|
|
(eb) =>
|
|
|
|
|
eb
|
|
|
|
|
.selectFrom("notebooks")
|
|
|
|
|
.whereRef("notebooks.id", "==", "relations.toId")
|
|
|
|
|
.where("notebooks.type", "==", "trash")
|
|
|
|
|
.limit(1)
|
|
|
|
|
.select("deletedBy"),
|
|
|
|
|
"!=",
|
|
|
|
|
"user"
|
|
|
|
|
)
|
|
|
|
|
.$narrowType<{ id: string }>()
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.selectFrom("subNotebooks")
|
|
|
|
|
.select("id")
|
|
|
|
|
.execute();
|
2024-02-05 22:05:55 +05:00
|
|
|
|
|
|
|
|
return deleteItems(
|
|
|
|
|
ids.map((ref) => ref.id),
|
|
|
|
|
...notebookIds
|
|
|
|
|
);
|
2024-02-05 16:14:33 +05:00
|
|
|
}
|
2023-08-21 13:32:06 +05:00
|
|
|
}
|