Add AbstractContact and AbstractUser for future inheritance

This commit is contained in:
Him188 2020-12-23 19:49:04 +08:00
parent 85853b88c1
commit db9ee62250
6 changed files with 56 additions and 26 deletions

View File

@ -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
)
}

View File

@ -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<QQAndroidBot>().unsafeWeakRef()
}

View File

@ -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
}

View File

@ -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<Friend> {
require(message.isContentNotEmpty()) { "message is empty" }

View File

@ -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<MemberInfo>
) : 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

View File

@ -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}" }