From 089d0eed8cbd9cf1c89596ec5e0304dbf03fb857 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 27 Jun 2021 18:39:11 +0800 Subject: [PATCH] Improve performance --- .../chat/receive/MessageSvc.PbDeleteMsg.kt | 10 ++-- .../chat/receive/MessageSvc.PbGetMsg.kt | 46 ++++++++++--------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbDeleteMsg.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbDeleteMsg.kt index 2f7c2aaec..21a7128c7 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbDeleteMsg.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbDeleteMsg.kt @@ -9,9 +9,6 @@ package net.mamoe.mirai.internal.network.protocol.packet.chat.receive -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.toList import kotlinx.io.core.ByteReadPacket import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.network.QQAndroidClient @@ -34,9 +31,8 @@ internal object MessageSvcPbDeleteMsg : OutgoingPacketFactory("Message ) } - internal suspend fun delete(bot: QQAndroidBot, messages: Flow) = + internal suspend fun delete(bot: QQAndroidBot, messages: List) = bot.network.run { - val map = messages.map { MsgSvc.PbDeleteMsgReq.MsgItem( fromUin = it.msgHead.fromUin, @@ -44,9 +40,9 @@ internal object MessageSvcPbDeleteMsg : OutgoingPacketFactory("Message // 群为84、好友为187。群通过其他方法删除,但测试结果显示通过187也能删除群消息。 msgType = 187, msgSeq = it.msgHead.msgSeq, - msgUid = it.msgHead.msgUid + msgUid = it.msgHead.msgUid, ) - }.toList() + } MessageSvcPbDeleteMsg(bot.client, map).sendWithoutExpect() } diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt index fe0e0eab5..083ab2ac0 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt @@ -12,9 +12,7 @@ package net.mamoe.mirai.internal.network.protocol.packet.chat.receive import kotlinx.coroutines.CoroutineName -import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.* import kotlinx.coroutines.launch import kotlinx.io.core.ByteReadPacket import net.mamoe.mirai.Bot @@ -60,11 +58,11 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory, - val syncCookie: ByteArray?, override val bot: Bot + val syncCookie: ByteArray?, override val bot: Bot, ) : AbstractEvent(), MultiPacket, Collection by delegate, @@ -92,10 +90,9 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory msg.msgHead.msgTime > it.lastReadTime.toLong() and 4294967295L } - }.also { - MessageSvcPbDeleteMsg.delete(bot, it) // 删除消息 + .flatMap { pair -> + pair.msg.asSequence() + .filter { msg: MsgComm.Msg -> msg.msgHead.msgTime > pair.lastReadTime.toLong() and 4294967295L } } + .toList() + .also { MessageSvcPbDeleteMsg.delete(bot, it) } // 删除消息 .filter { msg -> bot.client.syncingController.pbGetMessageCacheList.addCache( QQAndroidClient.MessageSvcSyncData.PbGetMessageSyncId( @@ -159,7 +161,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory = messages.toList() + val list: List = messages if (resp.syncFlag == MsgSvc.SyncFlag.STOP) { return GetMsgSuccess(list, resp.syncCookie, bot) } @@ -177,7 +179,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory