mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-09 19:50:27 +08:00
Fix ForwardMessage
length check; fix #1590
This commit is contained in:
parent
c1932559a7
commit
b1ad60fc11
@ -100,7 +100,7 @@ internal abstract class SendMessageHandler<C : Contact> {
|
||||
}
|
||||
|
||||
if (!contains(IgnoreLengthCheck)) {
|
||||
verityLength(this, contact)
|
||||
verifyLength(this, contact)
|
||||
}
|
||||
|
||||
this
|
||||
@ -274,6 +274,14 @@ internal suspend fun <C : Contact> SendMessageHandler<C>.transformSpecialMessage
|
||||
"ForwardMessage allows up to 200 nodes, but found ${forward.nodeList.size}"
|
||||
)
|
||||
}
|
||||
val tmp = ArrayList<SingleMessage>(
|
||||
forward.nodeList.sumOf { it.messageChain.size }
|
||||
)
|
||||
forward.nodeList.forEach { tmp.addAll(it.messageChain) }
|
||||
|
||||
// toMessageChain will lose some element
|
||||
@Suppress("INVISIBLE_MEMBER")
|
||||
createMessageChainImplOptimized(tmp).verifyLength(forward, contact)
|
||||
}
|
||||
|
||||
val resId = getMiraiImpl().uploadMessageHighway(
|
||||
|
@ -33,7 +33,7 @@ internal fun Contact.logMessageSent(message: Message) {
|
||||
|
||||
internal fun MessageChain.countImages(): Int = this.count { it is Image }
|
||||
|
||||
internal fun MessageChain.verityLength(
|
||||
internal fun MessageChain.verifyLength(
|
||||
originalMessage: Message, target: Contact,
|
||||
): Int {
|
||||
val chain = this
|
||||
@ -42,7 +42,11 @@ internal fun MessageChain.verityLength(
|
||||
throw MessageTooLargeException(
|
||||
target, originalMessage, this,
|
||||
"message(${
|
||||
chain.joinToString("", limit = 10)
|
||||
chain.joinToString("", limit = 10).let { rsp ->
|
||||
if (rsp.length > 100) {
|
||||
rsp.take(100) + "..."
|
||||
} else rsp
|
||||
}
|
||||
}) is too large. Allow up to 50 images or 5000 chars"
|
||||
)
|
||||
}
|
||||
|
@ -177,12 +177,12 @@ internal fun RichMessage.Key.forwardMessage(
|
||||
when {
|
||||
preview.size > 4 -> {
|
||||
preview.take(3).joinToString("") {
|
||||
"""<title size="26" color="#777777" maxLines="2" lineSpace="12">${it.xmlEnc()}</title>"""
|
||||
"""<title size="26" color="#777777" maxLines="2" lineSpace="12">${it.take(50).xmlEnc()}</title>"""
|
||||
} + """<title size="26" color="#777777" maxLines="2" lineSpace="12">...</title>"""
|
||||
}
|
||||
else -> {
|
||||
preview.joinToString("") {
|
||||
"""<title size="26" color="#777777" maxLines="2" lineSpace="12">${it.xmlEnc()}</title>"""
|
||||
"""<title size="26" color="#777777" maxLines="2" lineSpace="12">${it.take(50).xmlEnc()}</title>"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@
|
||||
package net.mamoe.mirai.internal.utils
|
||||
|
||||
import net.mamoe.mirai.contact.ContactOrBot
|
||||
import net.mamoe.mirai.internal.message.ForwardMessageInternal
|
||||
import net.mamoe.mirai.message.data.*
|
||||
import net.mamoe.mirai.utils.toInt
|
||||
import net.mamoe.mirai.utils.toLongUnsigned
|
||||
@ -65,6 +66,7 @@ internal fun SingleMessage.estimateLength(target: ContactOrBot, upTo: Int): Int
|
||||
is PlainText -> content.chineseLength(upTo)
|
||||
is At -> 60 //Magic number
|
||||
is AtAll -> 60 //Magic number
|
||||
is ForwardMessageInternal -> 0 // verified in SendMessageHandler<C>.transformSpecialMessages(message: Message)
|
||||
else -> this.toString().chineseLength(upTo)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user