2021-06-13 10:00:24 +05:00
|
|
|
import http from "../utils/http";
|
|
|
|
|
import Constants from "../utils/constants";
|
|
|
|
|
|
|
|
|
|
class Monograph {
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {import("./index").default} db
|
|
|
|
|
*/
|
|
|
|
|
constructor(db) {
|
|
|
|
|
this._db = db;
|
2021-06-15 10:09:10 +05:00
|
|
|
this.monographs;
|
2021-06-13 10:00:24 +05:00
|
|
|
}
|
|
|
|
|
|
2021-06-15 10:09:10 +05:00
|
|
|
async init() {
|
|
|
|
|
const user = await this._db.user.getUser();
|
|
|
|
|
const token = await this._db.user.tokenManager.getAccessToken();
|
2021-06-15 10:21:45 +05:00
|
|
|
if (!user || !token || !user.isEmailConfirmed) return;
|
2021-06-15 10:09:10 +05:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
2021-06-13 10:00:24 +05:00
|
|
|
async publish(noteId, opts) {
|
2021-06-15 10:09:10 +05:00
|
|
|
if (!this.monographs) await this.init();
|
|
|
|
|
|
|
|
|
|
let update = !!this.isPublished(noteId);
|
|
|
|
|
|
2021-06-13 10:00:24 +05:00
|
|
|
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 = {
|
2021-06-15 10:09:10 +05:00
|
|
|
id: this.monographs[noteId],
|
2021-06-13 10:00:24 +05:00
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 10:09:10 +05:00
|
|
|
const method = update ? http.patch.json : http.post.json;
|
2021-06-13 10:00:24 +05:00
|
|
|
|
|
|
|
|
const { id } = await method(
|
|
|
|
|
`${Constants.API_HOST}/monographs`,
|
|
|
|
|
monograph,
|
|
|
|
|
token
|
|
|
|
|
);
|
|
|
|
|
|
2021-06-15 10:09:10 +05:00
|
|
|
this.monographs[noteId] = id;
|
|
|
|
|
return id;
|
2021-06-13 10:00:24 +05:00
|
|
|
}
|
|
|
|
|
|
2021-06-15 10:09:10 +05:00
|
|
|
/**
|
|
|
|
|
* Unpublish a note
|
|
|
|
|
* @param {string} noteId id of the note to unpublish
|
|
|
|
|
*/
|
2021-06-13 10:00:24 +05:00
|
|
|
async unpublish(noteId) {
|
2021-06-15 10:09:10 +05:00
|
|
|
if (!this.monographs) await this.init();
|
|
|
|
|
|
2021-06-13 10:00:24 +05:00
|
|
|
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.");
|
|
|
|
|
|
2021-06-15 10:09:10 +05:00
|
|
|
if (!this.monographs[noteId])
|
|
|
|
|
throw new Error("This note is not published.");
|
2021-06-13 10:00:24 +05:00
|
|
|
|
|
|
|
|
await http.delete(
|
|
|
|
|
`${Constants.API_HOST}/monographs/${note.publishId}`,
|
|
|
|
|
token
|
|
|
|
|
);
|
|
|
|
|
|
2021-06-15 10:09:10 +05:00
|
|
|
delete this.monographs[noteId];
|
2021-06-13 10:00:24 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default Monograph;
|