Skip to content

Commit

Permalink
fix #1556 web_connector: add metadata to SSE messages
Browse files Browse the repository at this point in the history
Also clearing metadata after each response sent
And stop using a deprecated Vertx API
  • Loading branch information
Fabilin authored and vsct-jburet committed Nov 24, 2023
1 parent 3d56f7b commit 5087113
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
8 changes: 4 additions & 4 deletions bot/connector-web/src/main/kotlin/WebConnector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class WebConnector internal constructor(
// Fallback to previous property name for backward compatibility
?: propertyOrNull("allow_markdown").toBoolean()
)
private val channels by lazy { Channels(ChannelMongoDAO, messageProcessor) }
private val channels by lazy { Channels(ChannelMongoDAO) }
}

private val executor: Executor get() = injector.provide()
Expand All @@ -124,8 +124,8 @@ class WebConnector internal constructor(

router.route(path)
.handler(
// "*"+credentials is rejected by browsers, so we use the equivalent regex instead
CorsHandler.create(if (cookieAuth) ".*" else "*")
CorsHandler.create()
.addRelativeOrigin(".*") // "*"+credentials is rejected by browsers, so we use the equivalent regex instead
.allowedMethod(HttpMethod.POST)
.run {
if (sseEnabled) allowedMethod(HttpMethod.GET) else this
Expand Down Expand Up @@ -372,7 +372,7 @@ class WebConnector internal constructor(
private fun handleWebConnectorCallback(callback: WebConnectorCallback, event: Action) {
callback.addAction(event)
if (sseEnabled) {
channels.send(event)
channels.send(event.applicationId, event.recipientId, callback.createResponse(listOf(event)))
}
if (event.metadata.lastAnswer) {
callback.sendResponse()
Expand Down
8 changes: 6 additions & 2 deletions bot/connector-web/src/main/kotlin/WebConnectorCallback.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ internal class WebConnectorCallback(
this.metadata[metadata.type] = metadata.value
}

fun createResponse(actions: List<Action>): WebConnectorResponse {
val messages = actions.mapNotNull(messageProcessor::process)
return WebConnectorResponse(messages, metadata)
}

fun sendResponse() {
WebRequestInfosByEvent.invalidate(eventId)
val messages = actions.mapNotNull(messageProcessor::process)
context.response()
.putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.end(webMapper.writeValueAsString(WebConnectorResponse(messages, metadata)))
.end(webMapper.writeValueAsString(createResponse(actions)))
}
}
10 changes: 4 additions & 6 deletions bot/connector-web/src/main/kotlin/channel/Channels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
package ai.tock.bot.connector.web.channel

import ai.tock.bot.connector.web.WebConnectorResponse
import ai.tock.bot.connector.web.WebMessageProcessor
import ai.tock.bot.engine.action.Action
import ai.tock.bot.engine.user.PlayerId
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArrayList

internal class Channels(private val channelDAO: ChannelDAO, private val messageProcessor: WebMessageProcessor) {
internal class Channels(private val channelDAO: ChannelDAO) {

private val channelsByUser = ConcurrentHashMap<String, CopyOnWriteArrayList<Channel>>()

Expand Down Expand Up @@ -51,8 +50,7 @@ internal class Channels(private val channelDAO: ChannelDAO, private val messageP
}
}

fun send(action: Action) {
val messages = listOfNotNull(messageProcessor.process(action))
channelDAO.save(ChannelEvent(action.applicationId, action.recipientId.id, WebConnectorResponse(messages)))
fun send(applicationId: String, recipientId: PlayerId, response: WebConnectorResponse) {
channelDAO.save(ChannelEvent(applicationId, recipientId.id, response))
}
}

0 comments on commit 5087113

Please sign in to comment.