KI-AGENT: Veraltete Matrix-Raumreferenzen bereinigen

This commit is contained in:
2026-06-03 10:40:00 +02:00
parent ad74825781
commit c660f62120

View File

@@ -291,6 +291,16 @@ export function matrixService(server: FastifyInstance) {
roomKey: string roomKey: string
) => `#${tenantRoomAliasLocalpart(tenant, roomKey)}:${serverName()}` ) => `#${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 normalizeTenantRoomOptions = (options: MatrixTenantRoomOptions = {}) => {
const fallbackName = options.key || options.name || "Allgemeiner Chat" const fallbackName = options.key || options.name || "Allgemeiner Chat"
const key = normalizeMatrixAliasSeed(options.key || fallbackName) const key = normalizeMatrixAliasSeed(options.key || fallbackName)
@@ -836,9 +846,14 @@ export function matrixService(server: FastifyInstance) {
) => { ) => {
const normalizedOptions = normalizeTenantRoomOptions(options) const normalizedOptions = normalizeTenantRoomOptions(options)
const existing = await findTenantRoomMetadata(tenant.id, normalizedOptions.key) const existing = await findTenantRoomMetadata(tenant.id, normalizedOptions.key)
const expectedAlias = tenantRoomAlias(tenant, normalizedOptions.key)
if (existing) { if (existing) {
const hasStaleMatrixRoomId = !belongsToCurrentMatrixServer(existing.matrixRoomId)
const hasStaleMatrixAlias = !belongsToCurrentMatrixServer(existing.matrixAlias)
const shouldUpdate = const shouldUpdate =
hasStaleMatrixRoomId ||
hasStaleMatrixAlias ||
(options.name !== undefined && existing.name !== normalizedOptions.name) || (options.name !== undefined && existing.name !== normalizedOptions.name) ||
(options.topic !== undefined && existing.topic !== normalizedOptions.topic) || (options.topic !== undefined && existing.topic !== normalizedOptions.topic) ||
(options.type !== undefined && existing.type !== normalizedOptions.type) || (options.type !== undefined && existing.type !== normalizedOptions.type) ||
@@ -857,6 +872,9 @@ export function matrixService(server: FastifyInstance) {
entityType: options.entityType !== undefined ? normalizedOptions.entityType : existing.entityType, entityType: options.entityType !== undefined ? normalizedOptions.entityType : existing.entityType,
entityId: options.entityId !== undefined ? normalizedOptions.entityId : existing.entityId, entityId: options.entityId !== undefined ? normalizedOptions.entityId : existing.entityId,
entityUuid: options.entityUuid !== undefined ? normalizedOptions.entityUuid : existing.entityUuid, 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(), updatedAt: new Date(),
}) })
.where(eq(communicationRooms.id, existing.id)) .where(eq(communicationRooms.id, existing.id))
@@ -876,7 +894,7 @@ export function matrixService(server: FastifyInstance) {
entityType: normalizedOptions.entityType, entityType: normalizedOptions.entityType,
entityId: normalizedOptions.entityId, entityId: normalizedOptions.entityId,
entityUuid: normalizedOptions.entityUuid, entityUuid: normalizedOptions.entityUuid,
matrixAlias: tenantRoomAlias(tenant, normalizedOptions.key), matrixAlias: expectedAlias,
}) })
.returning() .returning()
@@ -960,8 +978,8 @@ export function matrixService(server: FastifyInstance) {
entityUuid: metadata.entityUuid, entityUuid: metadata.entityUuid,
alias, alias,
exists: false, exists: false,
roomId: metadata.matrixRoomId, roomId: belongsToCurrentMatrixServer(metadata.matrixRoomId) ? metadata.matrixRoomId : null,
parentSpaceRoomId: metadata.parentSpaceRoomId, parentSpaceRoomId: belongsToCurrentMatrixServer(metadata.parentSpaceRoomId) ? metadata.parentSpaceRoomId : null,
servers: [], servers: [],
} }
} }
@@ -1141,6 +1159,9 @@ export function matrixService(server: FastifyInstance) {
const ensureServiceUserJoinedRoom = async (room: { roomId?: string | null; alias?: string | null }) => { const ensureServiceUserJoinedRoom = async (room: { roomId?: string | null; alias?: string | null }) => {
const target = room.roomId || room.alias const target = room.roomId || room.alias
if (!target) return { ok: false, status: "missing_room" } if (!target) return { ok: false, status: "missing_room" }
if (!belongsToCurrentMatrixServer(target)) {
return { ok: false, status: "stale_room", roomId: target }
}
const serviceLogin = await ensureServiceAccessToken() const serviceLogin = await ensureServiceAccessToken()