Fix incorrect coroutine scope created for contacts in IMirai.newFriend and IMirai.newStranger

This commit is contained in:
Him188 2021-06-27 17:25:05 +08:00
parent dc54679acb
commit e1ffaa5410
8 changed files with 20 additions and 21 deletions

View File

@ -338,8 +338,8 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
override fun newFriend(bot: Bot, friendInfo: FriendInfo): FriendImpl {
return FriendImpl(
bot.asQQAndroidBot(),
bot.coroutineContext + SupervisorJob(bot.supervisorJob),
friendInfo
bot.coroutineContext,
friendInfo.impl(),
)
}
@ -347,8 +347,8 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
override fun newStranger(bot: Bot, strangerInfo: StrangerInfo): StrangerImpl {
return StrangerImpl(
bot.asQQAndroidBot(),
bot.coroutineContext + SupervisorJob(bot.supervisorJob),
strangerInfo
bot.coroutineContext,
strangerInfo.impl(),
)
}

View File

@ -9,15 +9,14 @@
package net.mamoe.mirai.internal.contact
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.utils.childScopeContext
import kotlin.coroutines.CoroutineContext
internal abstract class AbstractContact(
final override val bot: QQAndroidBot,
coroutineContext: CoroutineContext,
parentCoroutineContext: CoroutineContext,
) : Contact {
final override val coroutineContext: CoroutineContext = coroutineContext + SupervisorJob(coroutineContext[Job])
final override val coroutineContext: CoroutineContext = parentCoroutineContext.childScopeContext()
}

View File

@ -18,9 +18,9 @@ import kotlin.coroutines.CoroutineContext
internal abstract class AbstractMember(
final override val group: GroupImpl,
coroutineContext: CoroutineContext,
parentCoroutineContext: CoroutineContext,
memberInfo: MemberInfo,
) : AbstractUser(group.bot, coroutineContext, memberInfo), Member {
) : AbstractUser(group.bot, parentCoroutineContext, memberInfo), Member {
final override val info: MemberInfoImpl = memberInfo.cast()
override val nameCard: String get() = info.nameCard

View File

@ -41,9 +41,10 @@ internal val User.info: UserInfo? get() = this.castOrNull<AbstractUser>()?.info
internal abstract class AbstractUser(
bot: QQAndroidBot,
coroutineContext: CoroutineContext,
parentCoroutineContext: CoroutineContext,
userInfo: UserInfo,
) : User, AbstractContact(bot, coroutineContext) {
) : User, AbstractContact(bot, parentCoroutineContext) {
final override val id: Long = userInfo.uin
final override var nick: String = userInfo.nick
final override val remark: String = userInfo.remark

View File

@ -19,9 +19,9 @@ import kotlin.coroutines.CoroutineContext
internal class AnonymousMemberImpl(
group: GroupImpl,
coroutineContext: CoroutineContext,
parentCoroutineContext: CoroutineContext,
memberInfo: MemberInfo,
) : AnonymousMember, AbstractMember(group, coroutineContext, memberInfo) {
) : AnonymousMember, AbstractMember(group, parentCoroutineContext, memberInfo) {
init {
requireNotNull(memberInfo.anonymousId) { "anonymousId must not be null" }
}

View File

@ -63,13 +63,12 @@ internal inline fun Friend.checkIsFriendImpl(): FriendImpl {
internal class FriendImpl(
bot: QQAndroidBot,
coroutineContext: CoroutineContext,
parentCoroutineContext: CoroutineContext,
internal val friendInfo: FriendInfo,
) : Friend, AbstractUser(bot, coroutineContext, friendInfo) {
) : Friend, AbstractUser(bot, parentCoroutineContext, friendInfo) {
@Suppress("unused") // bug
val lastMessageSequence: AtomicInt = atomic(-1)
val friendPkgMsgParsingCache = C2CPkgMsgParsingCache()
override suspend fun delete() {
check(bot.friends[this.id] != null) {
"Friend ${this.id} had already been deleted"

View File

@ -68,11 +68,11 @@ internal fun Group.checkIsGroupImpl(): GroupImpl {
@Suppress("PropertyName")
internal class GroupImpl(
bot: QQAndroidBot,
coroutineContext: CoroutineContext,
parentCoroutineContext: CoroutineContext,
override val id: Long,
groupInfo: GroupInfo,
members: Sequence<MemberInfo>,
) : Group, AbstractContact(bot, coroutineContext) {
) : Group, AbstractContact(bot, parentCoroutineContext) {
companion object
val uin: Long = groupInfo.uin

View File

@ -49,9 +49,9 @@ internal inline fun Stranger.checkIsImpl(): StrangerImpl {
internal class StrangerImpl(
bot: QQAndroidBot,
coroutineContext: CoroutineContext,
parentCoroutineContext: CoroutineContext,
internal val strangerInfo: StrangerInfo,
) : Stranger, AbstractUser(bot, coroutineContext, strangerInfo) {
) : Stranger, AbstractUser(bot, parentCoroutineContext, strangerInfo) {
@Suppress("unused") // bug
val lastMessageSequence: AtomicInt = atomic(-1)
override suspend fun delete() {