diff --git a/packages/core/api/index.js b/packages/core/api/index.js index 44c3acffb..c480ac0d2 100644 --- a/packages/core/api/index.js +++ b/packages/core/api/index.js @@ -17,6 +17,7 @@ import Migrations from "./migrations"; import Outbox from "./outbox"; import UserManager from "./user-manager"; import http from "../utils/http"; +import Monograph from "./monograph"; /** * @type {EventSource} @@ -69,6 +70,7 @@ class Database { this.settings = new Settings(this); this.migrations = new Migrations(this); this.outbox = new Outbox(this); + this.monograph = new Monograph(this); // collections /** @type {Notes} */ diff --git a/packages/core/api/monograph.js b/packages/core/api/monograph.js new file mode 100644 index 000000000..2780c5941 --- /dev/null +++ b/packages/core/api/monograph.js @@ -0,0 +1,77 @@ +import http from "../utils/http"; +import Constants from "../utils/constants"; + +class Monograph { + /** + * + * @param {import("./index").default} db + */ + constructor(db) { + this._db = db; + } + + async publish(noteId, opts) { + 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."); + + const note = this._db.notes.note(noteId); + if (!note) throw new Error("No such note found."); + + const content = await this._db.content.raw(note.data.contentId); + if (!content) throw new Error("This note has no content."); + + const monograph = { + id: note.publishId, + title: note.title, + noteId: noteId, + userId: user.id, + selfDestruct: opts.selfDestruct, + }; + + if (opts.password) { + monograph.encryptedContent = await this._db.context.encrypt( + { password: opts.password }, + JSON.stringify({ type: content.type, data: content.data }) + ); + } else { + monograph.content = JSON.stringify({ + type: content.type, + data: content.data, + }); + } + + const method = opts.update ? http.patch.json : http.post.json; + + const { id } = await method( + `${Constants.API_HOST}/monographs`, + monograph, + token + ); + + await this._db.notes.add({ id: note.id, publishId: id }); + } + + async unpublish(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."); + + 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."); + + 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 }); + } +} +export default Monograph; diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js index 8cdba1651..f9cf871be 100644 --- a/packages/core/collections/notes.js +++ b/packages/core/collections/notes.js @@ -98,6 +98,7 @@ export default class Notes extends Collection { dateCreated: note.dateCreated, conflicted: !!note.conflicted, localOnly: !!note.localOnly, + publishId: note.publishId, }; if (!oldNote || oldNote.deleted) { diff --git a/packages/core/utils/http.js b/packages/core/utils/http.js index fbff1d9f4..89b3d9680 100644 --- a/packages/core/utils/http.js +++ b/packages/core/utils/http.js @@ -10,6 +10,10 @@ function patch(url, data, token) { return bodyRequest(url, data, false, token, "PATCH"); } +patch.json = function (url, data, token) { + return bodyRequest(url, data, true, token, "PATCH"); +}; + function post(url, data, token) { return bodyRequest(url, data, false, token, "POST"); }