server-ts: Port share/shaca/sattribute

This commit is contained in:
Elian Doran 2024-04-09 22:25:32 +03:00
parent c4c2259e69
commit e1d74cd2f5
No known key found for this signature in database
3 changed files with 42 additions and 45 deletions

View File

@ -1,24 +1,30 @@
"use strict"; "use strict";
import SNote = require("./snote");
const AbstractShacaEntity = require('./abstract_shaca_entity'); const AbstractShacaEntity = require('./abstract_shaca_entity');
type AttributeRow = [ string, string, string, string, string, boolean, number ];
class SAttribute extends AbstractShacaEntity { class SAttribute extends AbstractShacaEntity {
constructor([attributeId, noteId, type, name, value, isInheritable, position]) {
attributeId: string;
private noteId: string;
type: string;
name: string;
private position: number;
value: string;
isInheritable: boolean;
constructor([attributeId, noteId, type, name, value, isInheritable, position]: AttributeRow) {
super(); super();
/** @param {string} */
this.attributeId = attributeId; this.attributeId = attributeId;
/** @param {string} */
this.noteId = noteId; this.noteId = noteId;
/** @param {string} */
this.type = type; this.type = type;
/** @param {string} */
this.name = name; this.name = name;
/** @param {int} */
this.position = position; this.position = position;
/** @param {string} */
this.value = value; this.value = value;
/** @param {boolean} */
this.isInheritable = !!isInheritable; this.isInheritable = !!isInheritable;
this.shaca.attributes[this.attributeId] = this; this.shaca.attributes[this.attributeId] = this;
@ -53,41 +59,34 @@ class SAttribute extends AbstractShacaEntity {
} }
} }
/** @returns {boolean} */
get isAffectingSubtree() { get isAffectingSubtree() {
return this.isInheritable return this.isInheritable
|| (this.type === 'relation' && ['template', 'inherit'].includes(this.name)); || (this.type === 'relation' && ['template', 'inherit'].includes(this.name));
} }
/** @returns {string} */
get targetNoteId() { // alias get targetNoteId() { // alias
return this.type === 'relation' ? this.value : undefined; return this.type === 'relation' ? this.value : undefined;
} }
/** @returns {boolean} */
isAutoLink() { isAutoLink() {
return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name); return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name);
} }
/** @returns {SNote} */ get note(): SNote {
get note() {
return this.shaca.notes[this.noteId]; return this.shaca.notes[this.noteId];
} }
/** @returns {SNote|null} */ get targetNote(): SNote | null | undefined {
get targetNote() {
if (this.type === 'relation') { if (this.type === 'relation') {
return this.shaca.notes[this.value]; return this.shaca.notes[this.value];
} }
} }
/** @returns {SNote|null} */ getNote(): SNote | null {
getNote() {
return this.shaca.getNote(this.noteId); return this.shaca.getNote(this.noteId);
} }
/** @returns {SNote|null} */ getTargetNote(): SNote | null {
getTargetNote() {
if (this.type !== 'relation') { if (this.type !== 'relation') {
throw new Error(`Attribute '${this.attributeId}' is not relation`); throw new Error(`Attribute '${this.attributeId}' is not relation`);
} }
@ -112,4 +111,4 @@ class SAttribute extends AbstractShacaEntity {
} }
} }
module.exports = SAttribute; export = SAttribute;

View File

