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";
import SNote = require("./snote");
const AbstractShacaEntity = require('./abstract_shaca_entity');
type AttributeRow = [ string, string, string, string, string, boolean, number ];
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();
/** @param {string} */
this.attributeId = attributeId;
/** @param {string} */
this.noteId = noteId;
/** @param {string} */
this.type = type;
/** @param {string} */
this.name = name;
/** @param {int} */
this.position = position;
/** @param {string} */
this.value = value;
/** @param {boolean} */
this.isInheritable = !!isInheritable;
this.shaca.attributes[this.attributeId] = this;
@ -53,41 +59,34 @@ class SAttribute extends AbstractShacaEntity {
}
}
/** @returns {boolean} */
get isAffectingSubtree() {
return this.isInheritable
|| (this.type === 'relation' && ['template', 'inherit'].includes(this.name));
}
/** @returns {string} */
get targetNoteId() { // alias
return this.type === 'relation' ? this.value : undefined;
}
/** @returns {boolean} */
isAutoLink() {
return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name);
}
/** @returns {SNote} */
get note() {
get note(): SNote {
return this.shaca.notes[this.noteId];
}
/** @returns {SNote|null} */
get targetNote() {
get targetNote(): SNote | null | undefined {
if (this.type === 'relation') {
return this.shaca.notes[this.value];
}
}
/** @returns {SNote|null} */
getNote() {
getNote(): SNote | null {
return this.shaca.getNote(this.noteId);
}
/** @returns {SNote|null} */
getTargetNote() {
getTargetNote(): SNote | null {
if (this.type !== '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 AbstractShacaEntity = require('./abstract_shaca_entity');
import escape = require('escape-html');
import { AttributeRow } from '../../../becca/entities/rows';
import { Blob } from '../../../services/blob-interface';
import SAttachment = require('./sattachment');
import SAttribute = require('./sattribute');
const LABEL = 'label';
const RELATION = 'relation';
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 ];
class SNote extends AbstractShacaEntity {
private noteId: string;
noteId: string;
private title: string;
private type: 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 parents: SNote[];
private children: SNote[];
private ownedAttributes: any[]; // fixme: set right data type once SAttribute is ported.
private __attributeCache: any[] | null; // fixme
private __inheritableAttributeCache: any[] | null; // fixme
private targetRelations: any[]; // fixme: SAttribute[]
private ownedAttributes: SAttribute[];
private __attributeCache: SAttribute[] | null;
private __inheritableAttributeCache: SAttribute[] | null;
targetRelations: SAttribute[];
private attachments: SAttachment[];
constructor([noteId, title, type, mime, blobId, utcDateModified, isProtected]: NoteRow) {
@ -215,7 +215,6 @@ class SNote extends AbstractShacaEntity {
return this.__attributeCache;
}
/** @returns {SAttribute[]} */
__getInheritableAttributes(path: string[]) {
if (path.includes(this.noteId)) {
return [];
@ -279,25 +278,25 @@ class SNote extends AbstractShacaEntity {
/**
* @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); }
/**
* @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); }
/**
* @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); }
/**
* @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); }
@ -337,8 +336,8 @@ class SNote extends AbstractShacaEntity {
/**
* @param type - attribute type (label, relation, etc.)
* @param name - attribute name
* @returns {SAttribute} 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 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.
*/
getAttribute(type: string, name: string) {
const attributes = this.getAttributes();
@ -370,7 +369,7 @@ class SNote extends AbstractShacaEntity {
/**
* @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) {
return this.getAttributes(LABEL, name);
@ -386,7 +385,7 @@ class SNote extends AbstractShacaEntity {
/**
* @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) {
return this.getOwnedAttributes(LABEL, name);
@ -402,24 +401,24 @@ class SNote extends AbstractShacaEntity {
/**
* @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) {
return this.getAttributes(RELATION, name);
}
/**
* @param {string} name - relation name to filter
* @returns {SAttribute[]} all note's relations (attributes with type relation), excluding inherited ones
* @param name - relation name to filter
* @returns all note's relations (attributes with type relation), excluding inherited ones
*/
getOwnedRelations(name: string) {
return this.getOwnedAttributes(RELATION, name);
}
/**
* @param {string} type - (optional) attribute type to filter
* @param {string} name - (optional) attribute name to filter
* @returns {SAttribute[]} note's "owned" attributes - excluding inherited ones
* @param type - (optional) attribute type to filter
* @param name - (optional) attribute name to filter
* @returns note's "owned" attributes - excluding inherited ones
*/
getOwnedAttributes(type: string, name: string) {
// 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()
*/
@ -460,7 +459,6 @@ class SNote extends AbstractShacaEntity {
return !!this.targetRelations.find(rel => rel.name === 'template' || rel.name === 'inherit');
}
/** @returns {SAttribute[]} */
getTargetRelations() {
return this.targetRelations;
}

View File

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