KI-AGENT: Projekträume und Direktnachrichten integrieren

This commit is contained in:
2026-05-19 08:27:39 +02:00
parent 716de8a503
commit 5b3445c2dc
3 changed files with 338 additions and 28 deletions

View File

@@ -56,6 +56,7 @@ type MatrixTenantRoomOptions = {
entityType?: string | null
entityId?: number | null
entityUuid?: string | null
inviteUserIds?: string[]
}
type MatrixCachedValue<T = any> = {
@@ -247,6 +248,7 @@ export function matrixService(server: FastifyInstance) {
entityType: options.entityType || null,
entityId: options.entityId || null,
entityUuid: options.entityUuid || null,
inviteUserIds: options.inviteUserIds || [],
}
}
@@ -885,6 +887,7 @@ export function matrixService(server: FastifyInstance) {
const existing = await getTenantRoomStatus(tenant.id, key, name)
const userAccount = await provisionCurrentUser(userId, tenant.id)
const invitedMatrixUserIds = await matrixUserIdsForInvitees(userId, tenant.id, normalizedOptions.inviteUserIds || [])
const tenantSpace = await provisionCurrentTenantSpace(userId, tenant.id)
if (existing.exists) {
@@ -902,6 +905,8 @@ export function matrixService(server: FastifyInstance) {
invitedUserId: userAccount.matrixUserId,
}
await inviteUsersToRoom(existing.roomId, invitedMatrixUserIds)
matrixTenantRoomCache.set(cacheKey, {
exists: true,
cachedUntil: Date.now() + 30 * 60 * 1000,
@@ -923,7 +928,7 @@ export function matrixService(server: FastifyInstance) {
preset: "private_chat",
visibility: "private",
room_alias_name: tenantRoomAliasLocalpart(tenant, key),
invite: [userAccount.matrixUserId],
invite: Array.from(new Set([userAccount.matrixUserId, ...invitedMatrixUserIds])),
initial_state: [
{
type: "m.room.history_visibility",
@@ -994,6 +999,42 @@ export function matrixService(server: FastifyInstance) {
return value
}
const matrixUserIdsForInvitees = async (
currentUserId: string,
tenantId: number,
inviteUserIds: string[]
) => {
const uniqueUserIds = Array.from(new Set(inviteUserIds.filter((id) => id && id !== currentUserId)))
return await Promise.all(uniqueUserIds.map(async (inviteUserId) => {
const account = await provisionCurrentUser(inviteUserId, tenantId)
return account.matrixUserId
}))
}
const inviteUsersToRoom = async (roomId: string | null, matrixUserIds: string[]) => {
if (!roomId || !matrixUserIds.length) return
const serviceLogin = await ensureServiceAccessToken()
for (const matrixUserId of matrixUserIds) {
try {
await requestMatrixJson(
`/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/invite`,
serviceLogin.accessToken,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ user_id: matrixUserId }),
}
)
} catch (err: any) {
if (err.statusCode === 403 || err.statusCode === 400) continue
throw err
}
}
}
const ensureCurrentUserJoinedRoom = async (
userId: string,
tenantId: number | null,