Browse Source

[Review] MessageRecallEvent: make author and operator not null

Him188 5 years ago
parent
commit
976901603e

+ 12 - 15
mirai-core-api/src/commonMain/kotlin/event/events/message.kt

@@ -290,9 +290,9 @@ public sealed class MessageRecallEvent : BotEvent, AbstractEvent() {
     public abstract val authorId: Long
 
     /**
-     * 消息原发送人, 为 `null` 表示原消息发送人已经不是 bot 的好友或已经被移出群。
+     * 消息原发送人.
      */
-    public abstract val author: UserOrBot?
+    public abstract val author: UserOrBot
 
     /**
      * 消息 ids.
@@ -307,7 +307,7 @@ public sealed class MessageRecallEvent : BotEvent, AbstractEvent() {
     public abstract val messageInternalIds: IntArray
 
     /**
-     * 原发送时间
+     * 原发送时间戳, 单位为秒.
      */
     public abstract val messageTime: Int // seconds
 
@@ -322,20 +322,13 @@ public sealed class MessageRecallEvent : BotEvent, AbstractEvent() {
         /**
          * 撤回操作人, 好友的 [User.id]
          */
-        public val operatorId: Long
+        public val operatorId: Long,
+        public val operator: Friend,
     ) : MessageRecallEvent(), Packet {
-        @Deprecated("Binary compatibility.", level = DeprecationLevel.HIDDEN)
-        public fun getOperator(): Long = operatorId
-
-        /**
-         * 撤回操作人. 为 `null` 表示该用户已经不是 bot 的好友
-         */
-        public val operator: Friend? get() = bot.getFriend(operatorId)
-
         /**
          * 消息原发送人, 等于 [operator]
          */
-        override val author: Friend? get() = operator
+        override val author: Friend get() = operator
 
         public override val authorId: Long
             get() = operatorId
@@ -352,6 +345,7 @@ public sealed class MessageRecallEvent : BotEvent, AbstractEvent() {
             if (!messageInternalIds.contentEquals(other.messageInternalIds)) return false
             if (messageTime != other.messageTime) return false
             if (operatorId != other.operatorId) return false
+            if (operator != other.operator) return false
 
             return true
         }
@@ -362,6 +356,7 @@ public sealed class MessageRecallEvent : BotEvent, AbstractEvent() {
             result = 31 * result + messageInternalIds.contentHashCode()
             result = 31 * result + messageTime
             result = 31 * result + operatorId.hashCode()
+            result = 31 * result + operator.hashCode()
             return result
         }
     }
@@ -379,9 +374,9 @@ public sealed class MessageRecallEvent : BotEvent, AbstractEvent() {
          * 操作人. 为 null 时则为 [Bot] 操作.
          */
         public override val operator: Member?,
-        public override val group: Group
+        public override val group: Group,
+        public override val author: NormalMember,
     ) : MessageRecallEvent(), GroupOperableEvent, Packet {
-        override val author: NormalMember? get() = group[authorId]
 
         @Suppress("DuplicatedCode")
         override fun equals(other: Any?): Boolean {
@@ -397,6 +392,7 @@ public sealed class MessageRecallEvent : BotEvent, AbstractEvent() {
             if (messageTime != other.messageTime) return false
             if (operator != other.operator) return false
             if (group != other.group) return false
+            if (author != other.author) return false
 
             return true
         }
@@ -409,6 +405,7 @@ public sealed class MessageRecallEvent : BotEvent, AbstractEvent() {
             result = 31 * result + messageTime
             result = 31 * result + (operator?.hashCode() ?: 0)
             result = 31 * result + group.hashCode()
+            result = 31 * result + author.hashCode()
             return result
         }
     }

+ 8 - 6
mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.ReqPush.kt

@@ -396,7 +396,8 @@ private object Transformers732 : Map<Int, Lambda732> by mapOf(
                     recallReminder.recalledMsgList.mapToIntArray { it.msgRandom },
                     firstPkg.time,
                     operator,
-                    group
+                    group,
+                    group[firstPkg.authorUin] ?: return@lambda732 emptySequence()
                 )
             )
         }
@@ -469,13 +470,14 @@ internal object Transformers528 : Map<Long, Lambda528> by mapOf(
         ) : ProtoBuf
 
         return@lambda528 vProtobuf.loadAs(Sub8A.serializer()).msgInfo.asSequence()
-            .filter { it.botUin == bot.id }.map {
+            .filter { it.botUin == bot.id }.mapNotNull { info ->
                 MessageRecallEvent.FriendRecall(
                     bot = bot,
-                    messageIds = intArrayOf(it.srcId),
-                    messageInternalIds = intArrayOf(it.srcInternalId.toInt()),
-                    messageTime = it.time.toInt(),
-                    operatorId = it.fromUin
+                    messageIds = intArrayOf(info.srcId),
+                    messageInternalIds = intArrayOf(info.srcInternalId.toInt()),
+                    messageTime = info.time.toInt(),
+                    operatorId = info.fromUin,
+                    operator = bot.getFriend(info.fromUin) ?: return@mapNotNull null
                 )
             }
     },