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

148 lines
4.5 KiB
JavaScript
Raw Normal View History

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";
2020-12-05 13:19:41 +05:00
import { CHECK_IDS, sendCheckUserStatusEvent } from "../common";
2021-01-11 12:23:12 +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) {
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();
2021-02-25 19:41:17 +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.
// 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() });
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:
// if oldTopic.dateEdited > newTopic.dateEdited: we keep oldTopic.
2021-02-25 21:01:19 +05:00
else if (newTopic && oldTopic.dateEdited > newTopic.dateEdited) {
remoteNotebook.topics[newTopicIndex] = {
...oldTopic,
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();
let oldNotebook = this._collection.getItem(id);
2020-02-04 18:27:32 +05:00
if (
!oldNotebook &&
this.all.length >= 3 &&
2020-12-05 13:19:41 +05:00
!(await sendCheckUserStatusEvent(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,
dateCreated: notebook.dateCreated,
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
};
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 sort(this._collection.getItems()).desc((t) => t.pinned);
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 cleanup() {
await this._db.notes.init();
for (let notebook of this.all) {
for (let topic of notebook.topics) {
const clonedIds = topic.notes.slice();
topic.notes = [];
for (let noteId of clonedIds) {
if (!!this._db.notes.note(noteId)) topic.notes.push(noteId);
}
}
2021-08-16 11:11:22 +05:00
await this._collection.updateItem(notebook);
}
}
2020-02-04 18:27:32 +05:00
}