From ec5870e0b3d5b9637afca54bf231fdfb5b6524dc Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sat, 18 Feb 2023 22:56:27 +0800 Subject: [PATCH] [core] Get group incoming message source raw data directly --- .../android/api/android.api | 4 ++-- .../compatibility-validation/jvm/api/jvm.api | 4 ++-- .../kotlin/message/data/OnlineMessageSource.kt | 5 +++-- .../message/source/incomingSourceImpl.kt | 18 +++++++++++------- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/mirai-core-api/compatibility-validation/android/api/android.api b/mirai-core-api/compatibility-validation/android/api/android.api index 9ea913c95..1ccd73be6 100644 --- a/mirai-core-api/compatibility-validation/android/api/android.api +++ b/mirai-core-api/compatibility-validation/android/api/android.api @@ -4809,9 +4809,9 @@ public abstract class net/mamoe/mirai/message/data/OnlineMessageSource : net/mam public abstract class net/mamoe/mirai/message/data/OnlineMessageSource$Incoming : net/mamoe/mirai/message/data/OnlineMessageSource { public static final field Key Lnet/mamoe/mirai/message/data/OnlineMessageSource$Incoming$Key; - public final fun getFromId ()J + public fun getFromId ()J public abstract fun getSender ()Lnet/mamoe/mirai/contact/User; - public final fun getTargetId ()J + public fun getTargetId ()J } public abstract class net/mamoe/mirai/message/data/OnlineMessageSource$Incoming$FromFriend : net/mamoe/mirai/message/data/OnlineMessageSource$Incoming { diff --git a/mirai-core-api/compatibility-validation/jvm/api/jvm.api b/mirai-core-api/compatibility-validation/jvm/api/jvm.api index 3cf6ecb39..9b3723b40 100644 --- a/mirai-core-api/compatibility-validation/jvm/api/jvm.api +++ b/mirai-core-api/compatibility-validation/jvm/api/jvm.api @@ -4809,9 +4809,9 @@ public abstract class net/mamoe/mirai/message/data/OnlineMessageSource : net/mam public abstract class net/mamoe/mirai/message/data/OnlineMessageSource$Incoming : net/mamoe/mirai/message/data/OnlineMessageSource { public static final field Key Lnet/mamoe/mirai/message/data/OnlineMessageSource$Incoming$Key; - public final fun getFromId ()J + public fun getFromId ()J public abstract fun getSender ()Lnet/mamoe/mirai/contact/User; - public final fun getTargetId ()J + public fun getTargetId ()J } public abstract class net/mamoe/mirai/message/data/OnlineMessageSource$Incoming$FromFriend : net/mamoe/mirai/message/data/OnlineMessageSource$Incoming { diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/OnlineMessageSource.kt b/mirai-core-api/src/commonMain/kotlin/message/data/OnlineMessageSource.kt index 25fd4af8d..95e3eb8e3 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/OnlineMessageSource.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/OnlineMessageSource.kt @@ -151,8 +151,9 @@ public sealed class OnlineMessageSource : MessageSource() { public sealed class Incoming : OnlineMessageSource() { public abstract override val sender: User - public final override val fromId: Long get() = sender.id - public final override val targetId: Long get() = target.id + /// NOTE: DONT use final to avoid contact not available + public override val fromId: Long get() = sender.id + public override val targetId: Long get() = target.id @NotStableForInheritance public abstract class FromFriend @MiraiInternalApi constructor() : Incoming() { diff --git a/mirai-core/src/commonMain/kotlin/message/source/incomingSourceImpl.kt b/mirai-core/src/commonMain/kotlin/message/source/incomingSourceImpl.kt index bf65d3cf3..95c38c85f 100644 --- a/mirai-core/src/commonMain/kotlin/message/source/incomingSourceImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/source/incomingSourceImpl.kt @@ -206,7 +206,7 @@ internal class OnlineMessageSourceFromTempImpl( @Serializable(OnlineMessageSourceFromGroupImpl.Serializer::class) internal class OnlineMessageSourceFromGroupImpl( override val bot: Bot, - msg: List, + private val msg: List, ) : OnlineMessageSource.Incoming.FromGroup(), IncomingMessageSourceInternal { object Serializer : KSerializer by MessageSourceSerializerImpl("OnlineMessageSourceFromGroupImpl") @@ -229,11 +229,8 @@ internal class OnlineMessageSourceFromGroupImpl( override val subject: GroupImpl by lazy { - val groupCode = msg.first().msgHead.groupInfo?.groupCode - ?: error("cannot find groupCode for OnlineMessageSourceFromGroupImpl. msg=${msg.structureToString()}") - - val group = bot.getGroup(groupCode)?.checkIsGroupImpl() - ?: error("cannot find group for OnlineMessageSourceFromGroupImpl. msg=${msg.structureToString()}") + val group = bot.getGroup(targetId)?.checkIsGroupImpl() + ?: error("cannot find group for OnlineMessageSourceFromGroupImpl. Use `source.targetId` to get group id. msg=${msg.structureToString()}") group } @@ -245,13 +242,20 @@ internal class OnlineMessageSourceFromGroupImpl( if (member != null) return@lazy member val anonymousInfo = msg.first().msgBody.richText.elems.firstOrNull { it.anonGroupMsg != null } - ?: error("cannot find member for OnlineMessageSourceFromGroupImpl. msg=${msg.structureToString()}") + ?: error("cannot find member for OnlineMessageSourceFromGroupImpl. Use `source.fromId` to get sender id. msg=${msg.structureToString()}") anonymousInfo.run { group.newAnonymous(anonGroupMsg!!.anonNick.decodeToString(), anonGroupMsg.anonId.encodeBase64()) } } + override val fromId: Long get() = msg.first().msgHead.fromUin + override val targetId: Long + get() { + return msg.first().msgHead.groupInfo?.groupCode + ?: error("cannot find groupCode for OnlineMessageSourceFromGroupImpl. msg=${msg.structureToString()}") + } + private val jceData: ImMsgBody.SourceMsg by lazy { ImMsgBody.SourceMsg( origSeqs = intArrayOf(msg.first().msgHead.msgSeq),