2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
|
|
|
|
Copyright (C) 2022 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/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2020-04-15 23:45:56 +05:00
|
|
|
import Collection from "./collection";
|
2020-02-05 20:57:43 +05:00
|
|
|
import Notebook from "../models/notebook";
|
2020-03-19 11:30:05 +05:00
|
|
|
import getId from "../utils/id";
|
2021-10-26 23:05:47 +05:00
|
|
|
import { CHECK_IDS, checkIsUserPremium } from "../common";
|
2022-10-19 12:10:31 +05:00
|
|
|
import qclone from "qclone";
|
2020-02-04 18:27:32 +05:00
|
|
|
|
2020-04-15 23:45:56 +05:00
|
|
|
export default class Notebooks extends Collection {
|
2021-02-25 19:41:17 +05:00
|
|
|
async merge(remoteNotebook) {
|
2021-06-03 09:11:16 +05:00
|
|
|
if (remoteNotebook.deleted)
|
|
|
|
|
return await this._collection.addItem(remoteNotebook);
|
|
|
|
|
|
2021-02-25 19:41:17 +05:00
|
|
|
const id = remoteNotebook.id || getId();
|
|
|
|
|
let localNotebook = this._collection.getItem(id);
|
|
|
|
|
|
|
|
|
|
if (localNotebook && localNotebook.topics?.length) {
|
2021-02-25 20:23:51 +05:00
|
|
|
const lastSyncedTimestamp = await this._db.lastSynced();
|
2021-12-15 15:24:05 +05:00
|
|
|
let isChanged = false;
|
2021-02-25 20:23:51 +05:00
|
|
|
// merge new and old topics
|
2021-02-25 19:41:17 +05:00
|
|
|
for (let oldTopic of localNotebook.topics) {
|
|
|
|
|
const newTopicIndex = remoteNotebook.topics.findIndex(
|
|
|
|
|
(t) => t.id === oldTopic.id
|
|
|
|
|
);
|
|
|
|
|
const newTopic = remoteNotebook.topics[newTopicIndex];
|
|
|
|
|
|
|
|
|
|
// CASE 1: if topic exists in old notebook but not in new notebook, it's deleted.
|
2021-02-25 20:23:51 +05:00
|
|
|
// However, if the dateEdited of topic in the old notebook is > lastSyncedTimestamp
|
2021-02-25 19:41:17 +05:00
|
|
|
// it was newly added or edited so add it to the new notebook.
|
2021-02-25 20:23:51 +05:00
|
|
|
if (!newTopic && oldTopic.dateEdited > lastSyncedTimestamp) {
|
2021-02-25 21:01:19 +05:00
|
|
|
remoteNotebook.topics.push({ ...oldTopic, dateEdited: Date.now() });
|
2021-12-15 15:24:05 +05:00
|
|
|
isChanged = true;
|
2021-02-25 19:41:17 +05:00
|
|
|
}
|
2020-04-09 16:36:57 +05:00
|
|
|
|
2021-02-25 19:41:17 +05:00
|
|
|
// CASE 2: if topic exists in new notebook but not in old notebook, it's new.
|
|
|
|
|
// This case will be automatically handled as the new notebook is our source of truth.
|
|
|
|
|
|
|
|
|
|
// CASE 3: if topic exists in both notebooks:
|
2021-08-16 11:29:18 +05:00
|
|
|
// if oldTopic.dateEdited > newTopic.dateEdited: we keep oldTopic
|
|
|
|
|
// and merge the notes of both topics.
|
2021-02-25 21:01:19 +05:00
|
|
|
else if (newTopic && oldTopic.dateEdited > newTopic.dateEdited) {
|
|
|
|
|
remoteNotebook.topics[newTopicIndex] = {
|
|
|
|
|
...oldTopic,
|
2022-08-31 06:33:37 +05:00
|
|
|
dateEdited: Date.now()
|
2021-02-25 21:01:19 +05:00
|
|
|
};
|
2021-12-15 15:24:05 +05:00
|
|
|
isChanged = true;
|
2021-02-25 19:41:17 +05:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-13 12:21:00 +05:00
|
|
|
remoteNotebook.remote = !isChanged;
|
2020-04-09 16:36:57 +05:00
|
|
|
}
|
2021-02-25 19:41:17 +05:00
|
|
|
return await this._collection.addItem(remoteNotebook);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async add(notebookArg) {
|
|
|
|
|
if (!notebookArg) throw new Error("Notebook cannot be undefined or null.");
|
|
|
|
|
if (notebookArg.remote)
|
|
|
|
|
throw new Error(
|
|
|
|
|
"Please use db.notebooks.merge to merge remote notebooks"
|
|
|
|
|
);
|
2020-04-09 16:36:57 +05:00
|
|
|
|
2020-02-04 18:27:32 +05:00
|
|
|
//TODO reliably and efficiently check for duplicates.
|
2020-03-18 14:06:20 +05:00
|
|
|
const id = notebookArg.id || getId();
|
2020-02-22 21:53:56 +05:00
|
|
|
let oldNotebook = this._collection.getItem(id);
|
2020-02-04 18:27:32 +05:00
|
|
|
|
2020-11-23 15:33:00 +05:00
|
|
|
if (
|
|
|
|
|
!oldNotebook &&
|
|
|
|
|
this.all.length >= 3 &&
|
2021-10-26 23:05:47 +05:00
|
|
|
!(await checkIsUserPremium(CHECK_IDS.notebookAdd))
|
2020-11-23 15:33:00 +05:00
|
|
|
)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-02-04 18:27:32 +05:00
|
|
|
let notebook = {
|
|
|
|
|
...oldNotebook,
|
2022-08-31 06:33:37 +05:00
|
|
|
...notebookArg
|
2020-02-04 18:27:32 +05:00
|
|
|
};
|
|
|
|
|
|
2021-02-25 18:27:10 +05:00
|
|
|
if (!notebook.title) throw new Error("Notebook must contain a title.");
|
|
|
|
|
|
2020-02-04 18:27:32 +05:00
|
|
|
notebook = {
|
|
|
|
|
id,
|
|
|
|
|
type: "notebook",
|
|
|
|
|
title: notebook.title,
|
|
|
|
|
description: notebook.description,
|
|
|
|
|
pinned: !!notebook.pinned,
|
2020-02-06 18:47:42 +05:00
|
|
|
topics: notebook.topics || [],
|
2021-12-20 14:37:06 +05:00
|
|
|
|
|
|
|
|
dateCreated: notebook.dateCreated,
|
|
|
|
|
dateModified: notebook.dateModified,
|
2022-08-31 06:33:37 +05:00
|
|
|
dateEdited: Date.now()
|
2020-02-04 18:27:32 +05:00
|
|
|
};
|
|
|
|
|
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._collection.addItem(notebook);
|
2020-02-06 18:47:42 +05:00
|
|
|
|
2020-02-22 23:41:47 +05:00
|
|
|
if (!oldNotebook) {
|
|
|
|
|
await this.notebook(notebook).topics.add(...notebook.topics);
|
|
|
|
|
}
|
2020-04-09 16:36:57 +05:00
|
|
|
return id;
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
2020-03-23 15:06:12 +05:00
|
|
|
get raw() {
|
|
|
|
|
return this._collection.getRaw();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 18:27:32 +05:00
|
|
|
get all() {
|
2022-06-16 16:01:08 +05:00
|
|
|
return this._collection.getItems();
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 12:34:57 +05:00
|
|
|
get pinned() {
|
2021-02-16 21:38:32 +05:00
|
|
|
return this.all.filter((item) => item.pinned === true);
|
2020-03-02 12:34:57 +05:00
|
|
|
}
|
|
|
|
|
|
2021-02-16 16:56:06 +05:00
|
|
|
get deleted() {
|
2021-02-16 21:38:32 +05:00
|
|
|
return this.raw.filter((item) => item.dateDeleted > 0);
|
2021-02-16 16:56:06 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 22:46:57 +05:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {string} id The id of the notebook
|
|
|
|
|
* @returns {Notebook} The notebook of the given id
|
|
|
|
|
*/
|
2020-02-05 20:57:43 +05:00
|
|
|
notebook(id) {
|
2020-02-22 23:41:47 +05:00
|
|
|
let notebook = id.type ? id : this._collection.getItem(id);
|
2020-12-08 12:42:50 +05:00
|
|
|
if (!notebook || notebook.deleted) return;
|
2020-04-16 02:14:53 +05:00
|
|
|
return new Notebook(notebook, this._db);
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-05 01:12:36 +05:00
|
|
|
async delete(...ids) {
|
|
|
|
|
for (let id of ids) {
|
2020-02-05 20:57:43 +05:00
|
|
|
let notebook = this.notebook(id);
|
2020-02-05 01:12:36 +05:00
|
|
|
if (!notebook) continue;
|
2021-01-11 12:23:12 +05:00
|
|
|
const notebookData = qclone(notebook.data);
|
2022-09-06 22:57:54 +05:00
|
|
|
// await notebook.topics.delete(...notebook.data.topics);
|
2020-03-23 13:22:28 +05:00
|
|
|
await this._collection.removeItem(id);
|
2022-09-07 14:13:26 +05:00
|
|
|
await this._db.shortcuts.remove(id);
|
2021-01-11 12:23:12 +05:00
|
|
|
await this._db.trash.add(notebookData);
|
2020-02-05 01:12:36 +05:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-04 18:27:32 +05:00
|
|
|
}
|