From 4db04e2a76b995bd89559ee75b403c0506ebcacb Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 12 Nov 2021 21:33:44 +0800 Subject: [PATCH] Fix group detection; fix #1661 --- .../api/binary-compatibility-validator-android.api | 2 +- .../api/binary-compatibility-validator.api | 2 +- .../src/commonMain/kotlin/message/data/MessageSource.kt | 6 +++--- mirai-core/src/commonMain/kotlin/MiraiImpl.kt | 6 +++--- .../src/commonMain/kotlin/message/incomingSourceImpl.kt | 9 ++++++++- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api b/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api index dce2a8373..d6f64b16c 100644 --- a/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api +++ b/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api @@ -5027,7 +5027,7 @@ public abstract class net/mamoe/mirai/message/data/OnlineMessageSource$Incoming$ public final fun getGroup ()Lnet/mamoe/mirai/contact/Group; public abstract fun getSender ()Lnet/mamoe/mirai/contact/Member; public synthetic fun getSubject ()Lnet/mamoe/mirai/contact/Contact; - public final fun getSubject ()Lnet/mamoe/mirai/contact/Group; + public fun getSubject ()Lnet/mamoe/mirai/contact/Group; public synthetic fun getTarget ()Lnet/mamoe/mirai/contact/ContactOrBot; public final fun getTarget ()Lnet/mamoe/mirai/contact/Group; } diff --git a/binary-compatibility-validator/api/binary-compatibility-validator.api b/binary-compatibility-validator/api/binary-compatibility-validator.api index e36e15859..371a7128f 100644 --- a/binary-compatibility-validator/api/binary-compatibility-validator.api +++ b/binary-compatibility-validator/api/binary-compatibility-validator.api @@ -5027,7 +5027,7 @@ public abstract class net/mamoe/mirai/message/data/OnlineMessageSource$Incoming$ public final fun getGroup ()Lnet/mamoe/mirai/contact/Group; public abstract fun getSender ()Lnet/mamoe/mirai/contact/Member; public synthetic fun getSubject ()Lnet/mamoe/mirai/contact/Contact; - public final fun getSubject ()Lnet/mamoe/mirai/contact/Group; + public fun getSubject ()Lnet/mamoe/mirai/contact/Group; public synthetic fun getTarget ()Lnet/mamoe/mirai/contact/ContactOrBot; public final fun getTarget ()Lnet/mamoe/mirai/contact/Group; } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt index 900c358cb..4e266980d 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt @@ -558,9 +558,9 @@ public sealed class OnlineMessageSource : MessageSource() { // TODO: 2021/1/10 E AbstractPolymorphicMessageKey(Incoming, { it.safeCast() }) public abstract override val sender: Member - public final override val subject: Group get() = sender.group - public final override val target: Group get() = group - public inline val group: Group get() = sender.group + public override val subject: Group get() = sender.group + public final override val target: Group get() = subject + public inline val group: Group get() = subject } public companion object Key : diff --git a/mirai-core/src/commonMain/kotlin/MiraiImpl.kt b/mirai-core/src/commonMain/kotlin/MiraiImpl.kt index 327e3d9e0..3b23bf519 100644 --- a/mirai-core/src/commonMain/kotlin/MiraiImpl.kt +++ b/mirai-core/src/commonMain/kotlin/MiraiImpl.kt @@ -467,9 +467,9 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor { is OnlineMessageSourceToGroupImpl, is OnlineMessageSourceFromGroupImpl -> { - val group = when (source) { - is OnlineMessageSourceToGroupImpl -> source.target - is OnlineMessageSourceFromGroupImpl -> source.group + val group: Group = when (source) { + is OnlineMessageSourceToGroupImpl -> source.subject + is OnlineMessageSourceFromGroupImpl -> source.subject else -> error("stub") } if (bot.id != source.fromId) { diff --git a/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt b/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt index 01a3ceee1..0a8e8f5c4 100644 --- a/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt @@ -18,6 +18,7 @@ import net.mamoe.mirai.contact.Friend import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.Stranger import net.mamoe.mirai.internal.asQQAndroidBot +import net.mamoe.mirai.internal.contact.GroupImpl import net.mamoe.mirai.internal.contact.checkIsGroupImpl import net.mamoe.mirai.internal.contact.newAnonymous import net.mamoe.mirai.internal.getGroupByUinOrCodeOrFail @@ -174,13 +175,19 @@ internal class OnlineMessageSourceFromGroupImpl( } override val originalMessage: MessageChain get() = originalMessageLazy.value - override val sender: Member by lazy { + override val subject: GroupImpl by lazy { val groupCode = msg.first().msgHead.groupInfo?.groupCode ?: error("cannot find groupCode for OnlineMessageSourceFromGroupImpl. msg=${msg._miraiContentToString()}") val group = bot.getGroup(groupCode)?.checkIsGroupImpl() ?: error("cannot find group for OnlineMessageSourceFromGroupImpl. msg=${msg._miraiContentToString()}") + group + } + + override val sender: Member by lazy { + val group = subject + val member = group[msg.first().msgHead.fromUin] if (member != null) return@lazy member