From b36ddb18dd3d4d95359bad84f1a788f6b433c6d7 Mon Sep 17 00:00:00 2001 From: Gabriel Mata Date: Mon, 23 Sep 2024 15:18:53 -0500 Subject: [PATCH] feat: Added client remove all messages for a recipient issue #3 --- packages/client/src/dto/client.dto.ts | 11 +++++++++- .../src/lib/MessagePickupRepositoryClient.ts | 20 +++++++++++++++++- packages/client/src/lib/wsClient-test.ts | 21 ++++++++++++------- .../server/src/websocket/websocket.gateway.ts | 2 +- .../server/src/websocket/websocket.service.ts | 1 + 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/packages/client/src/dto/client.dto.ts b/packages/client/src/dto/client.dto.ts index 126fbe4..ee60fd7 100644 --- a/packages/client/src/dto/client.dto.ts +++ b/packages/client/src/dto/client.dto.ts @@ -1,4 +1,4 @@ -import { IsNotEmpty } from 'class-validator' +import { IsNotEmpty, IsString } from 'class-validator' export class ConnectionIdDto { @IsNotEmpty({ message: 'connectionId is required' }) @@ -12,3 +12,12 @@ export class AddLiveSessionDto { @IsNotEmpty({ message: 'sessionId is required' }) sessionId: string } + +export class RemoveAllMessagesDto { + @IsNotEmpty() + connectionId: string + + @IsNotEmpty() + @IsString() + recipientDid: string +} diff --git a/packages/client/src/lib/MessagePickupRepositoryClient.ts b/packages/client/src/lib/MessagePickupRepositoryClient.ts index 434cef7..8fac42e 100644 --- a/packages/client/src/lib/MessagePickupRepositoryClient.ts +++ b/packages/client/src/lib/MessagePickupRepositoryClient.ts @@ -1,6 +1,6 @@ import { Client } from 'rpc-websockets' import { Logger } from '@nestjs/common' -import { AddLiveSessionDto, ConnectionIdDto } from '../dto/client.dto' +import { AddLiveSessionDto, ConnectionIdDto, RemoveAllMessagesDto } from '../dto/client.dto' import { JsonRpcParamsMessage } from '../interfaces/interfaces' import { AddMessageOptions, @@ -167,6 +167,24 @@ export class MessagePickupRepositoryClient implements MessagePickupRepository { } } + /** + * Call the 'removeAllMessages' RPC method. + * @param params - Parameters to pass to the 'removeAllMessages' method. + * @returns {Promise} + */ + async removeAllMessages(params: RemoveAllMessagesDto): Promise { + try { + const result: unknown = await this.client.call('removeAllMessages', params, 2000) + + if (typeof result !== 'boolean') { + throw new Error('Unexpected result: Expected an object or null') + } + } catch (error) { + this.logger.error('Error calling removeAllMessages:', error) + throw error + } + } + /** * Call the 'getLiveSession' RPC method. * This method retrieves the live session data from the WebSocket server. diff --git a/packages/client/src/lib/wsClient-test.ts b/packages/client/src/lib/wsClient-test.ts index b7efb07..8dc5167 100644 --- a/packages/client/src/lib/wsClient-test.ts +++ b/packages/client/src/lib/wsClient-test.ts @@ -1,4 +1,4 @@ -import { MessagePickupRepositoryClient } from './MessagePickupRepositoryClient' +import { MessagePickupRepositoryClient } from '../lib/MessagePickupRepositoryClient' import { Logger } from '@nestjs/common' const logger = new Logger('WebSocketTestScript') @@ -9,7 +9,7 @@ const logger = new Logger('WebSocketTestScript') await client.connect() client.messageReceived((data) => { - console.log('Received message:', data) + logger.debug(`*** [messageReceive] listener: ${JSON.stringify(data, null, 2)} ****:`) }) const connectionId_1 = '8df6b644-0206-4d50-aade-a565d5c82683' @@ -130,20 +130,27 @@ const logger = new Logger('WebSocketTestScript') await new Promise((resolve) => setTimeout(resolve, 2000)) - const [removeMessages1] = await Promise.all([ - await client.removeMessages({ connectionId: connectionId_2, messageIds: messageIds1 }), + const [removeAllMessages] = await Promise.all([ + await client.removeAllMessages({ + connectionId: connectionId_2, + recipientDid: 'HBstN9Dusi7vJzuQ99fCmnvvrcKPAprCknp6XKmgHk89', + }), ]) logger.log(`client-1 RemoveMessages: ${JSON.stringify(removeMessages, null, 2)}`) - logger.log(`client-2 RemoveMessages: ${JSON.stringify(removeMessages1, null, 2)}`) + logger.log(`client-2 RemoveAllMessages: ${JSON.stringify(removeAllMessages, null, 2)}`) logger.log(`\n *** End takeFromQueue test ***\n`) await new Promise((resolve) => setTimeout(resolve, 5000)) // Eliminar sesiones en vivo logger.log(`*** Begin removeLiveSession test ***\n`) - const removeSession = await client.removeLiveSession({ connectionId: connectionId_1 }) - const removeSession1 = await client.removeLiveSession({ connectionId: connectionId_2 }) + const removeSession = await client.removeLiveSession({ + connectionId: connectionId_1, + }) + const removeSession1 = await client.removeLiveSession({ + connectionId: connectionId_2, + }) logger.log(`RemoveLiveSession client-1: ${removeSession} -- client-2: ${removeSession1}`) logger.log(`\n *** End removeLiveSession test ***\n`) } catch (error) { diff --git a/packages/server/src/websocket/websocket.gateway.ts b/packages/server/src/websocket/websocket.gateway.ts index 3af92e7..b5dedbd 100644 --- a/packages/server/src/websocket/websocket.gateway.ts +++ b/packages/server/src/websocket/websocket.gateway.ts @@ -124,7 +124,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy { await this.websocketService.removeAllMessage(params) return true } catch (error) { - this.logger.error('Error in removeMessages method', error.stack) + this.logger.error('Error in removeAllMessages method', error.stack) throw this.server.createError(500, 'Internal server error', { details: error.message }) } }) diff --git a/packages/server/src/websocket/websocket.service.ts b/packages/server/src/websocket/websocket.service.ts index 0d73de3..60f02d5 100644 --- a/packages/server/src/websocket/websocket.service.ts +++ b/packages/server/src/websocket/websocket.service.ts @@ -282,6 +282,7 @@ export class WebsocketService { */ async removeAllMessage(dto: RemoveAllMessagesDto): Promise { const { connectionId, recipientDid } = dto + this.logger.debug('[removeAllMessage] Method called with DTO:', dto) try { // Get the list of messages stored in Redis associated with the connectionId