From 4df800eaa92ddf935ee38fd7c76ed2fc796735ce Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 9 Feb 2021 19:07:31 +0600 Subject: [PATCH] fix of #291 --- CHANGELOG.md | 2 + tgbotapi.core/build.gradle | 1 + .../tgbotapi/types/ParseMode/ParseMode.kt | 8 ++ .../types/message/content/TextContent.kt | 28 +++--- .../internal/CaptionAndTextSourcesToText.kt | 97 ------------------- .../MessageEntity/StringFormattingTests.kt | 2 +- .../MessageEntity/TextPartsCreatingTests.kt | 4 +- tgbotapi.extensions.utils/build.gradle | 18 ++++ .../formatting/ResendingTextFormatting.kt | 14 +-- 9 files changed, 54 insertions(+), 120 deletions(-) delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/internal/CaptionAndTextSourcesToText.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 01bcbea7dc..8c4afa347e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ * Several extensions for `TelegramBotAPI` like `retrieveAccumulatedUpdates` have been added as a solution for [#293](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/293) * Links for `tg://user?id=` have been updated ([#292](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/292)) + * All usages of captions or texts in resends and same things have been replaced with `textSources` + * Global `defaultParseMode` has been added ([#291](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/291)) ## 0.32.4 diff --git a/tgbotapi.core/build.gradle b/tgbotapi.core/build.gradle index 9646921743..ff1a823d33 100644 --- a/tgbotapi.core/build.gradle +++ b/tgbotapi.core/build.gradle @@ -60,6 +60,7 @@ kotlin { dependencies { implementation kotlin('test-common') implementation kotlin('test-annotations-common') + implementation project(":tgbotapi.extensions.utils") } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt index af48b4dbee..13f9606176 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt @@ -35,6 +35,14 @@ typealias Markdown = MarkdownParseMode typealias MarkdownV2 = MarkdownV2ParseMode typealias HTML = HTMLParseMode +/** + * This variable respects to default parse mode used in places like next: + * + * * [dev.inmo.tgbotapi.types.message.content.TextContent.createResends] + * * + */ +var defaultParseMode: ParseMode = HTML + @Serializer(ParseMode::class) internal object ParseModeSerializerObject : KSerializer { override fun deserialize(decoder: Decoder): ParseMode { diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt index ff94ffb84e..2d098ebc64 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt @@ -5,8 +5,7 @@ import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.SendTextMessage import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.MessageIdentifier -import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode -import dev.inmo.tgbotapi.types.ParseMode.ParseMode +import dev.inmo.tgbotapi.types.ParseMode.* import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent @@ -31,19 +30,24 @@ data class TextContent( replyMarkup ) + @Deprecated( + "Useless due to fact that createResend currently use textSource and that will guarantee correct sending of message", + ReplaceWith("createResend") + ) override fun createResends( chatId: ChatIdentifier, disableNotification: Boolean, replyToMessageId: MessageIdentifier?, allowSendingWithoutReply: Boolean?, replyMarkup: KeyboardMarkup? - ): List>> = createResends( - chatId, - disableNotification, - replyToMessageId, - allowSendingWithoutReply, - replyMarkup, - HTMLParseMode + ): List>> = listOf( + createResend( + chatId, + disableNotification, + replyToMessageId, + allowSendingWithoutReply, + replyMarkup + ) ) @Deprecated( @@ -56,8 +60,6 @@ data class TextContent( replyToMessageId: MessageIdentifier?, allowSendingWithoutReply: Boolean?, replyMarkup: KeyboardMarkup?, - parseMode: ParseMode = HTMLParseMode - ): List>> = listOf( - createResend(chatId, disableNotification, replyToMessageId, allowSendingWithoutReply, replyMarkup) - ) + parseMode: ParseMode = defaultParseMode + ): List>> = createResends(chatId, disableNotification, replyToMessageId, allowSendingWithoutReply, replyMarkup) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/internal/CaptionAndTextSourcesToText.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/internal/CaptionAndTextSourcesToText.kt deleted file mode 100644 index c5347a12ad..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/internal/CaptionAndTextSourcesToText.kt +++ /dev/null @@ -1,97 +0,0 @@ -package dev.inmo.tgbotapi.utils.internal - -import dev.inmo.tgbotapi.CommonAbstracts.* -import dev.inmo.tgbotapi.types.ParseMode.* -import dev.inmo.tgbotapi.types.captionLength -import dev.inmo.tgbotapi.types.message.content.TextContent -import dev.inmo.tgbotapi.types.textLength - -internal fun createFormattedText( - entities: TextSourcesList, - partLength: Int = textLength.last, - mode: ParseMode = MarkdownParseMode -): List { - val texts = mutableListOf() - val textBuilder = StringBuilder(partLength) - for (entity in entities) { - val string = when (mode) { - is MarkdownParseMode -> entity.markdown - is MarkdownV2ParseMode -> entity.markdownV2 - is HTMLParseMode -> entity.html - } - if (textBuilder.length + string.length > partLength) { - if (textBuilder.isNotEmpty()) { - texts.add(textBuilder.toString()) - textBuilder.clear() - } - val chunked = string.chunked(partLength) - val last = chunked.last() - textBuilder.append(last) - val listToAdd = if (chunked.size > 1) { - chunked.subList(0, chunked.size - 1) - } else { - emptyList() - } - listToAdd.forEach { - texts.add(it) - } - } else { - textBuilder.append(string) - } - } - if (textBuilder.isNotEmpty()) { - texts.add(textBuilder.toString()) - textBuilder.clear() - } - return texts -} - - -internal fun createMarkdownText( - entities: TextSourcesList, - partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, MarkdownParseMode) - -internal fun TextSourcesList.toMarkdownTexts(): List = createMarkdownText( - this, - textLength.last -) -internal fun TextContent.toMarkdownTexts(): List = textSources.toMarkdownTexts() - - -internal fun createMarkdownV2Text( - entities: TextSourcesList, - partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, MarkdownV2ParseMode) - -internal fun TextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( - this, - captionLength.last -) -internal fun CaptionedInput.toMarkdownV2Captions(): List = textSources.toMarkdownV2Captions() - -internal fun TextSourcesList.toMarkdownV2Texts(): List = createMarkdownV2Text( - this, - textLength.last -) -internal fun TextContent.toMarkdownV2Texts(): List = textSources.toMarkdownV2Texts() - - -internal fun createHtmlText( - entities: TextSourcesList, - partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, HTMLParseMode) - -internal fun TextSourcesList.toHtmlCaptions(): List = createHtmlText( - this, - captionLength.last -) -internal fun CaptionedInput.toHtmlCaptions(): List = textSources.toHtmlCaptions() - -internal fun TextSourcesList.toHtmlTexts(): List = createHtmlText( - this, - textLength.last -) -internal fun TextContent.toHtmlTexts(): List = textSources.toHtmlTexts() - - diff --git a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt index 459bcdf648..1ba73f4c7f 100644 --- a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt +++ b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt @@ -2,8 +2,8 @@ package dev.inmo.tgbotapi.types.MessageEntity import dev.inmo.tgbotapi.CommonAbstracts.TextSource import dev.inmo.tgbotapi.CommonAbstracts.plus +import dev.inmo.tgbotapi.extensions.utils.formatting.* import dev.inmo.tgbotapi.types.MessageEntity.textsources.* -import dev.inmo.tgbotapi.utils.internal.* import kotlin.test.Test import kotlin.test.assertEquals diff --git a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt index ee3941f1ff..14e0a82f2e 100644 --- a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt +++ b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt @@ -1,8 +1,8 @@ package dev.inmo.tgbotapi.types.MessageEntity import dev.inmo.tgbotapi.CommonAbstracts.justTextSources -import dev.inmo.tgbotapi.utils.internal.toHtmlTexts -import dev.inmo.tgbotapi.utils.internal.toMarkdownV2Texts +import dev.inmo.tgbotapi.extensions.utils.formatting.toHtmlTexts +import dev.inmo.tgbotapi.extensions.utils.formatting.toMarkdownV2Texts import kotlin.test.Test import kotlin.test.assertEquals diff --git a/tgbotapi.extensions.utils/build.gradle b/tgbotapi.extensions.utils/build.gradle index 1d557205fe..dca79235e1 100644 --- a/tgbotapi.extensions.utils/build.gradle +++ b/tgbotapi.extensions.utils/build.gradle @@ -44,5 +44,23 @@ kotlin { api project(":tgbotapi.core") } } + + commonTest { + dependencies { + implementation kotlin('test-common') + implementation kotlin('test-annotations-common') + } + } + jvmTest { + dependencies { + implementation kotlin('test-junit') + } + } + jsTest { + dependencies { + implementation kotlin('test-junit') + implementation kotlin('test-js') + } + } } } diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/ResendingTextFormatting.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/ResendingTextFormatting.kt index 9d28d5215e..a8e0535d8d 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/ResendingTextFormatting.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/ResendingTextFormatting.kt @@ -8,15 +8,15 @@ import dev.inmo.tgbotapi.types.message.content.TextContent fun createFormattedText( entities: TextSourcesList, partLength: Int = textLength.last, - mode: ParseMode = MarkdownParseMode + mode: ParseMode = defaultParseMode ): List { val texts = mutableListOf() val textBuilder = StringBuilder(partLength) for (entity in entities) { val string = when (mode) { - is MarkdownParseMode -> entity.markdown - is MarkdownV2ParseMode -> entity.markdownV2 - is HTMLParseMode -> entity.html + is Markdown -> entity.markdown + is MarkdownV2 -> entity.markdownV2 + is HTML -> entity.html } if (textBuilder.length + string.length > partLength) { if (textBuilder.isNotEmpty()) { @@ -49,7 +49,7 @@ fun createFormattedText( fun createMarkdownText( entities: TextSourcesList, partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, MarkdownParseMode) +): List = createFormattedText(entities, partLength, Markdown) fun TextSourcesList.toMarkdownCaptions(): List = createMarkdownText( this, @@ -73,7 +73,7 @@ fun ExplainedInput.toMarkdownExplanations(): List = textSources.toMarkdo fun createMarkdownV2Text( entities: TextSourcesList, partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, MarkdownV2ParseMode) +): List = createFormattedText(entities, partLength, MarkdownV2) fun TextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( this, @@ -97,7 +97,7 @@ fun ExplainedInput.toMarkdownV2Explanations(): List = textSources.toMark fun createHtmlText( entities: TextSourcesList, partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, HTMLParseMode) +): List = createFormattedText(entities, partLength, HTML) fun TextSourcesList.toHtmlCaptions(): List = createHtmlText( this,