From 4276b67b2b5d4a8e83e90ea986be250be376b57e Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 13 Oct 2022 15:39:00 +0100 Subject: [PATCH] [core] Fix internal comments in `estimateLength` --- .../protocol/impl/ForwardMessageProtocol.kt | 20 +++++++++++++------ .../src/commonMain/kotlin/utils/type.kt | 9 ++++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/message/protocol/impl/ForwardMessageProtocol.kt b/mirai-core/src/commonMain/kotlin/message/protocol/impl/ForwardMessageProtocol.kt index 025c93057..ad4c1ec2e 100644 --- a/mirai-core/src/commonMain/kotlin/message/protocol/impl/ForwardMessageProtocol.kt +++ b/mirai-core/src/commonMain/kotlin/message/protocol/impl/ForwardMessageProtocol.kt @@ -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}" + ) + } + } } } \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/utils/type.kt b/mirai-core/src/commonMain/kotlin/utils/type.kt index f44b353f4..0aeea8f1e 100644 --- a/mirai-core/src/commonMain/kotlin/utils/type.kt +++ b/mirai-core/src/commonMain/kotlin/utils/type.kt @@ -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.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.transformSpecialMessages(message: Message) + is ForwardMessageInternal -> 0 // verified in ForwardMessageProtocol.ForwardMessageUploader.checkLength else -> this.toString().chineseLength(upTo) } }