From 2deb1601a8c203885bef1666721ce9e8ad3bf7ca Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 19 Feb 2020 18:28:58 +0800 Subject: [PATCH] No broadcast for events from friend message syncing --- .../network/QQAndroidBotNetworkHandler.kt | 17 ++++++++++++----- .../kotlin/net.mamoe.mirai/event/Subscribers.kt | 13 ++++++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt index 23d4d8983..848622d27 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt @@ -20,10 +20,7 @@ import kotlinx.io.core.buildPacket import kotlinx.io.core.use import net.mamoe.mirai.data.MultiPacket import net.mamoe.mirai.data.Packet -import net.mamoe.mirai.event.BroadcastControllable -import net.mamoe.mirai.event.CancellableEvent -import net.mamoe.mirai.event.Event -import net.mamoe.mirai.event.broadcast +import net.mamoe.mirai.event.* import net.mamoe.mirai.event.events.BotOfflineEvent import net.mamoe.mirai.event.events.BotOnlineEvent import net.mamoe.mirai.network.BotNetworkHandler @@ -296,7 +293,17 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler heartbeatJob = startHeartbeatJobOrKill() joinAll(friendListJob, groupJob) - MessageSvc.PbGetMsg(bot.client, MsgSvc.SyncFlag.START, currentTimeSeconds).sendAndExpect() + + withTimeoutOrNull(5000) { + lateinit var listener: Listener + listener = this.subscribeAlways { + if (it.packet is MessageSvc.PbGetMsg.GetMsgSuccess) { + listener.complete() + } + } + + MessageSvc.PbGetMsg(bot.client, MsgSvc.SyncFlag.START, currentTimeSeconds).sendWithoutExpect() + } ?: error("timeout syncing friend message history") bot.firstLoginSucceed = true diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt index fd6887e85..f3a2ae6a9 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt @@ -17,6 +17,9 @@ import net.mamoe.mirai.Bot import net.mamoe.mirai.event.internal.Handler import net.mamoe.mirai.event.internal.subscribeInternal import net.mamoe.mirai.utils.MiraiInternalAPI +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract import kotlin.coroutines.CoroutineContext /* @@ -109,9 +112,13 @@ inline fun CoroutineScope.subscribe(crossinline handler: sus * * @see subscribe 获取更多说明 */ -@UseExperimental(MiraiInternalAPI::class) -inline fun CoroutineScope.subscribeAlways(crossinline listener: suspend E.(E) -> Unit): Listener = - E::class.subscribeInternal(Handler { it.listener(it); ListeningStatus.LISTENING }) +@UseExperimental(MiraiInternalAPI::class, ExperimentalContracts::class) +inline fun CoroutineScope.subscribeAlways(crossinline listener: suspend E.(E) -> Unit): Listener { + contract { + callsInPlace(listener, InvocationKind.UNKNOWN) + } + return E::class.subscribeInternal(Handler { it.listener(it); ListeningStatus.LISTENING }) +} /** * 在指定的 [CoroutineScope] 下订阅所有 [E] 及其子类事件.