/** * Class model for Message/Entity Interface */ import type { EntityInterface, EntityModelInterface } from "@/types/entity"; import type { MessageInterface } from "@/types/message"; import { MessageObject } from "./message"; import { clonePlain } from './clone-plain'; import type { CollectionIdentifier, EntityIdentifier, ServiceIdentifier } from "@/services"; export class EntityObject implements EntityModelInterface { private _data!: EntityInterface; private _properties: MessageObject | undefined = undefined; constructor() { this._data = { '@type': 'mail:entity', version: 1, provider: '', service: '', collection: null, identifier: null, signature: null, created: null, modified: null, properties: {'@type': 'mail:message'}, }; } fromJson(data: EntityInterface): EntityObject { this._data = clonePlain(data); this._properties = undefined; return this; } toJson(): EntityInterface { const json = this._properties ? { ...this._data, properties: this._properties.toJson(), } : this._data; return clonePlain(json); } clone(): EntityObject { return new EntityObject().fromJson(this.toJson()); } /** Metadata Properties */ get provider(): string { return this._data.provider; } get service(): ServiceIdentifier { return this._data.service as ServiceIdentifier; } get collection(): CollectionIdentifier { return this._data.collection as CollectionIdentifier; } get identifier(): EntityIdentifier { return this._data.identifier as EntityIdentifier; } get signature(): string | null { return this._data.signature; } get created(): string | null { return this._data.created; } get modified(): string | null { return this._data.modified; } /** Message Object Properties */ get properties(): MessageObject { if (!this._properties) { this._properties = new MessageObject().fromJson(this._data.properties as MessageInterface); } return this._properties; } }