Skip to content

Commit f7ad14a

Browse files
committed
fix test and move Marks to a single file
1 parent d27392b commit f7ad14a

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* This file is part of simbot-component-telegram.
5+
*
6+
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms
7+
* of the GNU Lesser General Public License as published by the Free Software Foundation,
8+
* either version 3 of the License, or (at your option) any later version.
9+
*
10+
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram.
15+
* If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package love.forte.simbot.component.telegram.core.message
19+
20+
internal const val PROTECT_CONTENT_MARK: Int = 1 shl 0
21+
internal const val DISABLE_NOTIFICATION_MARK: Int = 1 shl 1
22+
23+
24+
internal class SendingMarks(
25+
private val value: Int,
26+
) {
27+
val isProtectContent: Boolean
28+
get() = value and PROTECT_CONTENT_MARK != 0
29+
30+
val isDisableNotification: Boolean
31+
get() = value and DISABLE_NOTIFICATION_MARK != 0
32+
33+
34+
}

simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/message/TelegramMessageResolver.kt

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import love.forte.simbot.telegram.api.TelegramApi
2727
import love.forte.simbot.telegram.api.message.*
2828
import love.forte.simbot.telegram.type.ChatId
2929
import love.forte.simbot.telegram.type.MessageId
30-
import kotlin.jvm.JvmInline
3130
import kotlin.jvm.JvmName
3231
import love.forte.simbot.telegram.type.Message as StdlibMessage
3332

@@ -74,10 +73,18 @@ internal suspend inline fun TelegramBotImpl.send(
7473
return send(messageContent.messages, chatId) { builderFactory() }
7574
}
7675

77-
internal suspend fun TelegramBotImpl.send(
76+
internal fun TelegramBotImpl.toReceipt(chatId: Long, result: Any): TelegramSingleMessageReceipt {
77+
return when (result) {
78+
is StdlibMessage -> result.toTelegramMessageReceipt(this)
79+
is MessageId -> result.toTelegramMessageReceipt(this, chatId)
80+
else -> error("Unexpected result type: $result")
81+
}
82+
}
83+
84+
internal suspend inline fun TelegramBotImpl.send(
7885
message: Message,
7986
chatId: Long,
80-
builderFactory: BuilderFactory = DefaultBuilderFactory
87+
crossinline builderFactory: BuilderFactory = { SendMessageApi.builder() }
8188
): TelegramMessageReceipt {
8289
val cid = ChatId(chatId)
8390
val (funcList, marks) = message.resolve(cid) {
@@ -88,21 +95,13 @@ internal suspend fun TelegramBotImpl.send(
8895
}
8996
}
9097

91-
fun toReceipt(result: Any): TelegramSingleMessageReceipt {
92-
return when (result) {
93-
is StdlibMessage -> result.toTelegramMessageReceipt(this)
94-
is MessageId -> result.toTelegramMessageReceipt(this, chatId)
95-
else -> error("Unexpected result type: $result")
96-
}
97-
}
98-
9998
when {
10099
funcList.isEmpty() -> error("Nothing to send, the message element list is empty.")
101100
funcList.size == 1 -> {
102101
val result = funcList.first()(marks).requestDataBy(this)
103102
return when (result) {
104-
is StdlibMessage -> toReceipt(result)
105-
is MessageId -> toReceipt(result)
103+
is StdlibMessage -> toReceipt(chatId, result)
104+
is MessageId -> toReceipt(chatId, result)
106105
else -> error("Unexpected result type: $result")
107106
}
108107
}
@@ -163,13 +162,13 @@ internal data class ResolveResult(
163162
val marks: SendingMarks
164163
)
165164

166-
internal suspend fun Message.resolve(
165+
internal suspend inline fun Message.resolve(
167166
chatId: ChatId,
168-
builderFactory: BuilderFactory = DefaultBuilderFactory
167+
crossinline builderFactory: BuilderFactory = { SendMessageApi.builder() }
169168
): ResolveResult {
170169
return when (val m = this) {
171170
is Message.Element -> {
172-
val context = SendingMessageResolverContext(builderFactory)
171+
val context = SendingMessageResolverContext { builderFactory() }
173172
for (resolver in sendingResolvers) {
174173
resolver.resolve(chatId, 0, m, this, context)
175174
}
@@ -183,7 +182,7 @@ internal suspend fun Message.resolve(
183182
return ResolveResult(emptyList(), SendingMarks(0))
184183
}
185184

186-
val context = SendingMessageResolverContext(builderFactory)
185+
val context = SendingMessageResolverContext { builderFactory() }
187186
m.forEachIndexed { index, element ->
188187
for (resolver in sendingResolvers) {
189188
resolver.resolve(chatId, index, element, this, context)
@@ -215,22 +214,8 @@ private val sendingResolvers = listOf(
215214

216215
internal typealias BuilderFactory = () -> SendMessageApi.Builder
217216

218-
internal val DefaultBuilderFactory: BuilderFactory = { SendMessageApi.builder() }
219-
220217
internal typealias SendingMessageResolvedFunction = (SendingMarks) -> TelegramApi<*> // * 只能是 Message 或 MessageId
221218

222-
private const val PROTECT_CONTENT_MARK = 1 shl 0
223-
private const val DISABLE_NOTIFICATION_MARK = 1 shl 1
224-
225-
226-
@JvmInline
227-
internal value class SendingMarks(internal val value: Int) {
228-
val isProtectContent: Boolean
229-
get() = value and PROTECT_CONTENT_MARK != 0
230-
val isDisableNotification: Boolean
231-
get() = value and DISABLE_NOTIFICATION_MARK != 0
232-
}
233-
234219
internal class SendingMessageResolverContext(
235220
private val builderFactory: BuilderFactory,
236221
) {

simbot-component-telegram-core/src/commonTest/kotlin/love/forte/simbot/component/telegram/core/message/TelegramTextEntitiesTests.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TelegramTextEntitiesTests {
3434
assertIs<TelegramMessageEntity.CustomEmoji>(customEmoji)
3535
assertIs<TelegramMessageEntity.Simple>(simple)
3636

37-
val resolved = listOf(
37+
val (resolved, marks) = listOf(
3838
textLink,
3939
textMention,
4040
Text { "Hello" },
@@ -45,7 +45,7 @@ class TelegramTextEntitiesTests {
4545
SendMessageApi.builder().also { it.chatId = ChatId(10000) }
4646
}
4747

48-
val body = resolved.first()().body
48+
val body = resolved.first()(marks).body
4949
assertIs<SendMessageApi.Body>(body)
5050
val text = body.text
5151
assertEquals("GitHub@forteHelloCODEEMOJI1=1", text)
@@ -110,7 +110,7 @@ class TelegramTextEntitiesTests {
110110
val customEmoji = TelegramMessageEntity.createCustomEmoji("EMOJI", "EMOJI".ID)
111111
val simple = TelegramMessageEntity.create("1=1", MessageEntityType.CODE)
112112

113-
val resolved = listOf(
113+
val (resolved, marks) = listOf(
114114
textLink,
115115
textMention,
116116
Text { "Hello" },
@@ -121,7 +121,7 @@ class TelegramTextEntitiesTests {
121121
SendMessageApi.builder().also { it.chatId = ChatId(10000) }
122122
}
123123

124-
val body = resolved.first()().body
124+
val body = resolved.first()(marks).body
125125
assertIs<SendMessageApi.Body>(body)
126126
val bodyText = body.text
127127
assertEquals("GitHub@forteHelloCODEEMOJI1=1", bodyText)

0 commit comments

Comments
 (0)