Skip to content

Commit

Permalink
feat: Added client remove all messages for a recipient issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmatau79 committed Sep 23, 2024
1 parent 18ee788 commit b36ddb1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
11 changes: 10 additions & 1 deletion packages/client/src/dto/client.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty } from 'class-validator'
import { IsNotEmpty, IsString } from 'class-validator'

export class ConnectionIdDto {
@IsNotEmpty({ message: 'connectionId is required' })
Expand All @@ -12,3 +12,12 @@ export class AddLiveSessionDto {
@IsNotEmpty({ message: 'sessionId is required' })
sessionId: string
}

export class RemoveAllMessagesDto {
@IsNotEmpty()
connectionId: string

@IsNotEmpty()
@IsString()
recipientDid: string
}
20 changes: 19 additions & 1 deletion packages/client/src/lib/MessagePickupRepositoryClient.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<void>}
*/
async removeAllMessages(params: RemoveAllMessagesDto): Promise<void> {
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.
Expand Down
21 changes: 14 additions & 7 deletions packages/client/src/lib/wsClient-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessagePickupRepositoryClient } from './MessagePickupRepositoryClient'
import { MessagePickupRepositoryClient } from '../lib/MessagePickupRepositoryClient'
import { Logger } from '@nestjs/common'

const logger = new Logger('WebSocketTestScript')
Expand All @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/websocket/websocket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
})
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/websocket/websocket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export class WebsocketService {
*/
async removeAllMessage(dto: RemoveAllMessagesDto): Promise<void> {
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
Expand Down

0 comments on commit b36ddb1

Please sign in to comment.