From b9b26b2af055fbf29b6c56ffcc8587628d575a35 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" <n@mamoe.net> Date: Fri, 17 Jan 2020 21:21:15 +0800 Subject: [PATCH] Message Improvements --- .../commonMain/kotlin/net.mamoe.mirai/message/data/At.kt | 4 ++++ .../commonMain/kotlin/net.mamoe.mirai/message/data/Face.kt | 4 ++++ .../kotlin/net.mamoe.mirai/message/data/Image.kt | 4 ++++ .../kotlin/net.mamoe.mirai/message/data/Message.kt | 2 ++ .../kotlin/net.mamoe.mirai/message/data/MessageChain.kt | 5 +++++ .../kotlin/net.mamoe.mirai/message/data/PlainText.kt | 7 +++++++ .../commonMain/kotlin/net.mamoe.mirai/message/data/XML.kt | 4 ++++ 7 files changed, 30 insertions(+) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/At.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/At.kt index 667bc16b0..457f91cbd 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/At.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/At.kt @@ -14,6 +14,10 @@ inline class At(val target: Long) : Message { override fun toString(): String = "[@$target]" // TODO: 2019/11/25 使用群名称进行 at. 因为手机端只会显示这个文字 companion object Key : Message.Key<At> + + override fun eq(other: Message): Boolean { + return other is At && other.target == this.target + } } /** diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Face.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Face.kt index 507ea34ee..8176aa510 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Face.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Face.kt @@ -9,6 +9,10 @@ inline class Face(val id: FaceId) : Message { override fun toString(): String = "[face${id.value}]" companion object Key : Message.Key<Face> + + override fun eq(other: Message): Boolean { + return other is Face && other.id == this.id + } } /** diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt index 453ac36b3..95dcdbdbb 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt @@ -19,6 +19,10 @@ inline class Image(inline val id: ImageId) : Message { override fun toString(): String = "[${id.value}]" companion object Key : Message.Key<Image> + + override fun eq(other: Message): Boolean { + return other is Image && other.id == this.id + } } inline val Image.idValue: String get() = id.value diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt index 767ef4db5..42a049cf5 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt @@ -97,6 +97,8 @@ interface Message { operator fun plus(another: Float): MessageChain = this.followedBy(another.toString().toMessage()) operator fun plus(another: Number): MessageChain = this.followedBy(another.toString().toMessage()) + + } /** diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt index 2cca4c404..c69b65fd8 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt @@ -41,6 +41,11 @@ interface MessageChain : Message, MutableList<Message> { */ operator fun <M : Message> get(key: Message.Key<M>): M = first(key) + override fun eq(other: Message): Boolean { + if(other is MessageChain && other.size != this.size) + return false + return this.toString() == other.toString() + } } /** diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt index 72ae01ac0..c0ce69020 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt @@ -6,6 +6,13 @@ inline class PlainText(val stringValue: String) : Message { override fun toString(): String = stringValue companion object Key : Message.Key<PlainText> + + override fun eq(other: Message): Boolean { + if(other is MessageChain){ + return other eq this.toString() + } + return other is PlainText && other.stringValue == this.stringValue + } } /** diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/XML.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/XML.kt index 78b0ca2a1..65b6bece2 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/XML.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/XML.kt @@ -11,6 +11,10 @@ inline class XMLMessage(val stringValue: String) : Message, SingleOnly { override fun followedBy(tail: Message): Nothing = error("XMLMessage Message cannot be followed") override fun toString(): String = stringValue + + override fun eq(other: Message): Boolean { + return other is XMLMessage && other.stringValue == this.stringValue + } } /**