Skip to content

Commit

Permalink
fix test and move Marks to a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Apr 13, 2024
1 parent d27392b commit f7ad14a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024. ForteScarlet.
*
* This file is part of simbot-component-telegram.
*
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram.
* If not, see <https://www.gnu.org/licenses/>.
*/

package love.forte.simbot.component.telegram.core.message

internal const val PROTECT_CONTENT_MARK: Int = 1 shl 0
internal const val DISABLE_NOTIFICATION_MARK: Int = 1 shl 1


internal class SendingMarks(
private val value: Int,
) {
val isProtectContent: Boolean
get() = value and PROTECT_CONTENT_MARK != 0

val isDisableNotification: Boolean
get() = value and DISABLE_NOTIFICATION_MARK != 0


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import love.forte.simbot.telegram.api.TelegramApi
import love.forte.simbot.telegram.api.message.*
import love.forte.simbot.telegram.type.ChatId
import love.forte.simbot.telegram.type.MessageId
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmName
import love.forte.simbot.telegram.type.Message as StdlibMessage

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

internal suspend fun TelegramBotImpl.send(
internal fun TelegramBotImpl.toReceipt(chatId: Long, result: Any): TelegramSingleMessageReceipt {
return when (result) {
is StdlibMessage -> result.toTelegramMessageReceipt(this)
is MessageId -> result.toTelegramMessageReceipt(this, chatId)
else -> error("Unexpected result type: $result")
}
}

internal suspend inline fun TelegramBotImpl.send(
message: Message,
chatId: Long,
builderFactory: BuilderFactory = DefaultBuilderFactory
crossinline builderFactory: BuilderFactory = { SendMessageApi.builder() }
): TelegramMessageReceipt {
val cid = ChatId(chatId)
val (funcList, marks) = message.resolve(cid) {
Expand All @@ -88,21 +95,13 @@ internal suspend fun TelegramBotImpl.send(
}
}

fun toReceipt(result: Any): TelegramSingleMessageReceipt {
return when (result) {
is StdlibMessage -> result.toTelegramMessageReceipt(this)
is MessageId -> result.toTelegramMessageReceipt(this, chatId)
else -> error("Unexpected result type: $result")
}
}

when {
funcList.isEmpty() -> error("Nothing to send, the message element list is empty.")
funcList.size == 1 -> {
val result = funcList.first()(marks).requestDataBy(this)
return when (result) {
is StdlibMessage -> toReceipt(result)
is MessageId -> toReceipt(result)
is StdlibMessage -> toReceipt(chatId, result)
is MessageId -> toReceipt(chatId, result)
else -> error("Unexpected result type: $result")
}
}
Expand Down Expand Up @@ -163,13 +162,13 @@ internal data class ResolveResult(
val marks: SendingMarks
)

internal suspend fun Message.resolve(
internal suspend inline fun Message.resolve(
chatId: ChatId,
builderFactory: BuilderFactory = DefaultBuilderFactory
crossinline builderFactory: BuilderFactory = { SendMessageApi.builder() }
): ResolveResult {
return when (val m = this) {
is Message.Element -> {
val context = SendingMessageResolverContext(builderFactory)
val context = SendingMessageResolverContext { builderFactory() }
for (resolver in sendingResolvers) {
resolver.resolve(chatId, 0, m, this, context)
}
Expand All @@ -183,7 +182,7 @@ internal suspend fun Message.resolve(
return ResolveResult(emptyList(), SendingMarks(0))
}

val context = SendingMessageResolverContext(builderFactory)
val context = SendingMessageResolverContext { builderFactory() }
m.forEachIndexed { index, element ->
for (resolver in sendingResolvers) {
resolver.resolve(chatId, index, element, this, context)
Expand Down Expand Up @@ -215,22 +214,8 @@ private val sendingResolvers = listOf(

internal typealias BuilderFactory = () -> SendMessageApi.Builder

internal val DefaultBuilderFactory: BuilderFactory = { SendMessageApi.builder() }

internal typealias SendingMessageResolvedFunction = (SendingMarks) -> TelegramApi<*> // * 只能是 Message 或 MessageId

private const val PROTECT_CONTENT_MARK = 1 shl 0
private const val DISABLE_NOTIFICATION_MARK = 1 shl 1


@JvmInline
internal value class SendingMarks(internal val value: Int) {
val isProtectContent: Boolean
get() = value and PROTECT_CONTENT_MARK != 0
val isDisableNotification: Boolean
get() = value and DISABLE_NOTIFICATION_MARK != 0
}

internal class SendingMessageResolverContext(
private val builderFactory: BuilderFactory,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TelegramTextEntitiesTests {
assertIs<TelegramMessageEntity.CustomEmoji>(customEmoji)
assertIs<TelegramMessageEntity.Simple>(simple)

val resolved = listOf(
val (resolved, marks) = listOf(
textLink,
textMention,
Text { "Hello" },
Expand All @@ -45,7 +45,7 @@ class TelegramTextEntitiesTests {
SendMessageApi.builder().also { it.chatId = ChatId(10000) }
}

val body = resolved.first()().body
val body = resolved.first()(marks).body
assertIs<SendMessageApi.Body>(body)
val text = body.text
assertEquals("GitHub@forteHelloCODEEMOJI1=1", text)
Expand Down Expand Up @@ -110,7 +110,7 @@ class TelegramTextEntitiesTests {
val customEmoji = TelegramMessageEntity.createCustomEmoji("EMOJI", "EMOJI".ID)
val simple = TelegramMessageEntity.create("1=1", MessageEntityType.CODE)

val resolved = listOf(
val (resolved, marks) = listOf(
textLink,
textMention,
Text { "Hello" },
Expand All @@ -121,7 +121,7 @@ class TelegramTextEntitiesTests {
SendMessageApi.builder().also { it.chatId = ChatId(10000) }
}

val body = resolved.first()().body
val body = resolved.first()(marks).body
assertIs<SendMessageApi.Body>(body)
val bodyText = body.text
assertEquals("GitHub@forteHelloCODEEMOJI1=1", bodyText)
Expand Down

0 comments on commit f7ad14a

Please sign in to comment.