Skip to content

Commit

Permalink
Merge pull request #5 from wireapp/staging
Browse files Browse the repository at this point in the history
merge for release 0.2.3
  • Loading branch information
LukasForst authored Oct 7, 2020
2 parents 3642a37 + 0a3ab8d commit 1943b27
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 84 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Makefile
.gradle
.idea
build
Expand Down
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ COPY --from=build /src/build/distributions/app.tar $APP_ROOT/
RUN mkdir $APP_ROOT/run
RUN tar -xvf app.tar --strip-components=1 -C $APP_ROOT/run

# ------------------- Wire common -----------------
# create version file
ARG release_version=development
ENV RELEASE_FILE_PATH=$APP_ROOT/run/release.txt
RUN echo $release_version > $RELEASE_FILE_PATH
# enable json logging
# TODO enable this once we fully migrate to JSON logging everywhere
# ENV JSON_LOGGING=true
# /------------------ Wire common -----------------

EXPOSE 8080
ENTRYPOINT ["/bin/sh", "-c", "/app/run/bin/polls"]
27 changes: 15 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
plugins {
kotlin("jvm") version "1.3.71"
kotlin("jvm") version "1.4.10"
application
distribution
id("net.nemerosa.versioning") version "2.12.1"
id("net.nemerosa.versioning") version "2.14.0"
}

group = "com.wire.bots.polls"
version = versioning.info?.tag ?: versioning.info?.lastTag ?: "development"

val mainClass = "com.wire.bots.polls.PollBotKt"
val mClass = "com.wire.bots.polls.PollBotKt"

application {
mainClassName = mainClass
mainClass.set(mClass)
}

