Skip to content

Commit

Permalink
move method validateMessages() from class Webservice to MessageHelper…
Browse files Browse the repository at this point in the history
… to make it publicly accessible
  • Loading branch information
paulchen committed Jun 23, 2024
1 parent 7c34e65 commit 4f88202
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
30 changes: 3 additions & 27 deletions src/main/kotlin/at/rueckgr/kotlin/rocketbot/Webservice.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.rueckgr.kotlin.rocketbot

import at.rueckgr.kotlin.rocketbot.util.Logging
import at.rueckgr.kotlin.rocketbot.util.MessageHelper
import at.rueckgr.kotlin.rocketbot.util.logger
import com.fasterxml.jackson.databind.SerializationFeature
import io.ktor.server.application.*
Expand Down Expand Up @@ -50,9 +51,9 @@ class Webservice(private val webserverPort: Int,
val message = call.receive<WebserviceMessage>()

logger().debug("Received message via webservice: {}", message)
val (validationMessage, status, validatedMessage) = validateMessage(message)
val (validationMessage, validatedMessage) = MessageHelper.instance.validateMessage(message)
if (StringUtils.isNotBlank(validationMessage)) {
call.respondText(validationMessage, status = status)
call.respondText(validationMessage, status = HttpStatusCode.BadRequest)
}
else {
Bot.webserviceMessageQueue.add(validatedMessage)
Expand All @@ -71,25 +72,6 @@ class Webservice(private val webserverPort: Int,
engine?.stop(100L, 100L)
}

private fun validateMessage(message: WebserviceMessage): ValidationResult {
if (StringUtils.isBlank(message.roomId) && StringUtils.isBlank(message.roomName)) {
return ValidationResult("One of roomId and roomName must be set", HttpStatusCode.BadRequest, message)
}
if (StringUtils.isNotBlank(message.roomId) && StringUtils.isNotBlank(message.roomName)) {
return ValidationResult("Only of roomId and roomName must be set", HttpStatusCode.BadRequest, message)
}
val roomId = if (message.roomName != null && StringUtils.isNotBlank(message.roomName)) {
Bot.subscriptionService.getChannelIdByName(message.roomName)
?: return ValidationResult("Unknown channel ${message.roomName}", HttpStatusCode.BadRequest, message)
}
else {
message.roomId
}

val validatedMessage = WebserviceMessage(roomId, null, message.message, message.parentMessageId, message.emoji, message.username)
return ValidationResult("", HttpStatusCode.OK, validatedMessage)
}

private fun verifyCredentials(credentials: UserPasswordCredential): UserIdPrincipal? =
when (webserviceUserValidator.validate(credentials.name, credentials.password)) {
true -> UserIdPrincipal(credentials.name)
Expand All @@ -105,9 +87,3 @@ data class WebserviceMessage(
val emoji: String? = null,
val username: String? = null
)

data class ValidationResult(
val validationMessage: String,
val httpStatusCode: HttpStatusCode,
val webserviceMessage: WebserviceMessage
)
27 changes: 27 additions & 0 deletions src/main/kotlin/at/rueckgr/kotlin/rocketbot/util/MessageHelper.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package at.rueckgr.kotlin.rocketbot.util

import at.rueckgr.kotlin.rocketbot.Bot
import at.rueckgr.kotlin.rocketbot.EventHandler
import at.rueckgr.kotlin.rocketbot.WebserviceMessage
import at.rueckgr.kotlin.rocketbot.websocket.SendMessageMessage
import org.apache.commons.lang3.StringUtils
import java.util.*
Expand Down Expand Up @@ -31,4 +33,29 @@ class MessageHelper {
"d" -> EventHandler.ChannelType.DIRECT
else -> EventHandler.ChannelType.OTHER
}

fun validateMessage(message: WebserviceMessage): ValidationResult {
if (StringUtils.isBlank(message.roomId) && StringUtils.isBlank(message.roomName)) {
return ValidationResult("One of roomId and roomName must be set", message)
}
if (StringUtils.isNotBlank(message.roomId) && StringUtils.isNotBlank(message.roomName)) {
return ValidationResult("Only of roomId and roomName must be set", message)
}
val roomId = if (message.roomName != null && StringUtils.isNotBlank(message.roomName)) {
Bot.subscriptionService.getChannelIdByName(message.roomName)
?: return ValidationResult("Unknown channel ${message.roomName}", message)
}
else {
message.roomId
}

val validatedMessage = WebserviceMessage(roomId, null, message.message, message.parentMessageId, message.emoji, message.username)
return ValidationResult("", validatedMessage)
}
}

data class ValidationResult(
val validationMessage: String,
val webserviceMessage: WebserviceMessage
)

0 comments on commit 4f88202

Please sign in to comment.