Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: use an internal monographs cache

thecodrra41e6c9f4f2f
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index c480ac0d2..4afb74109 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -90,6 +90,7 @@ class Database {
await this.settings.init();
await this.outbox.init();
await this.user.init();
+ await this.monograph.init();
await this.migrations.init();
await this.migrations.migrate();
diff --git a/packages/core/api/monograph.js b/packages/core/api/monograph.js
index 2780c5941..965b7f2a4 100644
--- a/packages/core/api/monograph.js
+++ b/packages/core/api/monograph.js
@@ -8,9 +8,50 @@ class Monograph {
*/
constructor(db) {
this._db = db;
+ this.monographs;
}
+ async init() {
+ const user = await this._db.user.getUser();
+ const token = await this._db.user.tokenManager.getAccessToken();
+ if (!user || !token) return;
+
+ const userMonographs = await http.get(`${Constants.API_HOST}/monographs`);
+ this.monographs = userMonographs.reduce((prev, curr) => {
+ prev[curr.noteId] = curr.id;
+ return prev;
+ }, {});
+ }
+
+ /**
+ * Check if note is published.
+ * @param {string} noteId id of the note
+ * @returns {boolean} Whether note is published or not.
+ */
+ async isPublished(noteId) {
+ return !!this.monographs[noteId];
+ }
+
+ /**
+ * Get note published monograph id
+ * @param {string} noteId id of the note
+ * @returns Monograph Id
+ */
+ async monograph(noteId) {
+ return this.monographs[noteId];
+ }
+
+ /**
+ * Publish a note as a monograph
+ * @param {string} noteId id of the note to publish
+ * @param {{password: string, selfDestruct: boolean}} opts Publish options
+ * @returns
+ */
async publish(noteId, opts) {
+ if (!this.monographs) await this.init();
+
+ let update = !!this.isPublished(noteId);
+
const user = await this._db.user.getUser();
const token = await this._db.user.tokenManager.getAccessToken();
if (!user || !token) throw new Error("Please login to publish a note.");
@@ -22,7 +63,7 @@ class Monograph {
if (!content) throw new Error("This note has no content.");
const monograph = {
- id: note.publishId,
+ id: this.monographs[noteId],
title: note.title,
noteId: noteId,
userId: user.id,
@@ -41,7 +82,7 @@ class Monograph {
});
}
- const method = opts.update ? http.patch.json : http.post.json;
+ const method = update ? http.patch.json : http.post.json;
const { id } = await method(
`${Constants.API_HOST}/monographs`,
@@ -49,10 +90,17 @@ class Monograph {
token
);
- await this._db.notes.add({ id: note.id, publishId: id });
+ this.monographs[noteId] = id;
+ return id;
}
+ /**
+ * Unpublish a note
+ * @param {string} noteId id of the note to unpublish
+ */
async unpublish(noteId) {
+ if (!this.monographs) await this.init();
+
const user = await this._db.user.getUser();
const token = await this._db.user.tokenManager.getAccessToken();
if (!user || !token) throw new Error("Please login to publish a note.");
@@ -60,18 +108,15 @@ class Monograph {
const note = this._db.notes.note(noteId);
if (!note) throw new Error("No such note found.");
- if (!note.publishId) throw new Error("This note is not published.");
+ if (!this.monographs[noteId])
+ throw new Error("This note is not published.");
await http.delete(
`${Constants.API_HOST}/monographs/${note.publishId}`,
token
);
- await this._db.notes.add({ id: note.id, publishId: undefined });
- }
-
- update(noteId, opts) {
- return this.publish(noteId, { ...opts, update: true });
+ delete this.monographs[noteId];
}
}
export default Monograph;
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index f9cf871be..8cdba1651 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -98,7 +98,6 @@ export default class Notes extends Collection {
dateCreated: note.dateCreated,
conflicted: !!note.conflicted,
localOnly: !!note.localOnly,
- publishId: note.publishId,
};
if (!oldNote || oldNote.deleted) {