From fc56fcecc5f34af144d8858da16353eaa41978b0 Mon Sep 17 00:00:00 2001 From: kagg886 Date: Tue, 12 Nov 2024 22:56:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20=E6=94=AF=E6=8C=81=E6=8F=90?= =?UTF-8?q?=E5=89=8D=E8=A7=A3=E6=9E=90=E5=90=ABcontent=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=9A=84type=E4=B8=BAforward=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E6=B6=88=E6=81=AF=20(#108)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/message/OnebotMessages.kt | 34 ++++- .../src/test/kotlin/ForwardMessageTest.kt | 129 ++++++++++++++++++ 2 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 overflow-core/src/test/kotlin/ForwardMessageTest.kt diff --git a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/message/OnebotMessages.kt b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/message/OnebotMessages.kt index 2fdf32f..de6b8d4 100644 --- a/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/message/OnebotMessages.kt +++ b/overflow-core/src/main/kotlin/top/mrxiaom/overflow/internal/message/OnebotMessages.kt @@ -3,7 +3,9 @@ package top.mrxiaom.overflow.internal.message import cn.evolvefield.onebot.sdk.entity.MsgId import cn.evolvefield.onebot.sdk.event.message.MessageEvent +import cn.evolvefield.onebot.sdk.response.group.ForwardMsgResp import cn.evolvefield.onebot.sdk.util.CQCode +import cn.evolvefield.onebot.sdk.util.JsonHelper.gson import com.google.gson.JsonParser import kotlinx.serialization.encodeToString import kotlinx.serialization.json.* @@ -411,11 +413,35 @@ internal object OnebotMessages { "forward" -> { val id = data["id"].string - if (id.isNotEmpty()) { - val nodes = Mirai.downloadForwardMessage(bot as Bot, id) - val raw = RawForwardMessage(nodes) - add(raw.render(ForwardMessage.DisplayStrategy)) + + val nodes = run { + val tryContent = kotlin.runCatching { + gson.fromJson(data.toString(), ForwardMsgResp::class.java) + }.getOrNull() + + //尝试将合并转发消息块作为完整的含数据消息进行解析, + //若报错则主动获取合并转发消息,否则将其中的数据构造为合并转发消息 + if (tryContent != null) { + return@run tryContent.message.map { + val msg = deserializeFromOneBot(bot, it.message) + ForwardMessage.Node( + it.sender!!.userId, + it.time, + it.sender!!.nickname.takeIf(String::isNotEmpty) ?: "QQ用户", + msg + ) + } + } + + if (id.isNotEmpty()) { + return@run Mirai.downloadForwardMessage(bot as Bot, id) + } + throw IllegalStateException("合并转发消息没有ID") } + + + val raw = RawForwardMessage(nodes) + add(raw.render(ForwardMessage.DisplayStrategy)) } "mface" -> { diff --git a/overflow-core/src/test/kotlin/ForwardMessageTest.kt b/overflow-core/src/test/kotlin/ForwardMessageTest.kt new file mode 100644 index 0000000..5eee558 --- /dev/null +++ b/overflow-core/src/test/kotlin/ForwardMessageTest.kt @@ -0,0 +1,129 @@ +import cn.evolvefield.onebot.sdk.response.group.ForwardMsgResp +import cn.evolvefield.onebot.sdk.util.JsonHelper.gson +import kotlinx.coroutines.runBlocking +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import net.mamoe.mirai.message.data.ForwardMessage +import net.mamoe.mirai.message.data.MessageChain +import org.junit.jupiter.api.Test +import top.mrxiaom.overflow.contact.RemoteBot +import top.mrxiaom.overflow.contact.RemoteBot.Companion.asRemoteBot +import top.mrxiaom.overflow.internal.message.OnebotMessages + +class ForwardMessageTest { + private val mockBot = object : RemoteBot { + override val appName: String + get() = "test" + override val appVersion: String + get() = "V1.0.0" + override val noPlatform: Boolean + get() = false + + override suspend fun executeAction(actionPath: String, params: String?): String { + TODO("Not yet implemented") + } + + override fun sendRawWebSocketMessage(message: String) { + TODO("Not yet implemented") + } + + override fun sendRawWebSocketMessage(message: ByteArray) { + TODO("Not yet implemented") + } + + override suspend fun getMsg(messageId: Int): MessageChain? { + TODO("Not yet implemented") + } + + } + + @Test + //仅用于将OnebotMessages.kt#425行的代码做测试,防止出现bug + fun testForwardCast() { + val it = Json.decodeFromString( + """ + { + "id": "7436377759204399490", + "content": [ + { + "self_id": 3405637452, + "user_id": 485184047, + "time": 1731415740, + "message_id": 726892516, + "message_seq": 726892516, + "real_id": 726892516, + "message_type": "group", + "sender": { + "user_id": 485184047, + "nickname": "上亦下心", + "card": "" + }, + "raw_message": "1", + "font": 14, + "sub_type": "normal", + "message": [ + { + "type": "text", + "data": { + "text": "1" + } + } + ], + "message_format": "array", + "post_type": "message", + "group_id": 284840486 + }, + { + "self_id": 3405637452, + "user_id": 485184047, + "time": 1731415746, + "message_id": 1835533293, + "message_seq": 1835533293, + "real_id": 1835533293, + "message_type": "group", + "sender": { + "user_id": 485184047, + "nickname": "上亦下心", + "card": "" + }, + "raw_message": "1", + "font": 14, + "sub_type": "normal", + "message": [ + { + "type": "text", + "data": { + "text": "1" + } + } + ], + "message_format": "array", + "post_type": "message", + "group_id": 284840486 + } + ] + } + """ + ) + + val resp = runBlocking { + gson.fromJson( + it.toString(), + ForwardMsgResp::class.java + ) + } + val result = runBlocking { + resp.message.map { + val msg = OnebotMessages.deserializeFromOneBot(mockBot, it.message) + ForwardMessage.Node( + it.sender!!.userId, + it.time, + it.sender!!.nickname.takeIf(String::isNotEmpty) ?: "QQ用户", + msg + ) + } + } + println(result) + } +} \ No newline at end of file