KI-AGENT: Matrix-Raum-API verallgemeinern

This commit is contained in:
2026-05-18 18:12:26 +02:00
parent 8824b1c9c8
commit f33ccf730a
3 changed files with 286 additions and 118 deletions

View File

@@ -3,6 +3,23 @@ import { matrixService } from "../modules/matrix.service"
export default async function communicationRoutes(server: FastifyInstance) {
const matrix = matrixService(server)
const handleMatrixError = (req: any, reply: any, err: any, fallbackMessage: string) => {
req.log.error(err)
return reply
.code(err.statusCode || 500)
.send({ error: err.message || fallbackMessage })
}
const roomOptionsFromRequest = (req: any) => {
const params = req.params as { roomKey?: string }
const body = (req.body || {}) as { key?: string, name?: string, topic?: string }
return {
key: params.roomKey || body.key,
name: body.name,
topic: body.topic,
}
}
server.get("/communication/matrix/status", async () => {
return matrix.getStatus()
@@ -21,10 +38,7 @@ export default async function communicationRoutes(server: FastifyInstance) {
try {
return await matrix.provisionCurrentUser(req.user.user_id, req.user.tenant_id)
} catch (err: any) {
req.log.error(err)
return reply
.code(err.statusCode || 500)
.send({ error: err.message || "Matrix provisioning failed" })
return handleMatrixError(req, reply, err, "Matrix provisioning failed")
}
})
@@ -32,10 +46,7 @@ export default async function communicationRoutes(server: FastifyInstance) {
try {
return await matrix.getTenantSpaceStatus(req.user.tenant_id)
} catch (err: any) {
req.log.error(err)
return reply
.code(err.statusCode || 500)
.send({ error: err.message || "Matrix tenant space status failed" })
return handleMatrixError(req, reply, err, "Matrix tenant space status failed")
}
})
@@ -43,10 +54,27 @@ export default async function communicationRoutes(server: FastifyInstance) {
try {
return await matrix.provisionCurrentTenantSpace(req.user.user_id, req.user.tenant_id)
} catch (err: any) {
req.log.error(err)
return reply
.code(err.statusCode || 500)
.send({ error: err.message || "Matrix tenant space provisioning failed" })
return handleMatrixError(req, reply, err, "Matrix tenant space provisioning failed")
}
})
server.get("/communication/matrix/rooms", async (req, reply) => {
try {
return await matrix.listTenantRooms(req.user.tenant_id)
} catch (err: any) {
return handleMatrixError(req, reply, err, "Matrix rooms failed")
}
})
server.post("/communication/matrix/rooms", async (req, reply) => {
try {
return await matrix.provisionTenantRoom(
req.user.user_id,
req.user.tenant_id,
roomOptionsFromRequest(req)
)
} catch (err: any) {
return handleMatrixError(req, reply, err, "Matrix room provisioning failed")
}
})
@@ -54,10 +82,7 @@ export default async function communicationRoutes(server: FastifyInstance) {
try {
return await matrix.getTenantRoomStatus(req.user.tenant_id, "allgemein", "Allgemeiner Chat")
} catch (err: any) {
req.log.error(err)
return reply
.code(err.statusCode || 500)
.send({ error: err.message || "Matrix room status failed" })
return handleMatrixError(req, reply, err, "Matrix room status failed")
}
})
@@ -68,10 +93,7 @@ export default async function communicationRoutes(server: FastifyInstance) {
name: "Allgemeiner Chat",
})
} catch (err: any) {
req.log.error(err)
return reply
.code(err.statusCode || 500)
.send({ error: err.message || "Matrix room provisioning failed" })
return handleMatrixError(req, reply, err, "Matrix room provisioning failed")
}
})
@@ -79,10 +101,7 @@ export default async function communicationRoutes(server: FastifyInstance) {
try {
return await matrix.getGeneralRoomMessages(req.user.user_id, req.user.tenant_id)
} catch (err: any) {
req.log.error(err)
return reply
.code(err.statusCode || 500)
.send({ error: err.message || "Matrix messages failed" })
return handleMatrixError(req, reply, err, "Matrix messages failed")
}
})
@@ -90,10 +109,7 @@ export default async function communicationRoutes(server: FastifyInstance) {
try {
return await matrix.getGeneralRoomMembers(req.user.user_id, req.user.tenant_id)
} catch (err: any) {
req.log.error(err)
return reply
.code(err.statusCode || 500)
.send({ error: err.message || "Matrix members failed" })
return handleMatrixError(req, reply, err, "Matrix members failed")
}
})
@@ -102,10 +118,66 @@ export default async function communicationRoutes(server: FastifyInstance) {
const body = req.body as { text?: string }
return await matrix.sendGeneralRoomMessage(req.user.user_id, req.user.tenant_id, body.text || "")
} catch (err: any) {
req.log.error(err)
return reply
.code(err.statusCode || 500)
.send({ error: err.message || "Matrix message send failed" })
return handleMatrixError(req, reply, err, "Matrix message send failed")
}
})
server.get("/communication/matrix/rooms/:roomKey", async (req, reply) => {
try {
const params = req.params as { roomKey: string }
return await matrix.getTenantRoomStatus(req.user.tenant_id, params.roomKey, params.roomKey)
} catch (err: any) {
return handleMatrixError(req, reply, err, "Matrix room status failed")
}
})
server.post("/communication/matrix/rooms/:roomKey/provision", async (req, reply) => {
try {
return await matrix.provisionTenantRoom(
req.user.user_id,
req.user.tenant_id,
roomOptionsFromRequest(req)
)
} catch (err: any) {
return handleMatrixError(req, reply, err, "Matrix room provisioning failed")
}
})
server.get("/communication/matrix/rooms/:roomKey/messages", async (req, reply) => {
try {
return await matrix.getTenantRoomMessages(
req.user.user_id,
req.user.tenant_id,
roomOptionsFromRequest(req)
)
} catch (err: any) {
return handleMatrixError(req, reply, err, "Matrix messages failed")
}
})
server.get("/communication/matrix/rooms/:roomKey/members", async (req, reply) => {
try {
return await matrix.getTenantRoomMembers(
req.user.user_id,
req.user.tenant_id,
roomOptionsFromRequest(req)
)
} catch (err: any) {
return handleMatrixError(req, reply, err, "Matrix members failed")
}
})
server.post("/communication/matrix/rooms/:roomKey/messages", async (req, reply) => {
try {
const body = req.body as { text?: string }
return await matrix.sendTenantRoomMessage(
req.user.user_id,
req.user.tenant_id,
roomOptionsFromRequest(req),
body.text || ""
)
} catch (err: any) {
return handleMatrixError(req, reply, err, "Matrix message send failed")
}
})
}