Skip to content

Commit

Permalink
fix(core): send messages now takes a connection id (#491)
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht authored Oct 20, 2021
1 parent e24eafd commit ed9db11
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
21 changes: 16 additions & 5 deletions packages/core/src/modules/basic-messages/BasicMessagesModule.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ConnectionRecord } from '../connections'
import type { BasicMessageTags } from './repository/BasicMessageRecord'

import { Lifecycle, scoped } from 'tsyringe'

import { Dispatcher } from '../../agent/Dispatcher'
import { MessageSender } from '../../agent/MessageSender'
import { createOutboundMessage } from '../../agent/helpers'
import { ConnectionService } from '../connections'

import { BasicMessageHandler } from './handlers'
import { BasicMessageService } from './services'
Expand All @@ -13,15 +14,25 @@ import { BasicMessageService } from './services'
export class BasicMessagesModule {
private basicMessageService: BasicMessageService
private messageSender: MessageSender

public constructor(dispatcher: Dispatcher, basicMessageService: BasicMessageService, messageSender: MessageSender) {
private connectionService: ConnectionService

public constructor(
dispatcher: Dispatcher,
basicMessageService: BasicMessageService,
messageSender: MessageSender,
connectionService: ConnectionService
) {
this.basicMessageService = basicMessageService
this.messageSender = messageSender
this.connectionService = connectionService
this.registerHandlers(dispatcher)
}

public async sendMessage(connection: ConnectionRecord, message: string) {
const outboundMessage = await this.basicMessageService.send(message, connection)
public async sendMessage(connectionId: string, message: string) {
const connection = await this.connectionService.getById(connectionId)

const basicMessage = await this.basicMessageService.createMessage(message, connection)
const outboundMessage = createOutboundMessage(connection, basicMessage)
await this.messageSender.sendMessage(outboundMessage)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { InboundMessageContext } from '../../../agent/models/InboundMessageContext'
import type { OutboundMessage } from '../../../types'
import type { ConnectionRecord } from '../../connections/repository/ConnectionRecord'
import type { BasicMessageReceivedEvent } from '../BasicMessageEvents'
import type { BasicMessageTags } from '../repository'

import { Lifecycle, scoped } from 'tsyringe'

import { EventEmitter } from '../../../agent/EventEmitter'
import { createOutboundMessage } from '../../../agent/helpers'
import { BasicMessageEventTypes } from '../BasicMessageEvents'
import { BasicMessageRole } from '../BasicMessageRole'
import { BasicMessage } from '../messages'
Expand All @@ -23,21 +21,20 @@ export class BasicMessageService {
this.eventEmitter = eventEmitter
}

public async send(message: string, connection: ConnectionRecord): Promise<OutboundMessage<BasicMessage>> {
const basicMessage = new BasicMessage({
content: message,
})
public async createMessage(message: string, connectionRecord: ConnectionRecord) {
const basicMessage = new BasicMessage({ content: message })

const basicMessageRecord = new BasicMessageRecord({
id: basicMessage.id,
sentTime: basicMessage.sentTime.toISOString(),
content: basicMessage.content,
connectionId: connection.id,
connectionId: connectionRecord.id,
role: BasicMessageRole.Sender,
})

await this.basicMessageRepository.save(basicMessageRecord)
return createOutboundMessage(connection, basicMessage)

return basicMessage
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/modules/routing/__tests__/mediation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('mediator establishment', () => {
expect(senderRecipientConnection.isReady).toBe(true)

const message = 'hello, world'
await senderAgent.basicMessages.sendMessage(senderRecipientConnection, message)
await senderAgent.basicMessages.sendMessage(senderRecipientConnection.id, message)

const basicMessage = await waitForBasicMessage(recipientAgent, {
content: message,
Expand Down Expand Up @@ -249,7 +249,7 @@ describe('mediator establishment', () => {
expect(senderRecipientConnection.isReady).toBe(true)

const message = 'hello, world'
await senderAgent.basicMessages.sendMessage(senderRecipientConnection, message)
await senderAgent.basicMessages.sendMessage(senderRecipientConnection.id, message)

const basicMessage = await waitForBasicMessage(recipientAgent, {
content: message,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/agents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('agents', () => {

test('send a message to connection', async () => {
const message = 'hello, world'
await aliceAgent.basicMessages.sendMessage(aliceConnection, message)
await aliceAgent.basicMessages.sendMessage(aliceConnection.id, message)

const basicMessage = await waitForBasicMessage(bobAgent, {
content: message,
Expand Down

0 comments on commit ed9db11

Please sign in to comment.