From e85d5df03e6fd564ae88cc310350cac6c354b23c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 1 Apr 2020 10:05:30 +0600 Subject: [PATCH 1/8] started 0.26.1 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eced9cdc8..4efa9e9eb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,8 @@ and size of retrieved updates is equal to 100 (max count of retrieved updates) * Extensions `getUpdates` now will receive only not nullable `limit` parameter +### 0.26.1 + ## 0.25.0 * Common: diff --git a/gradle.properties b/gradle.properties index 139532f266..cbb89ee1c5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,6 +7,6 @@ uuid_version=0.1.0 ktor_version=1.3.2 library_group=com.github.insanusmokrassar -library_version=0.26.0 +library_version=0.26.1 gradle_bintray_plugin_version=1.8.4 From a6aa4b8758cef24cf7b45d0218bc5c92829de901 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 1 Apr 2020 10:08:44 +0600 Subject: [PATCH 2/8] BotCommand checks --- CHANGELOG.md | 3 +++ .../TelegramBotAPI/types/BotCommand.kt | 11 ++++++++++- .../insanusmokrassar/TelegramBotAPI/types/Common.kt | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4efa9e9eb8..43a61ec3df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,9 @@ ### 0.26.1 +* `TelegramBotAPI`: + * `BotCommand` now will check and throw error in case when command or description lengths is/are incorrect + ## 0.25.0 * Common: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt index 1e3e9ee01d..0f52133bfb 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt @@ -9,4 +9,13 @@ data class BotCommand( val command: String, @SerialName(descriptionField) val description: String -) +) { + init { + if (command.length !in botCommandLengthLimit) { + error("Command size must be in range $botCommandLengthLimit, but actually have length ${command.length}") + } + if (description.length !in botCommandDescriptionLimit) { + error("Command description size must be in range $botCommandDescriptionLimit, but actually have length ${description.length}") + } + } +} diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index 97b58db5d1..d0142f6a8b 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -49,7 +49,8 @@ val customTitleLength = 0 .. 16 val diceResultLimit = 1 .. 6 -val botCommandLimit = 1 .. 32 +val botCommandLengthLimit = 1 .. 32 +val botCommandLimit = botCommandLengthLimit val botCommandDescriptionLimit = 3 .. 256 val botCommandsLimit = 0 .. 100 From 21a15db031287e986719ab49157095700253bdc9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Apr 2020 14:22:34 +0600 Subject: [PATCH 3/8] StorageFile improvement --- CHANGELOG.md | 4 ++++ .../requests/abstracts/InputFile.kt | 6 +++--- .../TelegramBotAPI/utils/StorageFile.kt | 17 ++++++++++++---- .../utils/StringFileExtension.kt | 6 ++++++ .../TelegramBotAPI/utils/StorageFile.kt | 19 ------------------ .../TelegramBotAPI/utils/StorageFile.kt | 20 ++++++++----------- 6 files changed, 34 insertions(+), 38 deletions(-) create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt delete mode 100644 TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 43a61ec3df..5d4a6606b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,10 @@ * `TelegramBotAPI`: * `BotCommand` now will check and throw error in case when command or description lengths is/are incorrect + * `StorageFile` now is common for all platforms + * JavaScript realization was removed due to its redundancy + * JVM realization was replaced with `fun` factory + * `StorageFile` now able to accept any factory of `Input` ## 0.25.0 diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt index 03065c533b..262623227d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt @@ -33,8 +33,8 @@ internal object InputFileSerializer : KSerializer { @Serializable(InputFileSerializer::class) data class MultipartFile ( val file: StorageFile, - val mimeType: String = file.contentType, - val filename: String = file.fileName + val mimeType: String = file.storageFileInfo.contentType, + val filename: String = file.storageFileInfo.fileName ) : InputFile() { - override val fileId: String = file.generateCustomName() + override val fileId: String = file.storageFileInfo.generateCustomName() } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt index c9fcf6700f..1acd7ffdbf 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt @@ -1,12 +1,21 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils +import com.benasher44.uuid.uuid4 import io.ktor.utils.io.core.Input import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient @Serializable -expect class StorageFile { - val contentType: String +data class StorageFileInfo( + val contentType: String, val fileName: String - fun generateCustomName(): String - fun asInput(): Input +) { + fun generateCustomName() = "${uuid4()}.${fileName.fileExtension}" +} + +data class StorageFile( + val storageFileInfo: StorageFileInfo, + private val inputSource: () -> Input +) { + fun asInput() = inputSource() } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt new file mode 100644 index 0000000000..5762b04a6c --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt @@ -0,0 +1,6 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +private val extensionRegex = Regex("[^.]*$") + +val String.fileExtension + get() = extensionRegex.find(this) ?.value ?: "" diff --git a/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt b/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt deleted file mode 100644 index 17bc529db9..0000000000 --- a/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.utils - -import com.benasher44.uuid.uuid4 -import io.ktor.utils.io.core.Input -import kotlinx.serialization.Serializable -import kotlinx.serialization.Transient - -@Serializable -actual data class StorageFile( - actual val contentType: String, - actual val fileName: String, - @Transient - val inputGetter: () -> Input = throw IllegalStateException("Can't create object without input"), - @Transient - val extension: String = throw IllegalStateException("Can't create object without extension") -) { - actual fun asInput(): Input = inputGetter() - actual fun generateCustomName(): String = "${uuid4()}.$extension" -} \ No newline at end of file diff --git a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt index 878b534f0c..0f2cbd22b1 100644 --- a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt +++ b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt @@ -1,20 +1,16 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils -import com.benasher44.uuid.uuid4 -import io.ktor.utils.io.core.Input import io.ktor.utils.io.streams.asInput -import kotlinx.serialization.Serializable -import kotlinx.serialization.Transient import java.io.File import java.nio.file.Files -@Serializable -actual class StorageFile( - @Transient - private val file: File = throw IllegalStateException("Can't create object without file") +fun StorageFile( + file: File +) = StorageFile( + StorageFileInfo( + Files.probeContentType(file.toPath()), + file.name + ) ) { - actual val contentType: String = Files.probeContentType(file.toPath()) - actual val fileName: String = file.name - actual fun generateCustomName(): String = "${uuid4()}.${file.extension}" - actual fun asInput(): Input = Files.newInputStream(file.toPath()).asInput() + file.inputStream().asInput() } From 9dc5a7624d8e43bcd36d3f1de2bdffabf7373015 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Apr 2020 14:26:32 +0600 Subject: [PATCH 4/8] complete CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d4a6606b9..b468e654aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ * JavaScript realization was removed due to its redundancy * JVM realization was replaced with `fun` factory * `StorageFile` now able to accept any factory of `Input` + * `StorageFileInfo` was added to avoid strange collisions with throws in `StorageFile` ## 0.25.0 From 4e49de0dd731b958e7dc931453a03c36201fa947 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Apr 2020 12:11:39 +0600 Subject: [PATCH 5/8] fix in hashtag --- CHANGELOG.md | 2 +- .../insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b468e654aa..e522d3011d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,7 +47,7 @@ * JVM realization was replaced with `fun` factory * `StorageFile` now able to accept any factory of `Input` * `StorageFileInfo` was added to avoid strange collisions with throws in `StorageFile` - + * Fixes issue with `hashTag` for markdown ## 0.25.0 * Common: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt index 6dfe3f0bc8..c112a00955 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt @@ -120,7 +120,7 @@ fun String.mentionHTML(): String = mention(String::toHtml) fun String.hashTagMarkdown(): String = hashTag(String::toMarkdown) -fun String.hashTagMarkdownV2(): String = hashTag(String::escapeMarkdownV2Common) +fun String.hashTagMarkdownV2(): String = hashTag(String::escapeMarkdownV2Common).escapeMarkdownV2Common() fun String.hashTagHTML(): String = hashTag(String::toHtml) From 048aa93044b7d27fa2e4848c5e98db74bb6f64c3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 5 Apr 2020 14:06:40 +0600 Subject: [PATCH 6/8] InvalidPhotoDimensionsException --- CHANGELOG.md | 2 ++ .../TelegramBotAPI/bot/exceptions/RequestException.kt | 4 ++++ .../TelegramBotAPI/utils/extensions/String.kt | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e522d3011d..a3b9e55d30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,8 @@ * `StorageFile` now able to accept any factory of `Input` * `StorageFileInfo` was added to avoid strange collisions with throws in `StorageFile` * Fixes issue with `hashTag` for markdown + * `InvalidPhotoDimensionsException` was added for cases when `PHOTO_INVALID_DIMENSION` answer received + ## 0.25.0 * Common: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/exceptions/RequestException.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/exceptions/RequestException.kt index 4db320d4c0..594771cdd5 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/exceptions/RequestException.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/exceptions/RequestException.kt @@ -14,6 +14,7 @@ fun newRequestException( description == "Bad Request: message to edit not found" -> MessageToEditNotFoundException(response, plainAnswer, message, cause) description.contains("Bad Request: message is not modified") -> MessageIsNotModifiedException(response, plainAnswer, message, cause) description == "Unauthorized" -> UnauthorizedException(response, plainAnswer, message, cause) + description.contains("PHOTO_INVALID_DIMENSIONS") -> InvalidPhotoDimensionsException(response, plainAnswer, message, cause) else -> null } } ?: CommonRequestException(response, plainAnswer, message, cause) @@ -41,3 +42,6 @@ class MessageIsNotModifiedException(response: Response, plainAnswer: String, mes class MessageToEditNotFoundException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) : RequestException(response, plainAnswer, message, cause) + +class InvalidPhotoDimensionsException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) : + RequestException(response, plainAnswer, message, cause) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt index 349d644f83..f40c30df2b 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt @@ -18,7 +18,7 @@ fun String.toMarkdown(): String { private val markdownV2LinkEscapes = mutableSetOf(')', '\\') private val markdownV2PreAndCodeEscapes = mutableSetOf('`', '\\') -private val markdownV2CommonEscapes = mutableSetOf( +private val markdownV2CommonEscapes = setOf( '_', '*', '[', ']', From 577436843d0f39fe57b55d4bde501c4455d89c9a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 6 Apr 2020 12:09:59 +0600 Subject: [PATCH 7/8] fixes --- .../TelegramBotAPI/utils/extensions/String.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt index f40c30df2b..3d8215326f 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt @@ -16,8 +16,8 @@ fun String.toMarkdown(): String { ) } -private val markdownV2LinkEscapes = mutableSetOf(')', '\\') -private val markdownV2PreAndCodeEscapes = mutableSetOf('`', '\\') +private val markdownV2LinkEscapes = setOf(')', '\\') +private val markdownV2PreAndCodeEscapes = setOf('`', '\\') private val markdownV2CommonEscapes = setOf( '_', '*', From 2bd5d53b2afc2334f59095bd79e446fbe54fd8d6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 6 Apr 2020 12:11:14 +0600 Subject: [PATCH 8/8] fill changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3b9e55d30..6642967bf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ * `StorageFileInfo` was added to avoid strange collisions with throws in `StorageFile` * Fixes issue with `hashTag` for markdown * `InvalidPhotoDimensionsException` was added for cases when `PHOTO_INVALID_DIMENSION` answer received + * Other fixes ## 0.25.0