From 47601918e2b1e27f819ce5ceb23d99493e895434 Mon Sep 17 00:00:00 2001 From: ryoii Date: Thu, 20 Feb 2020 19:17:29 +0800 Subject: [PATCH] Add IgnoreEventDTO remove UnknownMessagePacketDTO --- .../mirai/api/http/data/common/MessageDTO.kt | 24 ++++++++++++------- .../net/mamoe/mirai/api/http/util/Json.kt | 1 - 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt index beaa2a7d2..61ebc46f8 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt @@ -32,32 +32,36 @@ data class FriendMessagePacketDTO(val sender: QQDTO) : MessagePacketDTO() @SerialName("GroupMessage") data class GroupMessagePacketDTO(val sender: MemberDTO) : MessagePacketDTO() -@Serializable -@SerialName("UnKnownMessage") -data class UnKnownMessagePacketDTO(val msg: String) : MessagePacketDTO() // Message @Serializable @SerialName("Source") data class MessageSourceDTO(val uid: Long) : MessageDTO() + @Serializable @SerialName("At") data class AtDTO(val target: Long, val display: String = "") : MessageDTO() + @Serializable @SerialName("AtAll") data class AtAllDTO(val target: Long = 0) : MessageDTO() // target为保留字段 + @Serializable @SerialName("Face") data class FaceDTO(val faceId: Int) : MessageDTO() + @Serializable @SerialName("Plain") data class PlainDTO(val text: String) : MessageDTO() + @Serializable @SerialName("Image") data class ImageDTO(val imageId: String) : MessageDTO() + @Serializable @SerialName("Xml") data class XmlDTO(val xml: String) : MessageDTO() + @Serializable @SerialName("Unknown") data class UnknownMessageDTO(val text: String) : MessageDTO() @@ -67,7 +71,7 @@ data class UnknownMessageDTO(val text: String) : MessageDTO() * */ @Serializable sealed class MessagePacketDTO : EventDTO() { - lateinit var messageChain : MessageChainDTO + lateinit var messageChain: MessageChainDTO } typealias MessageChainDTO = List @@ -79,11 +83,15 @@ sealed class MessageDTO : DTO /* Extend function */ -fun MessagePacket<*, *>.toDTO(): MessagePacketDTO = when (this) { +fun MessagePacket<*, *>.toDTO() = when (this) { is FriendMessage -> FriendMessagePacketDTO(QQDTO(sender)) is GroupMessage -> GroupMessagePacketDTO(MemberDTO(sender)) - else -> UnKnownMessagePacketDTO("UnKnown Message Packet") -}.apply { messageChain = mutableListOf().also{ ls -> message.foreachContent { ls.add(it.toDTO()) }}} + else -> IgnoreEventDTO +}.apply { + if (this is MessagePacketDTO) { + messageChain = mutableListOf().also { ls -> message.foreachContent { ls.add(it.toDTO()) } } + } +} fun MessageChainDTO.toMessageChain(contact: Contact) = MessageChain().apply { this@toMessageChain.forEach { add(it.toMessage(contact)) } } @@ -111,5 +119,3 @@ fun MessageDTO.toMessage(contact: Contact) = when (this) { is MessageSourceDTO, is UnknownMessageDTO -> PlainText("assert cannot reach") } - - diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/util/Json.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/util/Json.kt index e6f50776d..7769a411e 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/util/Json.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/util/Json.kt @@ -48,7 +48,6 @@ object MiraiJson { polymorphic(EventDTO.serializer()) { GroupMessagePacketDTO::class with GroupMessagePacketDTO.serializer() FriendMessagePacketDTO::class with FriendMessagePacketDTO.serializer() - UnKnownMessagePacketDTO::class with UnKnownMessagePacketDTO.serializer() BotOnlineEventDTO::class with BotOnlineEventDTO.serializer() BotOfflineEventActiveDTO::class with BotOfflineEventActiveDTO.serializer()