@ -4,20 +4,20 @@ import sql = require('../../sql');
import utils = require('../../../services/utils'); import utils = require('../../../services/utils');
import AbstractShacaEntity = require('./abstract_shaca_entity'); import AbstractShacaEntity = require('./abstract_shaca_entity');
import escape = require('escape-html'); import escape = require('escape-html');
import { AttributeRow } from '../../../becca/entities/rows';
import { Blob } from '../../../services/blob-interface'; import { Blob } from '../../../services/blob-interface';
import SAttachment = require('./sattachment'); import SAttachment = require('./sattachment');
import SAttribute = require('./sattribute');
const LABEL = 'label'; const LABEL = 'label';
const RELATION = 'relation'; const RELATION = 'relation';
const CREDENTIALS = 'shareCredentials'; const CREDENTIALS = 'shareCredentials';
const isCredentials = (attr: AttributeRow) => attr.type === 'label' && attr.name === CREDENTIALS; const isCredentials = (attr: SAttribute) => attr.type === 'label' && attr.name === CREDENTIALS;
type NoteRow = [ string, string, string, string, string, string, boolean ]; type NoteRow = [ string, string, string, string, string, string, boolean ];
class SNote extends AbstractShacaEntity { class SNote extends AbstractShacaEntity {
private noteId: string; noteId: string;
private title: string; private title: string;
private type: string; private type: string;
private mime: string; private mime: string;
@ -27,10 +27,10 @@ class SNote extends AbstractShacaEntity {
private parentBranches: any[]; // fixme: set right data type once SBranch is ported. private parentBranches: any[]; // fixme: set right data type once SBranch is ported.
private parents: SNote[]; private parents: SNote[];
private children: SNote[]; private children: SNote[];
private ownedAttributes: any[]; // fixme: set right data type once SAttribute is ported. private ownedAttributes: SAttribute[];
private __attributeCache: any[] | null; // fixme private __attributeCache: SAttribute[] | null;
private __inheritableAttributeCache: any[] | null; // fixme private __inheritableAttributeCache: SAttribute[] | null;
private targetRelations: any[]; // fixme: SAttribute[] targetRelations: SAttribute[];
private attachments: SAttachment[]; private attachments: SAttachment[];
constructor([noteId, title, type, mime, blobId, utcDateModified, isProtected]: NoteRow) { constructor([noteId, title, type, mime, blobId, utcDateModified, isProtected]: NoteRow) {
@ -215,7 +215,6 @@ class SNote extends AbstractShacaEntity {
return this.__attributeCache; return this.__attributeCache;
} }
/** @returns {SAttribute[]} */
__getInheritableAttributes(path: string[]) { __getInheritableAttributes(path: string[]) {
if (path.includes(this.noteId)) { if (path.includes(this.noteId)) {
return []; return [];
@ -279,25 +278,25 @@ class SNote extends AbstractShacaEntity {
/** /**
* @param name - label name * @param name - label name
* @returns {SAttribute|null} label if it exists, null otherwise * @returns label if it exists, null otherwise
*/ */
getLabel(name: string) { return this.getAttribute(LABEL, name); } getLabel(name: string) { return this.getAttribute(LABEL, name); }
/** /**
* @param name - label name * @param name - label name
* @returns {SAttribute|null} label if it exists, null otherwise * @returns label if it exists, null otherwise
*/ */
getOwnedLabel(name: string) { return this.getOwnedAttribute(LABEL, name); } getOwnedLabel(name: string) { return this.getOwnedAttribute(LABEL, name); }
/** /**
* @param name - relation name * @param name - relation name
* @returns {SAttribute|null} relation if it exists, null otherwise * @returns relation if it exists, null otherwise
*/ */
getRelation(name: string) { return this.getAttribute(RELATION, name); } getRelation(name: string) { return this.getAttribute(RELATION, name); }
/** /**
* @param name - relation name * @param name - relation name
* @returns {SAttribute|null} relation if it exists, null otherwise * @returns relation if it exists, null otherwise
*/ */
getOwnedRelation(name: string) { return this.getOwnedAttribute(RELATION, name); } getOwnedRelation(name: string) { return this.getOwnedAttribute(RELATION, name); }
@ -337,8 +336,8 @@ class SNote extends AbstractShacaEntity {
/** /**
* @param type - attribute type (label, relation, etc.) * @param type - attribute type (label, relation, etc.)
* @param name - attribute name * @param name - attribute name
* @returns {SAttribute} attribute of the given type and name. If there are more such attributes, first is returned. * @returns attribute of the given type and name. If there are more such attributes, first is returned.
* Returns null if there's no such attribute belonging to this note. * Returns null if there's no such attribute belonging to this note.
*/ */
getAttribute(type: string, name: string) { getAttribute(type: string, name: string) {
const attributes = this.getAttributes(); const attributes = this.getAttributes();
@ -370,7 +369,7 @@ class SNote extends AbstractShacaEntity {
/** /**
* @param name - label name to filter * @param name - label name to filter
* @returns {SAttribute[]} all note's labels (attributes with type label), including inherited ones * @returns all note's labels (attributes with type label), including inherited ones
*/ */
getLabels(name: string) { getLabels(name: string) {
return this.getAttributes(LABEL, name); return this.getAttributes(LABEL, name);
@ -386,7 +385,7 @@ class SNote extends AbstractShacaEntity {
/** /**
* @param name - label name to filter * @param name - label name to filter
* @returns {SAttribute[]} all note's labels (attributes with type label), excluding inherited ones * @returns all note's labels (attributes with type label), excluding inherited ones
*/ */
getOwnedLabels(name: string) { getOwnedLabels(name: string) {
return this.getOwnedAttributes(LABEL, name); return this.getOwnedAttributes(LABEL, name);
@ -402,24 +401,24 @@ class SNote extends AbstractShacaEntity {
/** /**
* @param name - relation name to filter * @param name - relation name to filter
* @returns {SAttribute[]} all note's relations (attributes with type relation), including inherited ones * @returns all note's relations (attributes with type relation), including inherited ones
*/ */
getRelations(name: string) { getRelations(name: string) {
return this.getAttributes(RELATION, name); return this.getAttributes(RELATION, name);
} }
/** /**
* @param {string} name - relation name to filter * @param name - relation name to filter
* @returns {SAttribute[]} all note's relations (attributes with type relation), excluding inherited ones * @returns all note's relations (attributes with type relation), excluding inherited ones
*/ */
getOwnedRelations(name: string) { getOwnedRelations(name: string) {
return this.getOwnedAttributes(RELATION, name); return this.getOwnedAttributes(RELATION, name);
} }
/** /**
* @param {string} type - (optional) attribute type to filter * @param type - (optional) attribute type to filter
* @param {string} name - (optional) attribute name to filter * @param name - (optional) attribute name to filter
* @returns {SAttribute[]} note's "owned" attributes - excluding inherited ones * @returns note's "owned" attributes - excluding inherited ones
*/ */
getOwnedAttributes(type: string, name: string) { getOwnedAttributes(type: string, name: string) {
// it's a common mistake to include # or ~ into attribute name // it's a common mistake to include # or ~ into attribute name
@ -442,7 +441,7 @@ class SNote extends AbstractShacaEntity {
} }
/** /**
* @returns {SAttribute} attribute belonging to this specific note (excludes inherited attributes) * @returns attribute belonging to this specific note (excludes inherited attributes)
* *
* This method can be significantly faster than the getAttribute() * This method can be significantly faster than the getAttribute()
*/ */
@ -460,7 +459,6 @@ class SNote extends AbstractShacaEntity {
return !!this.targetRelations.find(rel => rel.name === 'template' || rel.name === 'inherit'); return !!this.targetRelations.find(rel => rel.name === 'template' || rel.name === 'inherit');
} }
/** @returns {SAttribute[]} */
getTargetRelations() { getTargetRelations() {
return this.targetRelations; return this.targetRelations;
} }

View File

@ -5,7 +5,7 @@ const shaca = require('./shaca.js');
const log = require('../../services/log'); const log = require('../../services/log');
const SNote = require('./entities/snote'); const SNote = require('./entities/snote');
const SBranch = require('./entities/sbranch.js'); const SBranch = require('./entities/sbranch.js');
const SAttribute = require('./entities/sattribute.js'); const SAttribute = require('./entities/sattribute');
const SAttachment = require('./entities/sattachment'); const SAttachment = require('./entities/sattachment');
const shareRoot = require('../share_root'); const shareRoot = require('../share_root');
const eventService = require('../../services/events'); const eventService = require('../../services/events');