From dc194a1d2311cfbf513cc3e0bf28d97cd5643537 Mon Sep 17 00:00:00 2001 From: ryoii Date: Thu, 12 Oct 2023 16:36:09 +0800 Subject: [PATCH] Code format --- .../http/adapter/internal/action/message.kt | 1 - .../adapter/internal/convertor/message.kt | 25 +++++++++++++++++-- .../api/http/adapter/internal/dto/message.kt | 23 +++++++++-------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/action/message.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/action/message.kt index 965339d3..9c3ef37f 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/action/message.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/action/message.kt @@ -27,7 +27,6 @@ import net.mamoe.mirai.console.util.ContactUtils.getContact import net.mamoe.mirai.console.util.cast import net.mamoe.mirai.contact.* import net.mamoe.mirai.contact.roaming.RoamingSupported -import net.mamoe.mirai.data.MemberInfo import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.Image.Key.queryUrl diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/message.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/message.kt index 5e56705c..f4157338 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/message.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/message.kt @@ -65,10 +65,28 @@ internal suspend fun Message.toDTO() = when (this) { is Face -> FaceDTO(id, FaceMap[id], isSuperFace = false) is SuperFace -> FaceDTO(face, FaceMap[face], isSuperFace = true) is PlainText -> PlainDTO(content) - is Image -> ImageDTO(imageId, queryUrl(), width = width, height = height, size = size, imageType = imageType.name, isEmoji = isEmoji) + is Image -> ImageDTO( + imageId, + queryUrl(), + width = width, + height = height, + size = size, + imageType = imageType.name, + isEmoji = isEmoji + ) + is FlashImage -> with(image) { - FlashImageDTO(imageId, queryUrl(), width = width, height = height, size = size, imageType = imageType.name, isEmoji = isEmoji) + FlashImageDTO( + imageId, + queryUrl(), + width = width, + height = height, + size = size, + imageType = imageType.name, + isEmoji = isEmoji + ) } + is OnlineAudio -> VoiceDTO(filename, urlForDownload, length = length) is ServiceMessage -> XmlDTO(content) is LightApp -> AppDTO(content) @@ -76,10 +94,12 @@ internal suspend fun Message.toDTO() = when (this) { groupId = when { source is OfflineMessageSource && (source as OfflineMessageSource).kind == MessageSourceKind.GROUP || source is OnlineMessageSource && (source as OnlineMessageSource).subject is Group -> source.targetId + else -> 0L }, // 避免套娃 origin = source.originalMessage.toDTO { it != UnknownMessageDTO && it !is QuoteDTO }) + is PokeMessage -> PokeMessageDTO(PokeMap[pokeType]) is Dice -> DiceDTO(value) is MarketFace -> MarketFaceDTO(id, name) @@ -87,6 +107,7 @@ internal suspend fun Message.toDTO() = when (this) { is ForwardMessage -> ForwardMessageDTO(null, nodeList.map { ForwardMessageNode(it.senderId, it.time, it.senderName, it.messageChain.toDTO { d -> d != UnknownMessageDTO }) }) + is FileMessage -> FileDTO(id, name, size) is OnlineShortVideo -> ShortVideoDTO(videoId, fileMd5.toHexString(), fileSize, fileFormat, filename, urlForDownload) is OfflineShortVideo -> ShortVideoDTO(videoId, fileMd5.toHexString(), fileSize, fileFormat, filename, null) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/dto/message.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/dto/message.kt index 9045f4c1..8156cdc6 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/dto/message.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/dto/message.kt @@ -99,7 +99,8 @@ internal data class AtAllDTO(val target: Long = 0) : MessageDTO() { @Serializable @SerialName("Face") -internal data class FaceDTO(val faceId: Int = -1, val name: String = "", val isSuperFace: Boolean = false) : MessageDTO() { +internal data class FaceDTO(val faceId: Int = -1, val name: String = "", val isSuperFace: Boolean = false) : + MessageDTO() { override suspend fun convertToMessage(contact: Contact, persistence: Persistence): Message { return when { faceId >= 0 -> Face(faceId) @@ -135,14 +136,17 @@ internal interface ImageLikeDTO { size = this@ImageLikeDTO.size isEmoji = this@ImageLikeDTO.isEmoji } + !url.isNullOrBlank() -> withContext(Dispatchers.IO) { url!!.useUrl { it.uploadAsImage(contact) } } + !path.isNullOrBlank() -> with(File(path!!)) { if (exists()) { inputStream().useStream { it.uploadAsImage(contact) } } else throw NoSuchFileException(this) } + !base64.isNullOrBlank() -> with(Base64.getDecoder().decode(base64)) { inputStream().useStream { it.uploadAsImage(contact) } } @@ -161,23 +165,23 @@ internal interface VoiceLikeDTO { suspend fun voiceLikeToMessage(contact: Contact) = when { contact !is AudioSupported -> null !voiceId.isNullOrBlank() -> OfflineAudio.Factory.create( - voiceId!!, - voiceId!!.substringBefore(".").toHexArray(), - 0, - AudioCodec.SILK, - null + voiceId!!, voiceId!!.substringBefore(".").toHexArray(), 0, AudioCodec.SILK, null ) + !url.isNullOrBlank() -> withContext(Dispatchers.IO) { url!!.useUrl { contact.uploadAudio(it) } } + !path.isNullOrBlank() -> with(File(path!!)) { if (exists()) { inputStream().useStream { contact.uploadAudio(it) } } else throw NoSuchFileException(this) } + !base64.isNullOrBlank() -> with(Base64.getDecoder().decode(base64)) { inputStream().useStream { contact.uploadAudio(it) } } + else -> null } } @@ -200,6 +204,7 @@ internal interface VedioLikeDTO { } } } + else -> { OfflineShortVideo( videoId, @@ -291,11 +296,7 @@ internal data class AppDTO(val content: String) : MessageDTO() { @Serializable @SerialName("Quote") internal data class QuoteDTO( - val id: Int, - val senderId: Long, - val targetId: Long, - val groupId: Long, - val origin: MessageChainDTO + val id: Int, val senderId: Long, val targetId: Long, val groupId: Long, val origin: MessageChainDTO ) : MessageDTO() @Serializable