From de07e0eeb88aeb5efaac03d8a74f5564795f41d6 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 27 Aug 2021 20:15:47 +0800 Subject: [PATCH] Use `getGroupByUinOrCodeOrFail` in OnlineMessageSourceFromTempImpl for tests --- mirai-core/src/commonMain/kotlin/QQAndroidBot.kt | 6 ++++++ .../src/commonMain/kotlin/message/incomingSourceImpl.kt | 7 +++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt b/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt index 68e26e7c1..efbea31d6 100644 --- a/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt +++ b/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt @@ -266,6 +266,12 @@ internal fun QQAndroidBot.getGroupByUin(uin: Long) = groups.firstOrNull { it.uin internal fun QQAndroidBot.getGroupByUinOrCode(uinOrCode: Long) = groups.firstOrNull { it.uin == uinOrCode } ?: groups.firstOrNull { it.id == uinOrCode } +/** + * uin first + */ +internal fun QQAndroidBot.getGroupByUinOrCodeOrFail(uinOrCode: Long) = + getGroupByUinOrCode(uinOrCode) ?: throw NoSuchElementException("group.code or uin=$uinOrCode") + /** * code first diff --git a/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt b/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt index cb81f6860..9b557a8f6 100644 --- a/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt @@ -20,7 +20,7 @@ import net.mamoe.mirai.contact.Stranger import net.mamoe.mirai.internal.asQQAndroidBot import net.mamoe.mirai.internal.contact.checkIsGroupImpl import net.mamoe.mirai.internal.contact.newAnonymous -import net.mamoe.mirai.internal.getGroupByUinOrFail +import net.mamoe.mirai.internal.getGroupByUinOrCodeOrFail import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm import net.mamoe.mirai.internal.network.protocol.data.proto.SourceMsg @@ -140,7 +140,10 @@ internal class OnlineMessageSourceFromTempImpl( override val sender: Member = with(msg.first().msgHead) { // it must be uin, see #1410 // corresponding test: net.mamoe.mirai.internal.notice.processors.MessageTest.group temp message test 2 - bot.asQQAndroidBot().getGroupByUinOrFail(c2cTmpMsgHead!!.groupUin).getOrFail(fromUin) + + // search for group code also is for tests. code may be passed as uin in tests. + // clashing is unlikely possible in real time, so it would not be a problem. + bot.asQQAndroidBot().getGroupByUinOrCodeOrFail(c2cTmpMsgHead!!.groupUin).getOrFail(fromUin) } private val jceData: ImMsgBody.SourceMsg by lazy { msg.toJceDataPrivate(internalIds) }