[core] Fix internal comments in estimateLength

This commit is contained in:
Him188
2022-10-13 15:39:00 +01:00
parent 73293251d1
commit 4276b67b2b
2 changed files with 20 additions and 9 deletions

View File

@@ -10,6 +10,7 @@
package net.mamoe.mirai.internal.message.protocol.impl
import net.mamoe.mirai.contact.MessageTooLargeException
import net.mamoe.mirai.internal.contact.AbstractContact
import net.mamoe.mirai.internal.message.data.forwardMessage
import net.mamoe.mirai.internal.message.flags.IgnoreLengthCheck
import net.mamoe.mirai.internal.message.protocol.MessageProtocol
@@ -38,12 +39,7 @@ internal class ForwardMessageProtocol : MessageProtocol() {
val contact = attributes[CONTACT]
if (!currentMessageChain.contains(IgnoreLengthCheck)) {
check(forward.nodeList.size <= 200) {
throw MessageTooLargeException(
contact, forward, forward,
"ForwardMessage allows up to 200 nodes, but found ${forward.nodeList.size}"
)
}
checkLength(forward, contact)
sequence {
forward.nodeList.forEach { yieldAll(it.messageChain) }
}.asIterable().verifyLength(forward, contact)
@@ -62,5 +58,17 @@ internal class ForwardMessageProtocol : MessageProtocol() {
forwardMessage = forward,
).toMessageChain()
}
private fun checkLength(
forward: ForwardMessage,
contact: AbstractContact
) {
check(forward.nodeList.size <= 200) {
throw MessageTooLargeException(
contact, forward, forward,
"ForwardMessage allows up to 200 nodes, but found ${forward.nodeList.size}"
)
}
}
}
}

View File

@@ -11,10 +11,9 @@ package net.mamoe.mirai.internal.utils
import net.mamoe.mirai.contact.ContactOrBot
import net.mamoe.mirai.internal.message.data.ForwardMessageInternal
import net.mamoe.mirai.internal.message.protocol.impl.ForwardMessageProtocol
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.utils.chineseLength
import net.mamoe.mirai.utils.toInt
import net.mamoe.mirai.utils.toLongUnsigned
internal fun Int.toIpV4AddressString(): String {
@@ -30,11 +29,15 @@ internal fun Int.toIpV4AddressString(): String {
}
}
}
internal fun Iterable<SingleMessage>.estimateLength(target: ContactOrBot, upTo: Int): Int =
sumUpTo(upTo) { it, up ->
it.estimateLength(target, up)
}
/**
* @see ForwardMessageProtocol.ForwardMessageUploader.checkLength
*/
internal fun SingleMessage.estimateLength(target: ContactOrBot, upTo: Int): Int {
return when (this) {
is QuoteReply -> 444 + this.source.originalMessage.estimateLength(target, upTo) // Magic number
@@ -42,7 +45,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)
is ForwardMessageInternal -> 0 // verified in ForwardMessageProtocol.ForwardMessageUploader.checkLength
else -> this.toString().chineseLength(upTo)
}
}