From b21921242eb09a110d111b856e85188f19945c49 Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 11 Jan 2026 22:35:41 +0100 Subject: [PATCH] notifications --- .../notifications/rocketchat-adapter.ts | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/services/notifications/rocketchat-adapter.ts b/lib/services/notifications/rocketchat-adapter.ts index c68d230..307885c 100644 --- a/lib/services/notifications/rocketchat-adapter.ts +++ b/lib/services/notifications/rocketchat-adapter.ts @@ -110,8 +110,9 @@ export class RocketChatAdapter implements NotificationAdapter { /** * Get user token for RocketChat API + * Creates a token for the specified RocketChat user ID */ - private async getUserToken(): Promise<{ authToken: string; userId: string } | null> { + private async getUserToken(rocketChatUserId: string): Promise<{ authToken: string; userId: string } | null> { try { const adminHeaders = { 'X-Auth-Token': process.env.ROCKET_CHAT_TOKEN!, @@ -119,19 +120,37 @@ export class RocketChatAdapter implements NotificationAdapter { 'Content-Type': 'application/json' }; + // Create token for the specific user const createTokenResponse = await fetch(`${this.baseUrl}/api/v1/users.createToken`, { method: 'POST', - headers: adminHeaders + headers: adminHeaders, + body: JSON.stringify({ + userId: rocketChatUserId + }) }); if (!createTokenResponse.ok) { logger.error('[ROCKETCHAT_ADAPTER] Failed to create user token', { status: createTokenResponse.status, + rocketChatUserId, }); return null; } const tokenData = await createTokenResponse.json(); + + if (!tokenData.success || !tokenData.data) { + logger.error('[ROCKETCHAT_ADAPTER] Invalid token response', { + response: tokenData, + }); + return null; + } + + logger.debug('[ROCKETCHAT_ADAPTER] User token created', { + rocketChatUserId, + tokenUserId: tokenData.data.userId, + }); + return { authToken: tokenData.data.authToken, userId: tokenData.data.userId @@ -139,6 +158,7 @@ export class RocketChatAdapter implements NotificationAdapter { } catch (error) { logger.error('[ROCKETCHAT_ADAPTER] Error getting user token', { error: error instanceof Error ? error.message : String(error), + rocketChatUserId, }); return null; } @@ -165,7 +185,9 @@ export class RocketChatAdapter implements NotificationAdapter { const rocketChatUserId = await this.getRocketChatUserId(email); if (!rocketChatUserId) { - logger.debug('[ROCKETCHAT_ADAPTER] User not found in RocketChat'); + logger.debug('[ROCKETCHAT_ADAPTER] User not found in RocketChat', { + emailHash: email ? Buffer.from(email.toLowerCase()).toString('base64').slice(0, 12) : null, + }); return { total: 0, unread: 0, @@ -178,9 +200,16 @@ export class RocketChatAdapter implements NotificationAdapter { }; } - const userToken = await this.getUserToken(); + logger.debug('[ROCKETCHAT_ADAPTER] Found RocketChat user', { + rocketChatUserId, + emailHash: Buffer.from(email.toLowerCase()).toString('base64').slice(0, 12), + }); + + const userToken = await this.getUserToken(rocketChatUserId); if (!userToken) { - logger.error('[ROCKETCHAT_ADAPTER] Could not get user token'); + logger.error('[ROCKETCHAT_ADAPTER] Could not get user token', { + rocketChatUserId, + }); return { total: 0, unread: 0,