From 8c5a22a14e7966bb05a118704269426d2882180c Mon Sep 17 00:00:00 2001
From: Karlatemp <kar@kasukusakura.com>
Date: Sat, 18 Feb 2023 23:44:48 +0800
Subject: [PATCH] [core] Try to fix rand id fetching

---
 .../roaming/AbstractRoamingMessages.kt        |  3 ++-
 .../message/source/incomingSourceImpl.kt      | 19 +++++++++++--------
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/mirai-core/src/commonMain/kotlin/contact/roaming/AbstractRoamingMessages.kt b/mirai-core/src/commonMain/kotlin/contact/roaming/AbstractRoamingMessages.kt
index 962a706a7..a181ef7d9 100644
--- a/mirai-core/src/commonMain/kotlin/contact/roaming/AbstractRoamingMessages.kt
+++ b/mirai-core/src/commonMain/kotlin/contact/roaming/AbstractRoamingMessages.kt
@@ -13,6 +13,7 @@ import net.mamoe.mirai.contact.Contact
 import net.mamoe.mirai.contact.roaming.RoamingMessage
 import net.mamoe.mirai.contact.roaming.RoamingMessages
 import net.mamoe.mirai.internal.contact.AbstractContact
+import net.mamoe.mirai.internal.message.source.decodeRandom
 import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm
 import net.mamoe.mirai.utils.mapToIntArray
 import net.mamoe.mirai.utils.toLongUnsigned
@@ -31,7 +32,7 @@ internal abstract class AbstractRoamingMessages : RoamingMessages {
         override val time: Long get() = message.msgHead.msgTime.toLongUnsigned()
         override val ids: IntArray by lazy { messages.mapToIntArray { it.msgHead.msgSeq } }
         override val internalIds: IntArray by lazy {
-            messages.mapToIntArray { it.msgBody.richText.attr?.random ?: 0 } // other client 消息的这个是0
+            messages.mapToIntArray { it.decodeRandom() }
         }
     }
 }
\ No newline at end of file
diff --git a/mirai-core/src/commonMain/kotlin/message/source/incomingSourceImpl.kt b/mirai-core/src/commonMain/kotlin/message/source/incomingSourceImpl.kt
index 95c38c85f..378204765 100644
--- a/mirai-core/src/commonMain/kotlin/message/source/incomingSourceImpl.kt
+++ b/mirai-core/src/commonMain/kotlin/message/source/incomingSourceImpl.kt
@@ -56,9 +56,7 @@ internal class OnlineMessageSourceFromFriendImpl(
     override val isRecalledOrPlanned: Boolean get() = _isRecalledOrPlanned.value
     override fun setRecalled(): Boolean = _isRecalledOrPlanned.compareAndSet(expect = false, update = true)
     override val ids: IntArray get() = sequenceIds // msg.msgBody.richText.attr!!.random
-    override val internalIds: IntArray = msg.mapToIntArray {
-        it.msgBody.richText.attr?.random ?: 0
-    } // other client 消息的这个是0
+    override val internalIds: IntArray = msg.mapToIntArray { it.decodeRandom() }
     override val time: Int = msg.first().msgHead.msgTime
     override var originalMessageLazy = lazy {
         msg.toMessageChainNoSource(bot, 0, MessageSourceKind.FRIEND)
@@ -95,9 +93,7 @@ internal class OnlineMessageSourceFromStrangerImpl(
     override fun setRecalled(): Boolean = _isRecalledOrPlanned.compareAndSet(expect = false, update = true)
 
     override val ids: IntArray get() = sequenceIds // msg.msgBody.richText.attr!!.random
-    override val internalIds: IntArray = msg.mapToIntArray {
-        it.msgBody.richText.attr?.random ?: 0
-    } // other client 消息的这个是0
+    override val internalIds: IntArray = msg.mapToIntArray { it.decodeRandom() }
     override val time: Int = msg.first().msgHead.msgTime
     override var originalMessageLazy = lazy {
         msg.toMessageChainNoSource(bot, 0, MessageSourceKind.STRANGER)
@@ -159,6 +155,13 @@ private fun List<MsgComm.Msg>.toJceDataPrivate(ids: IntArray): ImMsgBody.SourceM
     }
 }
 
+internal fun MsgComm.Msg.decodeRandom(): Int {
+    msgBody.richText.attr?.random?.let { return it }
+
+    // msg uin = 0x100000000000000 or rand.toLongUnsigned()
+    return msgHead.msgUid.toInt()
+}
+
 @Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
 @Serializable(OnlineMessageSourceFromTempImpl.Serializer::class)
 internal class OnlineMessageSourceFromTempImpl(
@@ -168,7 +171,7 @@ internal class OnlineMessageSourceFromTempImpl(
     object Serializer : KSerializer<MessageSource> by MessageSourceSerializerImpl("OnlineMessageSourceFromTemp")
 
     override val sequenceIds: IntArray = msg.mapToIntArray { it.msgHead.msgSeq }
-    override val internalIds: IntArray = msg.mapToIntArray { it.msgBody.richText.attr!!.random }
+    override val internalIds: IntArray = msg.mapToIntArray { it.decodeRandom() }
 
     private val _isRecalledOrPlanned = atomic(false)
 
@@ -217,7 +220,7 @@ internal class OnlineMessageSourceFromGroupImpl(
     override val isRecalledOrPlanned: Boolean get() = _isRecalledOrPlanned.value
     override fun setRecalled(): Boolean = _isRecalledOrPlanned.compareAndSet(expect = false, update = true)
     override val sequenceIds: IntArray = msg.mapToIntArray { it.msgHead.msgSeq }
-    override val internalIds: IntArray = msg.mapToIntArray { it.msgBody.richText.attr!!.random }
+    override val internalIds: IntArray = msg.mapToIntArray { it.decodeRandom() }
     override val ids: IntArray get() = sequenceIds
     override val time: Int = msg.first().msgHead.msgTime
     override var originalMessageLazy = lazy {