From abc43cc7e1c976b8bacd21c94a2a8334b4d2902b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 09:59:46 +0200 Subject: [PATCH 01/13] refactor: update transport coding --- packages/transport/src/modules/index.ts | 1 - .../RelationshipTemplateController.ts | 2 +- .../relationships/RelationshipsController.ts | 161 ++++++++---------- .../relationships/local/CachedRelationship.ts | 48 ------ .../relationships/local/Relationship.ts | 58 ++++--- .../sync/DatawalletModificationsProcessor.ts | 10 +- .../RelationshipExternalEventProcessor.ts | 2 +- 7 files changed, 113 insertions(+), 169 deletions(-) delete mode 100644 packages/transport/src/modules/relationships/local/CachedRelationship.ts diff --git a/packages/transport/src/modules/index.ts b/packages/transport/src/modules/index.ts index 700c9b3af..774eae270 100644 --- a/packages/transport/src/modules/index.ts +++ b/packages/transport/src/modules/index.ts @@ -71,7 +71,6 @@ export * from "./publicRelationshipTemplateReferences/PublicRelationshipTemplate export * from "./relationships/backbone/BackboneGetRelationships"; export * from "./relationships/backbone/BackbonePostRelationship"; export * from "./relationships/backbone/RelationshipClient"; -export * from "./relationships/local/CachedRelationship"; export * from "./relationships/local/PeerDeletionInfo"; export * from "./relationships/local/Relationship"; export * from "./relationships/local/RelationshipAuditLog"; diff --git a/packages/transport/src/modules/relationshipTemplates/RelationshipTemplateController.ts b/packages/transport/src/modules/relationshipTemplates/RelationshipTemplateController.ts index 240a7d781..2ed79f6d7 100644 --- a/packages/transport/src/modules/relationshipTemplates/RelationshipTemplateController.ts +++ b/packages/transport/src/modules/relationshipTemplates/RelationshipTemplateController.ts @@ -345,7 +345,7 @@ export class RelationshipTemplateController extends TransportController { } public async cleanupTemplatesOfDecomposedRelationship(relationship: Relationship): Promise { - const templateOfRelationship = await this.getRelationshipTemplate(relationship.cache!.templateId); + const templateOfRelationship = await this.getRelationshipTemplate(relationship.templateId); if (templateOfRelationship && (!templateOfRelationship.isOwn || templateOfRelationship.cache!.maxNumberOfAllocations === 1)) { await this.templates.delete(templateOfRelationship); } diff --git a/packages/transport/src/modules/relationships/RelationshipsController.ts b/packages/transport/src/modules/relationships/RelationshipsController.ts index 74c2fb487..2b8b51ab8 100644 --- a/packages/transport/src/modules/relationships/RelationshipsController.ts +++ b/packages/transport/src/modules/relationships/RelationshipsController.ts @@ -16,7 +16,6 @@ import { RelationshipSecretController } from "./RelationshipSecretController"; import { BackbonePutRelationshipsResponse } from "./backbone/BackbonePutRelationship"; import { BackboneRelationship } from "./backbone/BackboneRelationship"; import { RelationshipClient } from "./backbone/RelationshipClient"; -import { CachedRelationship } from "./local/CachedRelationship"; import { PeerDeletionInfo } from "./local/PeerDeletionInfo"; import { Relationship } from "./local/Relationship"; import { RelationshipAuditLog } from "./local/RelationshipAuditLog"; @@ -56,57 +55,57 @@ export class RelationshipsController extends TransportController { return relationships; } - public async updateCache(ids: string[]): Promise { - if (ids.length < 1) { - return []; - } + // public async updateCache(ids: string[]): Promise { + // if (ids.length < 1) { + // return []; + // } - const resultItems = (await this.client.getRelationships({ ids })).value; - const promises = []; - for await (const resultItem of resultItems) { - promises.push(this.updateExistingRelationshipInDb(resultItem.id, resultItem)); - } - return await Promise.all(promises); - } + // const resultItems = (await this.client.getRelationships({ ids })).value; + // const promises = []; + // for await (const resultItem of resultItems) { + // promises.push(this.updateExistingRelationshipInDb(resultItem.id, resultItem)); + // } + // return await Promise.all(promises); + // } - public async fetchCaches(ids: CoreId[]): Promise<{ id: CoreId; cache: CachedRelationship }[]> { - if (ids.length === 0) return []; + // public async fetchCaches(ids: CoreId[]): Promise<{ id: CoreId; cache: CachedRelationship }[]> { + // if (ids.length === 0) return []; - const backboneRelationships = await (await this.client.getRelationships({ ids: ids.map((id) => id.id) })).value.collect(); + // const backboneRelationships = await (await this.client.getRelationships({ ids: ids.map((id) => id.id) })).value.collect(); - const decryptionPromises = backboneRelationships.map(async (r) => { - const relationshipDoc = await this.relationships.read(r.id); - if (!relationshipDoc) { - this._log.error( - `Relationship '${r.id}' not found in local database and the cache fetching was therefore skipped. This should not happen and might be a bug in the application logic.` - ); - return; - } + // const decryptionPromises = backboneRelationships.map(async (r) => { + // const relationshipDoc = await this.relationships.read(r.id); + // if (!relationshipDoc) { + // this._log.error( + // `Relationship '${r.id}' not found in local database and the cache fetching was therefore skipped. This should not happen and might be a bug in the application logic.` + // ); + // return; + // } - const relationship = Relationship.from(relationshipDoc); + // const relationship = Relationship.from(relationshipDoc); - return { - id: CoreId.from(r.id), - cache: await this.decryptRelationship(r, relationship.relationshipSecretId) - }; - }); + // return { + // id: CoreId.from(r.id), + // cache: await this.decryptRelationship(r, relationship.relationshipSecretId) + // }; + // }); - const caches = await Promise.all(decryptionPromises); - return caches.filter((c) => c !== undefined); - } + // const caches = await Promise.all(decryptionPromises); + // return caches.filter((c) => c !== undefined); + // } - @log() - private async updateExistingRelationshipInDb(id: string, response: BackboneRelationship) { - const relationshipDoc = await this.relationships.read(id); - if (!relationshipDoc) throw TransportCoreErrors.general.recordNotFound(Relationship, id); + // @log() + // private async updateExistingRelationshipInDb(id: string, response: BackboneRelationship) { + // const relationshipDoc = await this.relationships.read(id); + // if (!relationshipDoc) throw TransportCoreErrors.general.recordNotFound(Relationship, id); - const relationship = Relationship.from(relationshipDoc); + // const relationship = Relationship.from(relationshipDoc); - await this.updateCacheOfRelationship(relationship, response); - relationship.status = response.status; - await this.relationships.update(relationshipDoc, relationship); - return relationship; - } + // await this.updateCacheOfRelationship(relationship, response); + // relationship.status = response.status; + // await this.relationships.update(relationshipDoc, relationship); + // return relationship; + // } public async getRelationshipToIdentity(address: CoreAddress, status?: RelationshipStatus): Promise { const query: any = { peerAddress: address.toString() }; @@ -246,10 +245,11 @@ export class RelationshipsController extends TransportController { } public async accept(relationshipId: CoreId): Promise { - const relationship = await this.getRelationshipWithCache(relationshipId); + const relationship = await this.getRelationship(relationshipId); + if (!relationship) throw TransportCoreErrors.general.recordNotFound(Relationship, relationshipId.toString()); this.assertRelationshipStatus(relationship, RelationshipStatus.Pending); - const lastAuditLogEntry = relationship.cache.auditLog[relationship.cache.auditLog.length - 1]; + const lastAuditLogEntry = relationship.auditLog[relationship.auditLog.length - 1]; if (!lastAuditLogEntry.createdBy.equals(relationship.peer.address)) { throw TransportCoreErrors.relationships.operationOnlyAllowedForPeer(`Only your peer can accept the relationship ${relationshipId.toString()}`); } @@ -257,10 +257,11 @@ export class RelationshipsController extends TransportController { } public async reject(relationshipId: CoreId): Promise { - const relationship = await this.getRelationshipWithCache(relationshipId); + const relationship = await this.getRelationship(relationshipId); + if (!relationship) throw TransportCoreErrors.general.recordNotFound(Relationship, relationshipId.toString()); this.assertRelationshipStatus(relationship, RelationshipStatus.Pending); - const lastAuditLogEntry = relationship.cache.auditLog[relationship.cache.auditLog.length - 1]; + const lastAuditLogEntry = relationship.auditLog[relationship.auditLog.length - 1]; if (!lastAuditLogEntry.createdBy.equals(relationship.peer.address)) { throw TransportCoreErrors.relationships.operationOnlyAllowedForPeer( `Only your peer can reject the relationship ${relationshipId.toString()}. Revoke the relationship instead.` @@ -270,10 +271,11 @@ export class RelationshipsController extends TransportController { } public async revoke(relationshipId: CoreId): Promise { - const relationship = await this.getRelationshipWithCache(relationshipId); + const relationship = await this.getRelationship(relationshipId); + if (!relationship) throw TransportCoreErrors.general.recordNotFound(Relationship, relationshipId.toString()); this.assertRelationshipStatus(relationship, RelationshipStatus.Pending); - const lastAuditLogEntry = relationship.cache.auditLog[relationship.cache.auditLog.length - 1]; + const lastAuditLogEntry = relationship.auditLog[relationship.auditLog.length - 1]; if (lastAuditLogEntry.createdBy.equals(relationship.peer.address)) { throw TransportCoreErrors.relationships.operationOnlyAllowedForPeer( `Only your peer can revoke the relationship ${relationshipId.toString()}. Reject the relationship instead.` @@ -283,17 +285,19 @@ export class RelationshipsController extends TransportController { } public async terminate(relationshipId: CoreId): Promise { - const relationship = await this.getRelationshipWithCache(relationshipId); + const relationship = await this.getRelationship(relationshipId); + if (!relationship) throw TransportCoreErrors.general.recordNotFound(Relationship, relationshipId.toString()); this.assertRelationshipStatus(relationship, RelationshipStatus.Active); return await this.completeOperationWithBackboneCall(RelationshipAuditLogEntryReason.Termination, relationshipId); } public async requestReactivation(relationshipId: CoreId): Promise { - const relationship = await this.getRelationshipWithCache(relationshipId); + const relationship = await this.getRelationship(relationshipId); + if (!relationship) throw TransportCoreErrors.general.recordNotFound(Relationship, relationshipId.toString()); this.assertRelationshipStatus(relationship, RelationshipStatus.Terminated); - const lastAuditLogEntry = relationship.cache.auditLog[relationship.cache.auditLog.length - 1]; + const lastAuditLogEntry = relationship.auditLog[relationship.auditLog.length - 1]; if (lastAuditLogEntry.reason === RelationshipAuditLogEntryReason.ReactivationRequested) { if (lastAuditLogEntry.createdBy.equals(relationship.peer.address)) { throw TransportCoreErrors.relationships.reactivationAlreadyRequested( @@ -307,10 +311,11 @@ export class RelationshipsController extends TransportController { } public async rejectReactivation(relationshipId: CoreId): Promise { - const relationship = await this.getRelationshipWithCache(relationshipId); + const relationship = await this.getRelationship(relationshipId); + if (!relationship) throw TransportCoreErrors.general.recordNotFound(Relationship, relationshipId.toString()); this.assertRelationshipStatus(relationship, RelationshipStatus.Terminated); - const lastAuditLogEntry = relationship.cache.auditLog[relationship.cache.auditLog.length - 1]; + const lastAuditLogEntry = relationship.auditLog[relationship.auditLog.length - 1]; if (lastAuditLogEntry.reason !== RelationshipAuditLogEntryReason.ReactivationRequested) { throw TransportCoreErrors.relationships.reactivationNotRequested(relationshipId.toString()); } @@ -324,10 +329,11 @@ export class RelationshipsController extends TransportController { } public async revokeReactivation(relationshipId: CoreId): Promise { - const relationship = await this.getRelationshipWithCache(relationshipId); + const relationship = await this.getRelationship(relationshipId); + if (!relationship) throw TransportCoreErrors.general.recordNotFound(Relationship, relationshipId.toString()); this.assertRelationshipStatus(relationship, RelationshipStatus.Terminated); - const lastAuditLogEntry = relationship.cache.auditLog[relationship.cache.auditLog.length - 1]; + const lastAuditLogEntry = relationship.auditLog[relationship.auditLog.length - 1]; if (lastAuditLogEntry.reason !== RelationshipAuditLogEntryReason.ReactivationRequested) { throw TransportCoreErrors.relationships.reactivationNotRequested(relationshipId.toString()); } @@ -340,10 +346,11 @@ export class RelationshipsController extends TransportController { } public async acceptReactivation(relationshipId: CoreId): Promise { - const relationship = await this.getRelationshipWithCache(relationshipId); + const relationship = await this.getRelationship(relationshipId); + if (!relationship) throw TransportCoreErrors.general.recordNotFound(Relationship, relationshipId.toString()); this.assertRelationshipStatus(relationship, RelationshipStatus.Terminated); - const lastAuditLogEntry = relationship.cache.auditLog[relationship.cache.auditLog.length - 1]; + const lastAuditLogEntry = relationship.auditLog[relationship.auditLog.length - 1]; if (lastAuditLogEntry.reason !== RelationshipAuditLogEntryReason.ReactivationRequested) { throw TransportCoreErrors.relationships.reactivationNotRequested(relationshipId.toString()); } @@ -355,7 +362,8 @@ export class RelationshipsController extends TransportController { } public async decompose(relationshipId: CoreId): Promise { - const relationship = await this.getRelationshipWithCache(relationshipId); + const relationship = await this.getRelationship(relationshipId); + if (!relationship) throw TransportCoreErrors.general.recordNotFound(Relationship, relationshipId.toString()); this.assertRelationshipStatus(relationship, RelationshipStatus.Terminated, RelationshipStatus.DeletionProposed); const result = await this.client.decomposeRelationship(relationshipId.toString()); @@ -370,28 +378,19 @@ export class RelationshipsController extends TransportController { this.eventBus.publish(new RelationshipDecomposedBySelfEvent(this.parent.identity.address.toString(), { relationshipId })); } - private async getRelationshipWithCache(id: CoreId): Promise { - const relationship = await this.getRelationship(id); - if (!relationship) throw TransportCoreErrors.general.recordNotFound(Relationship, id.toString()); - if (!relationship.cache) await this.updateCacheOfRelationship(relationship); - if (!relationship.cache) throw this.newCacheEmptyError(Relationship, id.toString()); - - return relationship as Relationship & { cache: CachedRelationship }; - } - private assertRelationshipStatus(relationship: Relationship, ...status: RelationshipStatus[]) { if (status.includes(relationship.status)) return; throw TransportCoreErrors.relationships.wrongRelationshipStatus(relationship.id.toString(), relationship.status); } - private async updateCacheOfRelationship(relationship: Relationship, response?: BackboneRelationship) { - response ??= (await this.client.getRelationship(relationship.id.toString())).value; + // private async updateCacheOfRelationship(relationship: Relationship, response?: BackboneRelationship) { + // response ??= (await this.client.getRelationship(relationship.id.toString())).value; - const cachedRelationship = await this.decryptRelationship(response, relationship.relationshipSecretId); + // const cachedRelationship = await this.decryptRelationship(response, relationship.relationshipSecretId); - relationship.setCache(cachedRelationship); - } + // relationship.setCache(cachedRelationship); + // } private async decryptRelationship(response: BackboneRelationship, relationshipSecretId: CoreId) { if (!response.creationContent) throw new TransportError("Creation content is missing"); @@ -402,13 +401,11 @@ export class RelationshipsController extends TransportController { const creationContent = await this.decryptCreationContent(response.creationContent, CoreAddress.from(response.from), relationshipSecretId); - const cachedRelationship = CachedRelationship.from({ + return { creationContent: creationContent.content, templateId, auditLog: RelationshipAuditLog.fromBackboneAuditLog(response.auditLog) - }); - - return cachedRelationship; + }; } private async prepareCreationContent(relationshipSecretId: CoreId, template: RelationshipTemplate, content: ISerializable): Promise { @@ -455,7 +452,7 @@ export class RelationshipsController extends TransportController { await this.secrets.convertSecrets(relationship.relationshipSecretId, cipher.publicCreationResponseContentCrypto); } - relationship.cache!.auditLog = RelationshipAuditLog.fromBackboneAuditLog(backboneRelationship.auditLog); + relationship.auditLog = RelationshipAuditLog.fromBackboneAuditLog(backboneRelationship.auditLog); relationship.status = backboneRelationship.status; await this.relationships.update(relationshipDoc, relationship); @@ -577,14 +574,6 @@ export class RelationshipsController extends TransportController { const relationship = Relationship.from(relationshipDoc); - if (!relationship.cache) { - await this.updateCacheOfRelationship(relationship); - } - - if (!relationship.cache) { - throw this.newCacheEmptyError(Relationship, id.toString()); - } - let backboneResponse: BackbonePutRelationshipsResponse; switch (operation) { case RelationshipAuditLogEntryReason.AcceptanceOfCreation: @@ -625,7 +614,7 @@ export class RelationshipsController extends TransportController { throw new TransportError("operation not supported"); } relationship.status = backboneResponse.status; - relationship.cache.auditLog = RelationshipAuditLog.fromBackboneAuditLog(backboneResponse.auditLog); + relationship.auditLog = RelationshipAuditLog.fromBackboneAuditLog(backboneResponse.auditLog); await this.relationships.update(relationshipDoc, relationship); this.publishEventAfterCompletedOperation(operation, relationship); diff --git a/packages/transport/src/modules/relationships/local/CachedRelationship.ts b/packages/transport/src/modules/relationships/local/CachedRelationship.ts deleted file mode 100644 index 9c6880c5c..000000000 --- a/packages/transport/src/modules/relationships/local/CachedRelationship.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { ISerializable, Serializable, serialize, type, validate } from "@js-soft/ts-serval"; -import { CoreDate, CoreId, ICoreDate, ICoreId } from "@nmshd/core-types"; -import { IRelationshipAuditLogEntry, RelationshipAuditLogEntry } from "./RelationshipAuditLogEntry"; - -export interface ICachedRelationship extends ISerializable { - templateId: ICoreId; - creationContent: ISerializable; - - lastMessageSentAt?: ICoreDate; - lastMessageReceivedAt?: ICoreDate; - auditLog: IRelationshipAuditLogEntry[]; -} - -@type("CachedRelationship") -export class CachedRelationship extends Serializable implements ICachedRelationship { - @validate() - @serialize() - public templateId: CoreId; - - @validate() - @serialize() - public creationContent: Serializable; - - @validate({ nullable: true }) - @serialize() - public lastMessageSentAt?: CoreDate; - - @validate({ nullable: true }) - @serialize() - public lastMessageReceivedAt?: CoreDate; - - @validate() - @serialize({ type: RelationshipAuditLogEntry }) - public auditLog: RelationshipAuditLogEntry[]; - - public static override preFrom(value: any): any { - if (typeof value.template !== "undefined") { - value.templateId = value.template.id; - delete value.template; - } - - return value; - } - - public static from(value: ICachedRelationship): CachedRelationship { - return this.fromAny(value); - } -} diff --git a/packages/transport/src/modules/relationships/local/Relationship.ts b/packages/transport/src/modules/relationships/local/Relationship.ts index 6ec1681f9..4ffe93d50 100644 --- a/packages/transport/src/modules/relationships/local/Relationship.ts +++ b/packages/transport/src/modules/relationships/local/Relationship.ts @@ -1,13 +1,13 @@ -import { ISerializable, serialize, type, validate } from "@js-soft/ts-serval"; -import { CoreDate, CoreId, ICoreId } from "@nmshd/core-types"; +import { ISerializable, Serializable, serialize, type, validate } from "@js-soft/ts-serval"; +import { CoreDate, CoreId, ICoreDate, ICoreId } from "@nmshd/core-types"; import { nameof } from "ts-simple-nameof"; import { CoreSynchronizable, ICoreSynchronizable, TransportError } from "../../../core"; import { Identity, IIdentity } from "../../accounts/data/Identity"; import { BackboneGetRelationshipResponse } from "../backbone/BackboneGetRelationships"; import { RelationshipStatus } from "../transmission/RelationshipStatus"; -import { CachedRelationship, ICachedRelationship } from "./CachedRelationship"; import { IPeerDeletionInfo, PeerDeletionInfo } from "./PeerDeletionInfo"; import { RelationshipAuditLog } from "./RelationshipAuditLog"; +import { IRelationshipAuditLogEntry, RelationshipAuditLogEntry } from "./RelationshipAuditLogEntry"; export interface IRelationship extends ICoreSynchronizable { relationshipSecretId: ICoreId; @@ -15,8 +15,12 @@ export interface IRelationship extends ICoreSynchronizable { peerDeletionInfo?: IPeerDeletionInfo; status: RelationshipStatus; - cache?: ICachedRelationship; - cachedAt?: CoreDate; + templateId: ICoreId; + creationContent: ISerializable; + + lastMessageSentAt?: ICoreDate; + lastMessageReceivedAt?: ICoreDate; + auditLog: IRelationshipAuditLogEntry[]; metadata?: any; metadataModifiedAt?: CoreDate; @@ -30,7 +34,15 @@ export class Relationship extends CoreSynchronizable implements IRelationship { nameof((r) => r.relationshipSecretId), nameof((r) => r.peer), nameof((r) => r.status), - nameof((r) => r.peerDeletionInfo) + nameof((r) => r.peerDeletionInfo), + nameof((r) => r.auditLog) + ]; + + public override readonly contentProperties = [ + nameof((r) => r.templateId), + nameof((r) => r.creationContent), + nameof((r) => r.lastMessageSentAt), + nameof((r) => r.lastMessageReceivedAt) ]; public override readonly metadataProperties = [nameof((r) => r.metadata), nameof((r) => r.metadataModifiedAt)]; @@ -51,13 +63,25 @@ export class Relationship extends CoreSynchronizable implements IRelationship { @serialize() public status: RelationshipStatus; + @validate() + @serialize() + public templateId: CoreId; + + @validate() + @serialize() + public creationContent: Serializable; + @validate({ nullable: true }) @serialize() - public cache?: CachedRelationship; + public lastMessageSentAt?: CoreDate; @validate({ nullable: true }) @serialize() - public cachedAt?: CoreDate; + public lastMessageReceivedAt?: CoreDate; + + @validate() + @serialize({ type: RelationshipAuditLogEntry }) + public auditLog: RelationshipAuditLogEntry[]; @validate({ nullable: true }) @serialize() @@ -73,7 +97,6 @@ export class Relationship extends CoreSynchronizable implements IRelationship { // Adds flattened peerAddress and templateId to the JSON stored in the database. // This helps us to boost the performance of database queries that include these fields. json.peerAddress = this.peer.address.toString(); - json.templateId = this.cache?.templateId.toString(); return json; } @@ -84,19 +107,14 @@ export class Relationship extends CoreSynchronizable implements IRelationship { creationContent: ISerializable, relationshipSecretId: CoreId ): Relationship { - const cache = CachedRelationship.from({ - creationContent, - templateId: CoreId.from(response.relationshipTemplateId), - auditLog: RelationshipAuditLog.fromBackboneAuditLog(response.auditLog) - }); - return Relationship.from({ id: CoreId.from(response.id), relationshipSecretId: relationshipSecretId, peer: peer, status: RelationshipStatus.Pending, - cache: cache, - cachedAt: CoreDate.utc() + creationContent, + templateId: CoreId.from(response.relationshipTemplateId), + auditLog: RelationshipAuditLog.fromBackboneAuditLog(response.auditLog) }); } @@ -104,12 +122,6 @@ export class Relationship extends CoreSynchronizable implements IRelationship { return this.fromAny(value); } - public setCache(cache: CachedRelationship): this { - this.cache = cache; - this.cachedAt = CoreDate.utc(); - return this; - } - public setMetadata(metadata: any): this { this.metadata = metadata; this.metadataModifiedAt = CoreDate.utc(); diff --git a/packages/transport/src/modules/sync/DatawalletModificationsProcessor.ts b/packages/transport/src/modules/sync/DatawalletModificationsProcessor.ts index 3be530725..1ed594ccb 100644 --- a/packages/transport/src/modules/sync/DatawalletModificationsProcessor.ts +++ b/packages/transport/src/modules/sync/DatawalletModificationsProcessor.ts @@ -14,8 +14,6 @@ import { FileController } from "../files/FileController"; import { CachedFile } from "../files/local/CachedFile"; import { File } from "../files/local/File"; import { MessageController } from "../messages/MessageController"; -import { CachedRelationship } from "../relationships/local/CachedRelationship"; -import { Relationship } from "../relationships/local/Relationship"; import { RelationshipsController } from "../relationships/RelationshipsController"; import { CachedRelationshipTemplate } from "../relationshipTemplates/local/CachedRelationshipTemplate"; import { RelationshipTemplate } from "../relationshipTemplates/local/RelationshipTemplate"; @@ -160,10 +158,6 @@ export class DatawalletModificationsProcessor { await this.saveNewCaches(caches.relationshipTemplates, DbCollectionName.RelationshipTemplates, RelationshipTemplate); await this.saveNewCaches(caches.tokens, DbCollectionName.Tokens, Token); await this.saveNewCaches(caches.identityDeletionProcesses, DbCollectionName.IdentityDeletionProcess, IdentityDeletionProcess); - - // Need to fetch the cache for relationships after the cache for relationship templates was fetched, because when building the relationship cache, the cache of thecorresponding relationship template is needed - const relationshipCaches = await this.cacheFetcher.fetchCacheFor({ relationships: cacheChangesGroupedByCollection.relationshipIds }); - await this.saveNewCaches(relationshipCaches.relationships, DbCollectionName.Relationships, Relationship); } @log() @@ -221,13 +215,12 @@ export class CacheFetcher { public async fetchCacheFor(input: FetchCacheInput): Promise { const caches = await Promise.all([ this.fetchCaches(this.fileController, input.files), - this.fetchCaches(this.relationshipController, input.relationships), this.fetchCaches(this.relationshipTemplateController, input.relationshipTemplates), this.fetchCaches(this.tokenController, input.tokens), this.fetchCaches(this.identityDeletionProcessController, input.identityDeletionProcesses) ]); - const output: FetchCacheOutput = { files: caches[0], relationships: caches[1], relationshipTemplates: caches[2], tokens: caches[3], identityDeletionProcesses: caches[4] }; + const output: FetchCacheOutput = { files: caches[0], relationshipTemplates: caches[1], tokens: caches[2], identityDeletionProcesses: caches[3] }; return output; } @@ -250,7 +243,6 @@ interface FetchCacheInput { interface FetchCacheOutput { files: FetchCacheOutputItem[]; - relationships: FetchCacheOutputItem[]; relationshipTemplates: FetchCacheOutputItem[]; tokens: FetchCacheOutputItem[]; identityDeletionProcesses: FetchCacheOutputItem[]; diff --git a/packages/transport/src/modules/sync/externalEventProcessors/RelationshipExternalEventProcessor.ts b/packages/transport/src/modules/sync/externalEventProcessors/RelationshipExternalEventProcessor.ts index 0d918e4ec..a47f1bd2e 100644 --- a/packages/transport/src/modules/sync/externalEventProcessors/RelationshipExternalEventProcessor.ts +++ b/packages/transport/src/modules/sync/externalEventProcessors/RelationshipExternalEventProcessor.ts @@ -12,6 +12,6 @@ export abstract class RelationshipExternalEventProcessor extends ExternalEventPr private hasRelationshipChanged(changedRelationship: Relationship, oldRelationship?: Relationship): boolean { if (!oldRelationship) return true; - return oldRelationship.cache!.auditLog.length !== changedRelationship.cache!.auditLog.length; + return oldRelationship.auditLog.length !== changedRelationship.auditLog.length; } } From aaa96fc80e7bf283a4b6fdfe57f5fa73c266c5b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 10:00:00 +0200 Subject: [PATCH 02/13] test: update transport tests --- .../transport/test/end2end/End2End.test.ts | 72 +++++++++---------- .../RelationshipTemplateController.test.ts | 2 +- .../RelationshipsController.test.ts | 62 +++++----------- .../RelationshipsCustomContent.test.ts | 4 +- .../relationships/decomposition.test.ts | 2 +- .../sync/SyncController.relationships.test.ts | 3 +- 6 files changed, 59 insertions(+), 86 deletions(-) diff --git a/packages/transport/test/end2end/End2End.test.ts b/packages/transport/test/end2end/End2End.test.ts index 848470fa9..b2578ac9e 100644 --- a/packages/transport/test/end2end/End2End.test.ts +++ b/packages/transport/test/end2end/End2End.test.ts @@ -94,8 +94,8 @@ describe("RelationshipTest: Accept", function () { expect(request.status).toStrictEqual(RelationshipStatus.Pending); - expect(request.cache?.auditLog).toHaveLength(1); - expect(request.cache!.auditLog[0].newStatus).toBe(RelationshipStatus.Pending); + expect(request.auditLog).toHaveLength(1); + expect(request.auditLog[0].newStatus).toBe(RelationshipStatus.Pending); const syncedRelationships = await TestUtil.syncUntilHasRelationships(from); expect(syncedRelationships).toHaveLength(1); @@ -107,8 +107,8 @@ describe("RelationshipTest: Accept", function () { expect(acceptedRelationshipFromSelf.id.toString()).toStrictEqual(relationshipId.toString()); expect(acceptedRelationshipFromSelf.status).toStrictEqual(RelationshipStatus.Active); - expect(acceptedRelationshipFromSelf.cache?.auditLog).toHaveLength(2); - expect(acceptedRelationshipFromSelf.cache!.auditLog[1].newStatus).toBe(RelationshipStatus.Active); + expect(acceptedRelationshipFromSelf.auditLog).toHaveLength(2); + expect(acceptedRelationshipFromSelf.auditLog[1].newStatus).toBe(RelationshipStatus.Active); expect(acceptedRelationshipFromSelf.peer).toBeDefined(); @@ -119,8 +119,8 @@ describe("RelationshipTest: Accept", function () { expect(acceptedRelationshipPeer.id.toString()).toStrictEqual(relationshipId.toString()); expect(acceptedRelationshipPeer.status).toStrictEqual(RelationshipStatus.Active); - expect(acceptedRelationshipPeer.cache?.auditLog).toHaveLength(2); - expect(acceptedRelationshipPeer.cache!.auditLog[1].newStatus).toBe(RelationshipStatus.Active); + expect(acceptedRelationshipPeer.auditLog).toHaveLength(2); + expect(acceptedRelationshipPeer.auditLog[1].newStatus).toBe(RelationshipStatus.Active); expect(acceptedRelationshipPeer.peer).toBeDefined(); expect(acceptedRelationshipPeer.peer.address.toString()).toStrictEqual(templateTo.cache?.identity.address.toString()); }); @@ -203,8 +203,8 @@ describe("RelationshipTest: Reject", function () { const rejectedRelationshipFromSelf = await from.relationships.reject(relationshipId); expect(rejectedRelationshipFromSelf.id.toString()).toStrictEqual(relationshipId.toString()); expect(rejectedRelationshipFromSelf.status).toStrictEqual(RelationshipStatus.Rejected); - expect(rejectedRelationshipFromSelf.cache?.auditLog).toHaveLength(2); - expect(rejectedRelationshipFromSelf.cache!.auditLog[1].newStatus).toBe(RelationshipStatus.Rejected); + expect(rejectedRelationshipFromSelf.auditLog).toHaveLength(2); + expect(rejectedRelationshipFromSelf.auditLog[1].newStatus).toBe(RelationshipStatus.Rejected); expect(rejectedRelationshipFromSelf.peer).toBeDefined(); @@ -215,8 +215,8 @@ describe("RelationshipTest: Reject", function () { expect(rejectedRelationshipPeer.id.toString()).toStrictEqual(relationshipId.toString()); expect(rejectedRelationshipPeer.status).toStrictEqual(RelationshipStatus.Rejected); - expect(rejectedRelationshipPeer.cache?.auditLog).toHaveLength(2); - expect(rejectedRelationshipPeer.cache!.auditLog[1].newStatus).toBe(RelationshipStatus.Rejected); + expect(rejectedRelationshipPeer.auditLog).toHaveLength(2); + expect(rejectedRelationshipPeer.auditLog[1].newStatus).toBe(RelationshipStatus.Rejected); expect(rejectedRelationshipPeer.peer).toBeDefined(); expect(rejectedRelationshipPeer.peer.address.toString()).toStrictEqual(templateTo.cache?.identity.address.toString()); }); @@ -304,8 +304,8 @@ describe("RelationshipTest: Revoke", function () { expect(revokedRelationshipSelf.id.toString()).toStrictEqual(relationshipId.toString()); expect(revokedRelationshipSelf.status).toStrictEqual(RelationshipStatus.Revoked); - expect(revokedRelationshipSelf.cache?.auditLog).toHaveLength(2); - expect(revokedRelationshipSelf.cache!.auditLog[1].newStatus).toBe(RelationshipStatus.Revoked); + expect(revokedRelationshipSelf.auditLog).toHaveLength(2); + expect(revokedRelationshipSelf.auditLog[1].newStatus).toBe(RelationshipStatus.Revoked); const syncedRelationshipsPeer = await TestUtil.syncUntilHasRelationships(templator); expect(syncedRelationshipsPeer).toHaveLength(1); @@ -313,8 +313,8 @@ describe("RelationshipTest: Revoke", function () { expect(revokedRelationshipPeer.status).toStrictEqual(RelationshipStatus.Revoked); expect(revokedRelationshipPeer.id.toString()).toStrictEqual(relationshipId.toString()); expect(revokedRelationshipPeer.status).toStrictEqual(RelationshipStatus.Revoked); - expect(revokedRelationshipPeer.cache?.auditLog).toHaveLength(2); - expect(revokedRelationshipPeer.cache!.auditLog[1].newStatus).toBe(RelationshipStatus.Revoked); + expect(revokedRelationshipPeer.auditLog).toHaveLength(2); + expect(revokedRelationshipPeer.auditLog[1].newStatus).toBe(RelationshipStatus.Revoked); expect(revokedRelationshipPeer.peer).toBeDefined(); }); @@ -399,8 +399,8 @@ describe("RelationshipTest: Terminate", function () { const terminatedRelationshipFromSelf = await from.relationships.terminate(relationshipId); expect(terminatedRelationshipFromSelf.id.toString()).toStrictEqual(relationshipId.toString()); expect(terminatedRelationshipFromSelf.status).toStrictEqual(RelationshipStatus.Terminated); - expect(terminatedRelationshipFromSelf.cache?.auditLog).toHaveLength(3); - expect(terminatedRelationshipFromSelf.cache!.auditLog[2].reason).toBe(RelationshipAuditLogEntryReason.Termination); + expect(terminatedRelationshipFromSelf.auditLog).toHaveLength(3); + expect(terminatedRelationshipFromSelf.auditLog[2].reason).toBe(RelationshipAuditLogEntryReason.Termination); const syncedRelationshipsPeer = await TestUtil.syncUntilHasRelationships(to); expect(syncedRelationshipsPeer).toHaveLength(1); @@ -408,8 +408,8 @@ describe("RelationshipTest: Terminate", function () { expect(terminatedRelationshipPeer.id.toString()).toStrictEqual(relationshipId.toString()); expect(terminatedRelationshipPeer.status).toStrictEqual(RelationshipStatus.Terminated); - expect(terminatedRelationshipPeer.cache?.auditLog).toHaveLength(3); - expect(terminatedRelationshipPeer.cache!.auditLog[2].reason).toBe(RelationshipAuditLogEntryReason.Termination); + expect(terminatedRelationshipPeer.auditLog).toHaveLength(3); + expect(terminatedRelationshipPeer.auditLog[2].reason).toBe(RelationshipAuditLogEntryReason.Termination); }); }); @@ -446,8 +446,8 @@ describe("RelationshipTest: Request Reactivation", function () { expect(reactivationRequestedRelationshipFromSelf.id.toString()).toStrictEqual(relationshipId.toString()); expect(reactivationRequestedRelationshipFromSelf.status).toStrictEqual(RelationshipStatus.Terminated); - expect(reactivationRequestedRelationshipFromSelf.cache?.auditLog).toHaveLength(4); - expect(reactivationRequestedRelationshipFromSelf.cache!.auditLog[3].reason).toBe(RelationshipAuditLogEntryReason.ReactivationRequested); + expect(reactivationRequestedRelationshipFromSelf.auditLog).toHaveLength(4); + expect(reactivationRequestedRelationshipFromSelf.auditLog[3].reason).toBe(RelationshipAuditLogEntryReason.ReactivationRequested); const syncedRelationshipsPeer = await TestUtil.syncUntilHasRelationships(to); expect(syncedRelationshipsPeer).toHaveLength(1); @@ -455,8 +455,8 @@ describe("RelationshipTest: Request Reactivation", function () { expect(reactivationRequestedRelationshipPeer.id.toString()).toStrictEqual(relationshipId.toString()); expect(reactivationRequestedRelationshipPeer.status).toStrictEqual(RelationshipStatus.Terminated); - expect(reactivationRequestedRelationshipPeer.cache?.auditLog).toHaveLength(4); - expect(reactivationRequestedRelationshipPeer.cache!.auditLog[3].reason).toBe(RelationshipAuditLogEntryReason.ReactivationRequested); + expect(reactivationRequestedRelationshipPeer.auditLog).toHaveLength(4); + expect(reactivationRequestedRelationshipPeer.auditLog[3].reason).toBe(RelationshipAuditLogEntryReason.ReactivationRequested); }); }); @@ -494,8 +494,8 @@ describe("RelationshipTest: Accept Reactivation", function () { const acceptedReactivatedRelationshipPeer = await to.relationships.acceptReactivation(relationshipId); expect(acceptedReactivatedRelationshipPeer.id.toString()).toStrictEqual(relationshipId.toString()); expect(acceptedReactivatedRelationshipPeer.status).toStrictEqual(RelationshipStatus.Active); - expect(acceptedReactivatedRelationshipPeer.cache?.auditLog).toHaveLength(5); - expect(acceptedReactivatedRelationshipPeer.cache!.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.AcceptanceOfReactivation); + expect(acceptedReactivatedRelationshipPeer.auditLog).toHaveLength(5); + expect(acceptedReactivatedRelationshipPeer.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.AcceptanceOfReactivation); const syncedRelationshipsFromSelf = await TestUtil.syncUntilHasRelationships(from); expect(syncedRelationshipsFromSelf).toHaveLength(1); @@ -503,8 +503,8 @@ describe("RelationshipTest: Accept Reactivation", function () { expect(acceptedReactivatedRelationshipFromSelf.id.toString()).toStrictEqual(relationshipId.toString()); expect(acceptedReactivatedRelationshipFromSelf.status).toStrictEqual(RelationshipStatus.Active); - expect(acceptedReactivatedRelationshipFromSelf.cache?.auditLog).toHaveLength(5); - expect(acceptedReactivatedRelationshipFromSelf.cache!.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.AcceptanceOfReactivation); + expect(acceptedReactivatedRelationshipFromSelf.auditLog).toHaveLength(5); + expect(acceptedReactivatedRelationshipFromSelf.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.AcceptanceOfReactivation); }); }); @@ -542,8 +542,8 @@ describe("RelationshipTest: Reject Reactivation", function () { const rejectedReactivatedRelationshipPeer = await to.relationships.rejectReactivation(relationshipId); expect(rejectedReactivatedRelationshipPeer.id.toString()).toStrictEqual(relationshipId.toString()); expect(rejectedReactivatedRelationshipPeer.status).toStrictEqual(RelationshipStatus.Terminated); - expect(rejectedReactivatedRelationshipPeer.cache?.auditLog).toHaveLength(5); - expect(rejectedReactivatedRelationshipPeer.cache!.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.RejectionOfReactivation); + expect(rejectedReactivatedRelationshipPeer.auditLog).toHaveLength(5); + expect(rejectedReactivatedRelationshipPeer.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.RejectionOfReactivation); const syncedRelationshipsFromSelf = await TestUtil.syncUntilHasRelationships(from); expect(syncedRelationshipsFromSelf).toHaveLength(1); @@ -551,8 +551,8 @@ describe("RelationshipTest: Reject Reactivation", function () { expect(rejectedReactivatedRelationshipFromSelf.id.toString()).toStrictEqual(relationshipId.toString()); expect(rejectedReactivatedRelationshipFromSelf.status).toStrictEqual(RelationshipStatus.Terminated); - expect(rejectedReactivatedRelationshipFromSelf.cache?.auditLog).toHaveLength(5); - expect(rejectedReactivatedRelationshipFromSelf.cache!.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.RejectionOfReactivation); + expect(rejectedReactivatedRelationshipFromSelf.auditLog).toHaveLength(5); + expect(rejectedReactivatedRelationshipFromSelf.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.RejectionOfReactivation); }); }); @@ -590,8 +590,8 @@ describe("RelationshipTest: Revoke Reactivation", function () { const revokedReactivatedRelationshipFromSelf = await from.relationships.revokeReactivation(relationshipId); expect(revokedReactivatedRelationshipFromSelf.id.toString()).toStrictEqual(relationshipId.toString()); expect(revokedReactivatedRelationshipFromSelf.status).toStrictEqual(RelationshipStatus.Terminated); - expect(revokedReactivatedRelationshipFromSelf.cache?.auditLog).toHaveLength(5); - expect(revokedReactivatedRelationshipFromSelf.cache!.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.RevocationOfReactivation); + expect(revokedReactivatedRelationshipFromSelf.auditLog).toHaveLength(5); + expect(revokedReactivatedRelationshipFromSelf.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.RevocationOfReactivation); const syncedRelationshipsPeer = await TestUtil.syncUntilHasRelationships(to); expect(syncedRelationshipsPeer).toHaveLength(1); @@ -599,8 +599,8 @@ describe("RelationshipTest: Revoke Reactivation", function () { expect(revokedReactivatedRelationshipPeer.id.toString()).toStrictEqual(relationshipId.toString()); expect(revokedReactivatedRelationshipPeer.status).toStrictEqual(RelationshipStatus.Terminated); - expect(revokedReactivatedRelationshipPeer.cache?.auditLog).toHaveLength(5); - expect(revokedReactivatedRelationshipPeer.cache!.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.RevocationOfReactivation); + expect(revokedReactivatedRelationshipPeer.auditLog).toHaveLength(5); + expect(revokedReactivatedRelationshipPeer.auditLog[4].reason).toBe(RelationshipAuditLogEntryReason.RevocationOfReactivation); }); }); @@ -643,8 +643,8 @@ describe("RelationshipTest: Decompose", function () { expect(decomposedRelationshipPeer.id.toString()).toStrictEqual(relationshipId.toString()); expect(decomposedRelationshipPeer.status).toStrictEqual(RelationshipStatus.DeletionProposed); - expect(decomposedRelationshipPeer.cache?.auditLog).toHaveLength(4); - expect(decomposedRelationshipPeer.cache!.auditLog[3].reason).toBe(RelationshipAuditLogEntryReason.Decomposition); + expect(decomposedRelationshipPeer.auditLog).toHaveLength(4); + expect(decomposedRelationshipPeer.auditLog[3].reason).toBe(RelationshipAuditLogEntryReason.Decomposition); await expect(to.relationships.decompose(relationshipId)).resolves.not.toThrow(); }); diff --git a/packages/transport/test/modules/relationshipTemplates/RelationshipTemplateController.test.ts b/packages/transport/test/modules/relationshipTemplates/RelationshipTemplateController.test.ts index 68384748c..b9daaa16c 100644 --- a/packages/transport/test/modules/relationshipTemplates/RelationshipTemplateController.test.ts +++ b/packages/transport/test/modules/relationshipTemplates/RelationshipTemplateController.test.ts @@ -245,7 +245,7 @@ describe("RelationshipTemplateController", function () { await sender.relationshipTemplates.cleanupTemplatesOfDecomposedRelationship(relationship); - const templateForRelationship = await sender.relationshipTemplates.getRelationshipTemplate(relationship.cache!.templateId); + const templateForRelationship = await sender.relationshipTemplates.getRelationshipTemplate(relationship.templateId); const otherTemplate = await sender.relationshipTemplates.getRelationshipTemplate(templateId); expect(templateForRelationship).toBeUndefined(); expect(otherTemplate).toBeUndefined(); diff --git a/packages/transport/test/modules/relationships/RelationshipsController.test.ts b/packages/transport/test/modules/relationships/RelationshipsController.test.ts index bbfa6cdb9..564e45a80 100644 --- a/packages/transport/test/modules/relationships/RelationshipsController.test.ts +++ b/packages/transport/test/modules/relationships/RelationshipsController.test.ts @@ -1,6 +1,6 @@ import { IDatabaseConnection } from "@js-soft/docdb-access-abstractions"; -import { CoreDate, CoreId } from "@nmshd/core-types"; -import { AccountController, CachedRelationship, Identity, Relationship, RelationshipStatus, Transport } from "../../../src"; +import { CoreId } from "@nmshd/core-types"; +import { AccountController, Identity, Relationship, RelationshipStatus, Transport } from "../../../src"; import { TestUtil } from "../../testHelpers/TestUtil"; describe("RelationshipsController", function () { @@ -15,23 +15,17 @@ describe("RelationshipsController", function () { let recipient3: AccountController; let senderRel: Relationship; let recipientRel: Relationship; - let tempDate: CoreDate; - function expectValidActiveFreshRelationship(relationship: Relationship, _: AccountController, peerAccount: AccountController, creationTime: CoreDate) { + function expectValidActiveFreshRelationship(relationship: Relationship, _: AccountController, peerAccount: AccountController) { expect(relationship.id).toBeInstanceOf(CoreId); expect(relationship.status).toStrictEqual(RelationshipStatus.Active); expect(relationship.peer).toBeInstanceOf(Identity); expect(relationship.peer.address).toStrictEqual(peerAccount.identity.address); - expect(relationship.cache!.templateId).toBeInstanceOf(CoreId); + expect(relationship.templateId).toBeInstanceOf(CoreId); - expect(relationship.cache).toBeInstanceOf(CachedRelationship); - expect(relationship.cachedAt).toBeInstanceOf(CoreDate); - expect(relationship.cachedAt!.isWithin(TestUtil.tempDateThreshold, TestUtil.tempDateThreshold, creationTime)).toBe(true); - expect(relationship.cache!.creationContent).toBeDefined(); - - expect(relationship.cache!.lastMessageReceivedAt).toBeUndefined(); - expect(relationship.cache!.lastMessageSentAt).toBeUndefined(); + expect(relationship.lastMessageReceivedAt).toBeUndefined(); + expect(relationship.lastMessageSentAt).toBeUndefined(); expect(relationship.relationshipSecretId).toBeDefined(); } @@ -62,7 +56,6 @@ describe("RelationshipsController", function () { }); test("should create a relationship and get it afterwards by the address", async function () { - tempDate = CoreDate.utc(); await TestUtil.addRelationship(sender, recipient1); senderRel = (await sender.relationships.getActiveRelationshipToIdentity(recipient1.identity.address))!; relId1 = senderRel.id; @@ -73,10 +66,10 @@ describe("RelationshipsController", function () { }); test("should set all the required relationship properties", function () { - expectValidActiveFreshRelationship(senderRel, sender, recipient1, tempDate); + expectValidActiveFreshRelationship(senderRel, sender, recipient1); expect(senderRel.metadata).toBeUndefined(); expect(senderRel.metadataModifiedAt).toBeUndefined(); - expectValidActiveFreshRelationship(recipientRel, recipient1, sender, tempDate); + expectValidActiveFreshRelationship(recipientRel, recipient1, sender); expect(recipientRel.metadata).toBeUndefined(); expect(recipientRel.metadataModifiedAt).toBeUndefined(); }); @@ -84,7 +77,7 @@ describe("RelationshipsController", function () { test("should set and get additional metadata", async function () { await sender.relationships.setRelationshipMetadata(senderRel, { myprop: true }); const senderRel2 = (await sender.relationships.getRelationship(senderRel.id))!; - expectValidActiveFreshRelationship(senderRel2, sender, recipient1, tempDate); + expectValidActiveFreshRelationship(senderRel2, sender, recipient1); expect(senderRel2.metadata).toBeDefined(); expect(senderRel2.metadata["myprop"]).toBe(true); expect(senderRel2.metadataModifiedAt).toBeDefined(); @@ -92,24 +85,15 @@ describe("RelationshipsController", function () { }); describe("Requestor", function () { - test("should get the cached relationships", async function () { - const relationships = await sender.relationships.getRelationships(); - expect(relationships).toHaveLength(1); - const rel1 = relationships[0]; - expect(rel1.cache).toBeDefined(); - expect(rel1.cachedAt).toBeDefined(); - }); - - test("should access the relationship cache by using get", async function () { + test("should access the relationship data by using get", async function () { const relationship = await sender.relationships.getRelationship(relId1); - expectValidActiveFreshRelationship(relationship!, sender, recipient1, tempDate); + expectValidActiveFreshRelationship(relationship!, sender, recipient1); }); test("should create a new relationship to another recipient", async function () { - tempDate = CoreDate.utc(); await TestUtil.addRelationship(sender, recipient2); senderRel = (await sender.relationships.getActiveRelationshipToIdentity(recipient2.identity.address))!; - expectValidActiveFreshRelationship(senderRel, sender, recipient2, tempDate); + expectValidActiveFreshRelationship(senderRel, sender, recipient2); }); test("should not create new relationship if templator has been deleted after requestor has loaded the template", async function () { @@ -144,34 +128,24 @@ describe("RelationshipsController", function () { }); describe("Templator", function () { - test("should get the cached relationships", async function () { - const relationships = await recipient1.relationships.getRelationships(); - expect(relationships).toHaveLength(1); - const rel1 = relationships[0]; - expect(rel1.cache).toBeDefined(); - expect(rel1.cachedAt).toBeDefined(); - }); - - test("should access the relationship cache by using get", async function () { + test("should access the relationship data by using get", async function () { const relationship = await recipient1.relationships.getRelationship(relId1); - expectValidActiveFreshRelationship(relationship!, recipient1, sender, tempDate); + expectValidActiveFreshRelationship(relationship!, recipient1, sender); }); test("should create a new relationship to another recipient", async function () { - tempDate = CoreDate.utc(); await TestUtil.addRelationship(recipient1, recipient2); senderRel = (await recipient1.relationships.getActiveRelationshipToIdentity(recipient2.identity.address))!; - expectValidActiveFreshRelationship(senderRel, recipient1, recipient2, tempDate); + expectValidActiveFreshRelationship(senderRel, recipient1, recipient2); }); - test("should have cached the relationship to another recipient", async function () { + test("should have stored the relationship to another recipient", async function () { const relationships = await recipient1.relationships.getRelationships(); expect(relationships).toHaveLength(2); const rel1 = relationships[0]; - expect(rel1.cache).toBeDefined(); - expect(rel1.cachedAt).toBeDefined(); + expect(rel1.templateId).toBeDefined(); const rel2 = relationships[1]; - expectValidActiveFreshRelationship(rel2, recipient1, recipient2, tempDate); + expectValidActiveFreshRelationship(rel2, recipient1, recipient2); }); }); }); diff --git a/packages/transport/test/modules/relationships/RelationshipsCustomContent.test.ts b/packages/transport/test/modules/relationships/RelationshipsCustomContent.test.ts index 8833d8037..ee1f10222 100644 --- a/packages/transport/test/modules/relationships/RelationshipsCustomContent.test.ts +++ b/packages/transport/test/modules/relationships/RelationshipsCustomContent.test.ts @@ -34,10 +34,10 @@ describe("Relationships Custom Content", function () { const template = await TestUtil.fetchRelationshipTemplateFromTokenReference(recipient, tokenReference); const customContent = Serializable.fromAny({ content: "TestToken" }); const relRecipient = await TestUtil.sendRelationship(recipient, template, customContent); - const relRecipientContent = relRecipient.cache!.creationContent; + const relRecipientContent = relRecipient.creationContent; const relSender = await TestUtil.syncUntilHasRelationships(sender); - const relSenderRequest = relSender[0].cache!.creationContent; + const relSenderRequest = relSender[0].creationContent; expect(relRecipientContent).toBeInstanceOf(JSONWrapper); const recipientToken = relRecipientContent as JSONWrapper; diff --git a/packages/transport/test/modules/relationships/decomposition.test.ts b/packages/transport/test/modules/relationships/decomposition.test.ts index 41f2c9d4f..d1c1e4e78 100644 --- a/packages/transport/test/modules/relationships/decomposition.test.ts +++ b/packages/transport/test/modules/relationships/decomposition.test.ts @@ -62,7 +62,7 @@ describe("Data cleanup after relationship decomposition", function () { }); test("templates should be deleted", async function () { - const templateForRelationship = await sender.relationshipTemplates.getRelationshipTemplate(relationship.cache!.templateId); + const templateForRelationship = await sender.relationshipTemplates.getRelationshipTemplate(relationship.templateId); const otherTemplate = await sender.relationshipTemplates.getRelationshipTemplate(templateId); expect(templateForRelationship).toBeUndefined(); expect(otherTemplate).toBeUndefined(); diff --git a/packages/transport/test/modules/sync/SyncController.relationships.test.ts b/packages/transport/test/modules/sync/SyncController.relationships.test.ts index 0ad38c6b7..954a9521a 100644 --- a/packages/transport/test/modules/sync/SyncController.relationships.test.ts +++ b/packages/transport/test/modules/sync/SyncController.relationships.test.ts @@ -42,7 +42,6 @@ describe("RelationshipSync", function () { relationshipOnRequestorDevice2 = await requestorDevice2.relationships.getRelationship(createdRelationship.id); expect(relationshipOnRequestorDevice2).toBeDefined(); - expect(relationshipOnRequestorDevice2?.cache).toBeDefined(); expect(relationshipOnRequestorDevice2!.toJSON()).toStrictEqualExcluding(createdRelationship.toJSON(), "cachedAt"); @@ -194,7 +193,7 @@ describe("RelationshipSync", function () { const relationship = (await TestUtil.addRelationship(requestor, templator)).acceptedRelationshipFromSelf; const relationshipId = relationship.id; - const templateId = relationship.cache!.templateId; + const templateId = relationship.templateId; await templator.syncEverything(); await requestor.syncEverything(); From b9c425ca9b60bc48f4703473b6b4ec511d0811eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 10:00:09 +0200 Subject: [PATCH 03/13] refactor: update consumption --- .../attributes/AttributesController.ts | 2 +- .../outgoing/OutgoingRequestsController.ts | 4 +- .../modules/requests/RequestEnd2End.test.ts | 4 +- .../requests/testHelpers/TestObjectFactory.ts | 325 ++++++++---------- 4 files changed, 152 insertions(+), 183 deletions(-) diff --git a/packages/consumption/src/modules/attributes/AttributesController.ts b/packages/consumption/src/modules/attributes/AttributesController.ts index 44fb97fed..cda529531 100644 --- a/packages/consumption/src/modules/attributes/AttributesController.ts +++ b/packages/consumption/src/modules/attributes/AttributesController.ts @@ -1542,7 +1542,7 @@ export class AttributesController extends ConsumptionBaseController { throw ConsumptionCoreErrors.attributes.wrongRelationshipStatusToSetDeletionInfo(); } - const deletionDate = relationship.cache!.auditLog[relationship.cache!.auditLog.length - 1].createdAt; + const deletionDate = relationship.auditLog[relationship.auditLog.length - 1].createdAt; await this.setDeletionInfoOfOwnSharedAttributes(relationship.peer.address.toString(), deletionDate); await this.setDeletionInfoOfPeerSharedAttributes(relationship.peer.address.toString(), deletionDate); diff --git a/packages/consumption/src/modules/requests/outgoing/OutgoingRequestsController.ts b/packages/consumption/src/modules/requests/outgoing/OutgoingRequestsController.ts index d81634ebd..a4d7900ed 100644 --- a/packages/consumption/src/modules/requests/outgoing/OutgoingRequestsController.ts +++ b/packages/consumption/src/modules/requests/outgoing/OutgoingRequestsController.ts @@ -175,7 +175,7 @@ export class OutgoingRequestsController extends ConsumptionBaseController { } // checking for an active relationship is not secure as in the meantime the relationship could have been accepted - const isFromNewRelationship = parsedParams.responseSource instanceof Relationship && parsedParams.responseSource.cache!.auditLog.length === 1; + const isFromNewRelationship = parsedParams.responseSource instanceof Relationship && parsedParams.responseSource.auditLog.length === 1; const requestContent = isFromNewRelationship ? templateContent.onNewRelationship : templateContent.onExistingRelationship; @@ -305,7 +305,7 @@ export class OutgoingRequestsController extends ConsumptionBaseController { this.assertRequestStatus(request, LocalRequestStatus.Open, LocalRequestStatus.Expired); - const responseSourceObjectCreationDate = responseSourceObject instanceof Message ? responseSourceObject.createdAt : responseSourceObject.cache!.auditLog[0].createdAt; + const responseSourceObjectCreationDate = responseSourceObject instanceof Message ? responseSourceObject.createdAt : responseSourceObject.auditLog[0].createdAt; if (request.status === LocalRequestStatus.Expired && request.isExpired(responseSourceObjectCreationDate)) { throw new ConsumptionError("Cannot complete an expired request with a response that was created before the expiration date"); } diff --git a/packages/consumption/test/modules/requests/RequestEnd2End.test.ts b/packages/consumption/test/modules/requests/RequestEnd2End.test.ts index f02e4ffbc..ad67cb0e1 100644 --- a/packages/consumption/test/modules/requests/RequestEnd2End.test.ts +++ b/packages/consumption/test/modules/requests/RequestEnd2End.test.ts @@ -95,7 +95,7 @@ describe("End2End Request/Response via Relationship Template", function () { template: rTemplate, creationContent: RelationshipCreationContent.from({ response: rLocalRequest.response!.content }) }); - rCreationContent = rRelationship.cache?.creationContent as RelationshipCreationContent; + rCreationContent = rRelationship.creationContent as RelationshipCreationContent; }); test("recipient: complete Local Request", async function () { @@ -111,7 +111,7 @@ describe("End2End Request/Response via Relationship Template", function () { }); test("sender: create Local Request and Response from Relationship Change", async function () { - sCreationContent = sRelationship.cache!.creationContent as RelationshipCreationContent; + sCreationContent = sRelationship.creationContent as RelationshipCreationContent; const response = sCreationContent.response; sLocalRequest = await sConsumptionController.outgoingRequests.createAndCompleteFromRelationshipTemplateResponse({ diff --git a/packages/consumption/test/modules/requests/testHelpers/TestObjectFactory.ts b/packages/consumption/test/modules/requests/testHelpers/TestObjectFactory.ts index e97e19e6f..3fa11faa8 100644 --- a/packages/consumption/test/modules/requests/testHelpers/TestObjectFactory.ts +++ b/packages/consumption/test/modules/requests/testHelpers/TestObjectFactory.ts @@ -22,7 +22,6 @@ import { import { CoreAddress, CoreDate, CoreId, ICoreId } from "@nmshd/core-types"; import { CoreBuffer, CryptoCipher, CryptoEncryptionAlgorithm, CryptoExchangeAlgorithm, CryptoSecretKey, CryptoSignatureAlgorithm, CryptoSignaturePublicKey } from "@nmshd/crypto"; import { - CachedRelationship, Identity, IMessage, IRelationship, @@ -54,22 +53,17 @@ export class TestObjectFactory { }), status: properties?.status ?? RelationshipStatus.Pending, relationshipSecretId: properties?.relationshipSecretId ?? CoreId.from("RELSEC1"), - cachedAt: properties?.cachedAt ?? CoreDate.from("2020-01-01T00:00:00.000Z"), - cache: - properties?.cache ?? - CachedRelationship.from({ - creationContent: {}, - auditLog: [ - { - createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.Creation, - newStatus: RelationshipStatus.Pending - } - ], - templateId: { id: "b9uMR7u7lsKLzRfVJNYb" } - }) + creationContent: {}, + auditLog: [ + { + createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.Creation, + newStatus: RelationshipStatus.Pending + } + ], + templateId: { id: "b9uMR7u7lsKLzRfVJNYb" } }); } @@ -87,31 +81,26 @@ export class TestObjectFactory { }), status: properties?.status ?? RelationshipStatus.Active, relationshipSecretId: properties?.relationshipSecretId ?? CoreId.from("RELSEC1"), - cachedAt: properties?.cachedAt ?? CoreDate.from("2020-01-02T00:00:00.000Z"), - cache: - properties?.cache ?? - CachedRelationship.from({ - creationContent: {}, - auditLog: [ - { - createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.Creation, - newStatus: RelationshipStatus.Pending - }, - - { - createdAt: CoreDate.from("2020-01-02T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.AcceptanceOfCreation, - oldStatus: RelationshipStatus.Pending, - newStatus: RelationshipStatus.Active - } - ], - templateId: { id: "b9uMR7u7lsKLzRfVJNYb" } - }) + creationContent: {}, + auditLog: [ + { + createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.Creation, + newStatus: RelationshipStatus.Pending + }, + + { + createdAt: CoreDate.from("2020-01-02T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.AcceptanceOfCreation, + oldStatus: RelationshipStatus.Pending, + newStatus: RelationshipStatus.Active + } + ], + templateId: { id: "b9uMR7u7lsKLzRfVJNYb" } }); } @@ -129,40 +118,35 @@ export class TestObjectFactory { }), status: properties?.status ?? RelationshipStatus.Terminated, relationshipSecretId: properties?.relationshipSecretId ?? CoreId.from("RELSEC1"), - cachedAt: properties?.cachedAt ?? CoreDate.from("2020-01-03T00:00:00.000Z"), - cache: - properties?.cache ?? - CachedRelationship.from({ - creationContent: {}, - auditLog: [ - { - createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.Creation, - newStatus: RelationshipStatus.Pending - }, - - { - createdAt: CoreDate.from("2020-01-02T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.AcceptanceOfCreation, - oldStatus: RelationshipStatus.Pending, - newStatus: RelationshipStatus.Active - }, - - { - createdAt: CoreDate.from("2020-01-03T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.Termination, - oldStatus: RelationshipStatus.Active, - newStatus: RelationshipStatus.Terminated - } - ], - templateId: { id: "b9uMR7u7lsKLzRfVJNYb" } - }) + creationContent: {}, + auditLog: [ + { + createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.Creation, + newStatus: RelationshipStatus.Pending + }, + + { + createdAt: CoreDate.from("2020-01-02T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.AcceptanceOfCreation, + oldStatus: RelationshipStatus.Pending, + newStatus: RelationshipStatus.Active + }, + + { + createdAt: CoreDate.from("2020-01-03T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.Termination, + oldStatus: RelationshipStatus.Active, + newStatus: RelationshipStatus.Terminated + } + ], + templateId: { id: "b9uMR7u7lsKLzRfVJNYb" } }); } @@ -186,31 +170,26 @@ export class TestObjectFactory { }), status: properties?.status ?? RelationshipStatus.Active, relationshipSecretId: properties?.relationshipSecretId ?? CoreId.from("RELSEC1"), - cachedAt: properties?.cachedAt ?? CoreDate.from("2020-01-03T00:00:00.000Z"), - cache: - properties?.cache ?? - CachedRelationship.from({ - creationContent: {}, - auditLog: [ - { - createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.Creation, - newStatus: RelationshipStatus.Pending - }, - - { - createdAt: CoreDate.from("2020-01-02T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.AcceptanceOfCreation, - oldStatus: RelationshipStatus.Pending, - newStatus: RelationshipStatus.Active - } - ], - templateId: CoreId.from("aTemplateId") - }) + creationContent: {}, + auditLog: [ + { + createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.Creation, + newStatus: RelationshipStatus.Pending + }, + + { + createdAt: CoreDate.from("2020-01-02T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.AcceptanceOfCreation, + oldStatus: RelationshipStatus.Pending, + newStatus: RelationshipStatus.Active + } + ], + templateId: CoreId.from("aTemplateId") }); } @@ -234,40 +213,35 @@ export class TestObjectFactory { }), status: properties?.status ?? RelationshipStatus.DeletionProposed, relationshipSecretId: properties?.relationshipSecretId ?? CoreId.from("RELSEC1"), - cachedAt: properties?.cachedAt ?? CoreDate.from("2022-01-03T00:00:00.000Z"), - cache: - properties?.cache ?? - CachedRelationship.from({ - creationContent: {}, - auditLog: [ - { - createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.Creation, - newStatus: RelationshipStatus.Pending - }, - - { - createdAt: CoreDate.from("2020-01-02T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.AcceptanceOfCreation, - oldStatus: RelationshipStatus.Pending, - newStatus: RelationshipStatus.Active - }, - - { - createdAt: CoreDate.from("2022-01-03T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.DecompositionDueToIdentityDeletion, - oldStatus: RelationshipStatus.Active, - newStatus: RelationshipStatus.DeletionProposed - } - ], - templateId: CoreId.from("aTemplateId") - }) + creationContent: {}, + auditLog: [ + { + createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.Creation, + newStatus: RelationshipStatus.Pending + }, + + { + createdAt: CoreDate.from("2020-01-02T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.AcceptanceOfCreation, + oldStatus: RelationshipStatus.Pending, + newStatus: RelationshipStatus.Active + }, + + { + createdAt: CoreDate.from("2022-01-03T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.DecompositionDueToIdentityDeletion, + oldStatus: RelationshipStatus.Active, + newStatus: RelationshipStatus.DeletionProposed + } + ], + templateId: CoreId.from("aTemplateId") }); } @@ -285,49 +259,44 @@ export class TestObjectFactory { }), status: properties?.status ?? RelationshipStatus.DeletionProposed, relationshipSecretId: properties?.relationshipSecretId ?? CoreId.from("RELSEC1"), - cachedAt: properties?.cachedAt ?? CoreDate.from("2020-01-04T00:00:00.000Z"), - cache: - properties?.cache ?? - CachedRelationship.from({ - creationContent: {}, - auditLog: [ - { - createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.Creation, - newStatus: RelationshipStatus.Pending - }, - - { - createdAt: CoreDate.from("2020-01-02T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.AcceptanceOfCreation, - oldStatus: RelationshipStatus.Pending, - newStatus: RelationshipStatus.Active - }, - - { - createdAt: CoreDate.from("2020-01-03T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.Termination, - oldStatus: RelationshipStatus.Active, - newStatus: RelationshipStatus.Terminated - }, - - { - createdAt: CoreDate.from("2020-01-04T00:00:00.000Z"), - createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), - createdByDevice: CoreId.from("DVC1"), - reason: RelationshipAuditLogEntryReason.Decomposition, - oldStatus: RelationshipStatus.Terminated, - newStatus: RelationshipStatus.DeletionProposed - } - ], - templateId: { id: "b9uMR7u7lsKLzRfVJNYb" } - }) + creationContent: {}, + auditLog: [ + { + createdAt: CoreDate.from("2020-01-01T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.Creation, + newStatus: RelationshipStatus.Pending + }, + + { + createdAt: CoreDate.from("2020-01-02T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.AcceptanceOfCreation, + oldStatus: RelationshipStatus.Pending, + newStatus: RelationshipStatus.Active + }, + + { + createdAt: CoreDate.from("2020-01-03T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.Termination, + oldStatus: RelationshipStatus.Active, + newStatus: RelationshipStatus.Terminated + }, + + { + createdAt: CoreDate.from("2020-01-04T00:00:00.000Z"), + createdBy: CoreAddress.from("did:e:a-domain:dids:anidentity2"), + createdByDevice: CoreId.from("DVC1"), + reason: RelationshipAuditLogEntryReason.Decomposition, + oldStatus: RelationshipStatus.Terminated, + newStatus: RelationshipStatus.DeletionProposed + } + ], + templateId: { id: "b9uMR7u7lsKLzRfVJNYb" } }); } From 31ca45cfabd560a12c2e7162b825f9c154c7c7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 10:00:17 +0200 Subject: [PATCH 04/13] refactor: update runtime --- .../transport/relationships/AcceptRelationship.ts | 4 ---- .../relationships/AcceptRelationshipReactivation.ts | 4 ---- .../transport/relationships/DecomposeRelationship.ts | 4 ---- .../transport/relationships/GetRelationships.ts | 6 +++--- .../transport/relationships/RejectRelationship.ts | 4 ---- .../relationships/RejectRelationshipReactivation.ts | 4 ---- .../transport/relationships/RelationshipMapper.ts | 11 +++-------- .../relationships/RequestRelationshipReactivation.ts | 4 ---- .../transport/relationships/RevokeRelationship.ts | 4 ---- .../relationships/RevokeRelationshipReactivation.ts | 4 ---- .../transport/relationships/TerminateRelationship.ts | 4 ---- 11 files changed, 6 insertions(+), 47 deletions(-) diff --git a/packages/runtime/src/useCases/transport/relationships/AcceptRelationship.ts b/packages/runtime/src/useCases/transport/relationships/AcceptRelationship.ts index b29e6359c..4bdbb5259 100644 --- a/packages/runtime/src/useCases/transport/relationships/AcceptRelationship.ts +++ b/packages/runtime/src/useCases/transport/relationships/AcceptRelationship.ts @@ -31,10 +31,6 @@ export class AcceptRelationshipUseCase extends UseCase((r) => r.peer)]: true, [nameof((r) => r.status)]: true, - [`${nameof((r) => r.templateId)}`]: true + [nameof((r) => r.templateId)]: true }, alias: { - [`${nameof((r) => r.templateId)}`]: `${nameof((r) => r.cache)}.${nameof((r) => r.templateId)}`, + [nameof((r) => r.templateId)]: `${nameof((r) => r.templateId)}`, [nameof((r) => r.status)]: nameof((r) => r.status), [nameof((r) => r.peer)]: `${nameof((r) => r.peer)}.${nameof((r) => r.address)}` } diff --git a/packages/runtime/src/useCases/transport/relationships/RejectRelationship.ts b/packages/runtime/src/useCases/transport/relationships/RejectRelationship.ts index 0caff6f77..032b3c5d7 100644 --- a/packages/runtime/src/useCases/transport/relationships/RejectRelationship.ts +++ b/packages/runtime/src/useCases/transport/relationships/RejectRelationship.ts @@ -31,10 +31,6 @@ export class RejectRelationshipUseCase extends UseCase this.toAuditLogEntryDTO(entry)), - creationContent: this.toCreationContent(relationship.cache.creationContent) + auditLog: relationship.auditLog.map((entry) => this.toAuditLogEntryDTO(entry)), + creationContent: this.toCreationContent(relationship.creationContent) }; } diff --git a/packages/runtime/src/useCases/transport/relationships/RequestRelationshipReactivation.ts b/packages/runtime/src/useCases/transport/relationships/RequestRelationshipReactivation.ts index ea7a83cc2..df9c8735a 100644 --- a/packages/runtime/src/useCases/transport/relationships/RequestRelationshipReactivation.ts +++ b/packages/runtime/src/useCases/transport/relationships/RequestRelationshipReactivation.ts @@ -31,10 +31,6 @@ export class RequestRelationshipReactivationUseCase extends UseCase Date: Fri, 18 Jul 2025 10:25:22 +0200 Subject: [PATCH 05/13] chore: update public api test --- packages/transport/test/modules/PublicAPI.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/transport/test/modules/PublicAPI.test.ts b/packages/transport/test/modules/PublicAPI.test.ts index e5cce4535..eaa450fd3 100644 --- a/packages/transport/test/modules/PublicAPI.test.ts +++ b/packages/transport/test/modules/PublicAPI.test.ts @@ -109,8 +109,7 @@ publicFunctions[RelationshipsController.name] = [ nameof((r) => r.sendRelationship), nameof((r) => r.accept), nameof((r) => r.reject), - nameof((r) => r.revoke), - nameof((r) => r.updateCache) + nameof((r) => r.revoke) ]; publicFunctions[RelationshipSecretController.name] = [ nameof((r) => r.init), @@ -135,7 +134,7 @@ publicFunctions[RelationshipTemplateController.name] = [ nameof((r) => r.deleteRelationshipTemplate), nameof((r) => r.getRelationshipTemplates), nameof((r) => r.getRelationshipTemplate), - nameof((r) => r.updateCache) + nameof((r) => r.updateCache) ]; publicFunctions[SecretController.name] = [ nameof((r) => r.init), From cb56566353f5a6487ecb636ef7dce5eb560409d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 10:56:58 +0200 Subject: [PATCH 06/13] chore: remove cache excludes --- .../sync/SyncController.messages.test.ts | 17 ++++------------- .../sync/SyncController.relationships.test.ts | 4 ++-- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/packages/transport/test/modules/sync/SyncController.messages.test.ts b/packages/transport/test/modules/sync/SyncController.messages.test.ts index 9e820f4c8..03c9e8799 100644 --- a/packages/transport/test/modules/sync/SyncController.messages.test.ts +++ b/packages/transport/test/modules/sync/SyncController.messages.test.ts @@ -28,9 +28,7 @@ describe("MessageSync", function () { test("default case A1 -> B2: sync should receive message on an onboarded device", async function () { const a1 = await TestUtil.createIdentityWithOneDevice(connection, { datawalletEnabled: true }); - const { device1: b1, device2: b2 } = await TestUtil.createIdentityWithTwoDevices(connection, { - datawalletEnabled: true - }); + const { device1: b1, device2: b2 } = await TestUtil.createIdentityWithTwoDevices(connection, { datawalletEnabled: true }); await TestUtil.addRelationship(a1, b1); @@ -44,10 +42,7 @@ describe("MessageSync", function () { }); test("default case A2 -> B1: an onboarded device should be able to send messages", async function () { - const { device1: a1, device2: a2 } = await TestUtil.createIdentityWithTwoDevices(connection, { - datawalletEnabled: true - }); - + const { device1: a1, device2: a2 } = await TestUtil.createIdentityWithTwoDevices(connection, { datawalletEnabled: true }); const b1 = await TestUtil.createIdentityWithOneDevice(connection, { datawalletEnabled: true }); await TestUtil.addRelationship(a1, b1); @@ -62,12 +57,8 @@ describe("MessageSync", function () { }); test("concurrent send A1 -> B1, A2 -> B1: sync should receive messages sent parallel to the same identity on all devices", async function () { - const { device1: a1, device2: a2 } = await TestUtil.createIdentityWithTwoDevices(connection, { - datawalletEnabled: true - }); - const { device1: b1, device2: b2 } = await TestUtil.createIdentityWithTwoDevices(connection, { - datawalletEnabled: true - }); + const { device1: a1, device2: a2 } = await TestUtil.createIdentityWithTwoDevices(connection, { datawalletEnabled: true }); + const { device1: b1, device2: b2 } = await TestUtil.createIdentityWithTwoDevices(connection, { datawalletEnabled: true }); await TestUtil.addRelationship(a1, b1); await a2.syncEverything(); diff --git a/packages/transport/test/modules/sync/SyncController.relationships.test.ts b/packages/transport/test/modules/sync/SyncController.relationships.test.ts index 954a9521a..149739656 100644 --- a/packages/transport/test/modules/sync/SyncController.relationships.test.ts +++ b/packages/transport/test/modules/sync/SyncController.relationships.test.ts @@ -43,7 +43,7 @@ describe("RelationshipSync", function () { relationshipOnRequestorDevice2 = await requestorDevice2.relationships.getRelationship(createdRelationship.id); expect(relationshipOnRequestorDevice2).toBeDefined(); - expect(relationshipOnRequestorDevice2!.toJSON()).toStrictEqualExcluding(createdRelationship.toJSON(), "cachedAt"); + expect(relationshipOnRequestorDevice2!.toJSON()).toStrictEqual(createdRelationship.toJSON()); await TestUtil.syncUntilHasRelationships(templatorDevice); @@ -179,7 +179,7 @@ describe("RelationshipSync", function () { templateOnDevice2 = await device2.relationshipTemplates.getRelationshipTemplate(templateOnDevice1.id); expect(templateOnDevice2).toBeDefined(); expect(templateOnDevice2?.cache).toBeDefined(); - expect(templateOnDevice2!.toJSON()).toStrictEqualExcluding(templateOnDevice1.toJSON(), "cachedAt"); + expect(templateOnDevice2!.toJSON()).toStrictEqual(templateOnDevice1.toJSON()); }); test("Synchronizing after both parties have decomposed simultaneously does not throw", async function () { From 14abf724722d81629b0fb13e0cb02b7b27cde0eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 11:13:13 +0200 Subject: [PATCH 07/13] chore: simplify cache fetcher --- .../src/modules/sync/DatawalletModificationsProcessor.ts | 6 ------ packages/transport/src/modules/sync/SyncController.ts | 9 +-------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/packages/transport/src/modules/sync/DatawalletModificationsProcessor.ts b/packages/transport/src/modules/sync/DatawalletModificationsProcessor.ts index 1ed594ccb..a0f25bd74 100644 --- a/packages/transport/src/modules/sync/DatawalletModificationsProcessor.ts +++ b/packages/transport/src/modules/sync/DatawalletModificationsProcessor.ts @@ -13,8 +13,6 @@ import { IdentityDeletionProcessController } from "../accounts/IdentityDeletionP import { FileController } from "../files/FileController"; import { CachedFile } from "../files/local/CachedFile"; import { File } from "../files/local/File"; -import { MessageController } from "../messages/MessageController"; -import { RelationshipsController } from "../relationships/RelationshipsController"; import { CachedRelationshipTemplate } from "../relationshipTemplates/local/CachedRelationshipTemplate"; import { RelationshipTemplate } from "../relationshipTemplates/local/RelationshipTemplate"; import { RelationshipTemplateController } from "../relationshipTemplates/RelationshipTemplateController"; @@ -49,8 +47,6 @@ export class DatawalletModificationsProcessor { private readonly collectionsWithCacheableItems: string[] = [ DbCollectionName.Files, - DbCollectionName.Messages, - DbCollectionName.Relationships, DbCollectionName.RelationshipTemplates, DbCollectionName.Tokens, DbCollectionName.IdentityDeletionProcess @@ -205,9 +201,7 @@ export class DatawalletModificationsProcessor { export class CacheFetcher { public constructor( private readonly fileController: FileController, - private readonly messageController: MessageController, private readonly relationshipTemplateController: RelationshipTemplateController, - private readonly relationshipController: RelationshipsController, private readonly tokenController: TokenController, private readonly identityDeletionProcessController: IdentityDeletionProcessController ) {} diff --git a/packages/transport/src/modules/sync/SyncController.ts b/packages/transport/src/modules/sync/SyncController.ts index 59cc2f9d0..984b186b4 100644 --- a/packages/transport/src/modules/sync/SyncController.ts +++ b/packages/transport/src/modules/sync/SyncController.ts @@ -30,14 +30,7 @@ export class SyncController extends TransportController { private _cacheFetcher?: CacheFetcher; private get cacheFetcher() { - this._cacheFetcher ??= new CacheFetcher( - this.parent.files, - this.parent.messages, - this.parent.relationshipTemplates, - this.parent.relationships, - this.parent.tokens, - this.parent.identityDeletionProcess - ); + this._cacheFetcher ??= new CacheFetcher(this.parent.files, this.parent.relationshipTemplates, this.parent.tokens, this.parent.identityDeletionProcess); return this._cacheFetcher; } From 7a1f955d6818e40b1caa72c4d2ec3dd853c5e2b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 11:13:22 +0200 Subject: [PATCH 08/13] chore: remove custom toJson --- .../src/modules/relationships/local/Relationship.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/transport/src/modules/relationships/local/Relationship.ts b/packages/transport/src/modules/relationships/local/Relationship.ts index 4ffe93d50..b3d6be5b6 100644 --- a/packages/transport/src/modules/relationships/local/Relationship.ts +++ b/packages/transport/src/modules/relationships/local/Relationship.ts @@ -91,16 +91,6 @@ export class Relationship extends CoreSynchronizable implements IRelationship { @serialize() public metadataModifiedAt?: CoreDate; - public override toJSON(verbose?: boolean | undefined, serializeAsString?: boolean | undefined): Object { - const json = super.toJSON(verbose, serializeAsString) as any; - - // Adds flattened peerAddress and templateId to the JSON stored in the database. - // This helps us to boost the performance of database queries that include these fields. - json.peerAddress = this.peer.address.toString(); - - return json; - } - public static fromBackboneAndCreationContent( response: BackboneGetRelationshipResponse, peer: IIdentity, From acea990eedd91c7597c990a51f140d7e48ba41dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 11:13:32 +0200 Subject: [PATCH 09/13] fix: query --- .../src/modules/relationships/RelationshipsController.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/transport/src/modules/relationships/RelationshipsController.ts b/packages/transport/src/modules/relationships/RelationshipsController.ts index 2b8b51ab8..9e7bc048d 100644 --- a/packages/transport/src/modules/relationships/RelationshipsController.ts +++ b/packages/transport/src/modules/relationships/RelationshipsController.ts @@ -10,6 +10,7 @@ import { TransportCoreErrors } from "../../core/TransportCoreErrors"; import { TransportIds } from "../../core/TransportIds"; import { RelationshipChangedEvent, RelationshipDecomposedBySelfEvent, RelationshipReactivationCompletedEvent, RelationshipReactivationRequestedEvent } from "../../events"; import { AccountController } from "../accounts/AccountController"; +import { Identity } from "../accounts/data/Identity"; import { RelationshipTemplate } from "../relationshipTemplates/local/RelationshipTemplate"; import { SynchronizedCollection } from "../sync/SynchronizedCollection"; import { RelationshipSecretController } from "./RelationshipSecretController"; @@ -108,7 +109,7 @@ export class RelationshipsController extends TransportController { // } public async getRelationshipToIdentity(address: CoreAddress, status?: RelationshipStatus): Promise { - const query: any = { peerAddress: address.toString() }; + const query: any = { [`${nameof((r) => r.peer)}.${nameof((r) => r.address)}`]: address.toString() }; if (status) query[`${nameof((r) => r.status)}`] = status; const relationships = await this.relationships.find(query); From 32de828bacf83af0c3247327c9b7415abcb86f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 11:33:28 +0200 Subject: [PATCH 10/13] fix: re-add cachedAt --- .../test/modules/sync/SyncController.relationships.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/transport/test/modules/sync/SyncController.relationships.test.ts b/packages/transport/test/modules/sync/SyncController.relationships.test.ts index 149739656..6001ab288 100644 --- a/packages/transport/test/modules/sync/SyncController.relationships.test.ts +++ b/packages/transport/test/modules/sync/SyncController.relationships.test.ts @@ -179,7 +179,7 @@ describe("RelationshipSync", function () { templateOnDevice2 = await device2.relationshipTemplates.getRelationshipTemplate(templateOnDevice1.id); expect(templateOnDevice2).toBeDefined(); expect(templateOnDevice2?.cache).toBeDefined(); - expect(templateOnDevice2!.toJSON()).toStrictEqual(templateOnDevice1.toJSON()); + expect(templateOnDevice2!.toJSON()).toStrictEqualExcluding(templateOnDevice1.toJSON(), "cachedAt"); }); test("Synchronizing after both parties have decomposed simultaneously does not throw", async function () { From 70458b20247d08aae06fc5445b83b6be94cc274e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 11:58:38 +0200 Subject: [PATCH 11/13] chore: rm comments --- .../relationships/RelationshipsController.ts | 60 ------------------- 1 file changed, 60 deletions(-) diff --git a/packages/transport/src/modules/relationships/RelationshipsController.ts b/packages/transport/src/modules/relationships/RelationshipsController.ts index 9e7bc048d..2ff8d2081 100644 --- a/packages/transport/src/modules/relationships/RelationshipsController.ts +++ b/packages/transport/src/modules/relationships/RelationshipsController.ts @@ -56,58 +56,6 @@ export class RelationshipsController extends TransportController { return relationships; } - // public async updateCache(ids: string[]): Promise { - // if (ids.length < 1) { - // return []; - // } - - // const resultItems = (await this.client.getRelationships({ ids })).value; - // const promises = []; - // for await (const resultItem of resultItems) { - // promises.push(this.updateExistingRelationshipInDb(resultItem.id, resultItem)); - // } - // return await Promise.all(promises); - // } - - // public async fetchCaches(ids: CoreId[]): Promise<{ id: CoreId; cache: CachedRelationship }[]> { - // if (ids.length === 0) return []; - - // const backboneRelationships = await (await this.client.getRelationships({ ids: ids.map((id) => id.id) })).value.collect(); - - // const decryptionPromises = backboneRelationships.map(async (r) => { - // const relationshipDoc = await this.relationships.read(r.id); - // if (!relationshipDoc) { - // this._log.error( - // `Relationship '${r.id}' not found in local database and the cache fetching was therefore skipped. This should not happen and might be a bug in the application logic.` - // ); - // return; - // } - - // const relationship = Relationship.from(relationshipDoc); - - // return { - // id: CoreId.from(r.id), - // cache: await this.decryptRelationship(r, relationship.relationshipSecretId) - // }; - // }); - - // const caches = await Promise.all(decryptionPromises); - // return caches.filter((c) => c !== undefined); - // } - - // @log() - // private async updateExistingRelationshipInDb(id: string, response: BackboneRelationship) { - // const relationshipDoc = await this.relationships.read(id); - // if (!relationshipDoc) throw TransportCoreErrors.general.recordNotFound(Relationship, id); - - // const relationship = Relationship.from(relationshipDoc); - - // await this.updateCacheOfRelationship(relationship, response); - // relationship.status = response.status; - // await this.relationships.update(relationshipDoc, relationship); - // return relationship; - // } - public async getRelationshipToIdentity(address: CoreAddress, status?: RelationshipStatus): Promise { const query: any = { [`${nameof((r) => r.peer)}.${nameof((r) => r.address)}`]: address.toString() }; if (status) query[`${nameof((r) => r.status)}`] = status; @@ -385,14 +333,6 @@ export class RelationshipsController extends TransportController { throw TransportCoreErrors.relationships.wrongRelationshipStatus(relationship.id.toString(), relationship.status); } - // private async updateCacheOfRelationship(relationship: Relationship, response?: BackboneRelationship) { - // response ??= (await this.client.getRelationship(relationship.id.toString())).value; - - // const cachedRelationship = await this.decryptRelationship(response, relationship.relationshipSecretId); - - // relationship.setCache(cachedRelationship); - // } - private async decryptRelationship(response: BackboneRelationship, relationshipSecretId: CoreId) { if (!response.creationContent) throw new TransportError("Creation content is missing"); From 5fec762c57a4a15c42eca45aafaeb0a232d25d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 11:58:55 +0200 Subject: [PATCH 12/13] chore: rm unused method --- .../relationships/RelationshipsController.ts | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/packages/transport/src/modules/relationships/RelationshipsController.ts b/packages/transport/src/modules/relationships/RelationshipsController.ts index 2ff8d2081..80e91c360 100644 --- a/packages/transport/src/modules/relationships/RelationshipsController.ts +++ b/packages/transport/src/modules/relationships/RelationshipsController.ts @@ -15,7 +15,6 @@ import { RelationshipTemplate } from "../relationshipTemplates/local/Relationshi import { SynchronizedCollection } from "../sync/SynchronizedCollection"; import { RelationshipSecretController } from "./RelationshipSecretController"; import { BackbonePutRelationshipsResponse } from "./backbone/BackbonePutRelationship"; -import { BackboneRelationship } from "./backbone/BackboneRelationship"; import { RelationshipClient } from "./backbone/RelationshipClient"; import { PeerDeletionInfo } from "./local/PeerDeletionInfo"; import { Relationship } from "./local/Relationship"; @@ -333,22 +332,6 @@ export class RelationshipsController extends TransportController { throw TransportCoreErrors.relationships.wrongRelationshipStatus(relationship.id.toString(), relationship.status); } - private async decryptRelationship(response: BackboneRelationship, relationshipSecretId: CoreId) { - if (!response.creationContent) throw new TransportError("Creation content is missing"); - - const templateId = CoreId.from(response.relationshipTemplateId); - - this._log.trace(`Parsing relationship creation content of ${response.id}...`); - - const creationContent = await this.decryptCreationContent(response.creationContent, CoreAddress.from(response.from), relationshipSecretId); - - return { - creationContent: creationContent.content, - templateId, - auditLog: RelationshipAuditLog.fromBackboneAuditLog(response.auditLog) - }; - } - private async prepareCreationContent(relationshipSecretId: CoreId, template: RelationshipTemplate, content: ISerializable): Promise { if (!template.cache) { throw this.newCacheEmptyError(RelationshipTemplate, template.id.toString()); From ce441f3ed6db01ead1fccb5ccaf177747f859e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= Date: Fri, 18 Jul 2025 12:45:26 +0200 Subject: [PATCH 13/13] chore: re-add check --- .../test/modules/relationships/RelationshipsController.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/transport/test/modules/relationships/RelationshipsController.test.ts b/packages/transport/test/modules/relationships/RelationshipsController.test.ts index 564e45a80..b1f93c727 100644 --- a/packages/transport/test/modules/relationships/RelationshipsController.test.ts +++ b/packages/transport/test/modules/relationships/RelationshipsController.test.ts @@ -23,7 +23,7 @@ describe("RelationshipsController", function () { expect(relationship.peer.address).toStrictEqual(peerAccount.identity.address); expect(relationship.templateId).toBeInstanceOf(CoreId); - + expect(relationship.creationContent).toBeDefined(); expect(relationship.lastMessageReceivedAt).toBeUndefined(); expect(relationship.lastMessageSentAt).toBeUndefined(); expect(relationship.relationshipSecretId).toBeDefined();