From db9ee62250482b3dd3e2936920a75afcc3fa2c26 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 23 Dec 2020 19:49:04 +0800 Subject: [PATCH] Add AbstractContact and AbstractUser for future inheritance --- mirai-core/src/commonMain/kotlin/MiraiImpl.kt | 3 -- .../kotlin/contact/AbstractContact.kt | 28 +++++++++++++++++++ .../commonMain/kotlin/contact/AbstractUser.kt | 24 ++++++++++++++++ .../commonMain/kotlin/contact/FriendImpl.kt | 15 +--------- .../commonMain/kotlin/contact/GroupImpl.kt | 10 ++----- .../network/QQAndroidBotNetworkHandler.kt | 2 +- 6 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt create mode 100644 mirai-core/src/commonMain/kotlin/contact/AbstractUser.kt diff --git a/mirai-core/src/commonMain/kotlin/MiraiImpl.kt b/mirai-core/src/commonMain/kotlin/MiraiImpl.kt index a673ba978..3bd2e5d0d 100644 --- a/mirai-core/src/commonMain/kotlin/MiraiImpl.kt +++ b/mirai-core/src/commonMain/kotlin/MiraiImpl.kt @@ -17,10 +17,8 @@ import kotlinx.serialization.json.* import net.mamoe.mirai.* import net.mamoe.mirai.contact.* import net.mamoe.mirai.data.* -import net.mamoe.mirai.event.broadcast import net.mamoe.mirai.event.events.BotInvitedJoinGroupRequestEvent import net.mamoe.mirai.event.events.MemberJoinRequestEvent -import net.mamoe.mirai.event.events.MessageRecallEvent import net.mamoe.mirai.event.events.NewFriendRequestEvent import net.mamoe.mirai.internal.contact.FriendImpl import net.mamoe.mirai.internal.contact.GroupImpl @@ -210,7 +208,6 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor { return FriendImpl( bot.asQQAndroidBot(), bot.coroutineContext + SupervisorJob(bot.supervisorJob), - friendInfo.uin, friendInfo ) } diff --git a/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt b/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt new file mode 100644 index 000000000..83d5eac95 --- /dev/null +++ b/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt @@ -0,0 +1,28 @@ +/* + * 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.internal.contact + +import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob +import net.mamoe.mirai.Bot +import net.mamoe.mirai.contact.Contact +import net.mamoe.mirai.internal.QQAndroidBot +import net.mamoe.mirai.utils.cast +import net.mamoe.mirai.utils.getValue +import net.mamoe.mirai.utils.unsafeWeakRef +import kotlin.coroutines.CoroutineContext + +internal abstract class AbstractContact( + bot: Bot, + coroutineContext: CoroutineContext, +) : Contact { + final override val coroutineContext: CoroutineContext = coroutineContext + SupervisorJob(coroutineContext[Job]) + final override val bot: QQAndroidBot by bot.cast().unsafeWeakRef() +} \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/contact/AbstractUser.kt b/mirai-core/src/commonMain/kotlin/contact/AbstractUser.kt new file mode 100644 index 000000000..29a8837fb --- /dev/null +++ b/mirai-core/src/commonMain/kotlin/contact/AbstractUser.kt @@ -0,0 +1,24 @@ +/* + * 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.internal.contact + +import net.mamoe.mirai.Bot +import net.mamoe.mirai.contact.User +import kotlin.coroutines.CoroutineContext + +internal abstract class AbstractUser( + bot: Bot, + coroutineContext: CoroutineContext, + friendInfo: net.mamoe.mirai.data.FriendInfo, +) : User, AbstractContact(bot, coroutineContext) { + final override val id: Long = friendInfo.uin + final override val nick: String = friendInfo.nick + final override val remark: String = friendInfo.remark +} \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/contact/FriendImpl.kt b/mirai-core/src/commonMain/kotlin/contact/FriendImpl.kt index 05d159fd9..c15a1d1a7 100644 --- a/mirai-core/src/commonMain/kotlin/contact/FriendImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/FriendImpl.kt @@ -20,8 +20,6 @@ package net.mamoe.mirai.internal.contact import kotlinx.atomicfu.AtomicInt import kotlinx.atomicfu.atomic -import kotlinx.coroutines.Job -import kotlinx.coroutines.SupervisorJob import net.mamoe.mirai.LowLevelApi import net.mamoe.mirai.contact.Friend import net.mamoe.mirai.data.FriendInfo @@ -41,8 +39,6 @@ import net.mamoe.mirai.message.data.Image import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.isContentNotEmpty import net.mamoe.mirai.utils.ExternalImage -import net.mamoe.mirai.utils.getValue -import net.mamoe.mirai.utils.unsafeWeakRef import net.mamoe.mirai.utils.verbose import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract @@ -79,20 +75,11 @@ internal inline fun Friend.checkIsFriendImpl(): FriendImpl { internal class FriendImpl( bot: QQAndroidBot, coroutineContext: CoroutineContext, - override val id: Long, internal val friendInfo: FriendInfo -) : Friend { - override val coroutineContext: CoroutineContext = coroutineContext + SupervisorJob(coroutineContext[Job]) - +) : Friend, AbstractUser(bot, coroutineContext, friendInfo) { @Suppress("unused") // bug val lastMessageSequence: AtomicInt = atomic(-1) - override val bot: QQAndroidBot by bot.unsafeWeakRef() - override val nick: String - get() = friendInfo.nick - override val remark: String - get() = friendInfo.remark - @Suppress("DuplicatedCode") override suspend fun sendMessage(message: Message): MessageReceipt { require(message.isContentNotEmpty()) { "message is empty" } diff --git a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt index 19e15d200..14468a5b0 100644 --- a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt @@ -12,8 +12,6 @@ package net.mamoe.mirai.internal.contact -import kotlinx.coroutines.Job -import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch import net.mamoe.mirai.LowLevelApi import net.mamoe.mirai.Mirai @@ -69,12 +67,8 @@ internal class GroupImpl( override val id: Long, groupInfo: GroupInfo, members: Sequence -) : Group { - companion object; - - override val coroutineContext: CoroutineContext = coroutineContext + SupervisorJob(coroutineContext[Job]) - - override val bot: QQAndroidBot by bot.unsafeWeakRef() +) : Group, AbstractContact(bot, coroutineContext) { + companion object val uin: Long = groupInfo.uin diff --git a/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt index d38e9075f..01ed0d005 100644 --- a/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/network/QQAndroidBotNetworkHandler.kt @@ -265,7 +265,7 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo data.friendList.forEach { // atomic bot.friends.delegate.add( - FriendImpl(bot, bot.coroutineContext, it.friendUin, FriendInfoImpl(it)) + FriendImpl(bot, bot.coroutineContext, FriendInfoImpl(it)) ).also { currentFriendCount++ } } logger.verbose { "正在加载好友列表 ${currentFriendCount}/${totalFriendCount}" }