Переглянути джерело

[Review] OtherClient:
- Change Bot.otherClients to ContactList<OtherClient>
- Change OtherClient.id to delegate info.appId
- Stabilize OtherClientInfo

Him188 5 роки тому
батько
коміт
b32090bf2a

+ 2 - 2
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<OtherClient>
 
 
     /**

+ 8 - 7
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<OtherClient> {
         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)
     ;
 

+ 0 - 25
mirai-core-api/src/commonMain/kotlin/contact/OtherClientList.kt

@@ -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<OtherClient> = ConcurrentLinkedQueue()
-) : Collection<OtherClient> 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.")
-}

+ 3 - 2
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<N : BotNetworkHandler> constructor(
         GlobalEventChannel.filterIsInstance<BotEvent>().filter { it.bot === this@AbstractBot }
 
     val otherClientsLock = Mutex() // lock sync
-    override val otherClients: OtherClientList = OtherClientList()
+    override val otherClients: ContactList<OtherClient> = ContactList()
 
     /**
      * Close server connection, resend login packet, BUT DOESN'T [BotNetworkHandler.init]

+ 4 - 1
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,

+ 0 - 1
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

+ 2 - 2
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