From c660f62120c7a56e68a0777d63f21e1a8d177ba9 Mon Sep 17 00:00:00 2001 From: florianfederspiel Date: Wed, 3 Jun 2026 10:40:00 +0200 Subject: [PATCH] KI-AGENT: Veraltete Matrix-Raumreferenzen bereinigen --- backend/src/modules/matrix.service.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/backend/src/modules/matrix.service.ts b/backend/src/modules/matrix.service.ts index 882dc0b..c75f49e 100644 --- a/backend/src/modules/matrix.service.ts +++ b/backend/src/modules/matrix.service.ts @@ -291,6 +291,16 @@ export function matrixService(server: FastifyInstance) { roomKey: string ) => `#${tenantRoomAliasLocalpart(tenant, roomKey)}:${serverName()}` + const matrixIdentifierServerName = (value?: string | null) => { + const match = value?.match(/^[!#@][^:]+:(.+)$/) + return match?.[1] || null + } + + const belongsToCurrentMatrixServer = (value?: string | null) => { + const identifierServerName = matrixIdentifierServerName(value) + return !identifierServerName || identifierServerName === serverName() + } + const normalizeTenantRoomOptions = (options: MatrixTenantRoomOptions = {}) => { const fallbackName = options.key || options.name || "Allgemeiner Chat" const key = normalizeMatrixAliasSeed(options.key || fallbackName) @@ -836,9 +846,14 @@ export function matrixService(server: FastifyInstance) { ) => { const normalizedOptions = normalizeTenantRoomOptions(options) const existing = await findTenantRoomMetadata(tenant.id, normalizedOptions.key) + const expectedAlias = tenantRoomAlias(tenant, normalizedOptions.key) if (existing) { + const hasStaleMatrixRoomId = !belongsToCurrentMatrixServer(existing.matrixRoomId) + const hasStaleMatrixAlias = !belongsToCurrentMatrixServer(existing.matrixAlias) const shouldUpdate = + hasStaleMatrixRoomId || + hasStaleMatrixAlias || (options.name !== undefined && existing.name !== normalizedOptions.name) || (options.topic !== undefined && existing.topic !== normalizedOptions.topic) || (options.type !== undefined && existing.type !== normalizedOptions.type) || @@ -857,6 +872,9 @@ export function matrixService(server: FastifyInstance) { entityType: options.entityType !== undefined ? normalizedOptions.entityType : existing.entityType, entityId: options.entityId !== undefined ? normalizedOptions.entityId : existing.entityId, entityUuid: options.entityUuid !== undefined ? normalizedOptions.entityUuid : existing.entityUuid, + matrixRoomId: hasStaleMatrixRoomId ? null : existing.matrixRoomId, + matrixAlias: hasStaleMatrixAlias ? expectedAlias : existing.matrixAlias, + parentSpaceRoomId: hasStaleMatrixRoomId ? null : existing.parentSpaceRoomId, updatedAt: new Date(), }) .where(eq(communicationRooms.id, existing.id)) @@ -876,7 +894,7 @@ export function matrixService(server: FastifyInstance) { entityType: normalizedOptions.entityType, entityId: normalizedOptions.entityId, entityUuid: normalizedOptions.entityUuid, - matrixAlias: tenantRoomAlias(tenant, normalizedOptions.key), + matrixAlias: expectedAlias, }) .returning() @@ -960,8 +978,8 @@ export function matrixService(server: FastifyInstance) { entityUuid: metadata.entityUuid, alias, exists: false, - roomId: metadata.matrixRoomId, - parentSpaceRoomId: metadata.parentSpaceRoomId, + roomId: belongsToCurrentMatrixServer(metadata.matrixRoomId) ? metadata.matrixRoomId : null, + parentSpaceRoomId: belongsToCurrentMatrixServer(metadata.parentSpaceRoomId) ? metadata.parentSpaceRoomId : null, servers: [], } } @@ -1141,6 +1159,9 @@ export function matrixService(server: FastifyInstance) { const ensureServiceUserJoinedRoom = async (room: { roomId?: string | null; alias?: string | null }) => { const target = room.roomId || room.alias if (!target) return { ok: false, status: "missing_room" } + if (!belongsToCurrentMatrixServer(target)) { + return { ok: false, status: "stale_room", roomId: target } + } const serviceLogin = await ensureServiceAccessToken()