From 4f882021595c08c2c9223c897b65e532051ba368 Mon Sep 17 00:00:00 2001 From: Paul Staroch Date: Sun, 23 Jun 2024 23:12:09 +0200 Subject: [PATCH] move method validateMessages() from class Webservice to MessageHelper to make it publicly accessible --- .../at/rueckgr/kotlin/rocketbot/Webservice.kt | 30 ++----------------- .../kotlin/rocketbot/util/MessageHelper.kt | 27 +++++++++++++++++ 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/at/rueckgr/kotlin/rocketbot/Webservice.kt b/src/main/kotlin/at/rueckgr/kotlin/rocketbot/Webservice.kt index a303eb4..db5ad68 100644 --- a/src/main/kotlin/at/rueckgr/kotlin/rocketbot/Webservice.kt +++ b/src/main/kotlin/at/rueckgr/kotlin/rocketbot/Webservice.kt @@ -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.* @@ -50,9 +51,9 @@ class Webservice(private val webserverPort: Int, val message = call.receive() 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) @@ -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) @@ -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 -) diff --git a/src/main/kotlin/at/rueckgr/kotlin/rocketbot/util/MessageHelper.kt b/src/main/kotlin/at/rueckgr/kotlin/rocketbot/util/MessageHelper.kt index 9e360f5..716337b 100644 --- a/src/main/kotlin/at/rueckgr/kotlin/rocketbot/util/MessageHelper.kt +++ b/src/main/kotlin/at/rueckgr/kotlin/rocketbot/util/MessageHelper.kt @@ -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.* @@ -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 +) +