2023-03-08 09:01:23 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const protectedSessionService = require('../../services/protected_session');
|
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const dateUtils = require('../../services/date_utils');
|
|
|
|
const becca = require('../becca');
|
|
|
|
const entityChangesService = require('../../services/entity_changes');
|
|
|
|
const AbstractBeccaEntity = require("./abstract_becca_entity");
|
|
|
|
|
|
|
|
/**
|
2023-03-16 11:03:28 +01:00
|
|
|
* NoteAttachment represent data related/attached to the note. Conceptually similar to attributes, but intended for
|
2023-03-08 09:01:23 +01:00
|
|
|
* larger amounts of data and generally not accessible to the user.
|
|
|
|
*
|
|
|
|
* @extends AbstractBeccaEntity
|
|
|
|
*/
|
2023-03-16 11:03:28 +01:00
|
|
|
class BNoteAttachment extends AbstractBeccaEntity {
|
|
|
|
static get entityName() { return "note_attachments"; }
|
|
|
|
static get primaryKeyName() { return "noteAttachmentId"; }
|
2023-03-16 12:11:00 +01:00
|
|
|
static get hashedProperties() { return ["noteAttachmentId", "parentId", "role", "mime", "title", "utcDateModified"]; }
|
2023-03-08 09:01:23 +01:00
|
|
|
|
|
|
|
constructor(row) {
|
|
|
|
super();
|
|
|
|
|
2023-03-16 12:11:00 +01:00
|
|
|
if (!row.parentId?.trim()) {
|
2023-03-16 11:03:28 +01:00
|
|
|
throw new Error("'noteId' must be given to initialize a NoteAttachment entity");
|
2023-03-16 12:11:00 +01:00
|
|
|
} else if (!row.role?.trim()) {
|
|
|
|
throw new Error("'role' must be given to initialize a NoteAttachment entity");
|
|
|
|
} else if (!row.mime?.trim()) {
|
|
|
|
throw new Error("'mime' must be given to initialize a NoteAttachment entity");
|
|
|
|
} else if (!row.title?.trim()) {
|
|
|
|
throw new Error("'title' must be given to initialize a NoteAttachment entity");
|
2023-03-08 09:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {string} needs to be set at the initialization time since it's used in the .setContent() */
|
2023-03-16 12:11:00 +01:00
|
|
|
this.noteAttachmentId = row.noteAttachmentId || `${this.noteId}_${this.name}`; // FIXME
|
|
|
|
/** @type {string} either noteId or noteRevisionId to which this attachment belongs */
|
|
|
|
this.parentId = row.parentId;
|
2023-03-08 09:01:23 +01:00
|
|
|
/** @type {string} */
|
2023-03-16 12:11:00 +01:00
|
|
|
this.role = row.role;
|
2023-03-08 09:01:23 +01:00
|
|
|
/** @type {string} */
|
|
|
|
this.mime = row.mime;
|
2023-03-16 12:11:00 +01:00
|
|
|
/** @type {string} */
|
|
|
|
this.title = row.title;
|
2023-03-08 09:01:23 +01:00
|
|
|
/** @type {boolean} */
|
|
|
|
this.isProtected = !!row.isProtected;
|
|
|
|
/** @type {string} */
|
|
|
|
this.utcDateModified = row.utcDateModified;
|
|
|
|
}
|
|
|
|
|
|
|
|
getNote() {
|
2023-03-16 12:11:00 +01:00
|
|
|
return becca.notes[this.parentId];
|
2023-03-08 09:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {boolean} true if the note has string content (not binary) */
|
|
|
|
isStringNote() {
|
|
|
|
return utils.isStringNote(this.type, this.mime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {*} */
|
|
|
|
getContent(silentNotFoundError = false) {
|
2023-03-16 11:03:28 +01:00
|
|
|
const res = sql.getRow(`SELECT content FROM note_attachment_contents WHERE noteAttachmentId = ?`, [this.noteAttachmentId]);
|
2023-03-08 09:01:23 +01:00
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
if (silentNotFoundError) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
else {
|
2023-03-16 11:03:28 +01:00
|
|
|
throw new Error(`Cannot find note attachment content for noteAttachmentId=${this.noteAttachmentId}`);
|
2023-03-08 09:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let content = res.content;
|
|
|
|
|
|
|
|
if (this.isProtected) {
|
|
|
|
if (protectedSessionService.isProtectedSessionAvailable()) {
|
|
|
|
content = protectedSessionService.decrypt(content);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
content = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isStringNote()) {
|
|
|
|
return content === null
|
|
|
|
? ""
|
|
|
|
: content.toString("UTF-8");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setContent(content) {
|
|
|
|
sql.transactional(() => {
|
2023-03-16 11:03:28 +01:00
|
|
|
this.save(); // also explicitly save note_attachment to update contentCheckSum
|
2023-03-08 09:01:23 +01:00
|
|
|
|
|
|
|
const pojo = {
|
2023-03-16 11:03:28 +01:00
|
|
|
noteAttachmentId: this.noteAttachmentId,
|
2023-03-08 09:01:23 +01:00
|
|
|
content: content,
|
|
|
|
utcDateModified: dateUtils.utcNowDateTime()
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.isProtected) {
|
|
|
|
if (protectedSessionService.isProtectedSessionAvailable()) {
|
|
|
|
pojo.content = protectedSessionService.encrypt(pojo.content);
|
|
|
|
} else {
|
2023-03-16 11:03:28 +01:00
|
|
|
throw new Error(`Cannot update content of noteAttachmentId=${this.noteAttachmentId} since we're out of protected session.`);
|
2023-03-08 09:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 11:03:28 +01:00
|
|
|
sql.upsert("note_attachment_contents", "noteAttachmentId", pojo);
|
2023-03-08 09:01:23 +01:00
|
|
|
|
|
|
|
entityChangesService.addEntityChange({
|
2023-03-16 11:03:28 +01:00
|
|
|
entityName: 'note_attachment_contents',
|
|
|
|
entityId: this.noteAttachmentId,
|
2023-03-16 11:02:07 +01:00
|
|
|
hash: this.contentCheckSum, // FIXME
|
2023-03-08 09:01:23 +01:00
|
|
|
isErased: false,
|
|
|
|
utcDateChanged: pojo.utcDateModified,
|
|
|
|
isSynced: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateCheckSum(content) {
|
2023-03-16 11:03:28 +01:00
|
|
|
return utils.hash(`${this.noteAttachmentId}|${content.toString()}`);
|
2023-03-08 09:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeSaving() {
|
|
|
|
if (!this.name.match(/^[a-z0-9]+$/i)) {
|
|
|
|
throw new Error(`Name must be alphanumerical, "${this.name}" given.`);
|
|
|
|
}
|
|
|
|
|
2023-03-16 12:11:00 +01:00
|
|
|
this.noteAttachmentId = `${this.noteId}_${this.name}`; // FIXME
|
2023-03-08 09:01:23 +01:00
|
|
|
|
|
|
|
super.beforeSaving();
|
|
|
|
|
|
|
|
this.utcDateModified = dateUtils.utcNowDateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
getPojo() {
|
|
|
|
return {
|
2023-03-16 11:03:28 +01:00
|
|
|
noteAttachmentId: this.noteAttachmentId,
|
2023-03-16 12:11:00 +01:00
|
|
|
parentId: this.parentId,
|
2023-03-08 09:01:23 +01:00
|
|
|
name: this.name,
|
|
|
|
mime: this.mime,
|
|
|
|
isProtected: !!this.isProtected,
|
2023-03-16 11:02:07 +01:00
|
|
|
contentCheckSum: this.contentCheckSum, // FIXME
|
2023-03-08 09:01:23 +01:00
|
|
|
isDeleted: false,
|
|
|
|
utcDateModified: this.utcDateModified
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getPojoToSave() {
|
|
|
|
const pojo = this.getPojo();
|
|
|
|
delete pojo.content; // not getting persisted
|
|
|
|
|
|
|
|
return pojo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 11:03:28 +01:00
|
|
|
module.exports = BNoteAttachment;
|