Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ 支持提前解析含content字段的type为forward类型的消息 #108

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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" -> {
Expand Down
129 changes: 129 additions & 0 deletions overflow-core/src/test/kotlin/ForwardMessageTest.kt
Original file line number Diff line number Diff line change
@@ -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<JsonObject>(
"""
{
"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)
}
}
Loading