Fix message length estimate, close #195

This commit is contained in:
Him188 2020-04-04 13:50:47 +08:00
parent f74e5d87ba
commit 220e6d42f5
4 changed files with 19 additions and 8 deletions

View File

@ -402,7 +402,12 @@ internal abstract class QQAndroidBotBase constructor(
val resId: String val resId: String
when (response) { when (response) {
is MultiMsg.ApplyUp.Response.MessageTooLarge -> is MultiMsg.ApplyUp.Response.MessageTooLarge ->
error("message is too large") error(
"Internal error: message is too large, but this should be handled before sending. Message content:" +
message.joinToString {
"${it::class.simpleName}(l=${it.toString().length})"
}
)
is MultiMsg.ApplyUp.Response.RequireUpload -> { is MultiMsg.ApplyUp.Response.RequireUpload -> {
resId = response.proto.msgResid resId = response.proto.msgResid

View File

@ -30,7 +30,6 @@ import net.mamoe.mirai.qqandroid.network.highway.HighwayHelper
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.TroopManagement import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.TroopManagement
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.image.ImgStore import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.image.ImgStore
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc
import net.mamoe.mirai.qqandroid.utils.estimateLength
import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import kotlin.contracts.ExperimentalContracts import kotlin.contracts.ExperimentalContracts
@ -285,7 +284,7 @@ internal class GroupImpl(
throw EventCancelledException("cancelled by GroupMessageSendEvent") throw EventCancelledException("cancelled by GroupMessageSendEvent")
} }
val length = event.message.estimateLength(5001) val length = event.message.toString().length
if (!(length <= 5000 && event.message.count { it is Image } <= 50)) { if (!(length <= 5000 && event.message.count { it is Image } <= 50)) {
throw MessageTooLargeException( throw MessageTooLargeException(
this, this,
@ -294,10 +293,17 @@ internal class GroupImpl(
"message(${event.message.joinToString( "message(${event.message.joinToString(
"", "",
limit = 10 limit = 10
)}) is too large. Allow up to 5000 in weight (Chinese char=4, English char=1, Quote=700, Image=800, others are estimated in String length.)" )}) is too large. Allow up to 50 images or 5000 chars"
) )
} }
if (length >= 800) {
val imageCount = event.message.count { it is Image }
if (length >= 800
|| imageCount >= 4
|| (event.message.any<QuoteReply>()
&& (imageCount != 0 || length > 100))
) {
return bot.lowLevelSendLongGroupMessage(this.id, event.message) return bot.lowLevelSendLongGroupMessage(this.id, event.message)
} }

View File

@ -49,7 +49,7 @@ internal fun MessageChain.calculateValidationDataForGroup(
groupCode: Long, groupCode: Long,
botMemberNameCard: String botMemberNameCard: String
): MessageValidationData { ): MessageValidationData {
val richTextElems = this.toRichTextElems(true, false) val richTextElems = this.toRichTextElems(forGroup = true, withGeneralFlags = false)
val msgTransmit = MsgTransmit.PbMultiMsgTransmit( val msgTransmit = MsgTransmit.PbMultiMsgTransmit(
msg = listOf( msg = listOf(

View File

@ -33,7 +33,7 @@ internal fun Int.toIpV4AddressString(): String {
} }
internal fun String.chineseLength(upTo: Int): Int { internal fun String.chineseLength(upTo: Int): Int {
return this.sumUpTo(upTo) { if (it in '\u0391'..'\uFFE5') 4 else 1 } return this.sumUpTo(upTo) { if (it in '\u0391'..'\uFFE5') 3 else 1 }
} }
internal fun MessageChain.estimateLength(upTo: Int = Int.MAX_VALUE): Int = internal fun MessageChain.estimateLength(upTo: Int = Int.MAX_VALUE): Int =
@ -47,7 +47,7 @@ internal fun SingleMessage.estimateLength(upTo: Int = Int.MAX_VALUE): Int {
is QuoteReply -> { is QuoteReply -> {
700 + source.originalMessage.estimateLength(upTo) 700 + source.originalMessage.estimateLength(upTo)
} }
is Image -> 300 // is Image -> 300
is PlainText -> stringValue.chineseLength(upTo) is PlainText -> stringValue.chineseLength(upTo)
is At -> display.chineseLength(upTo) is At -> display.chineseLength(upTo)
is AtAll -> display.chineseLength(upTo) is AtAll -> display.chineseLength(upTo)