From 80d4bdfe178344cca499d459266d211f93fe81fd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 30 Oct 2020 20:38:02 +0600 Subject: [PATCH] make audio and documents able to be media group items --- .../types/InputMedia/InputMediaAudio.kt | 28 ++++++++++++++++--- .../types/InputMedia/InputMediaDocument.kt | 22 ++++++++++++--- .../MediaGroupMemberInputMediaSerializer.kt | 4 +++ .../message/content/media/AudioContent.kt | 12 +++++--- .../message/content/media/DocumentContent.kt | 10 +++++-- 5 files changed, 62 insertions(+), 14 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaAudio.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaAudio.kt index 461d4f217b..f430f993eb 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaAudio.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaAudio.kt @@ -5,9 +5,13 @@ import dev.inmo.tgbotapi.CommonAbstracts.Performerable import dev.inmo.tgbotapi.requests.abstracts.* import dev.inmo.tgbotapi.types.ParseMode.ParseMode import dev.inmo.tgbotapi.types.ParseMode.parseModeField +import dev.inmo.tgbotapi.types.files.AudioFile +import dev.inmo.tgbotapi.types.files.PhotoSize import dev.inmo.tgbotapi.types.mediaField -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable +import dev.inmo.tgbotapi.types.message.content.media.AudioContent +import kotlinx.serialization.* + +internal const val audioInputMediaType = "audio" @Serializable data class InputMediaAudio( @@ -19,10 +23,26 @@ data class InputMediaAudio( override val performer: String? = null, override val title: String? = null, override val thumb: InputFile? = null -) : InputMedia, DuratedInputMedia, ThumbedInputMedia, TitledInputMedia, CaptionedOutput, Performerable { - override val type: String = "audio" +) : InputMedia, MediaGroupMemberInputMedia, DuratedInputMedia, ThumbedInputMedia, TitledInputMedia, CaptionedOutput, Performerable { + override val type: String = audioInputMediaType + + override fun serialize(format: StringFormat): String = format.encodeToString(serializer(), this) @SerialName(mediaField) override val media: String init { media = file.fileId } // crutch until js compiling will be fixed } + +fun AudioFile.toInputMediaAudio( + caption: String? = null, + parseMode: ParseMode? = null, + title: String? = this.title +): InputMediaAudio = InputMediaAudio( + fileId, + caption, + parseMode, + duration, + performer, + title, + thumb ?.fileId +) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaDocument.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaDocument.kt index 9d40fa4660..e6d918a278 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaDocument.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaDocument.kt @@ -4,9 +4,11 @@ import dev.inmo.tgbotapi.CommonAbstracts.CaptionedOutput import dev.inmo.tgbotapi.requests.abstracts.* import dev.inmo.tgbotapi.types.ParseMode.ParseMode import dev.inmo.tgbotapi.types.ParseMode.parseModeField +import dev.inmo.tgbotapi.types.files.DocumentFile import dev.inmo.tgbotapi.types.mediaField -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable +import kotlinx.serialization.* + +internal const val documentInputMediaType = "document" @Serializable data class InputMediaDocument( @@ -15,10 +17,22 @@ data class InputMediaDocument( @SerialName(parseModeField) override val parseMode: ParseMode? = null, override val thumb: InputFile? = null -) : InputMedia, ThumbedInputMedia, CaptionedOutput { - override val type: String = "document" +) : InputMedia, MediaGroupMemberInputMedia, ThumbedInputMedia, CaptionedOutput { + override val type: String = documentInputMediaType + + override fun serialize(format: StringFormat): String = format.encodeToString(serializer(), this) @SerialName(mediaField) override val media: String init { media = file.fileId } // crutch until js compiling will be fixed } + +fun DocumentFile.toInputMediaDocument( + caption: String? = null, + parseMode: ParseMode? = null +) = InputMediaDocument( + fileId, + caption, + parseMode, + thumb ?.fileId +) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/MediaGroupMemberInputMediaSerializer.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/MediaGroupMemberInputMediaSerializer.kt index 12faeb251c..2933aa558b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/MediaGroupMemberInputMediaSerializer.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/MediaGroupMemberInputMediaSerializer.kt @@ -16,6 +16,8 @@ internal object MediaGroupMemberInputMediaSerializer : KSerializer InputMediaPhoto.serializer().serialize(encoder, value) is InputMediaVideo -> InputMediaVideo.serializer().serialize(encoder, value) + is InputMediaAudio -> InputMediaAudio.serializer().serialize(encoder, value) + is InputMediaDocument -> InputMediaDocument.serializer().serialize(encoder, value) } } @@ -25,6 +27,8 @@ internal object MediaGroupMemberInputMediaSerializer : KSerializer nonstrictJsonFormat.decodeFromJsonElement(InputMediaPhoto.serializer(), json) videoInputMediaType -> nonstrictJsonFormat.decodeFromJsonElement(InputMediaVideo.serializer(), json) + audioInputMediaType -> nonstrictJsonFormat.decodeFromJsonElement(InputMediaAudio.serializer(), json) + documentInputMediaType -> nonstrictJsonFormat.decodeFromJsonElement(InputMediaDocument.serializer(), json) else -> error("Illegal type of incoming MediaGroupMemberInputMedia") } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AudioContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AudioContent.kt index dde0d121f2..07be31f7ca 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AudioContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AudioContent.kt @@ -1,18 +1,17 @@ package dev.inmo.tgbotapi.types.message.content.media -import dev.inmo.tgbotapi.CommonAbstracts.CaptionedInput import dev.inmo.tgbotapi.CommonAbstracts.TextPart import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.media.SendAudio import dev.inmo.tgbotapi.types.ChatIdentifier -import dev.inmo.tgbotapi.types.InputMedia.InputMediaAudio +import dev.inmo.tgbotapi.types.InputMedia.* import dev.inmo.tgbotapi.types.MessageIdentifier import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2 import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.files.AudioFile import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage -import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent +import dev.inmo.tgbotapi.types.message.content.abstracts.* import dev.inmo.tgbotapi.utils.toHtmlCaptions import dev.inmo.tgbotapi.utils.toMarkdownV2Captions @@ -20,7 +19,7 @@ data class AudioContent( override val media: AudioFile, override val caption: String? = null, override val captionEntities: List = emptyList() -) : MediaContent, CaptionedInput { +) : MediaGroupContent { override fun createResend( chatId: ChatIdentifier, disableNotification: Boolean, @@ -40,6 +39,11 @@ data class AudioContent( replyMarkup ) + override fun toMediaGroupMemberInputMedia(): MediaGroupMemberInputMedia = media.toInputMediaAudio( + toHtmlCaptions().firstOrNull(), + HTMLParseMode + ) + override fun asInputMedia(): InputMediaAudio = InputMediaAudio( media.fileId, toMarkdownV2Captions().firstOrNull(), diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/DocumentContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/DocumentContent.kt index 660347256a..530482b6db 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/DocumentContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/DocumentContent.kt @@ -5,7 +5,7 @@ import dev.inmo.tgbotapi.CommonAbstracts.TextPart import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.media.SendDocument import dev.inmo.tgbotapi.types.ChatIdentifier -import dev.inmo.tgbotapi.types.InputMedia.InputMediaDocument +import dev.inmo.tgbotapi.types.InputMedia.* import dev.inmo.tgbotapi.types.MessageIdentifier import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2 @@ -13,6 +13,7 @@ import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.files.DocumentFile import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent +import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent import dev.inmo.tgbotapi.utils.toHtmlCaptions import dev.inmo.tgbotapi.utils.toMarkdownV2Captions @@ -20,7 +21,7 @@ data class DocumentContent( override val media: DocumentFile, override val caption: String? = null, override val captionEntities: List = emptyList() -) : MediaContent, CaptionedInput { +) : MediaGroupContent { override fun createResend( chatId: ChatIdentifier, disableNotification: Boolean, @@ -37,6 +38,11 @@ data class DocumentContent( replyMarkup ) + override fun toMediaGroupMemberInputMedia(): MediaGroupMemberInputMedia = media.toInputMediaDocument( + toHtmlCaptions().firstOrNull(), + HTMLParseMode + ) + override fun asInputMedia(): InputMediaDocument = InputMediaDocument( media.fileId, toMarkdownV2Captions().firstOrNull(),