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-02 15:40:20 +05:00
|
|
|
import sort from "fast-sort";
|
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";
|
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) {
|
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-02-25 19:41:17 +05:00
|
|
|
|
2021-02-25 20:23:51 +05:00
|
|
|
// 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.
|
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-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(),
|
|
|
|
|
};
|
2021-02-25 19:41:17 +05:00
|
|
|
}
|
|
|
|
|
}
|
2021-02-25 21:01:19 +05:00
|
|
|
remoteNotebook.dateEdited = Date.now(); // we update the dateEdited so it can be synced back
|
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,
|
2020-04-09 16:36:57 +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,
|
2020-02-11 11:10:08 +05:00
|
|
|
dateCreated: notebook.dateCreated,
|
2021-02-13 14:24:02 +05:00
|
|
|
dateEdited: notebook.dateEdited,
|
2020-02-04 18:27:32 +05:00
|
|
|
pinned: !!notebook.pinned,
|
2020-02-06 18:47:42 +05:00
|
|
|
topics: notebook.topics || [],
|
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() {
|
2020-11-04 10:42:19 +05:00
|
|
|
return sort(this._collection.getItems()).desc((t) => t.pinned);
|
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);
|
2020-11-04 10:28:52 +05:00
|
|
|
await notebook.topics.delete(...notebook.data.topics);
|
2020-03-23 13:22:28 +05:00
|
|
|
await this._collection.removeItem(id);
|
2021-01-03 10:52:05 +05:00
|
|
|
await this._db.settings.unpin(id);
|
2021-01-11 12:23:12 +05:00
|
|
|
await this._db.trash.add(notebookData);
|
2020-02-05 01:12:36 +05:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-12 11:47:03 +05:00
|
|
|
|
|
|
|
|
async repairReferences() {
|
|
|
|
|
for (let notebook of this.all) {
|
2021-12-01 15:00:36 +05:00
|
|
|
const _notebook = this.notebook(notebook);
|
2021-11-12 11:47:03 +05:00
|
|
|
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
|
|
|
}
|