2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2021-06-13 10:00:24 +05:00
|
|
|
import http from "../utils/http";
|
|
|
|
|
import Constants from "../utils/constants";
|
|
|
|
|
|
2021-06-15 11:57:00 +05:00
|
|
|
class Monographs {
|
2021-06-13 10:00:24 +05:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {import("./index").default} db
|
|
|
|
|
*/
|
|
|
|
|
constructor(db) {
|
|
|
|
|
this._db = db;
|
2022-08-15 10:57:25 +05:00
|
|
|
this.monographs = undefined;
|
2021-06-13 10:00:24 +05:00
|
|
|
}
|
|
|
|
|
|
2021-06-16 11:53:05 +05:00
|
|
|
async deinit() {
|
2022-08-15 10:57:25 +05:00
|
|
|
this.monographs = undefined;
|
2023-08-21 13:32:06 +05:00
|
|
|
await this._db.storage().write("monographs", this.monographs);
|
2021-06-16 11:53:05 +05:00
|
|
|
}
|
|
|
|
|
|
2021-06-15 10:09:10 +05:00
|
|
|
async init() {
|
2021-06-15 10:56:22 +05:00
|
|
|
try {
|
2022-07-08 12:07:26 +05:00
|
|
|
const user = await this._db.user.getUser();
|
|
|
|
|
const token = await this._db.user.tokenManager.getAccessToken();
|
|
|
|
|
if (!user || !token || !user.isEmailConfirmed) return;
|
2023-08-21 13:32:06 +05:00
|
|
|
let monographs = await this._db.storage().read("monographs", true);
|
2021-09-13 09:26:25 +05:00
|
|
|
monographs = await http.get(`${Constants.API_HOST}/monographs`, token);
|
2023-08-21 13:32:06 +05:00
|
|
|
await this._db.storage().write("monographs", monographs);
|
2022-07-08 12:07:26 +05:00
|
|
|
|
|
|
|
|
if (monographs) this.monographs = monographs;
|
2021-06-15 10:56:22 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
2021-06-15 10:09:10 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if note is published.
|
|
|
|
|
* @param {string} noteId id of the note
|
|
|
|
|
* @returns {boolean} Whether note is published or not.
|
|
|
|
|
*/
|
2021-06-15 12:02:55 +05:00
|
|
|
isPublished(noteId) {
|
2022-08-15 10:57:25 +05:00
|
|
|
return this.monographs && this.monographs.indexOf(noteId) > -1;
|
2021-06-15 10:09:10 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get note published monograph id
|
|
|
|
|
* @param {string} noteId id of the note
|
|
|
|
|
* @returns Monograph Id
|
|
|
|
|
*/
|
2021-06-15 12:02:55 +05:00
|
|
|
monograph(noteId) {
|
2022-08-15 10:57:25 +05:00
|
|
|
if (!this.monographs) return;
|
|
|
|
|
|
2021-06-15 14:33:40 +05:00
|
|
|
return this.monographs[this.monographs.indexOf(noteId)];
|
2021-06-15 10:09:10 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Publish a note as a monograph
|
|
|
|
|
* @param {string} noteId id of the note to publish
|
|
|
|
|
* @param {{password: string, selfDestruct: boolean}} opts Publish options
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2022-08-15 10:57:25 +05:00
|
|
|
async publish(noteId, opts = { password: undefined, selfDestruct: false }) {
|
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.");
|
|
|
|
|
|
2021-10-20 08:57:56 +05:00
|
|
|
const content = await this._db.content.downloadMedia(
|
|
|
|
|
`monograph-${noteId}`,
|
2021-10-22 11:52:56 +05:00
|
|
|
await this._db.content.raw(note.data.contentId),
|
|
|
|
|
false
|
2021-10-20 08:57:56 +05:00
|
|
|
);
|
2021-06-13 10:00:24 +05:00
|
|
|
if (!content) throw new Error("This note has no content.");
|
|
|
|
|
|
|
|
|
|
const monograph = {
|
2021-06-15 14:33:40 +05:00
|
|
|
id: noteId,
|
2021-06-13 10:00:24 +05:00
|
|
|
title: note.title,
|
|
|
|
|
userId: user.id,
|
2022-08-31 06:33:37 +05:00
|
|
|
selfDestruct: opts.selfDestruct
|
2021-06-13 10:00:24 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (opts.password) {
|
2023-08-21 13:32:06 +05:00
|
|
|
monograph.encryptedContent = await this._db
|
|
|
|
|
.storage()
|
|
|
|
|
.encrypt(
|
|
|
|
|
{ password: opts.password },
|
|
|
|
|
JSON.stringify({ type: content.type, data: content.data })
|
|
|
|
|
);
|
2021-06-13 10:00:24 +05:00
|
|
|
} else {
|
|
|
|
|
monograph.content = JSON.stringify({
|
|
|
|
|
type: content.type,
|
2022-08-31 06:33:37 +05:00
|
|
|
data: content.data
|
2021-06-13 10:00:24 +05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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 14:33:40 +05:00
|
|
|
this.monographs.push(id);
|
2021-06-15 10:09:10 +05:00
|
|
|
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.");
|
|
|
|
|
|
2022-08-15 10:57:25 +05:00
|
|
|
// const note = this._db.notes.note(noteId);
|
|
|
|
|
// if (!note) throw new Error("No such note found.");
|
2021-06-13 10:00:24 +05:00
|
|
|
|
2021-06-15 14:33:40 +05:00
|
|
|
if (!this.isPublished(noteId))
|
2021-06-15 10:09:10 +05:00
|
|
|
throw new Error("This note is not published.");
|
2021-06-13 10:00:24 +05:00
|
|
|
|
2021-06-15 14:33:40 +05:00
|
|
|
await http.delete(`${Constants.API_HOST}/monographs/${noteId}`, token);
|
2021-06-13 10:00:24 +05:00
|
|
|
|
2021-06-15 14:33:40 +05:00
|
|
|
this.monographs.splice(this.monographs.indexOf(noteId), 1);
|
2021-06-13 10:00:24 +05:00
|
|
|
}
|
2021-06-16 09:33:15 +05:00
|
|
|
|
|
|
|
|
get all() {
|
2022-08-15 10:57:25 +05:00
|
|
|
if (!this.monographs) return [];
|
|
|
|
|
|
2021-06-16 09:33:15 +05:00
|
|
|
return this._db.notes.all.filter(
|
|
|
|
|
(note) => this.monographs.indexOf(note.id) > -1
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-08-15 10:57:25 +05:00
|
|
|
|
|
|
|
|
async get(monographId) {
|
|
|
|
|
return await http.get(`${Constants.API_HOST}/monographs/${monographId}`);
|
|
|
|
|
}
|
2021-06-13 10:00:24 +05:00
|
|
|
}
|
2021-06-15 11:57:00 +05:00
|
|
|
export default Monographs;
|