KI-AGENT: Matrix-Einbettung stabilisieren

This commit is contained in:
2026-05-18 18:56:37 +02:00
parent 7c68ce61f2
commit c93ea4284d
5 changed files with 129 additions and 5 deletions

View File

@@ -35,6 +35,11 @@ type MatrixUserSession = {
validUntilMs: number
}
type MatrixLoginTokenResponse = {
login_token: string
expires_in_ms: number
}
type MatrixTenantRoomOptions = {
key?: string
name?: string
@@ -1146,6 +1151,40 @@ export function matrixService(server: FastifyInstance) {
}
}
const createElementRoomSession = async (
userId: string,
tenantId: number | null,
options: MatrixTenantRoomOptions = {}
) => {
const room = await provisionTenantRoom(userId, tenantId, options)
const session = await ensureCurrentUserJoinedRoom(userId, tenantId, {
roomId: room.roomId,
alias: room.alias,
})
const token = await requestMatrixJson<MatrixLoginTokenResponse>(
"/_matrix/client/v1/login/get_token",
session.accessToken,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({}),
}
)
return {
roomId: room.roomId,
alias: room.alias,
key: room.key,
name: room.name,
matrixUserId: session.matrixUserId,
loginToken: token.login_token,
expiresInMs: token.expires_in_ms,
homeserverUrl: homeserverUrl(),
serverName: serverName(),
}
}
const listTenantCommunicationUsers = async (tenantId: number | null) => {
const tenant = await getCurrentTenant(tenantId)
const rows = await server.db
@@ -1302,6 +1341,7 @@ export function matrixService(server: FastifyInstance) {
getTenantRoomMessages,
getTenantRoomMembers,
sendTenantRoomMessage,
createElementRoomSession,
syncTenantRoomMembers,
getGeneralRoomMessages,
getGeneralRoomMembers,

View File

@@ -125,6 +125,17 @@ export default async function communicationRoutes(server: FastifyInstance) {
}
})
server.post("/communication/matrix/rooms/general/session", async (req, reply) => {
try {
return await matrix.createElementRoomSession(req.user.user_id, req.user.tenant_id, {
key: "allgemein",
name: "Allgemeiner Chat",
})
} catch (err: any) {
return handleMatrixError(req, reply, err, "Matrix session failed")
}
})
server.post("/communication/matrix/rooms/general/members/sync", async (req, reply) => {
try {
return await matrix.syncTenantRoomMembers(req.user.user_id, req.user.tenant_id, {
@@ -190,6 +201,18 @@ export default async function communicationRoutes(server: FastifyInstance) {
}
})
server.post("/communication/matrix/rooms/:roomKey/session", async (req, reply) => {
try {
return await matrix.createElementRoomSession(
req.user.user_id,
req.user.tenant_id,
roomOptionsFromRequest(req)
)
} catch (err: any) {
return handleMatrixError(req, reply, err, "Matrix session failed")
}
})
server.post("/communication/matrix/rooms/:roomKey/members/sync", async (req, reply) => {
try {
return await matrix.syncTenantRoomMembers(