Skip to content

Commit a7e3375

Browse files
committed
Extract VotingProcessor to a separate module for reuse
1 parent 8bfd242 commit a7e3375

File tree

7 files changed

+85
-30
lines changed

7 files changed

+85
-30
lines changed

core/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
dependencies {
6-
api(libs.bundles.tgbotapi)
6+
api(libs.tgbotapi.core)
77
implementation(libs.log4j.api)
88

99
testImplementation(libs.junit.jupiter.api)

jep/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ plugins {
55
dependencies {
66
api(project(":core"))
77
api(project(":votes"))
8+
api(libs.tgbotapi.core)
89
implementation(project(":votes:tgbotapi-extensions"))
10+
implementation(project(":votes:voting-processor"))
11+
implementation(libs.tgbotapi.extensions.api)
912
implementation(libs.log4j.api)
1013
implementation(libs.jsoup)
1114

jep/src/main/kotlin/by/jprof/telegram/bot/jep/JEPUpdateProcessor.kt

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import by.jprof.telegram.bot.core.UpdateProcessor
44
import by.jprof.telegram.bot.votes.dao.VotesDAO
55
import by.jprof.telegram.bot.votes.model.Votes
66
import by.jprof.telegram.bot.votes.tgbotapi_extensions.toInlineKeyboardMarkup
7+
import by.jprof.telegram.bot.votes.voting_processor.VotingProcessor
78
import dev.inmo.tgbotapi.CommonAbstracts.justTextSources
89
import dev.inmo.tgbotapi.bot.RequestsExecutor
9-
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
10-
import dev.inmo.tgbotapi.extensions.api.edit.ReplyMarkup.editMessageReplyMarkup
1110
import dev.inmo.tgbotapi.extensions.api.send.reply
12-
import dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery
13-
import dev.inmo.tgbotapi.types.CallbackQuery.MessageDataCallbackQuery
1411
import dev.inmo.tgbotapi.types.MessageEntity.textsources.TextLinkTextSource
1512
import dev.inmo.tgbotapi.types.MessageEntity.textsources.URLTextSource
1613
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2ParseMode
@@ -26,11 +23,18 @@ import kotlinx.coroutines.launch
2623
import kotlinx.coroutines.supervisorScope
2724
import org.apache.logging.log4j.LogManager
2825

26+
private fun votesConstructor(votesId: String): Votes = Votes(votesId, listOf("\uD83D\uDC4D", "\uD83D\uDC4E"))
27+
2928
class JEPUpdateProcessor(
3029
private val jepSummary: JEPSummary,
3130
private val votesDAO: VotesDAO,
3231
private val bot: RequestsExecutor,
33-
) : UpdateProcessor {
32+
) : VotingProcessor(
33+
"JEP",
34+
votesDAO,
35+
::votesConstructor,
36+
bot,
37+
), UpdateProcessor {
3438
companion object {
3539
val logger = LogManager.getLogger(JEPUpdateProcessor::class.java)!!
3640
val linkRegex = "https?://openjdk\\.java\\.net/jeps/(\\d+)/?".toRegex()
@@ -57,27 +61,6 @@ class JEPUpdateProcessor(
5761
}
5862
}
5963

60-
private suspend fun processCallbackQuery(callbackQuery: CallbackQuery) {
61-
logger.debug("Processing callback query: {}", callbackQuery)
62-
63-
(callbackQuery as? MessageDataCallbackQuery)?.data?.takeIf { it.startsWith("JEP") }?.let { data ->
64-
val (votesId, vote) = data.split(":").takeIf { it.size == 2 } ?: return
65-
val fromUserId = callbackQuery.user.id.chatId.toString()
66-
67-
logger.debug("Tracking {}'s '{}' vote for {}", fromUserId, vote, votesId)
68-
69-
val votes = votesDAO.get(votesId) ?: votesId.toVotes()
70-
val updatedVotes = votes.copy(votes = votes.votes + (fromUserId to vote))
71-
72-
votesDAO.save(updatedVotes)
73-
bot.answerCallbackQuery(callbackQuery)
74-
bot.editMessageReplyMarkup(
75-
message = callbackQuery.message,
76-
replyMarkup = updatedVotes.toInlineKeyboardMarkup()
77-
)
78-
}
79-
}
80-
8164
private fun extractJEPs(message: Message): List<String>? =
8265
(message as? ContentMessage<*>)?.let { contentMessage ->
8366
(contentMessage.content as? TextContent)?.let { content ->
@@ -107,7 +90,7 @@ class JEPUpdateProcessor(
10790
"Cast your vote for *JEP $jep* now ⤵️"
10891
}
10992
val votesId = jep.toVotesID()
110-
val votes = votesDAO.get(votesId) ?: votesId.toVotes()
93+
val votes = votesDAO.get(votesId) ?: votesConstructor(votesId)
11194

11295
bot.reply(
11396
to = message,
@@ -118,6 +101,4 @@ class JEPUpdateProcessor(
118101
}
119102

120103
private fun String.toVotesID() = "JEP-$this"
121-
122-
private fun String.toVotes() = Votes(this, listOf("\uD83D\uDC4D", "\uD83D\uDC4E"))
123104
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ include(":utils:dynamodb")
66
include(":votes")
77
include(":votes:dynamodb")
88
include(":votes:tgbotapi-extensions")
9+
include(":votes:voting-processor")
910
include(":core")
1011
include(":jep")
1112
include(":runners:lambda")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
kotlin("jvm")
3+
}
4+
5+
dependencies {
6+
api(libs.tgbotapi.core)
7+
implementation(project(":votes"))
8+
implementation(project(":votes:tgbotapi-extensions"))
9+
implementation(libs.tgbotapi.extensions.api)
10+
implementation(libs.log4j.api)
11+
12+
testImplementation(libs.junit.jupiter.api)
13+
testImplementation(libs.junit.jupiter.params)
14+
testImplementation(libs.mockk)
15+
testRuntimeOnly(libs.junit.jupiter.engine)
16+
testRuntimeOnly(libs.log4j.core)
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package by.jprof.telegram.bot.votes.voting_processor
2+
3+
import by.jprof.telegram.bot.votes.dao.VotesDAO
4+
import by.jprof.telegram.bot.votes.model.Votes
5+
import by.jprof.telegram.bot.votes.tgbotapi_extensions.toInlineKeyboardMarkup
6+
import dev.inmo.tgbotapi.bot.RequestsExecutor
7+
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
8+
import dev.inmo.tgbotapi.extensions.api.edit.ReplyMarkup.editMessageReplyMarkup
9+
import dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery
10+
import dev.inmo.tgbotapi.types.CallbackQuery.MessageDataCallbackQuery
11+
import org.apache.logging.log4j.LogManager
12+
13+
abstract class VotingProcessor(
14+
private val prefix: String,
15+
private val votesDAO: VotesDAO,
16+
private val votesConstructor: (String) -> Votes,
17+
private val bot: RequestsExecutor,
18+
) {
19+
companion object {
20+
val logger = LogManager.getLogger(VotingProcessor::class.java)!!
21+
}
22+
23+
suspend fun processCallbackQuery(callbackQuery: CallbackQuery) {
24+
logger.debug("Processing callback query: {}", callbackQuery)
25+
26+
(callbackQuery as? MessageDataCallbackQuery)?.data?.takeIf { it.startsWith(prefix) }?.let { data ->
27+
val (votesId, vote) = data.split(":").takeIf { it.size == 2 } ?: return
28+
val fromUserId = callbackQuery.user.id.chatId.toString()
29+
30+
logger.debug("Tracking {}'s '{}' vote for {}", fromUserId, vote, votesId)
31+
32+
val votes = votesDAO.get(votesId) ?: votesConstructor(votesId)
33+
val updatedVotes = votes.copy(votes = votes.votes + (fromUserId to vote))
34+
35+
votesDAO.save(updatedVotes)
36+
bot.answerCallbackQuery(callbackQuery)
37+
bot.editMessageReplyMarkup(
38+
message = callbackQuery.message,
39+
replyMarkup = updatedVotes.toInlineKeyboardMarkup()
40+
)
41+
}
42+
}
43+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package by.jprof.telegram.bot.votes.voting_processor
2+
3+
import org.junit.jupiter.api.Test
4+
5+
internal class VotingProcessorTest {
6+
@Test
7+
fun processCallbackQuery() {
8+
TODO()
9+
}
10+
}

0 commit comments

Comments
 (0)