mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
server-ts: Convert a few classes in becca
This commit is contained in:
parent
e04bd5aaf0
commit
6dd2cd39aa
@ -7,6 +7,8 @@ import NotFoundError = require('../errors/not_found_error');
|
|||||||
* There's a similar frontend cache Froca, and share cache Shaca.
|
* There's a similar frontend cache Froca, and share cache Shaca.
|
||||||
*/
|
*/
|
||||||
class Becca {
|
class Becca {
|
||||||
|
notes!: Record<string, BNote>;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.reset();
|
this.reset();
|
||||||
}
|
}
|
||||||
|
@ -1,47 +1,44 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const utils = require('../../services/utils');
|
import utils = require('../../services/utils');
|
||||||
const sql = require('../../services/sql');
|
import sql = require('../../services/sql');
|
||||||
const entityChangesService = require('../../services/entity_changes.js');
|
import entityChangesService = require('../../services/entity_changes.js');
|
||||||
const eventService = require('../../services/events.js');
|
import eventService = require('../../services/events');
|
||||||
const dateUtils = require('../../services/date_utils');
|
import dateUtils = require('../../services/date_utils');
|
||||||
const cls = require('../../services/cls');
|
import cls = require('../../services/cls');
|
||||||
const log = require('../../services/log');
|
import log = require('../../services/log');
|
||||||
const protectedSessionService = require('../../services/protected_session');
|
import protectedSessionService = require('../../services/protected_session');
|
||||||
const blobService = require('../../services/blob.js');
|
import blobService = require('../../services/blob.js');
|
||||||
|
import Becca = require('../becca-interface');
|
||||||
|
|
||||||
let becca = null;
|
let becca: Becca | null = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for all backend entities.
|
* Base class for all backend entities.
|
||||||
*/
|
*/
|
||||||
class AbstractBeccaEntity {
|
abstract class AbstractBeccaEntity {
|
||||||
/** @protected */
|
|
||||||
beforeSaving() {
|
protected utcDateModified?: string;
|
||||||
|
|
||||||
|
protected beforeSaving() {
|
||||||
if (!this[this.constructor.primaryKeyName]) {
|
if (!this[this.constructor.primaryKeyName]) {
|
||||||
this[this.constructor.primaryKeyName] = utils.newEntityId();
|
this[this.constructor.primaryKeyName] = utils.newEntityId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @protected */
|
protected getUtcDateChanged() {
|
||||||
getUtcDateChanged() {
|
|
||||||
return this.utcDateModified || this.utcDateCreated;
|
return this.utcDateModified || this.utcDateCreated;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected get becca(): Becca {
|
||||||
* @protected
|
|
||||||
* @returns {Becca}
|
|
||||||
*/
|
|
||||||
get becca() {
|
|
||||||
if (!becca) {
|
if (!becca) {
|
||||||
becca = require('../becca.js');
|
becca = require('../becca');
|
||||||
}
|
}
|
||||||
|
|
||||||
return becca;
|
return becca as Becca;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @protected */
|
protected putEntityChange(isDeleted: boolean) {
|
||||||
putEntityChange(isDeleted) {
|
|
||||||
entityChangesService.putEntityChange({
|
entityChangesService.putEntityChange({
|
||||||
entityName: this.constructor.entityName,
|
entityName: this.constructor.entityName,
|
||||||
entityId: this[this.constructor.primaryKeyName],
|
entityId: this[this.constructor.primaryKeyName],
|
||||||
@ -52,11 +49,7 @@ class AbstractBeccaEntity {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected generateHash(isDeleted: boolean): string {
|
||||||
* @protected
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
generateHash(isDeleted) {
|
|
||||||
let contentToHash = "";
|
let contentToHash = "";
|
||||||
|
|
||||||
for (const propertyName of this.constructor.hashedProperties) {
|
for (const propertyName of this.constructor.hashedProperties) {
|
||||||
@ -70,25 +63,16 @@ class AbstractBeccaEntity {
|
|||||||
return utils.hash(contentToHash).substr(0, 10);
|
return utils.hash(contentToHash).substr(0, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @protected */
|
protected getPojoToSave() {
|
||||||
getPojoToSave() {
|
|
||||||
return this.getPojo();
|
return this.getPojo();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected abstract getPojo(): {};
|
||||||
* @protected
|
|
||||||
* @abstract
|
|
||||||
*/
|
|
||||||
getPojo() {
|
|
||||||
throw new Error(`Unimplemented getPojo() for entity '${this.constructor.name}'`)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves entity - executes SQL, but doesn't commit the transaction on its own
|
* Saves entity - executes SQL, but doesn't commit the transaction on its own
|
||||||
*
|
|
||||||
* @returns {this}
|
|
||||||
*/
|
*/
|
||||||
save(opts = {}) {
|
save(opts = {}): this {
|
||||||
const entityName = this.constructor.entityName;
|
const entityName = this.constructor.entityName;
|
||||||
const primaryKeyName = this.constructor.primaryKeyName;
|
const primaryKeyName = this.constructor.primaryKeyName;
|
||||||
|
|
||||||
@ -124,8 +108,7 @@ class AbstractBeccaEntity {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @protected */
|
protected _setContent(content, opts = {}) {
|
||||||
_setContent(content, opts = {}) {
|
|
||||||
// client code asks to save entity even if blobId didn't change (something else was changed)
|
// client code asks to save entity even if blobId didn't change (something else was changed)
|
||||||
opts.forceSave = !!opts.forceSave;
|
opts.forceSave = !!opts.forceSave;
|
||||||
opts.forceFrontendReload = !!opts.forceFrontendReload;
|
opts.forceFrontendReload = !!opts.forceFrontendReload;
|
||||||
@ -243,11 +226,7 @@ class AbstractBeccaEntity {
|
|||||||
return newBlobId;
|
return newBlobId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected _getContent(): string | Buffer {
|
||||||
* @protected
|
|
||||||
* @returns {string|Buffer}
|
|
||||||
*/
|
|
||||||
_getContent() {
|
|
||||||
const row = sql.getRow(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);
|
const row = sql.getRow(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);
|
||||||
|
|
||||||
if (!row) {
|
if (!row) {
|
||||||
@ -261,8 +240,6 @@ class AbstractBeccaEntity {
|
|||||||
* Mark the entity as (soft) deleted. It will be completely erased later.
|
* Mark the entity as (soft) deleted. It will be completely erased later.
|
||||||
*
|
*
|
||||||
* This is a low-level method, for notes and branches use `note.deleteNote()` and 'branch.deleteBranch()` instead.
|
* This is a low-level method, for notes and branches use `note.deleteNote()` and 'branch.deleteBranch()` instead.
|
||||||
*
|
|
||||||
* @param [deleteId=null]
|
|
||||||
*/
|
*/
|
||||||
markAsDeleted(deleteId = null) {
|
markAsDeleted(deleteId = null) {
|
||||||
const entityId = this[this.constructor.primaryKeyName];
|
const entityId = this[this.constructor.primaryKeyName];
|
||||||
@ -306,4 +283,4 @@ class AbstractBeccaEntity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = AbstractBeccaEntity;
|
export = AbstractBeccaEntity;
|
@ -1,16 +1,24 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const utils = require('../../services/utils');
|
import utils = require('../../services/utils');
|
||||||
const dateUtils = require('../../services/date_utils');
|
import dateUtils = require('../../services/date_utils');
|
||||||
const AbstractBeccaEntity = require('./abstract_becca_entity.js');
|
import AbstractBeccaEntity = require('./abstract_becca_entity.js');
|
||||||
const sql = require('../../services/sql');
|
import sql = require('../../services/sql');
|
||||||
const protectedSessionService = require('../../services/protected_session');
|
import protectedSessionService = require('../../services/protected_session');
|
||||||
const log = require('../../services/log');
|
import log = require('../../services/log');
|
||||||
|
import { AttachmentRow } from './rows';
|
||||||
|
|
||||||
const attachmentRoleToNoteTypeMapping = {
|
const attachmentRoleToNoteTypeMapping = {
|
||||||
'image': 'image'
|
'image': 'image'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface ContentOpts {
|
||||||
|
/** will also save this BAttachment entity */
|
||||||
|
forceFullSave: boolean;
|
||||||
|
/** override frontend heuristics on when to reload, instruct to reload */
|
||||||
|
forceFrontendReload: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attachment represent data related/attached to the note. Conceptually similar to attributes, but intended for
|
* 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.
|
* larger amounts of data and generally not accessible to the user.
|
||||||
@ -22,7 +30,24 @@ class BAttachment extends AbstractBeccaEntity {
|
|||||||
static get primaryKeyName() { return "attachmentId"; }
|
static get primaryKeyName() { return "attachmentId"; }
|
||||||
static get hashedProperties() { return ["attachmentId", "ownerId", "role", "mime", "title", "blobId", "utcDateScheduledForErasureSince"]; }
|
static get hashedProperties() { return ["attachmentId", "ownerId", "role", "mime", "title", "blobId", "utcDateScheduledForErasureSince"]; }
|
||||||
|
|
||||||
constructor(row) {
|
noteId?: number;
|
||||||
|
attachmentId?: string;
|
||||||
|
/** either noteId or revisionId to which this attachment belongs */
|
||||||
|
ownerId: string;
|
||||||
|
role: string;
|
||||||
|
mime: string;
|
||||||
|
title: string;
|
||||||
|
type?: keyof typeof attachmentRoleToNoteTypeMapping;
|
||||||
|
position?: number;
|
||||||
|
blobId: string;
|
||||||
|
isProtected?: boolean;
|
||||||
|
dateModified?: string;
|
||||||
|
utcDateScheduledForErasureSince?: string;
|
||||||
|
/** optionally added to the entity */
|
||||||
|
contentLength?: number;
|
||||||
|
isDecrypted?: boolean;
|
||||||
|
|
||||||
|
constructor(row: AttachmentRow) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
if (!row.ownerId?.trim()) {
|
if (!row.ownerId?.trim()) {
|
||||||
@ -35,43 +60,23 @@ class BAttachment extends AbstractBeccaEntity {
|
|||||||
throw new Error("'title' must be given to initialize a Attachment entity");
|
throw new Error("'title' must be given to initialize a Attachment entity");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {string} */
|
|
||||||
this.attachmentId = row.attachmentId;
|
this.attachmentId = row.attachmentId;
|
||||||
/**
|
|
||||||
* either noteId or revisionId to which this attachment belongs
|
|
||||||
* @type {string}
|
|
||||||
*/
|
|
||||||
this.ownerId = row.ownerId;
|
this.ownerId = row.ownerId;
|
||||||
/** @type {string} */
|
|
||||||
this.role = row.role;
|
this.role = row.role;
|
||||||
/** @type {string} */
|
|
||||||
this.mime = row.mime;
|
this.mime = row.mime;
|
||||||
/** @type {string} */
|
|
||||||
this.title = row.title;
|
this.title = row.title;
|
||||||
/** @type {int} */
|
|
||||||
this.position = row.position;
|
this.position = row.position;
|
||||||
/** @type {string} */
|
|
||||||
this.blobId = row.blobId;
|
this.blobId = row.blobId;
|
||||||
/** @type {boolean} */
|
|
||||||
this.isProtected = !!row.isProtected;
|
this.isProtected = !!row.isProtected;
|
||||||
/** @type {string} */
|
|
||||||
this.dateModified = row.dateModified;
|
this.dateModified = row.dateModified;
|
||||||
/** @type {string} */
|
|
||||||
this.utcDateModified = row.utcDateModified;
|
this.utcDateModified = row.utcDateModified;
|
||||||
/** @type {string} */
|
|
||||||
this.utcDateScheduledForErasureSince = row.utcDateScheduledForErasureSince;
|
this.utcDateScheduledForErasureSince = row.utcDateScheduledForErasureSince;
|
||||||
|
|
||||||
/**
|
|
||||||
* optionally added to the entity
|
|
||||||
* @type {int}
|
|
||||||
*/
|
|
||||||
this.contentLength = row.contentLength;
|
this.contentLength = row.contentLength;
|
||||||
|
|
||||||
this.decrypt();
|
this.decrypt();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {BAttachment} */
|
copy(): BAttachment {
|
||||||
copy() {
|
|
||||||
return new BAttachment({
|
return new BAttachment({
|
||||||
ownerId: this.ownerId,
|
ownerId: this.ownerId,
|
||||||
role: this.role,
|
role: this.role,
|
||||||
@ -82,13 +87,12 @@ class BAttachment extends AbstractBeccaEntity {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {BNote} */
|
getNote(): BNote {
|
||||||
getNote() {
|
|
||||||
return this.becca.notes[this.ownerId];
|
return this.becca.notes[this.ownerId];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {boolean} true if the note has string content (not binary) */
|
/** @returns true if the note has string content (not binary) */
|
||||||
hasStringContent() {
|
hasStringContent(): boolean {
|
||||||
return utils.isStringNote(this.type, this.mime);
|
return utils.isStringNote(this.type, this.mime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,32 +114,24 @@ class BAttachment extends AbstractBeccaEntity {
|
|||||||
|
|
||||||
if (!this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) {
|
if (!this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) {
|
||||||
try {
|
try {
|
||||||
this.title = protectedSessionService.decryptString(this.title);
|
this.title = protectedSessionService.decryptString(this.title) || "";
|
||||||
this.isDecrypted = true;
|
this.isDecrypted = true;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e: any) {
|
||||||
log.error(`Could not decrypt attachment ${this.attachmentId}: ${e.message} ${e.stack}`);
|
log.error(`Could not decrypt attachment ${this.attachmentId}: ${e.message} ${e.stack}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {string|Buffer} */
|
getContent(): string | Buffer {
|
||||||
getContent() {
|
|
||||||
return this._getContent();
|
return this._getContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
setContent(content: any, opts: ContentOpts) {
|
||||||
* @param content
|
|
||||||
* @param {object} [opts]
|
|
||||||
* @param {object} [opts.forceSave=false] - will also save this BAttachment entity
|
|
||||||
* @param {object} [opts.forceFrontendReload=false] - override frontend heuristics on when to reload, instruct to reload
|
|
||||||
*/
|
|
||||||
setContent(content, opts) {
|
|
||||||
this._setContent(content, opts);
|
this._setContent(content, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {{note: BNote, branch: BBranch}} */
|
convertToNote(): { note: BNote, branch: BBranch } {
|
||||||
convertToNote() {
|
|
||||||
if (this.type === 'search') {
|
if (this.type === 'search') {
|
||||||
throw new Error(`Note of type search cannot have child notes`);
|
throw new Error(`Note of type search cannot have child notes`);
|
||||||
}
|
}
|
||||||
@ -195,7 +191,7 @@ class BAttachment extends AbstractBeccaEntity {
|
|||||||
super.beforeSaving();
|
super.beforeSaving();
|
||||||
|
|
||||||
if (this.position === undefined || this.position === null) {
|
if (this.position === undefined || this.position === null) {
|
||||||
this.position = 10 + sql.getValue(`SELECT COALESCE(MAX(position), 0)
|
this.position = 10 + sql.getValue<number>(`SELECT COALESCE(MAX(position), 0)
|
||||||
FROM attachments
|
FROM attachments
|
||||||
WHERE ownerId = ?`, [this.noteId]);
|
WHERE ownerId = ?`, [this.noteId]);
|
||||||
}
|
}
|
||||||
@ -210,7 +206,7 @@ class BAttachment extends AbstractBeccaEntity {
|
|||||||
ownerId: this.ownerId,
|
ownerId: this.ownerId,
|
||||||
role: this.role,
|
role: this.role,
|
||||||
mime: this.mime,
|
mime: this.mime,
|
||||||
title: this.title,
|
title: this.title || undefined,
|
||||||
position: this.position,
|
position: this.position,
|
||||||
blobId: this.blobId,
|
blobId: this.blobId,
|
||||||
isProtected: !!this.isProtected,
|
isProtected: !!this.isProtected,
|
||||||
@ -228,7 +224,7 @@ class BAttachment extends AbstractBeccaEntity {
|
|||||||
|
|
||||||
if (pojo.isProtected) {
|
if (pojo.isProtected) {
|
||||||
if (this.isDecrypted) {
|
if (this.isDecrypted) {
|
||||||
pojo.title = protectedSessionService.encrypt(pojo.title);
|
pojo.title = protectedSessionService.encrypt(pojo.title || "") || undefined;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// updating protected note outside of protected session means we will keep original ciphertexts
|
// updating protected note outside of protected session means we will keep original ciphertexts
|
||||||
@ -240,4 +236,4 @@ class BAttachment extends AbstractBeccaEntity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = BAttachment;
|
export = BAttachment;
|
@ -1,17 +1,17 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const protectedSessionService = require('../../services/protected_session.js');
|
import protectedSessionService = require('../../services/protected_session');
|
||||||
const log = require('../../services/log');
|
import log = require('../../services/log');
|
||||||
const sql = require('../../services/sql');
|
import sql = require('../../services/sql');
|
||||||
const utils = require('../../services/utils');
|
import utils = require('../../services/utils');
|
||||||
const dateUtils = require('../../services/date_utils');
|
import dateUtils = require('../../services/date_utils');
|
||||||
const AbstractBeccaEntity = require('./abstract_becca_entity.js');
|
import AbstractBeccaEntity = require('./abstract_becca_entity.js');
|
||||||
const BRevision = require('./brevision.js');
|
import BRevision = require('./brevision.js');
|
||||||
const BAttachment = require('./battachment.js');
|
import BAttachment = require('./battachment.ts');
|
||||||
const TaskContext = require('../../services/task_context.js');
|
import TaskContext = require('../../services/task_context.js');
|
||||||
const dayjs = require("dayjs");
|
import dayjs = require("dayjs");
|
||||||
const utc = require('dayjs/plugin/utc');
|
import utc = require('dayjs/plugin/utc');
|
||||||
const eventService = require('../../services/events.js');
|
import eventService = require('../../services/events.ts');
|
||||||
dayjs.extend(utc);
|
dayjs.extend(utc);
|
||||||
|
|
||||||
const LABEL = 'label';
|
const LABEL = 'label';
|
@ -1,7 +1,9 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const dateUtils = require('../../services/date_utils');
|
import { RecentNoteRow } from "./rows";
|
||||||
const AbstractBeccaEntity = require('./abstract_becca_entity.js');
|
|
||||||
|
import dateUtils = require('../../services/date_utils');
|
||||||
|
import AbstractBeccaEntity = require('./abstract_becca_entity.js');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RecentNote represents recently visited note.
|
* RecentNote represents recently visited note.
|
||||||
@ -12,14 +14,15 @@ class BRecentNote extends AbstractBeccaEntity {
|
|||||||
static get entityName() { return "recent_notes"; }
|
static get entityName() { return "recent_notes"; }
|
||||||
static get primaryKeyName() { return "noteId"; }
|
static get primaryKeyName() { return "noteId"; }
|
||||||
|
|
||||||
constructor(row) {
|
noteId: string;
|
||||||
|
notePath: string;
|
||||||
|
utcDateCreated: string;
|
||||||
|
|
||||||
|
constructor(row: RecentNoteRow) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
/** @type {string} */
|
|
||||||
this.noteId = row.noteId;
|
this.noteId = row.noteId;
|
||||||
/** @type {string} */
|
|
||||||
this.notePath = row.notePath;
|
this.notePath = row.notePath;
|
||||||
/** @type {string} */
|
|
||||||
this.utcDateCreated = row.utcDateCreated || dateUtils.utcNowDateTime();
|
this.utcDateCreated = row.utcDateCreated || dateUtils.utcNowDateTime();
|
||||||
}
|
}
|
||||||
|
|
@ -1,12 +1,22 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const protectedSessionService = require('../../services/protected_session');
|
import protectedSessionService = require('../../services/protected_session');
|
||||||
const utils = require('../../services/utils');
|
import utils = require('../../services/utils');
|
||||||
const dateUtils = require('../../services/date_utils');
|
import dateUtils = require('../../services/date_utils');
|
||||||
const becca = require('../becca.js');
|
import becca = require('../becca.js');
|
||||||
const AbstractBeccaEntity = require('./abstract_becca_entity.js');
|
import AbstractBeccaEntity = require('./abstract_becca_entity.js');
|
||||||
const sql = require('../../services/sql');
|
import sql = require('../../services/sql');
|
||||||
const BAttachment = require('./battachment.js');
|
import BAttachment = require('./battachment.js');
|
||||||
|
import { AttachmentRow, RevisionRow } from './rows';
|
||||||
|
|
||||||
|
interface ContentOpts {
|
||||||
|
/** will also save this BRevision entity */
|
||||||
|
forceSave: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GetByIdOpts {
|
||||||
|
includeContentLength?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Revision represents a snapshot of note's title and content at some point in the past.
|
* Revision represents a snapshot of note's title and content at some point in the past.
|
||||||
@ -20,40 +30,39 @@ class BRevision extends AbstractBeccaEntity {
|
|||||||
static get hashedProperties() { return ["revisionId", "noteId", "title", "isProtected", "dateLastEdited", "dateCreated",
|
static get hashedProperties() { return ["revisionId", "noteId", "title", "isProtected", "dateLastEdited", "dateCreated",
|
||||||
"utcDateLastEdited", "utcDateCreated", "utcDateModified", "blobId"]; }
|
"utcDateLastEdited", "utcDateCreated", "utcDateModified", "blobId"]; }
|
||||||
|
|
||||||
constructor(row, titleDecrypted = false) {
|
revisionId: string;
|
||||||
|
noteId: string;
|
||||||
|
type: string;
|
||||||
|
mime: string;
|
||||||
|
isProtected: boolean;
|
||||||
|
title: string;
|
||||||
|
blobId: string;
|
||||||
|
dateLastEdited: string;
|
||||||
|
dateCreated: string;
|
||||||
|
utcDateLastEdited: string;
|
||||||
|
utcDateCreated: string;
|
||||||
|
contentLength?: number;
|
||||||
|
|
||||||
|
constructor(row: RevisionRow, titleDecrypted = false) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
/** @type {string} */
|
|
||||||
this.revisionId = row.revisionId;
|
this.revisionId = row.revisionId;
|
||||||
/** @type {string} */
|
|
||||||
this.noteId = row.noteId;
|
this.noteId = row.noteId;
|
||||||
/** @type {string} */
|
|
||||||
this.type = row.type;
|
this.type = row.type;
|
||||||
/** @type {string} */
|
|
||||||
this.mime = row.mime;
|
this.mime = row.mime;
|
||||||
/** @type {boolean} */
|
|
||||||
this.isProtected = !!row.isProtected;
|
this.isProtected = !!row.isProtected;
|
||||||
/** @type {string} */
|
|
||||||
this.title = row.title;
|
this.title = row.title;
|
||||||
/** @type {string} */
|
|
||||||
this.blobId = row.blobId;
|
this.blobId = row.blobId;
|
||||||
/** @type {string} */
|
|
||||||
this.dateLastEdited = row.dateLastEdited;
|
this.dateLastEdited = row.dateLastEdited;
|
||||||
/** @type {string} */
|
|
||||||
this.dateCreated = row.dateCreated;
|
this.dateCreated = row.dateCreated;
|
||||||
/** @type {string} */
|
|
||||||
this.utcDateLastEdited = row.utcDateLastEdited;
|
this.utcDateLastEdited = row.utcDateLastEdited;
|
||||||
/** @type {string} */
|
|
||||||
this.utcDateCreated = row.utcDateCreated;
|
this.utcDateCreated = row.utcDateCreated;
|
||||||
/** @type {string} */
|
|
||||||
this.utcDateModified = row.utcDateModified;
|
this.utcDateModified = row.utcDateModified;
|
||||||
/** @type {int} */
|
|
||||||
this.contentLength = row.contentLength;
|
this.contentLength = row.contentLength;
|
||||||
|
|
||||||
if (this.isProtected && !titleDecrypted) {
|
if (this.isProtected && !titleDecrypted) {
|
||||||
this.title = protectedSessionService.isProtectedSessionAvailable()
|
const decryptedTitle = protectedSessionService.isProtectedSessionAvailable() ? protectedSessionService.decryptString(this.title) : null;
|
||||||
? protectedSessionService.decryptString(this.title)
|
this.title = decryptedTitle || "[protected]";
|
||||||
: "[protected]";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,16 +89,13 @@ class BRevision extends AbstractBeccaEntity {
|
|||||||
*
|
*
|
||||||
* This is the same approach as is used for Note's content.
|
* This is the same approach as is used for Note's content.
|
||||||
*/
|
*/
|
||||||
|
getContent(): string | Buffer {
|
||||||
/** @returns {string|Buffer} */
|
|
||||||
getContent() {
|
|
||||||
return this._getContent();
|
return this._getContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {*}
|
|
||||||
* @throws Error in case of invalid JSON */
|
* @throws Error in case of invalid JSON */
|
||||||
getJsonContent() {
|
getJsonContent(): {} | null {
|
||||||
const content = this.getContent();
|
const content = this.getContent();
|
||||||
|
|
||||||
if (!content || !content.trim()) {
|
if (!content || !content.trim()) {
|
||||||
@ -99,8 +105,8 @@ class BRevision extends AbstractBeccaEntity {
|
|||||||
return JSON.parse(content);
|
return JSON.parse(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {*|null} valid object or null if the content cannot be parsed as JSON */
|
/** @returns valid object or null if the content cannot be parsed as JSON */
|
||||||
getJsonContentSafely() {
|
getJsonContentSafely(): {} | null {
|
||||||
try {
|
try {
|
||||||
return this.getJsonContent();
|
return this.getJsonContent();
|
||||||
}
|
}
|
||||||
@ -109,18 +115,12 @@ class BRevision extends AbstractBeccaEntity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
setContent(content: any, opts: ContentOpts) {
|
||||||
* @param content
|
|
||||||
* @param {object} [opts]
|
|
||||||
* @param {object} [opts.forceSave=false] - will also save this BRevision entity
|
|
||||||
*/
|
|
||||||
setContent(content, opts) {
|
|
||||||
this._setContent(content, opts);
|
this._setContent(content, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {BAttachment[]} */
|
getAttachments(): BAttachment[] {
|
||||||
getAttachments() {
|
return sql.getRows<AttachmentRow>(`
|
||||||
return sql.getRows(`
|
|
||||||
SELECT attachments.*
|
SELECT attachments.*
|
||||||
FROM attachments
|
FROM attachments
|
||||||
WHERE ownerId = ?
|
WHERE ownerId = ?
|
||||||
@ -128,8 +128,7 @@ class BRevision extends AbstractBeccaEntity {
|
|||||||
.map(row => new BAttachment(row));
|
.map(row => new BAttachment(row));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {BAttachment|null} */
|
getAttachmentById(attachmentId: String, opts: GetByIdOpts = {}): BAttachment | null {
|
||||||
getAttachmentById(attachmentId, opts = {}) {
|
|
||||||
opts.includeContentLength = !!opts.includeContentLength;
|
opts.includeContentLength = !!opts.includeContentLength;
|
||||||
|
|
||||||
const query = opts.includeContentLength
|
const query = opts.includeContentLength
|
||||||
@ -139,13 +138,12 @@ class BRevision extends AbstractBeccaEntity {
|
|||||||
WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`
|
WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`
|
||||||
: `SELECT * FROM attachments WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`;
|
: `SELECT * FROM attachments WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`;
|
||||||
|
|
||||||
return sql.getRows(query, [this.revisionId, attachmentId])
|
return sql.getRows<AttachmentRow>(query, [this.revisionId, attachmentId])
|
||||||
.map(row => new BAttachment(row))[0];
|
.map(row => new BAttachment(row))[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {BAttachment[]} */
|
getAttachmentsByRole(role: string): BAttachment[] {
|
||||||
getAttachmentsByRole(role) {
|
return sql.getRows<AttachmentRow>(`
|
||||||
return sql.getRows(`
|
|
||||||
SELECT attachments.*
|
SELECT attachments.*
|
||||||
FROM attachments
|
FROM attachments
|
||||||
WHERE ownerId = ?
|
WHERE ownerId = ?
|
||||||
@ -155,8 +153,7 @@ class BRevision extends AbstractBeccaEntity {
|
|||||||
.map(row => new BAttachment(row));
|
.map(row => new BAttachment(row));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {BAttachment} */
|
getAttachmentByTitle(title: string): BAttachment {
|
||||||
getAttachmentByTitle(title) {
|
|
||||||
// cannot use SQL to filter by title since it can be encrypted
|
// cannot use SQL to filter by title since it can be encrypted
|
||||||
return this.getAttachments().filter(attachment => attachment.title === title)[0];
|
return this.getAttachments().filter(attachment => attachment.title === title)[0];
|
||||||
}
|
}
|
||||||
@ -174,7 +171,7 @@ class BRevision extends AbstractBeccaEntity {
|
|||||||
type: this.type,
|
type: this.type,
|
||||||
mime: this.mime,
|
mime: this.mime,
|
||||||
isProtected: this.isProtected,
|
isProtected: this.isProtected,
|
||||||
title: this.title,
|
title: this.title || undefined,
|
||||||
blobId: this.blobId,
|
blobId: this.blobId,
|
||||||
dateLastEdited: this.dateLastEdited,
|
dateLastEdited: this.dateLastEdited,
|
||||||
dateCreated: this.dateCreated,
|
dateCreated: this.dateCreated,
|
||||||
@ -193,7 +190,7 @@ class BRevision extends AbstractBeccaEntity {
|
|||||||
|
|
||||||
if (pojo.isProtected) {
|
if (pojo.isProtected) {
|
||||||
if (protectedSessionService.isProtectedSessionAvailable()) {
|
if (protectedSessionService.isProtectedSessionAvailable()) {
|
||||||
pojo.title = protectedSessionService.encrypt(this.title);
|
pojo.title = protectedSessionService.encrypt(this.title) || undefined;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// updating protected note outside of protected session means we will keep original ciphertexts
|
// updating protected note outside of protected session means we will keep original ciphertexts
|
36
src/becca/entities/rows.ts
Normal file
36
src/becca/entities/rows.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
export interface AttachmentRow {
|
||||||
|
attachmentId?: string;
|
||||||
|
ownerId: string;
|
||||||
|
role: string;
|
||||||
|
mime: string;
|
||||||
|
title?: string;
|
||||||
|
position?: number;
|
||||||
|
blobId: string;
|
||||||
|
isProtected?: boolean;
|
||||||
|
dateModified?: string;
|
||||||
|
utcDateModified?: string;
|
||||||
|
utcDateScheduledForErasureSince?: string;
|
||||||
|
contentLength?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RevisionRow {
|
||||||
|
revisionId: string;
|
||||||
|
noteId: string;
|
||||||
|
type: string;
|
||||||
|
mime: string;
|
||||||
|
isProtected: boolean;
|
||||||
|
title: string;
|
||||||
|
blobId: string;
|
||||||
|
dateLastEdited: string;
|
||||||
|
dateCreated: string;
|
||||||
|
utcDateLastEdited: string;
|
||||||
|
utcDateCreated: string;
|
||||||
|
utcDateModified: string;
|
||||||
|
contentLength?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RecentNoteRow {
|
||||||
|
noteId: string;
|
||||||
|
notePath: string;
|
||||||
|
utcDateCreated?: string;
|
||||||
|
}
|
@ -59,4 +59,4 @@ class NoteSet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = NoteSet;
|
export = NoteSet;
|
Loading…
x
Reference in New Issue
Block a user