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

182 lines
5.8 KiB
JavaScript
Raw Normal View History

2022-08-30 16:13:11 +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/>.
*/
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";
import { CHECK_IDS, checkIsUserPremium } from "../common";
2021-01-11 12:23:12 +05:00
import { qclone } from "qclone";
2021-08-16 11:29:18 +05:00
import setManipulator from "../utils/set";
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) {
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) {
const lastSyncedTimestamp = await this._db.lastSynced();
let isChanged = false;
// merge new and old topics
2021-02-25 19:41:17 +05:00
// We need to handle 3 cases:
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.
// 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.
if (!newTopic && oldTopic.dateEdited > lastSyncedTimestamp) {
2021-02-25 21:01:19 +05:00
remoteNotebook.topics.push({ ...oldTopic, dateEdited: Date.now() });
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,
2021-11-11 13:06:07 +05:00
notes: setManipulator.union(oldTopic.notes, newTopic.notes),
2021-02-25 21:01:19 +05:00
dateEdited: Date.now(),
};
isChanged = true;
2021-02-25 19:41:17 +05:00
}
// CASE 4: if topic exists in both notebooks:
// if newTopic.dateEdited > oldTopic.dateEdited: we iterate
// on all notes that are not in newTopic (if any)
// and dereference them.
else if (newTopic && newTopic.dateEdited > oldTopic.dateEdited) {
const removedNotes = setManipulator.complement(
oldTopic.notes,
newTopic.notes
);
await this.notebook(remoteNotebook.id)
.topics.topic(oldTopic.id)
.delete(...removedNotes);
}
2021-02-25 19:41:17 +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();
let oldNotebook = this._collection.getItem(id);
2020-02-04 18:27:32 +05:00
if (
!oldNotebook &&
this.all.length >= 3 &&
!(await checkIsUserPremium(CHECK_IDS.notebookAdd))
)
return;
2020-02-04 18:27:32 +05:00
let notebook = {
...oldNotebook,
2020-04-09 16:36:57 +05:00
...notebookArg,
2020-02-04 18:27:32 +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,
dateEdited: Date.now(),
2020-02-04 18:27:32 +05:00
};
await this._collection.addItem(notebook);
2020-02-06 18:47:42 +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() {
return this._collection.getItems();
2020-02-04 18:27:32 +05:00
}
get pinned() {
return this.all.filter((item) => item.pinned === true);
}
2021-02-16 16:56:06 +05:00
get deleted() {
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) {
let notebook = id.type ? id : this._collection.getItem(id);
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
}
async delete(...ids) {
for (let id of ids) {
2020-02-05 20:57:43 +05:00
let notebook = this.notebook(id);
if (!notebook) continue;
2021-01-11 12:23:12 +05:00
const notebookData = qclone(notebook.data);
await notebook.topics.delete(...notebook.data.topics);
2020-03-23 13:22:28 +05:00
await this._collection.removeItem(id);
await this._db.settings.unpin(id);
2021-01-11 12:23:12 +05:00
await this._db.trash.add(notebookData);
}
}
async repairReferences() {
for (let notebook of this.all) {
const _notebook = this.notebook(notebook);
for (let topic of notebook.topics) {
const _topic = _notebook.topics.topic(topic.id);
await _topic.add(...topic.notes);
}
}
}
2020-02-04 18:27:32 +05:00
}