From b32090bf2a57951650adab9d02fdde89a4968ec2 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 8 Jan 2021 11:42:44 +0800 Subject: [PATCH] [Review] OtherClient: - Change Bot.otherClients to ContactList - Change OtherClient.id to delegate info.appId - Stabilize OtherClientInfo --- mirai-core-api/src/commonMain/kotlin/Bot.kt | 4 +-- .../commonMain/kotlin/contact/OtherClient.kt | 15 +++++------ .../kotlin/contact/OtherClientList.kt | 25 ------------------- .../src/commonMain/kotlin/AbstractBot.kt | 5 ++-- .../kotlin/contact/OtherClientImpl.kt | 5 +++- .../chat/receive/MessageSvc.PbGetMsg.kt | 1 - .../network/protocol/packet/login/StatSvc.kt | 4 +-- 7 files changed, 19 insertions(+), 40 deletions(-) delete mode 100644 mirai-core-api/src/commonMain/kotlin/contact/OtherClientList.kt diff --git a/mirai-core-api/src/commonMain/kotlin/Bot.kt b/mirai-core-api/src/commonMain/kotlin/Bot.kt index a7bbf99cc..7d3fa19ac 100644 --- a/mirai-core-api/src/commonMain/kotlin/Bot.kt +++ b/mirai-core-api/src/commonMain/kotlin/Bot.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 Mamoe Technologies and contributors. * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. @@ -85,7 +85,7 @@ public interface Bot : CoroutineScope, ContactOrBot, UserOrBot { /** * 其他设备列表 */ - public val otherClients: OtherClientList + public val otherClients: ContactList /** diff --git a/mirai-core-api/src/commonMain/kotlin/contact/OtherClient.kt b/mirai-core-api/src/commonMain/kotlin/contact/OtherClient.kt index 22f7cd37f..9194efc88 100644 --- a/mirai-core-api/src/commonMain/kotlin/contact/OtherClient.kt +++ b/mirai-core-api/src/commonMain/kotlin/contact/OtherClient.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 Mamoe Technologies and contributors. * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. @@ -21,6 +21,7 @@ import net.mamoe.mirai.utils.BotConfiguration.MiraiProtocol.ANDROID_PHONE import net.mamoe.mirai.utils.ExternalResource import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiInternalApi +import net.mamoe.mirai.utils.toLongUnsigned /** * 其他设备. 如当 [Bot] 以 [ANDROID_PHONE] 登录时, 还可以有其他设备以 [ANDROID_PAD], iOS, PC 或其他设备登录. @@ -34,9 +35,11 @@ public interface OtherClient : Contact { public override val bot: Bot /** - * 与 [Bot.id] 相同 + * 识别 id, 仅运行时使用. + * + * 此 id 由其他客户端控制, 重启可能会变化. */ - public override val id: Long get() = bot.id + public override val id: Long get() = info.appId.toLongUnsigned() override suspend fun sendMessage(message: Message): MessageReceipt { throw UnsupportedOperationException("OtherClientImpl.sendMessage is not yet supported.") @@ -47,14 +50,10 @@ public interface OtherClient : Contact { } } -@MiraiInternalApi -public inline val OtherClient.appId: Int - get() = info.appId public inline val OtherClient.platform: Platform get() = info.platform public inline val OtherClient.deviceName: String get() = info.deviceName public inline val OtherClient.deviceKind: String get() = info.deviceKind -@MiraiExperimentalApi public data class OtherClientInfo @MiraiInternalApi constructor( /** @@ -62,6 +61,7 @@ public data class OtherClientInfo @MiraiInternalApi constructor( * * 不可能有 [appId] 相同的两个客户端t在线. */ + @MiraiInternalApi public val appId: Int, /** @@ -99,6 +99,7 @@ public enum class Platform( MOBILE(2, 2), // android WINDOWS(1, 3), + @MiraiExperimentalApi UNKNOWN(0, 0) ; diff --git a/mirai-core-api/src/commonMain/kotlin/contact/OtherClientList.kt b/mirai-core-api/src/commonMain/kotlin/contact/OtherClientList.kt deleted file mode 100644 index 67be94759..000000000 --- a/mirai-core-api/src/commonMain/kotlin/contact/OtherClientList.kt +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2019-2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -package net.mamoe.mirai.contact - -import net.mamoe.mirai.utils.MiraiExperimentalApi -import net.mamoe.mirai.utils.MiraiInternalApi -import java.util.concurrent.ConcurrentLinkedQueue - -public class OtherClientList internal constructor( - @MiraiInternalApi @JvmField - public val delegate: MutableCollection = ConcurrentLinkedQueue() -) : Collection by delegate { - @MiraiExperimentalApi - public operator fun get(appId: Int): OtherClient? = this.find { it.appId == appId } - - public fun getOrFail(appId: Int): OtherClient = - get(appId) ?: throw NoSuchElementException("OtherClient with appId=$appId not found.") -} \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/AbstractBot.kt b/mirai-core/src/commonMain/kotlin/AbstractBot.kt index a27d448be..4f4de85d8 100644 --- a/mirai-core/src/commonMain/kotlin/AbstractBot.kt +++ b/mirai-core/src/commonMain/kotlin/AbstractBot.kt @@ -21,7 +21,8 @@ import io.ktor.util.* import kotlinx.coroutines.* import kotlinx.coroutines.sync.Mutex import net.mamoe.mirai.Bot -import net.mamoe.mirai.contact.OtherClientList +import net.mamoe.mirai.contact.ContactList +import net.mamoe.mirai.contact.OtherClient import net.mamoe.mirai.event.* import net.mamoe.mirai.event.Listener.EventPriority.MONITOR import net.mamoe.mirai.event.events.BotEvent @@ -79,7 +80,7 @@ internal abstract class AbstractBot constructor( GlobalEventChannel.filterIsInstance().filter { it.bot === this@AbstractBot } val otherClientsLock = Mutex() // lock sync - override val otherClients: OtherClientList = OtherClientList() + override val otherClients: ContactList = ContactList() /** * Close server connection, resend login packet, BUT DOESN'T [BotNetworkHandler.init] diff --git a/mirai-core/src/commonMain/kotlin/contact/OtherClientImpl.kt b/mirai-core/src/commonMain/kotlin/contact/OtherClientImpl.kt index 97f600413..6affaeacb 100644 --- a/mirai-core/src/commonMain/kotlin/contact/OtherClientImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/OtherClientImpl.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 Mamoe Technologies and contributors. * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. @@ -18,6 +18,9 @@ import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.utils.ExternalResource import kotlin.coroutines.CoroutineContext +internal inline val OtherClient.appId: Int + get() = info.appId + internal class OtherClientImpl( bot: Bot, coroutineContext: CoroutineContext, 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 f7073158a..343068608 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 @@ -22,7 +22,6 @@ import net.mamoe.mirai.Mirai import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.MemberPermission import net.mamoe.mirai.contact.NormalMember -import net.mamoe.mirai.contact.appId import net.mamoe.mirai.data.MemberInfo import net.mamoe.mirai.event.AbstractEvent import net.mamoe.mirai.event.Event diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt index e67cc6a10..11623ff9c 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 Mamoe Technologies and contributors. * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. @@ -16,11 +16,11 @@ import kotlinx.io.core.ByteReadPacket import kotlinx.serialization.protobuf.ProtoBuf import net.mamoe.mirai.Mirai import net.mamoe.mirai.contact.ClientKind -import net.mamoe.mirai.contact.appId import net.mamoe.mirai.event.events.BotOfflineEvent import net.mamoe.mirai.event.events.OtherClientOfflineEvent import net.mamoe.mirai.event.events.OtherClientOnlineEvent import net.mamoe.mirai.internal.QQAndroidBot +import net.mamoe.mirai.internal.contact.appId import net.mamoe.mirai.internal.createOtherClient import net.mamoe.mirai.internal.message.contextualBugReportException import net.mamoe.mirai.internal.network.Packet