repositories {
Expand All @@ -22,10 +22,11 @@ dependencies {
// stdlib
implementation(kotlin("stdlib-jdk8"))
// extension functions
implementation("ai.blindspot.ktoolz", "ktoolz", "1.0.6")
implementation("pw.forst.tools", "katlib", "1.1.2")


// Ktor server dependencies
val ktorVersion = "1.3.2"
val ktorVersion = "1.4.1"
implementation("io.ktor", "ktor-server-core", ktorVersion)
implementation("io.ktor", "ktor-server-netty", ktorVersion)
implementation("io.ktor", "ktor-jackson", ktorVersion)
Expand All @@ -39,29 +40,31 @@ dependencies {

// Prometheus metrics
implementation("io.ktor", "ktor-metrics-micrometer", ktorVersion)
implementation("io.micrometer", "micrometer-registry-prometheus", "1.4.1")
implementation("io.micrometer", "micrometer-registry-prometheus", "1.5.5")

// logging
implementation("io.github.microutils", "kotlin-logging", "1.7.9")
implementation("io.github.microutils", "kotlin-logging", "2.0.3")
// if-else in logback.xml
implementation("org.codehaus.janino", "janino", "3.1.2")
implementation("ch.qos.logback", "logback-classic", "1.2.3")

// DI
val kodeinVersion = "6.5.4"
val kodeinVersion = "6.5.5"
implementation("org.kodein.di", "kodein-di-generic-jvm", kodeinVersion)
implementation("org.kodein.di", "kodein-di-framework-ktor-server-jvm", kodeinVersion)

// database
implementation("org.postgresql", "postgresql", "42.2.2")

val exposedVersion = "0.22.1"
val exposedVersion = "0.27.1"
implementation("org.jetbrains.exposed", "exposed-core", exposedVersion)
implementation("org.jetbrains.exposed", "exposed-dao", exposedVersion)
implementation("org.jetbrains.exposed", "exposed-jdbc", exposedVersion)
implementation("org.jetbrains.exposed", "exposed-java-time", exposedVersion)
implementation("pw.forst", "exposed-upsert", "1.0")

// database migrations from the code
implementation("org.flywaydb", "flyway-core", "6.3.2")
implementation("org.flywaydb", "flyway-core", "7.0.0")
}

tasks {
Expand All @@ -82,7 +85,7 @@ tasks {

register<Jar>("fatJar") {
manifest {
attributes["Main-Class"] = mainClass
attributes["Main-Class"] = mClass
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
archiveFileName.set("polls.jar")
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wire.bots.polls.integration_tests.routing

import ai.blindspot.ktoolz.extensions.whenNull
import pw.forst.tools.katlib.whenNull
import com.wire.bots.polls.integration_tests.dto.Conversation
import com.wire.bots.polls.integration_tests.dto.ProxyResponseMessage
import com.wire.bots.polls.integration_tests.store.tokenStorage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.wire.bots.polls.integration_tests.setup

import ai.blindspot.ktoolz.extensions.getEnv
import ai.blindspot.ktoolz.extensions.whenNull
import pw.forst.tools.katlib.getEnv
import pw.forst.tools.katlib.whenNull
import com.wire.bots.polls.integration_tests.dto.BotApiConfiguration
import com.wire.bots.polls.integration_tests.setup.EnvConfigVariables.BOT_API
import com.wire.bots.polls.integration_tests.setup.EnvConfigVariables.SERVICE_TOKEN
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wire.bots.polls.integration_tests

import ai.blindspot.ktoolz.extensions.newLine
import pw.forst.tools.katlib.newLine
import com.wire.bots.polls.integration_tests.dto.botRequest
import com.wire.bots.polls.integration_tests.dto.init
import com.wire.bots.polls.integration_tests.dto.reaction
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/wire/bots/polls/dao/PollRepository.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.wire.bots.polls.dao

import ai.blindspot.ktoolz.extensions.mapToSet
import com.wire.bots.polls.dto.PollAction
import com.wire.bots.polls.dto.PollDto
import com.wire.bots.polls.dto.Question
Expand All @@ -14,6 +13,7 @@ import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import pw.forst.exposed.insertOrUpdate
import pw.forst.tools.katlib.mapToSet

/**
* Simple repository for handling database transactions on one place.
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/wire/bots/polls/parser/PollFactory.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.wire.bots.polls.parser

import ai.blindspot.ktoolz.extensions.newLine
import ai.blindspot.ktoolz.extensions.whenNull
import com.wire.bots.polls.dto.PollDto
import com.wire.bots.polls.dto.UsersInput
import mu.KLogging
import pw.forst.tools.katlib.newLine
import pw.forst.tools.katlib.whenNull

/**
* Class used for creating the polls from the text. Parsing and creating the poll objects.
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/wire/bots/polls/routing/MessagesRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.wire.bots.polls.routing
import com.wire.bots.polls.dto.roman.Message
import com.wire.bots.polls.services.AuthService
import com.wire.bots.polls.services.MessagesHandlingService
import com.wire.bots.polls.setup.logging.USER_ID
import com.wire.bots.polls.utils.mdc
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.request.receive
Expand Down Expand Up @@ -36,6 +38,9 @@ fun Routing.messages(k: LazyKodein) {
call.respond(HttpStatusCode.BadRequest, "Bot did not understand the message.")
}.onSuccess {
routingLogger.debug { "Message parsed." }
// includes user id to current MDC
mdc(USER_ID) { it.userId }

handler.handle(it)
routingLogger.debug { "Responding OK" }
call.respond(HttpStatusCode.OK)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.wire.bots.polls.services

import ai.blindspot.ktoolz.extensions.whenNull
import io.ktor.http.Headers
import mu.KLogging
import pw.forst.tools.katlib.whenNull

/**
* Authentication service.
Expand Down
67 changes: 29 additions & 38 deletions src/main/kotlin/com/wire/bots/polls/services/PollService.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.wire.bots.polls.services

import ai.blindspot.ktoolz.extensions.whenNull
import ai.blindspot.ktoolz.extensions.whenTrue
import com.wire.bots.polls.dao.PollRepository
import com.wire.bots.polls.dto.PollAction
import com.wire.bots.polls.dto.UsersInput
import com.wire.bots.polls.dto.bot.confirmVote
import com.wire.bots.polls.dto.bot.newPoll
import com.wire.bots.polls.parser.PollFactory
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import mu.KLogging
import pw.forst.tools.katlib.whenNull
import pw.forst.tools.katlib.whenTrue
import java.util.UUID

/**
Expand Down Expand Up @@ -40,21 +38,18 @@ class PollService(
val pollId = repository.savePoll(poll, pollId = UUID.randomUUID().toString(), userId = usersInput.userId, botSelfId = botId)
logger.info { "Poll successfully created with id: $pollId" }

// send response with async way
GlobalScope.launch {
proxySenderService.send(
token,
message = newPoll(
id = pollId,
body = poll.question.body,
buttons = poll.options,
mentions = poll.question.mentions
)
).whenNull {
logger.error { "It was not possible to send the poll to the Roman!" }
}?.also { (messageId) ->
logger.info { "Poll successfully created with id: $messageId" }
}
proxySenderService.send(
token,
message = newPoll(
id = pollId,
body = poll.question.body,
buttons = poll.options,
mentions = poll.question.mentions
)
).whenNull {
logger.error { "It was not possible to send the poll to the Roman!" }
}?.also { (messageId) ->
logger.info { "Poll successfully created with id: $messageId" }
}

return pollId
Expand All @@ -63,10 +58,8 @@ class PollService(

private suspend fun pollNotParsedFallback(token: String, usersInput: UsersInput) {
usersInput.input.startsWith("/poll").whenTrue {
GlobalScope.launch {
logger.info { "Command started with /poll, sending usage to user." }
userCommunicationService.reactionToWrongCommand(token)
}
logger.info { "Command started with /poll, sending usage to user." }
userCommunicationService.reactionToWrongCommand(token)
}

}
Expand All @@ -79,20 +72,18 @@ class PollService(
repository.vote(pollAction)
logger.info { "Vote registered." }

GlobalScope.launch {
proxySenderService.send(
token = token,
message = confirmVote(
pollId = pollAction.pollId,
offset = pollAction.optionId,
userId = pollAction.userId
)
).whenNull {
logger.error { "It was not possible to send response to vote." }
}?.also { (messageId) ->
logger.info { "Proxy received confirmation for vote under id: $messageId" }
sendStatsIfAllVoted(token, pollAction.pollId)
}
proxySenderService.send(
token = token,
message = confirmVote(
pollId = pollAction.pollId,
offset = pollAction.optionId,
userId = pollAction.userId
)
).whenNull {
logger.error { "It was not possible to send response to vote." }
}?.also { (messageId) ->
logger.info { "Proxy received confirmation for vote under id: $messageId" }
sendStatsIfAllVoted(token, pollAction.pollId)
}
}

Expand Down Expand Up @@ -122,7 +113,7 @@ class PollService(
val stats = statsFormattingService.formatStats(pollId, conversationMembersCount)
.whenNull { logger.warn { "It was not possible to format stats for poll $pollId" } } ?: return

GlobalScope.launch { proxySenderService.send(token, stats) }
proxySenderService.send(token, stats)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.wire.bots.polls.services

import ai.blindspot.ktoolz.extensions.createJson
import com.wire.bots.polls.dto.bot.BotMessage
import com.wire.bots.polls.dto.roman.Response
import com.wire.bots.polls.utils.appendPath
Expand All @@ -15,6 +14,7 @@ import io.ktor.http.ContentType
import io.ktor.http.contentType
import io.ktor.http.isSuccess
import mu.KLogging
import pw.forst.tools.katlib.createJson
import java.nio.charset.Charset

/**
Expand All @@ -32,7 +32,7 @@ class ProxySenderService(private val client: HttpClient, config: ProxyConfigurat
* Send given message with provided token.
*/
suspend fun send(token: String, message: BotMessage): Response? {
logger.debug { "Sending\n:${createJson(message)}" }
logger.debug { "Sending: ${createJson(message)}" }

return client.post<HttpStatement>(body = message) {
url(conversationEndpoint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.wire.bots.polls.services

import ai.blindspot.ktoolz.extensions.newLine
import ai.blindspot.ktoolz.extensions.whenNull
import com.wire.bots.polls.dao.PollRepository
import com.wire.bots.polls.dto.bot.BotMessage
import com.wire.bots.polls.dto.bot.statsMessage
import mu.KLogging
import pw.forst.tools.katlib.newLine
import pw.forst.tools.katlib.whenNull

class StatsFormattingService(
private val repository: PollRepository
Expand Down Expand Up @@ -39,7 +39,7 @@ class StatsFormattingService(

private fun formatVotes(stats: Map<Pair<Int, String>, Int>, conversationMembers: Int?): String {
// we can use assert as the result size is checked
val maxVotes = stats.values.max()!!
val maxVotes = requireNotNull(stats.values.maxOrNull()) { "There were no stats!" }
return stats
.map { (option, votingUsers) ->
VotingOption(if (votingUsers == maxVotes) "**" else "*", option.second, votingUsers)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.wire.bots.polls.setup

import ai.blindspot.ktoolz.extensions.getEnv
import ai.blindspot.ktoolz.extensions.whenNull
import com.wire.bots.polls.dto.conf.DatabaseConfiguration
import com.wire.bots.polls.services.ProxyConfiguration
import com.wire.bots.polls.setup.EnvConfigVariables.DB_PASSWORD
Expand All @@ -13,6 +11,8 @@ import com.wire.bots.polls.utils.createLogger
import org.kodein.di.Kodein.MainBuilder
import org.kodein.di.generic.bind
import org.kodein.di.generic.singleton
import pw.forst.tools.katlib.getEnv
import pw.forst.tools.katlib.whenNull
import java.io.File

private val logger = createLogger("EnvironmentLoaderLogger")
Expand Down
Loading

0 comments on commit 1943b27

Please sign in to comment.