Notes/src/becca/entities/battachment.js

184 lines
6.1 KiB
JavaScript
Raw Normal View History

"use strict";
const utils = require('../../services/utils');
const dateUtils = require('../../services/date_utils');
const becca = require('../becca');
const AbstractBeccaEntity = require("./abstract_becca_entity");
2023-04-11 22:55:50 +02:00
const sql = require("../../services/sql");
2023-04-14 16:49:06 +02:00
const attachmentRoleToNoteTypeMapping = {
'image': 'image'
};
/**
2023-03-16 12:17:55 +01:00
* Attachment represent data related/attached to the note. Conceptually similar to attributes, but intended for
* larger amounts of data and generally not accessible to the user.
*
* @extends AbstractBeccaEntity
*/
2023-03-16 12:17:55 +01:00
class BAttachment extends AbstractBeccaEntity {
static get entityName() { return "attachments"; }
static get primaryKeyName() { return "attachmentId"; }
static get hashedProperties() { return ["attachmentId", "parentId", "role", "mime", "title", "blobId",
2023-04-21 00:19:17 +02:00
"utcDateScheduledForErasureSince", "utcDateModified"]; }
constructor(row) {
super();
2023-03-16 12:11:00 +01:00
if (!row.parentId?.trim()) {
2023-03-16 18:34:39 +01:00
throw new Error("'parentId' must be given to initialize a Attachment entity");
2023-03-16 12:11:00 +01:00
} else if (!row.role?.trim()) {
2023-03-16 12:17:55 +01:00
throw new Error("'role' must be given to initialize a Attachment entity");
2023-03-16 12:11:00 +01:00
} else if (!row.mime?.trim()) {
2023-03-16 12:17:55 +01:00
throw new Error("'mime' must be given to initialize a Attachment entity");
2023-03-16 12:11:00 +01:00
} else if (!row.title?.trim()) {
2023-03-16 12:17:55 +01:00
throw new Error("'title' must be given to initialize a Attachment entity");
}
/** @type {string} */
2023-03-16 16:37:31 +01:00
this.attachmentId = row.attachmentId;
2023-03-16 12:11:00 +01:00
/** @type {string} either noteId or noteRevisionId to which this attachment belongs */
this.parentId = row.parentId;
/** @type {string} */
2023-03-16 12:11:00 +01:00
this.role = row.role;
/** @type {string} */
this.mime = row.mime;
2023-03-16 12:11:00 +01:00
/** @type {string} */
this.title = row.title;
2023-04-11 22:55:50 +02:00
/** @type {number} */
this.position = row.position;
2023-03-16 18:34:39 +01:00
/** @type {string} */
this.blobId = row.blobId;
/** @type {boolean} */
this.isProtected = !!row.isProtected;
/** @type {string} */
2023-04-03 23:47:24 +02:00
this.dateModified = row.dateModified;
/** @type {string} */
this.utcDateModified = row.utcDateModified;
2023-04-03 23:47:24 +02:00
/** @type {string} */
2023-04-21 00:19:17 +02:00
this.utcDateScheduledForErasureSince = row.utcDateScheduledForErasureSince;
}
/** @returns {BAttachment} */
copy() {
return new BAttachment({
parentId: this.parentId,
role: this.role,
mime: this.mime,
title: this.title,
blobId: this.blobId,
isProtected: this.isProtected,
2023-04-21 00:19:17 +02:00
utcDateScheduledForErasureSince: this.utcDateScheduledForErasureSince
});
}
getNote() {
2023-03-16 12:11:00 +01:00
return becca.notes[this.parentId];
}
/** @returns {boolean} true if the note has string content (not binary) */
isStringNote() {
return utils.isStringNote(this.type, this.mime);
}
/** @returns {*} */
getContent() {
return this._getContent();
}
2023-03-16 16:37:31 +01:00
/**
* @param content
* @param {object} [opts]
* @param {object} [opts.forceSave=false] - will also save this BAttachment entity
* @param {object} [opts.forceCold=false] - blob has to be saved as cold
2023-03-16 16:37:31 +01:00
*/
setContent(content, opts) {
this._setContent(content, opts);
}
2023-04-14 16:49:06 +02:00
/**
* @returns {{note: BNote, branch: BBranch}}
*/
convertToNote() {
if (this.type === 'search') {
throw new Error(`Note of type search cannot have child notes`);
}
if (!this.getNote()) {
throw new Error("Cannot find note of this attachment. It is possible that this is note revision's attachment. " +
"Converting note revision's attachments to note is not (yet) supported.");
}
if (!(this.role in attachmentRoleToNoteTypeMapping)) {
throw new Error(`Mapping from attachment role '${this.role}' to note's type is not defined`);
}
if (!this.isContentAvailable()) { // isProtected is same for attachment
throw new Error(`Cannot convert protected attachment outside of protected session`);
}
const noteService = require('../../services/notes');
const { note, branch } = noteService.createNewNote({
parentNoteId: this.parentId,
title: this.title,
type: attachmentRoleToNoteTypeMapping[this.role],
mime: this.mime,
content: this.getContent(),
isProtected: this.isProtected
});
this.markAsDeleted();
if (this.role === 'image' && this.type === 'text') {
const origContent = this.getContent();
const oldAttachmentUrl = `api/attachment/${this.attachmentId}/image/`;
const newNoteUrl = `api/images/${note.noteId}/`;
const fixedContent = utils.replaceAll(origContent, oldAttachmentUrl, newNoteUrl);
if (origContent !== fixedContent) {
this.setContent(fixedContent);
}
}
return { note, branch };
}
beforeSaving() {
super.beforeSaving();
2023-04-11 22:55:50 +02:00
if (this.position === undefined || this.position === null) {
this.position = 10 + sql.getValue(`SELECT COALESCE(MAX(position), 0)
FROM attachments
WHERE parentId = ?`, [this.noteId]);
}
2023-04-03 23:47:24 +02:00
this.dateModified = dateUtils.localNowDateTime();
this.utcDateModified = dateUtils.utcNowDateTime();
}
getPojo() {
return {
2023-03-16 12:17:55 +01:00
attachmentId: this.attachmentId,
2023-03-16 12:11:00 +01:00
parentId: this.parentId,
2023-03-16 17:43:37 +01:00
role: this.role,
mime: this.mime,
2023-03-16 17:43:37 +01:00
title: this.title,
2023-04-11 22:55:50 +02:00
position: this.position,
2023-03-16 18:34:39 +01:00
blobId: this.blobId,
isProtected: !!this.isProtected,
isDeleted: false,
2023-04-03 23:47:24 +02:00
dateModified: this.dateModified,
utcDateModified: this.utcDateModified,
2023-04-21 00:19:17 +02:00
utcDateScheduledForErasureSince: this.utcDateScheduledForErasureSince
};
}
getPojoToSave() {
2023-03-16 17:43:37 +01:00
return this.getPojo();
}
}
2023-03-16 12:17:55 +01:00
module.exports = BAttachment;