KI-AGENT: Veraltete Matrix-Raumreferenzen bereinigen
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user