Skip to content

Commit 1b5a001

Browse files
committed
Allow to process InlineMessageIdDataCallbackQuery in VotingProcessor
1 parent d59a7aa commit 1b5a001

File tree

2 files changed

+125
-32
lines changed

2 files changed

+125
-32
lines changed

votes/voting-processor/src/main/kotlin/by/jprof/telegram/bot/votes/voting_processor/VotingProcessor.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import dev.inmo.tgbotapi.bot.RequestsExecutor
77
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
88
import dev.inmo.tgbotapi.extensions.api.edit.ReplyMarkup.editMessageReplyMarkup
99
import dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery
10+
import dev.inmo.tgbotapi.types.CallbackQuery.DataCallbackQuery
11+
import dev.inmo.tgbotapi.types.CallbackQuery.InlineMessageIdDataCallbackQuery
1012
import dev.inmo.tgbotapi.types.CallbackQuery.MessageDataCallbackQuery
1113
import org.apache.logging.log4j.LogManager
1214

@@ -21,9 +23,9 @@ abstract class VotingProcessor(
2123
}
2224

2325
suspend fun processCallbackQuery(callbackQuery: CallbackQuery) {
24-
logger.debug("Processing callback query: {}", callbackQuery)
26+
logger.debug("[{}] Processing callback query: {}", prefix, callbackQuery)
2527

26-
(callbackQuery as? MessageDataCallbackQuery)?.data?.takeIf { it.startsWith(prefix) }?.let { data ->
28+
(callbackQuery as? DataCallbackQuery)?.data?.takeIf { it.startsWith(prefix) }?.let { data ->
2729
val (votesId, vote) = data.split(":").takeIf { it.size == 2 } ?: return
2830
val fromUserId = callbackQuery.user.id.chatId.toString()
2931

@@ -34,10 +36,24 @@ abstract class VotingProcessor(
3436

3537
votesDAO.save(updatedVotes)
3638
bot.answerCallbackQuery(callbackQuery)
37-
bot.editMessageReplyMarkup(
38-
message = callbackQuery.message,
39-
replyMarkup = updatedVotes.toInlineKeyboardMarkup()
40-
)
39+
40+
when (callbackQuery) {
41+
is MessageDataCallbackQuery -> {
42+
bot.editMessageReplyMarkup(
43+
message = callbackQuery.message,
44+
replyMarkup = updatedVotes.toInlineKeyboardMarkup()
45+
)
46+
}
47+
is InlineMessageIdDataCallbackQuery -> {
48+
bot.editMessageReplyMarkup(
49+
inlineMessageId = callbackQuery.inlineMessageId,
50+
replyMarkup = updatedVotes.toInlineKeyboardMarkup()
51+
)
52+
}
53+
else -> {
54+
logger.error("Unknown callback query type: {}", callbackQuery::class.simpleName)
55+
}
56+
}
4157
}
4258
}
4359

votes/voting-processor/src/test/kotlin/by/jprof/telegram/bot/votes/voting_processor/VotingProcessorTest.kt

Lines changed: 103 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ package by.jprof.telegram.bot.votes.voting_processor
33
import by.jprof.telegram.bot.votes.dao.VotesDAO
44
import by.jprof.telegram.bot.votes.model.Votes
55
import by.jprof.telegram.bot.votes.tgbotapi_extensions.toInlineKeyboardMarkup
6+
import com.soywiz.klock.DateTime
67
import dev.inmo.tgbotapi.bot.RequestsExecutor
78
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
89
import dev.inmo.tgbotapi.extensions.api.edit.ReplyMarkup.editMessageReplyMarkup
10+
import dev.inmo.tgbotapi.types.CallbackQuery.InlineMessageIdDataCallbackQuery
911
import dev.inmo.tgbotapi.types.CallbackQuery.MessageDataCallbackQuery
1012
import dev.inmo.tgbotapi.types.CallbackQuery.MessageGameShortNameCallbackQuery
1113
import dev.inmo.tgbotapi.types.ChatId
12-
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
14+
import dev.inmo.tgbotapi.types.CommonUser
15+
import dev.inmo.tgbotapi.types.UserId
16+
import dev.inmo.tgbotapi.types.chat.GroupChatImpl
17+
import dev.inmo.tgbotapi.types.message.CommonGroupContentMessageImpl
1318
import dev.inmo.tgbotapi.types.message.content.TextContent
1419
import io.mockk.*
1520
import io.mockk.impl.annotations.MockK
1621
import io.mockk.junit5.MockKExtension
1722
import kotlinx.coroutines.runBlocking
1823
import org.junit.jupiter.api.BeforeEach
19-
import org.junit.jupiter.api.Disabled
2024
import org.junit.jupiter.api.Test
2125
import org.junit.jupiter.api.extension.ExtendWith
2226

@@ -44,50 +48,123 @@ internal class VotingProcessorTest {
4448
}
4549

4650
@Test
47-
@Disabled
48-
fun processCallbackQuery() = runBlocking {
49-
val callbackQuery = mockk<MessageDataCallbackQuery> {
50-
every { id } returns ""
51-
every { user } returns mockk {
52-
every { id } returns ChatId(42L)
53-
}
54-
every { chatInstance } returns ""
55-
every { message.hint(ContentMessage::class) } returns mockk<ContentMessage<*>> {
56-
every { messageId } returns 1L
57-
every { content.hint(TextContent::class) } returns TextContent("")
58-
every { chat } returns mockk {
59-
every { id } returns ChatId(1L)
60-
}
61-
}
62-
every { data } returns "TEST-42:+"
63-
}
51+
fun processMessageDataCallbackQuery() = runBlocking {
52+
val message = CommonGroupContentMessageImpl(
53+
chat = GroupChatImpl(
54+
id = ChatId(1L),
55+
title = "Test"
56+
),
57+
messageId = 1L,
58+
user = CommonUser(UserId(2L), "Test 2"),
59+
date = DateTime.now(),
60+
forwardInfo = null,
61+
editDate = null,
62+
replyTo = null,
63+
replyMarkup = null,
64+
content = TextContent(""),
65+
senderBot = null,
66+
)
67+
val callbackQuery = MessageDataCallbackQuery(
68+
id = "",
69+
user = CommonUser(UserId(1L), "Test 1"),
70+
chatInstance = "",
71+
message = message,
72+
data = "TEST-42:+"
73+
)
6474

6575
coEvery { votesDAO.get("TEST-42") } returns Votes("TEST-42", listOf("+", "-"))
6676
coEvery { votesDAO.save(any()) } just runs
6777
coEvery { bot.answerCallbackQuery(callbackQuery) } returns true
6878

6979
sut.processCallbackQuery(callbackQuery)
7080

71-
coVerify(exactly = 1) { votesDAO.save(Votes("TEST-42", listOf("+", "-"), mapOf("42" to "+"))) }
81+
coVerify(exactly = 1) { votesDAO.save(Votes("TEST-42", listOf("+", "-"), mapOf("1" to "+"))) }
7282
coVerify(exactly = 1) { bot.answerCallbackQuery(callbackQuery) }
7383
coVerify(exactly = 1) {
7484
bot.editMessageReplyMarkup(
75-
message = callbackQuery.message,
76-
replyMarkup = Votes("TEST-42", listOf("+", "-"), mapOf("42" to "+")).toInlineKeyboardMarkup()
85+
message = message,
86+
replyMarkup = Votes("TEST-42", listOf("+", "-"), mapOf("1" to "+")).toInlineKeyboardMarkup(),
7787
)
7888
}
7989

8090
clearAllMocks()
8191
}
8292

8393
@Test
84-
@Disabled
85-
fun processCallbackQueryForNewVotes() {
86-
TODO()
94+
fun processCallbackQueryForNewVotes() = runBlocking {
95+
val message = CommonGroupContentMessageImpl(
96+
chat = GroupChatImpl(
97+
id = ChatId(1L),
98+
title = "Test"
99+
),
100+
messageId = 1L,
101+
user = CommonUser(UserId(2L), "Test 2"),
102+
date = DateTime.now(),
103+
forwardInfo = null,
104+
editDate = null,
105+
replyTo = null,
106+
replyMarkup = null,
107+
content = TextContent(""),
108+
senderBot = null,
109+
)
110+
val callbackQuery = MessageDataCallbackQuery(
111+
id = "",
112+
user = CommonUser(UserId(1L), "Test 1"),
113+
chatInstance = "",
114+
message = message,
115+
data = "TEST-42:+"
116+
)
117+
118+
coEvery { votesDAO.get("TEST-42") } returns null
119+
every { votesConstructor.invoke("TEST-42") } returns Votes("TEST-42", listOf("+", "-", "="))
120+
coEvery { votesDAO.save(any()) } just runs
121+
coEvery { bot.answerCallbackQuery(callbackQuery) } returns true
122+
123+
sut.processCallbackQuery(callbackQuery)
124+
125+
coVerify(exactly = 1) { votesDAO.save(Votes("TEST-42", listOf("+", "-", "="), mapOf("1" to "+"))) }
126+
coVerify(exactly = 1) { bot.answerCallbackQuery(callbackQuery) }
127+
coVerify(exactly = 1) {
128+
bot.editMessageReplyMarkup(
129+
message = message,
130+
replyMarkup = Votes("TEST-42", listOf("+", "-", "="), mapOf("1" to "+")).toInlineKeyboardMarkup(),
131+
)
132+
}
133+
134+
clearAllMocks()
135+
}
136+
137+
@Test
138+
fun processInlineMessageIdDataCallbackQuery() = runBlocking {
139+
val callbackQuery = InlineMessageIdDataCallbackQuery(
140+
id = "",
141+
user = CommonUser(UserId(1L), "Test 1"),
142+
chatInstance = "",
143+
inlineMessageId = "300",
144+
data = "TEST-42:+"
145+
)
146+
147+
coEvery { votesDAO.get("TEST-42") } returns null
148+
every { votesConstructor.invoke("TEST-42") } returns Votes("TEST-42", listOf("+", "-"))
149+
coEvery { votesDAO.save(any()) } just runs
150+
coEvery { bot.answerCallbackQuery(callbackQuery) } returns true
151+
152+
sut.processCallbackQuery(callbackQuery)
153+
154+
coVerify(exactly = 1) { votesDAO.save(Votes("TEST-42", listOf("+", "-"), mapOf("1" to "+"))) }
155+
coVerify(exactly = 1) { bot.answerCallbackQuery(callbackQuery) }
156+
coVerify(exactly = 1) {
157+
bot.editMessageReplyMarkup(
158+
inlineMessageId = "300",
159+
replyMarkup = Votes("TEST-42", listOf("+", "-"), mapOf("1" to "+")).toInlineKeyboardMarkup(),
160+
)
161+
}
162+
163+
clearAllMocks()
87164
}
88165

89166
@Test
90-
fun processNonMessageDataCallbackQuery() = runBlocking {
167+
fun processUnknownCallbackQuery() = runBlocking {
91168
sut.processCallbackQuery(
92169
MessageGameShortNameCallbackQuery(
93170
id = "test",

0 commit comments

Comments
 (0)