From 8df45b4072171b4e53577c2b09c30687c7c22ff6 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 30 Mar 2020 16:01:30 +0800 Subject: [PATCH] Remove `_lowLevelSendLongGroupMessage`, fix length counting --- .../net/mamoe/mirai/qqandroid/QQAndroidBot.kt | 19 ++++++----- .../mirai/qqandroid/contact/GroupImpl.kt | 33 +++++++++---------- .../net/mamoe/mirai/qqandroid/utils/type.kt | 14 ++++++++ .../kotlin/net.mamoe.mirai/lowLevelApi.kt | 10 ------ 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt index 92d47daa5..85764aa94 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt @@ -371,15 +371,13 @@ internal abstract class QQAndroidBotBase constructor( @LowLevelAPI @MiraiExperimentalAPI - override suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: Message): MessageReceipt { - val chain = message.asMessageChain() - check(chain.toString().length <= 4500 && chain.count { it is Image } <= 50) { "message is too large. Allow up to 4500 chars or 50 images" } + internal suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: MessageChain): MessageReceipt { val group = getGroup(groupCode) val time = currentTimeSeconds network.run { - val data = chain.calculateValidationDataForGroup( + val data = message.calculateValidationDataForGroup( sequenceId = client.atomicNextMessageSequenceId(), time = time.toInt(), random = Random.nextInt().absoluteValue.toUInt(), @@ -433,10 +431,15 @@ internal abstract class QQAndroidBotBase constructor( return group.sendMessage( RichMessage.longMessage( - brief = chain.toString().let { // already cached - if (it.length > 27) { - it.take(27) + "..." - } else it + brief = message.joinToString(limit = 27){ + when(it){ + is PlainText -> it.stringValue + is At -> it.display + is AtAll -> it.display + is Image -> "[图片]" + is Face -> "[表情]" + else -> "" + } }, resId = resId, timeSeconds = time diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt index 9978645fe..7653f3e24 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt @@ -30,8 +30,7 @@ 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.image.ImgStore import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc -import net.mamoe.mirai.qqandroid.utils.chineseLength -import net.mamoe.mirai.qqandroid.utils.sumUpTo +import net.mamoe.mirai.qqandroid.utils.estimateLength import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString import net.mamoe.mirai.utils.* import kotlin.contracts.ExperimentalContracts @@ -277,30 +276,30 @@ internal class GroupImpl( @JvmSynthetic override suspend fun sendMessage(message: Message): MessageReceipt { check(!isBotMuted) { "bot is muted. Remaining seconds=$botMuteRemaining" } - val event = GroupMessageSendEvent(this, message.asMessageChain()).broadcast() - if (event.isCancelled) { - throw EventCancelledException("cancelled by GroupMessageSendEvent") - } + + val msg: MessageChain if (message !is LongMessage) { - if (event.message.sumUpTo(800) { it, upTo -> - when (it) { - is QuoteReply -> 700 - is Image -> 300 - is PlainText -> it.stringValue.chineseLength(upTo) - else -> it.toString().length - } - } >= 800) { - return bot._lowLevelSendLongGroupMessage(this.id, message) + val event = GroupMessageSendEvent(this, message.asMessageChain()).broadcast() + if (event.isCancelled) { + throw EventCancelledException("cancelled by GroupMessageSendEvent") } - } + + val length = event.message.estimateLength(4501) + check(length <= 4500 && event.message.count { it is Image } <= 50) { "message is too large. Allow up to 4500 chars or 50 images" } + if (length >= 800) { + return bot._lowLevelSendLongGroupMessage(this.id, event.message) + } + + msg = event.message + } else msg = message.asMessageChain() lateinit var source: MessageSourceFromSendGroup bot.network.run { val response: MessageSvc.PbSendMsg.Response = MessageSvc.PbSendMsg.ToGroup( bot.client, id, - event.message + msg ) { source = it source.startWaitingSequenceId(this) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt index 84b87bdd0..ad5a1234d 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt @@ -12,6 +12,10 @@ package net.mamoe.mirai.qqandroid.utils +import net.mamoe.mirai.message.data.Image +import net.mamoe.mirai.message.data.MessageChain +import net.mamoe.mirai.message.data.PlainText +import net.mamoe.mirai.message.data.QuoteReply import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName @@ -34,6 +38,16 @@ internal fun String.chineseLength(upTo: Int): Int { return this.sumUpTo(upTo) { if (it in '\u0391'..'\uFFE5') 3 else 1 } } +internal fun MessageChain.estimateLength(upTo: Int = Int.MAX_VALUE): Int = + sumUpTo(upTo) { it, up -> + when (it) { + is QuoteReply -> 700 + is Image -> 300 + is PlainText -> it.stringValue.chineseLength(up) + else -> it.toString().chineseLength(up) + } + } + internal inline fun Iterable.sumUpTo(upTo: Int, selector: (T, remaining: Int) -> Int): Int { var sum = 0 for (element in this) { diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/lowLevelApi.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/lowLevelApi.kt index 3c48df4be..4dc1a0432 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/lowLevelApi.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/lowLevelApi.kt @@ -13,8 +13,6 @@ import kotlinx.coroutines.Job import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.QQ import net.mamoe.mirai.data.* -import net.mamoe.mirai.message.MessageReceipt -import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.MessageSource import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiInternalAPI @@ -142,14 +140,6 @@ interface LowLevelBotAPIAccessor { @LowLevelAPI @MiraiExperimentalAPI suspend fun _lowLevelGetGroupActiveData(groupId: Long): GroupActiveData - - /** - * 发送长消息 - */ - @SinceMirai("0.31.0") - @LowLevelAPI - @MiraiExperimentalAPI - suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: Message): MessageReceipt } /**