diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eced9cdc8..6642967bf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,19 @@ 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 + +* `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` + * `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 * 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/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/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 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/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) 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..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,9 +16,9 @@ fun String.toMarkdown(): String { ) } -private val markdownV2LinkEscapes = mutableSetOf(')', '\\') -private val markdownV2PreAndCodeEscapes = mutableSetOf('`', '\\') -private val markdownV2CommonEscapes = mutableSetOf( +private val markdownV2LinkEscapes = setOf(')', '\\') +private val markdownV2PreAndCodeEscapes = setOf('`', '\\') +private val markdownV2CommonEscapes = setOf( '_', '*', '[', ']', 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() } 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