From f6e9f306d4c61a4bf1a1f777cfad4a49f676bcd0 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 5 Dec 2020 12:57:34 +0800 Subject: [PATCH] Redesign Message.Key. #693. - `ConstrainSingle` now extends a more general type `SingleMessage` instead of `MessageMetadata` for extendability - `Message.Key` moved to `ConstrainSingle.Key` - Delete `companion object Key`s from duplicable messages --- .../src/commonMain/kotlin/message/data/At.kt | 5 +- .../commonMain/kotlin/message/data/AtAll.kt | 3 -- .../kotlin/message/data/CustomMessage.kt | 11 ++--- .../commonMain/kotlin/message/data/Face.kt | 8 +--- .../kotlin/message/data/ForwardMessage.kt | 4 +- .../kotlin/message/data/HummerMessage.kt | 32 +++++-------- .../commonMain/kotlin/message/data/Image.kt | 12 ++--- .../commonMain/kotlin/message/data/Message.kt | 46 +++++++++++-------- .../kotlin/message/data/MessageChain.kt | 12 ++--- .../kotlin/message/data/MessageSource.kt | 24 +++++----- .../kotlin/message/data/PlainText.kt | 4 +- .../kotlin/message/data/QuoteReply.kt | 4 +- .../kotlin/message/data/RichMessage.kt | 10 ++-- .../commonMain/kotlin/message/data/Voice.kt | 4 +- .../commonMain/kotlin/message/data/impl.kt | 19 +------- .../message.data/ConstrainSingleTest.kt | 5 +- .../commonMain/kotlin/message/conversions.kt | 4 +- .../commonMain/kotlin/message/imagesImpl.kt | 4 +- .../src/jvmTest/kotlin/ContentEqualsTest.kt | 6 +-- 19 files changed, 90 insertions(+), 127 deletions(-) diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/At.kt b/mirai-core-api/src/commonMain/kotlin/message/data/At.kt index 1317fc101..681166aea 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/At.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/At.kt @@ -52,10 +52,7 @@ private constructor( public override fun toString(): String = "[mirai:at:$target,$display]" public override fun contentToString(): String = this.display - public companion object Key : Message.Key { - public override val typeName: String - get() = "At" - + public companion object { /** * 构造一个 [At], 仅供内部使用, 否则可能造成消息无法发出的问题. */ diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt b/mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt index 8bfd2c3eb..ed2e94d26 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt @@ -30,11 +30,8 @@ private const val displayA = "@全体成员" */ @Serializable public object AtAll : - Message.Key, MessageContent, CodableMessage { public const val display: String = displayA - public override val typeName: String - get() = "AtAll" @Suppress("SpellCheckingInspection") public override fun toString(): String = "[mirai:atall]" diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/CustomMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/CustomMessage.kt index 93d46198c..077c752b4 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/CustomMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/CustomMessage.kt @@ -60,7 +60,7 @@ public sealed class CustomMessage : SingleMessage { * 应确保唯一且不变. */ public final override val typeName: String - ) : Message.Key { + ) : ConstrainSingle.Key { init { @Suppress("LeakingThis") @@ -114,8 +114,7 @@ public sealed class CustomMessage : SingleMessage { public override fun load(input: ByteArray): M = json.decodeFromString(serializer(), String(input)) } - public companion object Key : Message.Key { - override val typeName: String get() = "CustomMessage" + public companion object { private val factories: LockFreeLinkedList> = LockFreeLinkedList() internal fun register(factory: Factory) { @@ -196,14 +195,12 @@ public fun T.toByteArray(): ByteArray { @Serializable @MiraiExperimentalApi public abstract class CustomMessageMetadata : CustomMessage(), MessageMetadata { - public companion object Key : Message.Key { - override val typeName: String get() = "CustomMessageMetadata" - } - public open fun customToString(): ByteArray = customToStringImpl(this.getFactory()) final override fun toString(): String = "[mirai:custom:${getFactory().typeName}:${String(customToString())}]" + + public companion object } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Face.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Face.kt index faa1b5355..e9403f523 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Face.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Face.kt @@ -27,8 +27,7 @@ public data class Face(public val id: Int) : // used in delegation public override fun toString(): String = "[mirai:face:$id]" public override fun contentToString(): String = - if (id >= 0 && id <= 255) - FaceName.names[id] + if (id in 0..255) FaceName.names[id] else "[表情]" public override fun equals(other: Any?): Boolean = other is Face && other.id == this.id @@ -38,10 +37,7 @@ public data class Face(public val id: Int) : // used in delegation * @author LamGC */ @Suppress("SpellCheckingInspection", "unused") - public companion object IdList : Message.Key { - public override val typeName: String - get() = "Face" - + public companion object { public const val unknown: Int = 0xff public const val jingya: Int = 0 public const val piezui: Int = 1 diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt index 319fcbbc2..4eb12c2a9 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt @@ -174,9 +174,7 @@ public data class ForwardMessage( public val message: Message } - public companion object Key : Message.Key { - public override val typeName: String get() = "ForwardMessage" - } + public companion object } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/HummerMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/HummerMessage.kt index 3006ce6ef..07eb14e69 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/HummerMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/HummerMessage.kt @@ -16,7 +16,6 @@ package net.mamoe.mirai.message.data import kotlinx.serialization.Contextual import kotlinx.serialization.Serializable import net.mamoe.mirai.message.code.CodableMessage -import net.mamoe.mirai.message.data.PokeMessage.Types import net.mamoe.mirai.message.data.VipFace.Companion import net.mamoe.mirai.message.data.VipFace.Kind @@ -28,10 +27,7 @@ import net.mamoe.mirai.message.data.VipFace.Kind */ @Serializable public sealed class HummerMessage : MessageContent { - public companion object Key : Message.Key { - public override val typeName: String - get() = "HummerMessage" - } + public companion object // has service type etc. } @@ -45,7 +41,7 @@ public sealed class HummerMessage : MessageContent { * ## mirai 码支持 * 格式: [mirai:poke:*[name]*,*[type]*,*[id]*] * - * @see Types 使用伴生对象中的常量 + * @see PokeMessage.Companion 使用伴生对象中的常量 */ @Serializable public data class PokeMessage internal constructor( @@ -58,10 +54,7 @@ public data class PokeMessage internal constructor( public val id: Int ) : HummerMessage(), CodableMessage { @Suppress("DEPRECATION_ERROR", "DEPRECATION", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") - public companion object Types : Message.Key { - public override val typeName: String - get() = "PokeMessage" - + public companion object { /** 戳一戳 */ @JvmField public val Poke: PokeMessage = PokeMessage("戳一戳", 1, -1) @@ -182,9 +175,7 @@ public data class VipFace internal constructor( } @Suppress("DEPRECATION_ERROR", "DEPRECATION", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") - public companion object : Message.Key { - override val typeName: String get() = "VipFace" - + public companion object { @JvmStatic public val LiuLian: Kind = 9 to "榴莲" @@ -254,8 +245,12 @@ public data class VipFace internal constructor( * @see Image 查看图片相关信息 */ @Serializable -public sealed class FlashImage : MessageContent, HummerMessage(), CodableMessage { - public companion object Key : Message.Key { +public sealed class FlashImage : MessageContent, HummerMessage(), CodableMessage, ConstrainSingle { + override val key: ConstrainSingle.Key get() = Key + + public companion object Key : ConstrainSingle.Key { + override val typeName: String get() = "FlashImage" + /** * 将普通图片转换为闪照. */ @@ -280,9 +275,6 @@ public sealed class FlashImage : MessageContent, HummerMessage(), CodableMessage public operator fun invoke(imageId: String): FlashImage { return invoke(Image(imageId)) } - - public override val typeName: String - get() = "FlashImage" } /** @@ -315,7 +307,7 @@ public inline fun FriendImage.flash(): FriendFlashImage = FlashImage(this) as Fr */ @Serializable public data class GroupFlashImage(@Contextual override val image: GroupImage) : FlashImage() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "GroupFlashImage" } } @@ -325,7 +317,7 @@ public data class GroupFlashImage(@Contextual override val image: GroupImage) : */ @Serializable public data class FriendFlashImage(@Contextual override val image: FriendImage) : FlashImage() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "FriendFlashImage" } } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt index b6f7dcc6b..1516544cd 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt @@ -109,9 +109,7 @@ public interface Image : Message, MessageContent, CodableMessage { } } - public companion object : Message.Key { - override val typeName: String get() = "Image" - + public companion object { /** * 通过 [Image.imageId] 构造一个 [Image] 以便发送. * 这个图片必须是服务器已经存在的图片. @@ -189,9 +187,7 @@ public val Image.md5: ByteArray // NotOnlineImage public abstract class FriendImage @MiraiInternalApi public constructor() : AbstractImage() { // change to sealed in the future. - public companion object Key : Message.Key { - public override val typeName: String get() = "FriendImage" - } + public companion object } /** @@ -203,9 +199,7 @@ public abstract class FriendImage @MiraiInternalApi public constructor() : // CustomFace public abstract class GroupImage @MiraiInternalApi public constructor() : AbstractImage() { // change to sealed in the future. - public companion object Key : Message.Key { - public override val typeName: String get() = "GroupImage" - } + public companion object } /** diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt index cf5372f41..7afe23deb 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt @@ -27,9 +27,10 @@ import net.mamoe.mirai.message.MessageEvent import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.MessageSerializer import net.mamoe.mirai.message.MessageSerializerImpl -import net.mamoe.mirai.message.data.Message.Key +import net.mamoe.mirai.message.data.ConstrainSingle.Key import net.mamoe.mirai.utils.MiraiExperimentalApi import kotlin.contracts.contract +import kotlin.reflect.KClass /** * 可发送的或从服务器接收的消息. @@ -78,23 +79,6 @@ import kotlin.contracts.contract */ @Serializable(Message.Serializer::class) public interface Message { // must be interface. Don't consider any changes. - /** - * 类型 Key. 由伴生对象实现, 表示一个 [Message] 对象的类型. - * - * 除 [MessageChain] 外, 每个 [Message] 类型都拥有一个`伴生对象`(companion object) 来持有一个 Key - * 在 [MessageChain.get] 时将会使用到这个 Key 进行判断类型. - * - * #### 用例 - * [MessageChain.get]: 允许使用数组访问操作符获取指定类型的消息元素 ```val image: Image = chain[Image]``` - * - * @param M 指代持有这个 Key 的消息类型 - */ - public interface Key { - /** - * 此 [Key] 指代的 [Message] 类型名. 一般为 `class.simpleName`, 如 "QuoteReply", "PlainText" - */ - public val typeName: String - } /** * 将 `this` 和 [tail] 连接. @@ -311,12 +295,36 @@ public interface MessageMetadata : SingleMessage { * * 实现此接口的元素将会在连接时自动处理替换. */ -public interface ConstrainSingle : MessageMetadata { +public interface ConstrainSingle : SingleMessage { /** * 用于判断是否为同一种元素的 [Key] * @see Key 查看更多信息 */ public val key: Key + + /** + * 类型 Key. 由伴生对象实现, 表示一个 [Message] 对象的类型. + * + * 除 [MessageChain] 外, 每个 [Message] 类型都拥有一个伴生对象 (companion object) 来持有一个 Key + * + * 在 [MessageChain.get] 时将会使用到这个 Key 进行判断类型. + * + * #### 用例 + * [MessageChain.get][MessageChain.get]: 允许使用数组访问操作符获取指定类型的消息元素 + * ``` + * val image: Image = chain[Image] + * ``` + * + * @param M 指代持有这个 Key 的消息类型 + */ + public interface Key { + /** + * 此 [Key] 指代的 [Message] 类型名. 一般为 [KClass.simpleName], 如 "QuoteReply", "PlainText" + * + * 仅用于提示作用. + */ + public val typeName: String + } } /** diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt index 4b0d61a56..df000d6a7 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt @@ -83,7 +83,7 @@ public interface MessageChain : Message, List, RandomAccess { * @see MessageChain.getOrFail 在找不到此类型的元素时抛出 [NoSuchElementException] */ @JvmName("first") - public operator fun get(key: Message.Key): M? = firstOrNull(key) + public operator fun get(key: ConstrainSingle.Key): M? = firstOrNull(key) public object Serializer : KSerializer { private val delegate = ListSerializer(Message.Serializer) @@ -102,8 +102,8 @@ public interface MessageChain : Message, List, RandomAccess { */ @JvmOverloads public inline fun MessageChain.getOrFail( - key: Message.Key, - crossinline lazyMessage: (key: Message.Key) -> String = { key.typeName } + key: ConstrainSingle.Key, + crossinline lazyMessage: (key: ConstrainSingle.Key) -> String = { key.typeName } ): M = firstOrNull(key) ?: throw NoSuchElementException(lazyMessage(key)) @@ -174,7 +174,7 @@ public inline fun MessageChain.anyIsInstance(): Boolean = */ @JvmSynthetic @Suppress("UNCHECKED_CAST") -public fun MessageChain.firstOrNull(key: Message.Key): M? = firstOrNullImpl(key) +public fun MessageChain.firstOrNull(key: ConstrainSingle.Key): M? = firstOrNullImpl(key) /** * 获取第一个 [M] 类型的 [Message] 实例 @@ -182,7 +182,7 @@ public fun MessageChain.firstOrNull(key: Message.Key): M? = fir */ @JvmSynthetic @Suppress("UNCHECKED_CAST") -public inline fun MessageChain.first(key: Message.Key): M = +public inline fun MessageChain.first(key: ConstrainSingle.Key): M = firstOrNull(key) ?: throw NoSuchElementException("Message type ${key.typeName} not found in chain $this") /** @@ -190,7 +190,7 @@ public inline fun MessageChain.first(key: Message.Key): M = */ @JvmSynthetic @Suppress("UNCHECKED_CAST") -public inline fun MessageChain.any(key: Message.Key): Boolean = firstOrNull(key) != null +public inline fun MessageChain.any(key: ConstrainSingle.Key): Boolean = firstOrNull(key) != null // endregion accessors diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt index baf2cb73c..536b1ea89 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt @@ -63,7 +63,7 @@ import net.mamoe.mirai.utils.LazyProperty */ @Serializable(MessageSourceSerializer::class) public sealed class MessageSource : Message, MessageMetadata, ConstrainSingle { - public final override val key: Message.Key get() = Key + public final override val key: ConstrainSingle.Key get() = Key /** * 所属 [Bot.id] @@ -132,7 +132,7 @@ public sealed class MessageSource : Message, MessageMetadata, ConstrainSingle { + public companion object Key : ConstrainSingle.Key { override val typeName: String get() = "MessageSource" /** @@ -258,7 +258,7 @@ public inline val MessageSource.botOrNull: Bot? * @see OnlineMessageSource.toOffline 转为 [OfflineMessageSource] */ public sealed class OnlineMessageSource : MessageSource() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "OnlineMessageSource" } @@ -294,7 +294,7 @@ public sealed class OnlineMessageSource : MessageSource() { * 由 [机器人主动发送消息][Contact.sendMessage] 产生的 [MessageSource], 可通过 [MessageReceipt] 获得. */ public sealed class Outgoing : OnlineMessageSource() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "OnlineMessageSource.Outgoing" } @@ -305,7 +305,7 @@ public sealed class OnlineMessageSource : MessageSource() { public final override val targetId: Long get() = target.id public abstract class ToFriend : Outgoing() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "OnlineMessageSource.Outgoing.ToFriend" } @@ -315,7 +315,7 @@ public sealed class OnlineMessageSource : MessageSource() { } public abstract class ToTemp : Outgoing() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "OnlineMessageSource.Outgoing.ToTemp" } @@ -325,7 +325,7 @@ public sealed class OnlineMessageSource : MessageSource() { } public abstract class ToGroup : Outgoing() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "OnlineMessageSource.Outgoing.ToGroup" } @@ -338,7 +338,7 @@ public sealed class OnlineMessageSource : MessageSource() { * 接收到的一条消息的 [MessageSource] */ public sealed class Incoming : OnlineMessageSource() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "OnlineMessageSource.Incoming" } @@ -348,7 +348,7 @@ public sealed class OnlineMessageSource : MessageSource() { public final override val targetId: Long get() = target.id public abstract class FromFriend : Incoming() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "OnlineMessageSource.Incoming.FromFriend" } @@ -359,7 +359,7 @@ public sealed class OnlineMessageSource : MessageSource() { } public abstract class FromTemp : Incoming() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "OnlineMessageSource.Incoming.FromTemp" } @@ -370,7 +370,7 @@ public sealed class OnlineMessageSource : MessageSource() { } public abstract class FromGroup : Incoming() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "OnlineMessageSource.Incoming.FromGroup" } @@ -389,7 +389,7 @@ public sealed class OnlineMessageSource : MessageSource() { * @see buildMessageSource 构建一个 [OfflineMessageSource] */ public abstract class OfflineMessageSource : MessageSource() { - public companion object Key : Message.Key { + public companion object Key : ConstrainSingle.Key { public override val typeName: String get() = "OfflineMessageSource" } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/PlainText.kt b/mirai-core-api/src/commonMain/kotlin/message/data/PlainText.kt index 6c3a3e036..8bdf8f050 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/PlainText.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/PlainText.kt @@ -30,9 +30,7 @@ public data class PlainText( public override fun toString(): String = content public override fun contentToString(): String = content - public companion object Key : Message.Key { - public override val typeName: String get() = "PlainText" - } + public companion object } /** diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/QuoteReply.kt b/mirai-core-api/src/commonMain/kotlin/message/data/QuoteReply.kt index 06373ef74..1ff18f90e 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/QuoteReply.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/QuoteReply.kt @@ -40,12 +40,12 @@ import net.mamoe.mirai.message.data.MessageSource.Key.recall */ @Serializable public data class QuoteReply(public val source: MessageSource) : Message, MessageMetadata, ConstrainSingle<QuoteReply> { - public companion object Key : Message.Key<QuoteReply> { + public companion object Key : ConstrainSingle.Key<QuoteReply> { public override val typeName: String get() = "QuoteReply" } - public override val key: Message.Key<QuoteReply> get() = Key + public override val key: ConstrainSingle.Key<QuoteReply> get() = Key // TODO: 2020/12/2 QuoteReply.toString public override fun toString(): String = "[mirai:quote:${source.ids},${source.internalIds}]" diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/RichMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/RichMessage.kt index 8101abbbb..29e4a540a 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/RichMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/RichMessage.kt @@ -45,7 +45,7 @@ public interface RichMessage : MessageContent { * @suppress 此 API 不稳定, 可能在任意时刻被删除 */ @MiraiExperimentalApi - public companion object Templates : Message.Key<RichMessage> { + public companion object Templates : ConstrainSingle.Key<RichMessage> { /** * @suppress 此 API 不稳定, 可能在任意时刻被删除 @@ -93,7 +93,7 @@ public interface RichMessage : MessageContent { */ @Serializable public data class LightApp(override val content: String) : RichMessage { - public companion object Key : Message.Key<LightApp> { + public companion object Key : ConstrainSingle.Key<LightApp> { public override val typeName: String get() = "LightApp" } @@ -116,7 +116,7 @@ public class SimpleServiceMessage( public override val serviceId: Int, public override val content: String ) : ServiceMessage { - public companion object Key : Message.Key<ServiceMessage> { + public companion object Key : ConstrainSingle.Key<ServiceMessage> { public override val typeName: String get() = "ServiceMessage" } @@ -146,7 +146,7 @@ public class SimpleServiceMessage( * @see SimpleServiceMessage */ public interface ServiceMessage : RichMessage { - public companion object Key : Message.Key<ServiceMessage> { + public companion object Key : ConstrainSingle.Key<ServiceMessage> { public override val typeName: String get() = "ServiceMessage" } @@ -258,7 +258,7 @@ internal class LongMessage internal constructor(override val content: String, va AbstractServiceMessage() { override val serviceId: Int get() = 35 - companion object Key : Message.Key<LongMessage> { + companion object Key : ConstrainSingle.Key<LongMessage> { override val typeName: String get() = "LongMessage" } } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt index 0428e10ae..86bda5f77 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt @@ -21,7 +21,7 @@ import net.mamoe.mirai.utils.MiraiInternalApi @MiraiExperimentalApi public abstract class PttMessage : MessageContent { - public companion object Key : Message.Key<PttMessage> { + public companion object Key : ConstrainSingle.Key<PttMessage> { public override val typeName: String get() = "PttMessage" } @@ -43,7 +43,7 @@ public class Voice @MiraiInternalApi constructor( private val _url: String ) : PttMessage() { - public companion object Key : Message.Key<Voice> { + public companion object Key : ConstrainSingle.Key<Voice> { override val typeName: String get() = "Voice" } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/impl.kt b/mirai-core-api/src/commonMain/kotlin/message/data/impl.kt index 77e2ebed2..4578a4864 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/impl.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/impl.kt @@ -14,9 +14,6 @@ package net.mamoe.mirai.message.data import net.mamoe.mirai.utils.MiraiExperimentalApi -import kotlin.jvm.JvmMultifileClass -import kotlin.jvm.JvmName -import kotlin.jvm.JvmSynthetic import kotlin.native.concurrent.SharedImmutable // region image @@ -25,7 +22,7 @@ import kotlin.native.concurrent.SharedImmutable //// IMPLEMENTATIONS //// ///////////////////////// -private fun Message.hasDuplicationOfConstrain(key: Message.Key<*>): Boolean { +private fun Message.hasDuplicationOfConstrain(key: ConstrainSingle.Key<*>): Boolean { return when (this) { is SingleMessage -> (this as? ConstrainSingle<*>)?.key == key is CombinedMessage -> return this.left.hasDuplicationOfConstrain(key) || this.tail.hasDuplicationOfConstrain(key) @@ -191,16 +188,9 @@ internal inline fun <T> List<T>.indexOfFirst(offset: Int, predicate: (T) -> Bool @JvmSynthetic @Suppress("UNCHECKED_CAST", "DEPRECATION_ERROR", "DEPRECATION") -internal fun <M : Message> MessageChain.firstOrNullImpl(key: Message.Key<M>): M? = when (key) { - At -> firstIsInstanceOrNull<At>() - AtAll -> firstIsInstanceOrNull<AtAll>() - PlainText -> firstIsInstanceOrNull<PlainText>() - Image -> firstIsInstanceOrNull<Image>() +internal fun <M : Message> MessageChain.firstOrNullImpl(key: ConstrainSingle.Key<M>): M? = when (key) { // OnlineImage -> firstIsInstanceOrNull<OnlineImage>() // OfflineImage -> firstIsInstanceOrNull<OfflineImage>() - GroupImage -> firstIsInstanceOrNull<GroupImage>() - FriendImage -> firstIsInstanceOrNull<FriendImage>() - Face -> firstIsInstanceOrNull<Face>() QuoteReply -> firstIsInstanceOrNull<QuoteReply>() MessageSource -> firstIsInstanceOrNull<MessageSource>() OnlineMessageSource -> firstIsInstanceOrNull<OnlineMessageSource>() @@ -215,14 +205,9 @@ internal fun <M : Message> MessageChain.firstOrNullImpl(key: Message.Key<M>): M? LongMessage -> firstIsInstanceOrNull() RichMessage -> firstIsInstanceOrNull<RichMessage>() LightApp -> firstIsInstanceOrNull<LightApp>() - PokeMessage -> firstIsInstanceOrNull<PokeMessage>() - HummerMessage -> firstIsInstanceOrNull<HummerMessage>() FlashImage -> firstIsInstanceOrNull<FlashImage>() GroupFlashImage -> firstIsInstanceOrNull<GroupFlashImage>() FriendFlashImage -> firstIsInstanceOrNull<FriendFlashImage>() - CustomMessage -> firstIsInstanceOrNull() - CustomMessageMetadata -> firstIsInstanceOrNull() - ForwardMessage -> firstIsInstanceOrNull() PttMessage -> firstIsInstanceOrNull<PttMessage>() Voice -> firstIsInstanceOrNull<Voice>() else -> { diff --git a/mirai-core-api/src/commonTest/kotlin/message.data/ConstrainSingleTest.kt b/mirai-core-api/src/commonTest/kotlin/message.data/ConstrainSingleTest.kt index 47c02d0a5..0c8facbca 100644 --- a/mirai-core-api/src/commonTest/kotlin/message.data/ConstrainSingleTest.kt +++ b/mirai-core-api/src/commonTest/kotlin/message.data/ConstrainSingleTest.kt @@ -15,14 +15,15 @@ import kotlin.test.assertTrue internal class TestConstrainSingleMessage : ConstrainSingle<TestConstrainSingleMessage>, Any() { - companion object Key : Message.Key<TestConstrainSingleMessage> { + companion object Key : ConstrainSingle.Key<TestConstrainSingleMessage> { override val typeName: String get() = "TestMessage" } override fun toString(): String = "<TestConstrainSingleMessage#${super.hashCode()}>" + override fun contentToString(): String = "" - override val key: Message.Key<TestConstrainSingleMessage> + override val key: ConstrainSingle.Key<TestConstrainSingleMessage> get() = Key } diff --git a/mirai-core/src/commonMain/kotlin/message/conversions.kt b/mirai-core/src/commonMain/kotlin/message/conversions.kt index 0601b37ac..f371f88ff 100644 --- a/mirai-core/src/commonMain/kotlin/message/conversions.kt +++ b/mirai-core/src/commonMain/kotlin/message/conversions.kt @@ -436,14 +436,14 @@ internal fun List<ImMsgBody.Elem>.joinToMessageChain(groupIdOrZero: Long, botId: CustomMessage.load(this) }.fold( onFailure = { - if (it is CustomMessage.Key.CustomMessageFullDataDeserializeInternalException) { + if (it is CustomMessage.Companion.CustomMessageFullDataDeserializeInternalException) { throw IllegalStateException( "Internal error: " + "exception while deserializing CustomMessage head data," + " data=${element.customElem.data.toUHexString()}", it ) } else { - it as CustomMessage.Key.CustomMessageFullDataDeserializeUserException + it as CustomMessage.Companion.CustomMessageFullDataDeserializeUserException throw IllegalStateException( "User error: " + "exception while deserializing CustomMessage body," + diff --git a/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt b/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt index c453308e2..a326982d0 100644 --- a/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt @@ -146,7 +146,7 @@ internal val firstOnlineBotInstance: Bot get() = Bot.botInstancesSequence.firstO @Suppress("EXPOSED_SUPER_INTERFACE") internal interface OnlineImage : Image, ConstOriginUrlAware { - companion object Key : Message.Key<OnlineImage> { + companion object Key : ConstrainSingle.Key<OnlineImage> { override val typeName: String get() = "OnlineImage" } @@ -160,7 +160,7 @@ internal interface OnlineImage : Image, ConstOriginUrlAware { * 一般由 [Contact.uploadImage] 得到 */ internal interface OfflineImage : Image { - companion object Key : Message.Key<OfflineImage> { + companion object Key : ConstrainSingle.Key<OfflineImage> { override val typeName: String get() = "OfflineImage" } } diff --git a/mirai-core/src/jvmTest/kotlin/ContentEqualsTest.kt b/mirai-core/src/jvmTest/kotlin/ContentEqualsTest.kt index ffa6390a1..ec81529ae 100644 --- a/mirai-core/src/jvmTest/kotlin/ContentEqualsTest.kt +++ b/mirai-core/src/jvmTest/kotlin/ContentEqualsTest.kt @@ -11,21 +11,21 @@ package net.mamoe.mirai.internal import net.mamoe.mirai.message.data.ConstrainSingle import net.mamoe.mirai.message.data.Image -import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.buildMessageChain import kotlin.test.Test import kotlin.test.assertFalse import kotlin.test.assertTrue internal class TestConstrainSingleMessage : ConstrainSingle<TestConstrainSingleMessage>, Any() { - companion object Key : Message.Key<TestConstrainSingleMessage> { + companion object Key : ConstrainSingle.Key<TestConstrainSingleMessage> { override val typeName: String get() = "TestMessage" } override fun toString(): String = "<TestConstrainSingleMessage#${super.hashCode()}>" + override fun contentToString(): String = "" - override val key: Message.Key<TestConstrainSingleMessage> + override val key: ConstrainSingle.Key<TestConstrainSingleMessage> get() = Key }