From 94d859a4e6cf410d3e79d63f97ffa11a54771bbe Mon Sep 17 00:00:00 2001 From: ryoii Date: Sun, 23 Feb 2020 19:18:14 +0800 Subject: [PATCH] Http api better image with url --- .../mirai/api/http/data/common/BotEventDTO.kt | 2 +- .../mirai/api/http/data/common/MessageDTO.kt | 42 +++++++++++-------- .../mirai/api/http/queue/MessageQueue.kt | 2 +- .../api/http/route/MessageRouteModule.kt | 2 + 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/BotEventDTO.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/BotEventDTO.kt index e94a0c5e4..28630c173 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/BotEventDTO.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/BotEventDTO.kt @@ -12,7 +12,7 @@ import net.mamoe.mirai.utils.MiraiExperimentalAPI sealed class BotEventDTO : EventDTO() @UseExperimental(MiraiExperimentalAPI::class) -fun BotEvent.toDTO() = when(this) { +suspend fun BotEvent.toDTO() = when(this) { is MessagePacket<*, *> -> toDTO() else -> when(this) { is BotOnlineEvent -> BotOnlineEventDTO(bot.uin) 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 b0906898a..187c49a0e 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 @@ -58,7 +58,7 @@ data class PlainDTO(val text: String) : MessageDTO() @Serializable @SerialName("Image") -data class ImageDTO(val imageId: String) : MessageDTO() +data class ImageDTO(val imageId: String? = null, val url: String? = null) : MessageDTO() @Serializable @SerialName("Xml") @@ -85,41 +85,47 @@ sealed class MessageDTO : DTO /* Extend function */ -fun MessagePacket<*, *>.toDTO() = when (this) { +suspend fun MessagePacket<*, *>.toDTO() = when (this) { is FriendMessage -> FriendMessagePacketDTO(QQDTO(sender)) is GroupMessage -> GroupMessagePacketDTO(MemberDTO(sender)) else -> IgnoreEventDTO }.apply { - if (this is MessagePacketDTO) { messageChain = message.toDTOChain() } - // else: `this` is bot event + if (this is MessagePacketDTO) { + // 将MessagePacket中的所有Message转为DTO对象,并添加到messageChain + // foreachContent会忽略MessageSource,一次主动获取 + messageChain = mutableListOf(messageDTO(message[MessageSource])).apply { + message.foreachContent { content -> messageDTO(content).takeUnless { it == UnknownMessageDTO }?.let(::add) } + } + // else: `this` is bot event + } } -fun MessageChain.toDTOChain() = mutableListOf(this[MessageSource].toDTO()).apply { - foreachContent { content -> content.toDTO().takeUnless { it == UnknownMessageDTO }?.let(::add) } -} - -fun MessageChainDTO.toMessageChain(contact: Contact) = +suspend fun MessageChainDTO.toMessageChain(contact: Contact) = buildMessageChain { this@toMessageChain.forEach { it.toMessage(contact)?.let(::add) } } @UseExperimental(ExperimentalUnsignedTypes::class) -fun Message.toDTO() = when (this) { - is MessageSource -> MessageSourceDTO(id) - is At -> AtDTO(target, display) +suspend fun MessagePacket<*, *>.messageDTO(message: Message) = when (message) { + is MessageSource -> MessageSourceDTO(message.id) + is At -> AtDTO(message.target, message.display) is AtAll -> AtAllDTO(0L) - is Face -> FaceDTO(id) - is PlainText -> PlainDTO(stringValue) - is Image -> ImageDTO(imageId) - is XMLMessage -> XmlDTO(stringValue) + is Face -> FaceDTO(message.id) + is PlainText -> PlainDTO(message.stringValue) + is Image -> ImageDTO(message.imageId, message.url()) + is XMLMessage -> XmlDTO(message.stringValue) else -> UnknownMessageDTO } @UseExperimental(ExperimentalUnsignedTypes::class, MiraiInternalAPI::class) -fun MessageDTO.toMessage(contact: Contact) = when (this) { +suspend fun MessageDTO.toMessage(contact: Contact) = when (this) { is AtDTO -> At((contact as Group)[target]) is AtAllDTO -> AtAll is FaceDTO -> Face(faceId) is PlainDTO -> PlainText(text) - is ImageDTO -> Image(imageId) + is ImageDTO -> when { + !imageId.isNullOrBlank() -> Image(imageId) + !url.isNullOrBlank() -> contact.uploadImage(URL(url)) + else -> null + } is XmlDTO -> XMLMessage(xml) is MessageSourceDTO, is UnknownMessageDTO -> null } diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/queue/MessageQueue.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/queue/MessageQueue.kt index e497d56e6..1cb1a54cb 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/queue/MessageQueue.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/queue/MessageQueue.kt @@ -23,7 +23,7 @@ class MessageQueue : ConcurrentLinkedDeque() { val quoteCacheSize = 4096 val quoteCache = LinkedHashMap() - fun fetch(size: Int): List { + suspend fun fetch(size: Int): List { var count = size val ret = ArrayList(count) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt index 04631b2d1..e5b7b0bb7 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt @@ -46,12 +46,14 @@ fun Application.messageModule() { it.session.bot.getFriend(it.target).apply { sendMessage(it.messageChain.toMessageChain(this)) // this aka QQ } + call.respondStateCode(StateCode.Success) } miraiVerify("/sendGroupMessage") { it.session.bot.getGroup(it.target).apply { sendMessage(it.messageChain.toMessageChain(this)) // this aka Group } + call.respondStateCode(StateCode.Success) } miraiVerify("/sendQuoteMessage") {