From 8b93922f821eda42890c2db3e888cfe57e6f60d4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 1 Jan 2020 21:28:00 +0600 Subject: [PATCH 01/20] starts 0.21.0 --- CHANGELOG.md | 2 ++ build.gradle | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 350c2f1f7e..40f7c89333 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 0.21.0 TelegramBotAPI 4.5 + ## 0.20.0 MPP Migration * Time library change: `joda-time` -> `com.soywiz.korlibs.klock:klock` diff --git a/build.gradle b/build.gradle index d15f48af65..4a20b41134 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ plugins { id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" } -project.version = "0.20.4" +project.version = "0.21.0" project.group = "com.github.insanusmokrassar" apply from: "publish.gradle" From 4dd2a8437c60e00c7445db4c67ce97dab14bbea7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 1 Jan 2020 21:38:05 +0600 Subject: [PATCH 02/20] added the field slowModeDelay to the ExtendedSupergroupChat objects --- CHANGELOG.md | 2 ++ .../com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt | 1 + .../types/chat/abstracts/extended/ExtendedSupergroupChat.kt | 1 + .../types/chat/extended/ExtendedSupergroupChatImpl.kt | 2 ++ 4 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40f7c89333..9bf1065e12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.21.0 TelegramBotAPI 4.5 +* Added the field `slowModeDelay` to the `ExtendedSupergroupChat` objects. + ## 0.20.0 MPP Migration * Time library change: `joda-time` -> `com.soywiz.korlibs.klock:klock` diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index dd07501d98..60c4e11d78 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -57,6 +57,7 @@ const val languageCodeField = "language_code" const val textEntitiesField = "text_entities" const val stickerSetNameField = "set_name" const val stickerSetNameFullField = "sticker_set_name" +const val slowModeDelayField = "slow_mode_delay" const val maskPositionField = "mask_position" const val phoneNumberField = "phone_number" const val userIdField = "user_id" diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/abstracts/extended/ExtendedSupergroupChat.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/abstracts/extended/ExtendedSupergroupChat.kt index 5dd9f3d8ca..4979a0bd5c 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/abstracts/extended/ExtendedSupergroupChat.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/abstracts/extended/ExtendedSupergroupChat.kt @@ -4,6 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.StickerSetName import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.SupergroupChat interface ExtendedSupergroupChat : SupergroupChat, ExtendedGroupChat { + val slowModeDelay: Long? val stickerSetName: StickerSetName? val canSetStickerSet: Boolean } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/extended/ExtendedSupergroupChatImpl.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/extended/ExtendedSupergroupChatImpl.kt index 80a13e37f2..6f906ff566 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/extended/ExtendedSupergroupChatImpl.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/chat/extended/ExtendedSupergroupChatImpl.kt @@ -29,6 +29,8 @@ data class ExtendedSupergroupChatImpl( override val pinnedMessage: Message? = null, @SerialName(stickerSetNameFullField) override val stickerSetName: StickerSetName? = null, + @SerialName(slowModeDelayField) + override val slowModeDelay: Long? = null, @SerialName(canSetStickerSetField) override val canSetStickerSet: Boolean = false ) : ExtendedSupergroupChat From b4e4bed622de16b4075533913041dae3e4403c33 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 1 Jan 2020 21:45:39 +0600 Subject: [PATCH 03/20] added the new request SetChatAdministratorCustomTitle to manage the custom titles of administrators promoted by the bot. --- CHANGELOG.md | 2 ++ .../SetChatAdministratorCustomTitle.kt | 24 +++++++++++++++++++ .../TelegramBotAPI/types/Common.kt | 1 + 3 files changed, 27 insertions(+) create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/members/SetChatAdministratorCustomTitle.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bf1065e12..c2174381db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.21.0 TelegramBotAPI 4.5 +* Added the new request `SetChatAdministratorCustomTitle` to manage the custom titles of administrators promoted by the +bot. * Added the field `slowModeDelay` to the `ExtendedSupergroupChat` objects. ## 0.20.0 MPP Migration diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/members/SetChatAdministratorCustomTitle.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/members/SetChatAdministratorCustomTitle.kt new file mode 100644 index 0000000000..31f8aa3a62 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/members/SetChatAdministratorCustomTitle.kt @@ -0,0 +1,24 @@ +package com.github.insanusmokrassar.TelegramBotAPI.requests.chat.members + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.types.ChatRequest +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest +import com.github.insanusmokrassar.TelegramBotAPI.requests.chat.abstracts.ChatMemberRequest +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import kotlinx.serialization.* +import kotlinx.serialization.internal.BooleanSerializer + +@Serializable +data class SetChatAdministratorCustomTitle( + @SerialName(chatIdField) + override val chatId: ChatId, + @SerialName(userIdField) + override val userId: UserId, + @SerialName(customTitleField) + val customTitle: String +) : ChatMemberRequest { + override fun method(): String = "setChatAdministratorCustomTitle" + override val resultDeserializer: DeserializationStrategy + get() = BooleanSerializer + override val requestSerializer: SerializationStrategy<*> + get() = RestrictChatMember.serializer() +} \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index 60c4e11d78..be43314b59 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -95,6 +95,7 @@ const val switchInlineQueryField = "switch_inline_query" const val isAnimatedField = "is_animated" const val inviteLinkField = "invite_link" const val pinnedMessageField = "pinned_message" +const val customTitleField = "custom_title" const val requestWriteAccessField = "request_write_access" From 47428dd6f912babebd2831df948c5d0dfd9e9771 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 1 Jan 2020 21:51:11 +0600 Subject: [PATCH 04/20] now any administrator object instance have nullable field --- CHANGELOG.md | 1 + .../types/ChatMember/AdministratorChatMemberImpl.kt | 3 ++- .../TelegramBotAPI/types/ChatMember/CreatorChatMember.kt | 6 ++++-- .../TelegramBotAPI/types/ChatMember/RawChatMember.kt | 9 ++++++--- .../ChatMember/abstracts/AdministratorChatMember.kt | 1 + 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2174381db..93e0bc469e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 0.21.0 TelegramBotAPI 4.5 +* Now any administrator object instance have `customTitle` nullable field * Added the new request `SetChatAdministratorCustomTitle` to manage the custom titles of administrators promoted by the bot. * Added the field `slowModeDelay` to the `ExtendedSupergroupChat` objects. diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/AdministratorChatMemberImpl.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/AdministratorChatMemberImpl.kt index 168b61ff8f..23662f990e 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/AdministratorChatMemberImpl.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/AdministratorChatMemberImpl.kt @@ -13,5 +13,6 @@ data class AdministratorChatMemberImpl( override val canInviteUsers: Boolean, override val canRestrictMembers: Boolean, override val canPinMessages: Boolean, - override val canPromoteMembers: Boolean + override val canPromoteMembers: Boolean, + override val customTitle: String? ) : AdministratorChatMember diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/CreatorChatMember.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/CreatorChatMember.kt index b00de59489..d92d84b2d8 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/CreatorChatMember.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/CreatorChatMember.kt @@ -3,8 +3,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.ChatMember import com.github.insanusmokrassar.TelegramBotAPI.types.ChatMember.abstracts.AdministratorChatMember import com.github.insanusmokrassar.TelegramBotAPI.types.User -data class CreatorChatMember(override val user: User) : - AdministratorChatMember { +data class CreatorChatMember( + override val user: User, + override val customTitle: String? +) : AdministratorChatMember { override val canBeEdited: Boolean = true override val canChangeInfo: Boolean = true override val canPostMessages: Boolean = true diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/RawChatMember.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/RawChatMember.kt index 278f851e73..ac40748e8a 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/RawChatMember.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/RawChatMember.kt @@ -39,11 +39,13 @@ internal data class RawChatMember( @SerialName(canSendOtherMessagesField) private val canSendOtherMessages: Boolean = false, @SerialName(canAddWebPagePreviewsField) - private val canAddWebPagePreviews: Boolean = false + private val canAddWebPagePreviews: Boolean = false, + @SerialName(customTitleField) + private val customTitle: String? = null ) { val asChatMember: ChatMember by lazy { when (status) { - "creator" -> CreatorChatMember(user) + "creator" -> CreatorChatMember(user, customTitle) "administrator" -> AdministratorChatMemberImpl( user, canBeEdited, @@ -54,7 +56,8 @@ internal data class RawChatMember( canInviteUsers, canRestrictMembers, canPinMessages, - canPromoteMembers + canPromoteMembers, + customTitle ) "member" -> MemberChatMember(user) "restricted" -> RestrictedChatMember( diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/abstracts/AdministratorChatMember.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/abstracts/AdministratorChatMember.kt index 27bc3e5842..f99ce2ef25 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/abstracts/AdministratorChatMember.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatMember/abstracts/AdministratorChatMember.kt @@ -7,4 +7,5 @@ interface AdministratorChatMember : SpecialRightsChatMember { val canRemoveMessages: Boolean val canRestrictMembers: Boolean val canPromoteMembers: Boolean + val customTitle: String? } \ No newline at end of file From 5ee472305ceb68ac1ecac42fa1e41bdaf776402a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 1 Jan 2020 21:56:53 +0600 Subject: [PATCH 05/20] add restrictions check in SetChatAdministratorCustomTitle --- .../chat/members/SetChatAdministratorCustomTitle.kt | 13 +++++++++++-- .../insanusmokrassar/TelegramBotAPI/types/Common.kt | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/members/SetChatAdministratorCustomTitle.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/members/SetChatAdministratorCustomTitle.kt index 31f8aa3a62..0595455bf2 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/members/SetChatAdministratorCustomTitle.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/chat/members/SetChatAdministratorCustomTitle.kt @@ -1,12 +1,15 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.chat.members -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.types.ChatRequest -import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.requests.chat.abstracts.ChatMemberRequest import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.* import kotlinx.serialization.internal.BooleanSerializer +/** + * Representation of https://core.telegram.org/bots/api#setchatadministratorcustomtitle + * + * Please, remember about restrictions for characters in custom title + */ @Serializable data class SetChatAdministratorCustomTitle( @SerialName(chatIdField) @@ -21,4 +24,10 @@ data class SetChatAdministratorCustomTitle( get() = BooleanSerializer override val requestSerializer: SerializationStrategy<*> get() = RestrictChatMember.serializer() + + init { + if (customTitle.length !in customTitleLength) { + throw IllegalArgumentException("Custom title length must be in range $customTitleLength, but was ${customTitle.length}") + } + } } \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index be43314b59..dea85bbbdc 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -40,6 +40,8 @@ val livePeriodLimit = 60 .. 86400 val inlineQueryAnswerResultsLimit = 0 .. 50 +val customTitleLength = 0 .. 16 + const val chatIdField = "chat_id" const val messageIdField = "message_id" const val updateIdField = "update_id" From 198e15a937dd4d0f885b4556ced71c8a08e345f0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 1 Jan 2020 22:18:51 +0600 Subject: [PATCH 06/20] now ChatPhoto have two additional fields: smallFileUniqueId and bigFileUniqueId --- CHANGELOG.md | 1 + .../insanusmokrassar/TelegramBotAPI/types/ChatPhoto.kt | 10 +++++++--- .../insanusmokrassar/TelegramBotAPI/types/Common.kt | 7 +++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93e0bc469e..ec26d1aef5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 0.21.0 TelegramBotAPI 4.5 +* Now `ChatPhoto` have two additional fields: `smallFileUniqueId` and `bigFileUniqueId` * Now any administrator object instance have `customTitle` nullable field * Added the new request `SetChatAdministratorCustomTitle` to manage the custom titles of administrators promoted by the bot. diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatPhoto.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatPhoto.kt index ba6784430d..3e2e76811f 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatPhoto.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatPhoto.kt @@ -5,8 +5,12 @@ import kotlinx.serialization.Serializable @Serializable data class ChatPhoto( - @SerialName("small_file_id") + @SerialName(smallFileIdField) val smallFileId: String, - @SerialName("big_file_id") - val bigFileId: String + @SerialName(bigFileIdField) + val bigFileId: String, + @SerialName(smallFileUniqueIdField) + val smallFileUniqueId: FileUniqueId, + @SerialName(bigFileUniqueIdField) + val bigFileUniqueId: FileUniqueId ) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index dea85bbbdc..e2ec69dca0 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -18,6 +18,7 @@ typealias StartParameter = String typealias InlineMessageIdentifier = String typealias PollIdentifier = String typealias StickerSetName = String +typealias FileUniqueId = String val callbackQueryAnswerLength = 0 until 200 val captionLength = 0 until 1024 @@ -221,6 +222,12 @@ const val yShiftField = "y_shift" const val scaleField = "scale" +const val smallFileIdField = "small_file_id" +const val bigFileIdField = "big_file_id" +const val smallFileUniqueIdField = "small_file_unique_id" +const val bigFileUniqueIdField = "big_file_unique_id" + + const val currencyField = "currency" const val startParameterField = "start_parameter" const val totalAmountField = "total_amount" From bba37d88894705feb294352f4fc40ac89789034e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 1 Jan 2020 22:26:19 +0600 Subject: [PATCH 07/20] all TelegramMediaFile instances now have field fileUniqueId, which represents file_unique_id field from API --- CHANGELOG.md | 1 + .../github/insanusmokrassar/TelegramBotAPI/types/Common.kt | 2 ++ .../TelegramBotAPI/types/files/AnimationFile.kt | 4 ++++ .../insanusmokrassar/TelegramBotAPI/types/files/AudioFile.kt | 4 ++++ .../TelegramBotAPI/types/files/DocumentFile.kt | 4 ++++ .../insanusmokrassar/TelegramBotAPI/types/files/File.kt | 4 ++++ .../insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt | 4 ++++ .../insanusmokrassar/TelegramBotAPI/types/files/PhotoSize.kt | 4 ++++ .../insanusmokrassar/TelegramBotAPI/types/files/Sticker.kt | 2 ++ .../insanusmokrassar/TelegramBotAPI/types/files/VideoFile.kt | 4 ++++ .../TelegramBotAPI/types/files/VideoNoteFile.kt | 4 ++++ .../insanusmokrassar/TelegramBotAPI/types/files/VoiceFile.kt | 4 ++++ .../TelegramBotAPI/types/files/abstracts/TelegramMediaFile.kt | 2 ++ 13 files changed, 43 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec26d1aef5..137240d8d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 0.21.0 TelegramBotAPI 4.5 +* All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API * Now `ChatPhoto` have two additional fields: `smallFileUniqueId` and `bigFileUniqueId` * Now any administrator object instance have `customTitle` nullable field * Added the new request `SetChatAdministratorCustomTitle` to manage the custom titles of administrators promoted by the diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index e2ec69dca0..873e7c49de 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -227,6 +227,8 @@ const val bigFileIdField = "big_file_id" const val smallFileUniqueIdField = "small_file_unique_id" const val bigFileUniqueIdField = "big_file_unique_id" +const val fileUniqueIdField = "file_unique_id" + const val currencyField = "currency" const val startParameterField = "start_parameter" diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/AnimationFile.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/AnimationFile.kt index 2894fb044e..ced764d938 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/AnimationFile.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/AnimationFile.kt @@ -1,6 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId +import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId +import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -9,6 +11,8 @@ import kotlinx.serialization.Serializable data class AnimationFile( @SerialName(fileIdField) override val fileId: FileId, + @SerialName(fileUniqueIdField) + override val fileUniqueId: FileUniqueId, override val width: Int, override val height: Int, override val duration: Long? = null, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/AudioFile.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/AudioFile.kt index eeff93e261..70681158ac 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/AudioFile.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/AudioFile.kt @@ -2,6 +2,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.Performerable import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId +import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId +import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -10,6 +12,8 @@ import kotlinx.serialization.Serializable data class AudioFile( @SerialName(fileIdField) override val fileId: FileId, + @SerialName(fileUniqueIdField) + override val fileUniqueId: FileUniqueId, override val duration: Long? = null, override val performer: String? = null, override val title: String? = null, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/DocumentFile.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/DocumentFile.kt index 4235c29696..80034393c5 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/DocumentFile.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/DocumentFile.kt @@ -1,6 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId +import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId +import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -9,6 +11,8 @@ import kotlinx.serialization.Serializable data class DocumentFile( @SerialName(fileIdField) override val fileId: FileId, + @SerialName(fileUniqueIdField) + override val fileUniqueId: FileUniqueId, @SerialName(fileSizeField) override val fileSize: Long? = null, override val thumb: PhotoSize? = null, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/File.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/File.kt index 6badacd212..812daa78b7 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/File.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/File.kt @@ -1,6 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId +import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId +import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -9,6 +11,8 @@ import kotlinx.serialization.Serializable data class File( @SerialName(fileIdField) override val fileId: FileId, + @SerialName(fileUniqueIdField) + override val fileUniqueId: FileUniqueId, @SerialName(fileSizeField) override val fileSize: Long? = null ): TelegramMediaFile diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt index 6b85ec49ed..c2a01cab85 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PathedFile.kt @@ -1,6 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId +import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId +import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper import kotlinx.serialization.SerialName @@ -10,6 +12,8 @@ import kotlinx.serialization.Serializable data class PathedFile( @SerialName(fileIdField) override val fileId: FileId, + @SerialName(fileUniqueIdField) + override val fileUniqueId: FileUniqueId, @SerialName(filePathField) val filePath: String, @SerialName(fileSizeField) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PhotoSize.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PhotoSize.kt index 6b977bf1c3..d42e3c4669 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PhotoSize.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/PhotoSize.kt @@ -1,6 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId +import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId +import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* import kotlinx.serialization.* import kotlinx.serialization.internal.ArrayListSerializer @@ -19,6 +21,8 @@ object PhotoSerializer : KSerializer by ArrayListSerializer( data class PhotoSize( @SerialName(fileIdField) override val fileId: FileId, + @SerialName(fileUniqueIdField) + override val fileUniqueId: FileUniqueId, @SerialName(fileSizeField) override val fileSize: Long? = null, override val width: Int, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/Sticker.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/Sticker.kt index fd779dad0e..21eeb15cce 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/Sticker.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/Sticker.kt @@ -11,6 +11,8 @@ import kotlinx.serialization.Serializable data class Sticker( @SerialName(fileIdField) override val fileId: FileId, + @SerialName(fileUniqueIdField) + override val fileUniqueId: FileUniqueId, @SerialName(widthField) override val width: Int, @SerialName(heightField) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VideoFile.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VideoFile.kt index f9e7d74e87..a232dafc7a 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VideoFile.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VideoFile.kt @@ -1,6 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId +import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId +import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -9,6 +11,8 @@ import kotlinx.serialization.Serializable data class VideoFile( @SerialName(fileIdField) override val fileId: FileId, + @SerialName(fileUniqueIdField) + override val fileUniqueId: FileUniqueId, override val width: Int, override val height: Int, override val duration: Long? = null, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VideoNoteFile.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VideoNoteFile.kt index 90bfb35239..98b058acfb 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VideoNoteFile.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VideoNoteFile.kt @@ -1,6 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId +import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId +import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -9,6 +11,8 @@ import kotlinx.serialization.Serializable data class VideoNoteFile( @SerialName(fileIdField) override val fileId: FileId, + @SerialName(fileUniqueIdField) + override val fileUniqueId: FileUniqueId, @SerialName("length") override val width: Int, override val duration: Long? = null, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VoiceFile.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VoiceFile.kt index b0ee5a26a7..6bcc6226f8 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VoiceFile.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/VoiceFile.kt @@ -1,6 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId +import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId +import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -9,6 +11,8 @@ import kotlinx.serialization.Serializable data class VoiceFile( @SerialName(fileIdField) override val fileId: FileId, + @SerialName(fileUniqueIdField) + override val fileUniqueId: FileUniqueId, override val duration: Long? = null, @SerialName(mimeTypeField) override val mimeType: String? = null, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/abstracts/TelegramMediaFile.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/abstracts/TelegramMediaFile.kt index c4749caa59..67a34b9457 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/abstracts/TelegramMediaFile.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/files/abstracts/TelegramMediaFile.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId +import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId internal const val fileIdField = "file_id" internal const val fileSizeField = "file_size" @@ -11,5 +12,6 @@ internal const val filePathField = "file_path" */ interface TelegramMediaFile { val fileId: FileId + val fileUniqueId: FileUniqueId val fileSize: Long? } From e7495468a2002630536ae82ca72707c9cd1c8fcd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 2 Jan 2020 23:10:30 +0600 Subject: [PATCH 08/20] added support of strikethrough and underline --- CHANGELOG.md | 4 ++++ .../types/MessageEntity/RawMessageEntity.kt | 5 ++++- .../MessageEntity/StrikethroughMessageEntity.kt | 10 ++++++++++ .../types/MessageEntity/UnderlineMessageEntity.kt | 10 ++++++++++ .../textsources/StrikethroughTextSource.kt | 12 ++++++++++++ .../textsources/UnderlineTextSource.kt | 12 ++++++++++++ .../TelegramBotAPI/utils/StringFormatting.kt | 13 +++++++++++++ 7 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 137240d8d6..d693385ccf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ bot. ## 0.20.0 MPP Migration +* Added support of strikethrough and underline + * Added `UnderlineTextSource` and `UnderlineMessageEntity` + * Added `StrikethroughTextSource` and `StrikethroughMessageEntity` + * Added support in `RawMessageEntity` * Time library change: `joda-time` -> `com.soywiz.korlibs.klock:klock` * `Currencied` now using as `currency` value with type `String` * For `Java` there is `Currencied#javaCurrency` extension function, which will give an old currency work way diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt index 489531935c..83fc584dd5 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.User +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable import kotlinx.serialization.internal.ArrayListSerializer @@ -28,7 +29,9 @@ internal data class RawMessageEntity( "code" -> CodeTextMessageEntity(offset, length, sourceSubstring) "pre" -> PreTextMessageEntity(offset, length, sourceSubstring) "text_link" -> TextLinkMessageEntity(offset, length, sourceSubstring, url ?: throw IllegalStateException("URL must not be null for text link")) - "text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention")) + "text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user as PrivateChat) + "underline" -> UnderlineMessageEntity(offset, length, sourceSubstring) + "strikethrough" -> StrikethroughMessageEntity(offset, length, sourceSubstring) else -> throw IllegalArgumentException("Unknown type of message entity") } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt new file mode 100644 index 0000000000..494980e8d0 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt @@ -0,0 +1,10 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.StrikethroughTextSource + +class StrikethroughMessageEntity( + override val offset: Int, + override val length: Int, + override val sourceString: String +) : MessageEntity, TextSource by StrikethroughTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt new file mode 100644 index 0000000000..7f630187cb --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt @@ -0,0 +1,10 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.UnderlineTextSource + +class UnderlineMessageEntity( + override val offset: Int, + override val length: Int, + override val sourceString: String +) : MessageEntity, TextSource by UnderlineTextSource(sourceString) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt new file mode 100644 index 0000000000..2cd6305be3 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughMarkdown + +class StrikethroughTextSource( + sourceString: String +) : TextSource { + override val asHtmlSource: String = sourceString.strikethroughHTML() + override val asMarkdownSource: String = sourceString.strikethroughMarkdown() +} \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt new file mode 100644 index 0000000000..f0d6ed4904 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt @@ -0,0 +1,12 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineHTML +import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineMarkdown + +class UnderlineTextSource( + sourceString: String +) : TextSource { + override val asMarkdownSource: String = sourceString.underlineMarkdown() + override val asHtmlSource: String = sourceString.underlineHTML() +} \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt index 1eb31850c4..7a1c6e7238 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt @@ -41,6 +41,19 @@ fun String.preHTML(): String = htmlDefault(htmlPreControl) fun String.emailMarkdown(): String = linkMarkdown("mailto://$this") fun String.emailHTML(): String = linkHTML("mailto://$this") +/** + * Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct + */ +fun String.strikethroughMarkdown(): String = map { it + "\u0336" }.joinToString("") +fun String.strikethroughHTML(): String = htmlDefault("s") + + +/** + * Crutch for support of underline in default markdown. Simply add modifier, but it will not look like correct + */ +fun String.underlineMarkdown(): String = map { it + "\u0347" }.joinToString("") +fun String.underlineHTML(): String = htmlDefault("u") + private inline infix fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) { this From 482d924070ab19ac3776bf46b35e54534f18313c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 2 Jan 2020 23:55:26 +0600 Subject: [PATCH 09/20] refactor and preparing to multilevel message entities --- CHANGELOG.md | 15 +++++++++---- .../CommonAbstracts/TextSource.kt | 1 + .../MessageEntity/BoldTextMessageEntity.kt | 4 ++-- .../MessageEntity/BotCommandMessageEntity.kt | 4 ++-- .../MessageEntity/CodeTextMessageEntity.kt | 4 ++-- .../types/MessageEntity/EMailMessageEntity.kt | 4 ++-- .../MessageEntity/HashTagMessageEntity.kt | 4 ++-- .../MessageEntity/ItalicTextMessageEntity.kt | 4 ++-- .../MessageEntity/MentionMessageEntity.kt | 4 ++-- .../types/MessageEntity/MessageEntity.kt | 2 ++ .../MessageEntity/PhoneNumberMessageEntity.kt | 4 ++-- .../MessageEntity/PreTextMessageEntity.kt | 4 ++-- .../types/MessageEntity/RawMessageEntity.kt | 2 +- .../MessageEntity/RegularTextMessageEntity.kt | 4 ++-- .../StrikethroughMessageEntity.kt | 4 ++-- .../MessageEntity/TextLinkMessageEntity.kt | 4 ++-- .../MessageEntity/TextMentionMessageEntity.kt | 12 ++-------- .../types/MessageEntity/URLMessageEntity.kt | 7 +++--- .../MessageEntity/UnderlineMessageEntity.kt | 4 ++-- .../textsources/BoldTextSource.kt | 8 ++++--- .../textsources/BotCommandTextSource.kt | 10 +++++---- .../textsources/CodeTextSource.kt | 8 ++++--- .../textsources/EMailTextSource.kt | 8 ++++--- .../textsources/HashTagTextSource.kt | 8 ++++--- .../textsources/ItalicTextSource.kt | 8 ++++--- .../textsources/MentionTextSource.kt | 8 ++++--- .../textsources/PhoneNumberTextSource.kt | 8 ++++--- .../textsources/PreTextSource.kt | 8 ++++--- .../textsources/RegularTextSource.kt | 8 ++++--- .../textsources/StrikethroughTextSource.kt | 8 ++++--- .../textsources/TextLinkTextSource.kt | 6 ++--- .../textsources/TextMentionTextSource.kt | 12 +++------- .../textsources/URLTextSource.kt | 8 ++++--- .../textsources/UnderlineTextSource.kt | 8 ++++--- .../TelegramBotAPI/utils/StringFormatting.kt | 22 ++++++++++--------- .../TelegramBotAPI/utils/extensions/String.kt | 6 +++++ 36 files changed, 137 insertions(+), 106 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d693385ccf..8dac4d4575 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ ## 0.21.0 TelegramBotAPI 4.5 +* Added support of strikethrough and underline + * Added `UnderlineTextSource` and `UnderlineMessageEntity` + * Added `StrikethroughTextSource` and `StrikethroughMessageEntity` + * Added support in `RawMessageEntity` +* Now `TextSource` have its `rawSource` const + * `sourceString` in `MessageEntity` now is deprecated + * All `MessageEntity` classes now have no income parameter `sourceString` + * In most part of `TextSource` classes `asMarkdownSource` and `asHtmlSource` now are getters instead if fields + * All parseMode-specific functions in `StringFormatting` now are not `infix` +* Removed constructor of `TextMentionMessageEntity`, which was deprecated previously +* Removed constructor of `TextMentionTextSource`, which was deprecated previously * All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API * Now `ChatPhoto` have two additional fields: `smallFileUniqueId` and `bigFileUniqueId` * Now any administrator object instance have `customTitle` nullable field @@ -11,10 +22,6 @@ bot. ## 0.20.0 MPP Migration -* Added support of strikethrough and underline - * Added `UnderlineTextSource` and `UnderlineMessageEntity` - * Added `StrikethroughTextSource` and `StrikethroughMessageEntity` - * Added support in `RawMessageEntity` * Time library change: `joda-time` -> `com.soywiz.korlibs.klock:klock` * `Currencied` now using as `currency` value with type `String` * For `Java` there is `Currencied#javaCurrency` extension function, which will give an old currency work way diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt index 6a40155809..0369f0dbc8 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts interface TextSource { + val rawSource: String val asMarkdownSource: String val asHtmlSource: String } \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt index 2e02d6b935..ef5b0dbc96 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt @@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown data class BoldTextMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by BoldTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by BoldTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt index 088956a44b..8e755225db 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt @@ -8,8 +8,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.commandMarkdown data class BotCommandMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String, - private val botCommandTextSource: BotCommandTextSource = BotCommandTextSource(sourceString) + override val rawSource: String, + private val botCommandTextSource: BotCommandTextSource = BotCommandTextSource(rawSource) ) : MessageEntity, TextSource by botCommandTextSource { val command: String get() = botCommandTextSource.command diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt index bb45546d75..6b65c2e0a7 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt @@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown data class CodeTextMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by CodeTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by CodeTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt index f5456fbdea..342bd7b5d4 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt @@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown data class EMailMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by EMailTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by EMailTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt index 63733e5d8a..65dd2c2ac1 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt @@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown data class HashTagMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by HashTagTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by HashTagTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt index db25785295..38d9510bb3 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt @@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown data class ItalicTextMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by ItalicTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by ItalicTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt index 9886f4592e..de484c6925 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt @@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown class MentionMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by MentionTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by MentionTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt index 79a68f9997..0021ec72a1 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt @@ -5,5 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource interface MessageEntity : TextSource { val offset: Int val length: Int + @Deprecated("Due to opportunity to get the same string from rawSource const", ReplaceWith("rawSource")) val sourceString: String + get() = rawSource } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt index 79a5fb5357..dfc3710bab 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt @@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown data class PhoneNumberMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by PhoneNumberTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by PhoneNumberTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt index 80943d942b..b28c189c26 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt @@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown data class PreTextMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by PreTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by PreTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt index 83fc584dd5..b732fcb1dc 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt @@ -29,7 +29,7 @@ internal data class RawMessageEntity( "code" -> CodeTextMessageEntity(offset, length, sourceSubstring) "pre" -> PreTextMessageEntity(offset, length, sourceSubstring) "text_link" -> TextLinkMessageEntity(offset, length, sourceSubstring, url ?: throw IllegalStateException("URL must not be null for text link")) - "text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user as PrivateChat) + "text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention")) "underline" -> UnderlineMessageEntity(offset, length, sourceSubstring) "strikethrough" -> StrikethroughMessageEntity(offset, length, sourceSubstring) else -> throw IllegalArgumentException("Unknown type of message entity") diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt index b7e683f80a..b9834832d0 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt @@ -8,5 +8,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown data class RegularTextMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by RegularTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by RegularTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt index 494980e8d0..315abd4309 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt @@ -6,5 +6,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsource class StrikethroughMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by StrikethroughTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by StrikethroughTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt index 576f8043bc..4a8f704ded 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt @@ -8,6 +8,6 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown data class TextLinkMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String, + override val rawSource: String, val url: String -) : MessageEntity, TextSource by TextLinkTextSource(sourceString, url) +) : MessageEntity, TextSource by TextLinkTextSource(rawSource, url) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt index ad7334e9af..7a4f8aaa89 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt @@ -10,14 +10,6 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown class TextMentionMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String, + override val rawSource: String, val privateChat: PrivateChat -) : MessageEntity, TextSource by TextMentionTextSource(sourceString, privateChat) { - @Deprecated("Deprecated due to the fact that there is more common constructor") - constructor( - offset: Int, - length: Int, - sourceString: String, - user: User - ) : this(offset, length, sourceString, user as PrivateChat) -} +) : MessageEntity, TextSource by TextMentionTextSource(rawSource, privateChat) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt index 482ad28fe6..61df04059b 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt @@ -8,7 +8,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown data class URLMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by URLTextSource(sourceString) { - val url: String = sourceString + override val rawSource: String +) : MessageEntity, TextSource by URLTextSource(rawSource) { + val url: String + get() = rawSource } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt index 7f630187cb..29519fdf64 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt @@ -6,5 +6,5 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsource class UnderlineMessageEntity( override val offset: Int, override val length: Int, - override val sourceString: String -) : MessageEntity, TextSource by UnderlineTextSource(sourceString) + override val rawSource: String +) : MessageEntity, TextSource by UnderlineTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt index 9d1a1f7d7e..fda182dc83 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.boldHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown class BoldTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.boldMarkdown() - override val asHtmlSource: String = sourceString.boldHTML() + override val asMarkdownSource: String + get() = rawSource.boldMarkdown() + override val asHtmlSource: String + get() = rawSource.boldHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt index 2c6eb4e0a3..0eb749548f 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt @@ -7,12 +7,14 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.commandMarkdown private val commandRegex = Regex("[/!][^@\\s]*") class BotCommandTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.commandMarkdown() - override val asHtmlSource: String = sourceString.commandHTML() + override val asMarkdownSource: String + get() = rawSource.commandMarkdown() + override val asHtmlSource: String + get() = rawSource.commandHTML() val command: String by lazy { - commandRegex.find(sourceString) ?.value ?.substring(1) ?: sourceString.substring(1)// skip first symbol like "/" or "!" + commandRegex.find(rawSource) ?.value ?.substring(1) ?: rawSource.substring(1)// skip first symbol like "/" or "!" } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt index ab0066a60b..0ff5d3334a 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.codeHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown class CodeTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.codeMarkdown() - override val asHtmlSource: String = sourceString.codeHTML() + override val asMarkdownSource: String + get() = rawSource.codeMarkdown() + override val asHtmlSource: String + get() = rawSource.codeHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt index 34b5ed34ce..135b31df98 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.emailHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown class EMailTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.emailMarkdown() - override val asHtmlSource: String = sourceString.emailHTML() + override val asMarkdownSource: String + get() = rawSource.emailMarkdown() + override val asHtmlSource: String + get() = rawSource.emailHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt index 4894fabecf..fb5302d769 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown class HashTagTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.hashTagMarkdown() - override val asHtmlSource: String = sourceString.hashTagHTML() + override val asMarkdownSource: String + get() = rawSource.hashTagMarkdown() + override val asHtmlSource: String + get() = rawSource.hashTagHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt index 2e1d1170e0..2027728f64 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.italicHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown class ItalicTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.italicMarkdown() - override val asHtmlSource: String = sourceString.italicHTML() + override val asMarkdownSource: String + get() = rawSource.italicMarkdown() + override val asHtmlSource: String + get() = rawSource.italicHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt index 8d2b0cc71a..a865ef6df4 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown class MentionTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.mentionMarkdown() - override val asHtmlSource: String = sourceString.mentionHTML() + override val asMarkdownSource: String + get() = rawSource.mentionMarkdown() + override val asHtmlSource: String + get() = rawSource.mentionHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt index f5004fa7d5..d9948a3ea2 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown class PhoneNumberTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.phoneMarkdown() - override val asHtmlSource: String = sourceString.phoneHTML() + override val asMarkdownSource: String + get() = rawSource.phoneMarkdown() + override val asHtmlSource: String + get() = rawSource.phoneHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt index 7a0fea96dc..dd8abe9637 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.preHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown class PreTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.preMarkdown() - override val asHtmlSource: String = sourceString.preHTML() + override val asMarkdownSource: String + get() = rawSource.preMarkdown() + override val asHtmlSource: String + get() = rawSource.preHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt index 0125346add..e35c6bd53b 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown class RegularTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.toMarkdown() - override val asHtmlSource: String = sourceString.toHtml() + override val asMarkdownSource: String + get() = rawSource.toMarkdown() + override val asHtmlSource: String + get() = rawSource.toHtml() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt index 2cd6305be3..fa7d3b74cd 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughMarkdown class StrikethroughTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asHtmlSource: String = sourceString.strikethroughHTML() - override val asMarkdownSource: String = sourceString.strikethroughMarkdown() + override val asHtmlSource: String + get() = rawSource.strikethroughHTML() + override val asMarkdownSource: String + get() = rawSource.strikethroughMarkdown() } \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt index a9e7ae225a..7356ac0b31 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt @@ -5,9 +5,9 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown class TextLinkTextSource( - sourceString: String, + override val rawSource: String, url: String ) : TextSource { - override val asMarkdownSource: String = sourceString.linkMarkdown(url) - override val asHtmlSource: String = sourceString.linkHTML(url) + override val asMarkdownSource: String = rawSource.linkMarkdown(url) + override val asHtmlSource: String = rawSource.linkHTML(url) } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt index 720436e8d7..3cc4643d55 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt @@ -7,15 +7,9 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown class TextMentionTextSource( - sourceString: String, + override val rawSource: String, privateChat: PrivateChat ) : TextSource { - @Deprecated("Deprecated due to the fact that there is more common constructor") - constructor( - sourceString: String, - user: User - ) : this(sourceString, user as PrivateChat) - - override val asMarkdownSource: String = sourceString.mentionMarkdown(privateChat.id) - override val asHtmlSource: String = sourceString.mentionHTML(privateChat.id) + override val asMarkdownSource: String = rawSource.mentionMarkdown(privateChat.id) + override val asHtmlSource: String = rawSource.mentionHTML(privateChat.id) } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt index 581af0f015..f96a169373 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown class URLTextSource( - sourceString: String + override val rawSource: String ) : TextSource{ - override val asMarkdownSource: String = sourceString.linkMarkdown(sourceString) - override val asHtmlSource: String = sourceString.linkHTML(sourceString) + override val asMarkdownSource: String + get() = rawSource.linkMarkdown(rawSource) + override val asHtmlSource: String + get() = rawSource.linkHTML(rawSource) } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt index f0d6ed4904..d7cef33cdc 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt @@ -5,8 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineHTML import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineMarkdown class UnderlineTextSource( - sourceString: String + override val rawSource: String ) : TextSource { - override val asMarkdownSource: String = sourceString.underlineMarkdown() - override val asHtmlSource: String = sourceString.underlineHTML() + override val asMarkdownSource: String + get() = rawSource.underlineMarkdown() + override val asHtmlSource: String + get() = rawSource.underlineHTML() } \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt index 7a1c6e7238..d9c5266817 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt @@ -14,12 +14,14 @@ const val htmlBoldControl = "b" const val htmlItalicControl = "i" const val htmlCodeControl = "code" const val htmlPreControl = "pre" +const val htmlUnderlineControl = "u" +const val htmlStrikethroughControl = "s" -private infix fun String.markdownDefault(controlSymbol: String) = "$controlSymbol$this$controlSymbol" -private infix fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>$this" +private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol$this$controlSymbol" +private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>$this" -infix fun String.linkMarkdown(link: String): String = "[$this]($link)" -infix fun String.linkHTML(link: String): String = "$this" +fun String.linkMarkdown(link: String): String = "[$this]($link)" +fun String.linkHTML(link: String): String = "$this" fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl) @@ -45,32 +47,32 @@ fun String.emailHTML(): String = linkHTML("mailto://$this") * Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct */ fun String.strikethroughMarkdown(): String = map { it + "\u0336" }.joinToString("") -fun String.strikethroughHTML(): String = htmlDefault("s") +fun String.strikethroughHTML(): String = htmlDefault(htmlStrikethroughControl) /** * Crutch for support of underline in default markdown. Simply add modifier, but it will not look like correct */ fun String.underlineMarkdown(): String = map { it + "\u0347" }.joinToString("") -fun String.underlineHTML(): String = htmlDefault("u") +fun String.underlineHTML(): String = htmlDefault(htmlUnderlineControl) -private inline infix fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) { +private inline fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) { this } else { "@${adapt()}" } -private inline infix fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) { +private inline fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) { this } else { "#${adapt()}" } -infix fun String.mentionMarkdown(userId: UserId): String = linkMarkdown(userId.link) -infix fun String.mentionHTML(userId: UserId): String = linkHTML(userId.link) +fun String.mentionMarkdown(userId: UserId): String = linkMarkdown(userId.link) +fun String.mentionHTML(userId: UserId): String = linkHTML(userId.link) fun String.mentionMarkdown(): String = mention(String::toMarkdown) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt index 9c2c4d9c33..040c6aa846 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt @@ -7,6 +7,12 @@ fun String.toMarkdown(): String { ).replace( "_", "\\_" + ).replace( + "`", + "\\`" + ).replace( + "[", + "\\[" ) } From f68edebd24190ad58f65555b2fb5a3c8bc46c9bd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 2 Jan 2020 23:58:16 +0600 Subject: [PATCH 10/20] RawMessageEntity will not throw exception --- CHANGELOG.md | 2 ++ .../TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dac4d4575..43da309d6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ * All `MessageEntity` classes now have no income parameter `sourceString` * In most part of `TextSource` classes `asMarkdownSource` and `asHtmlSource` now are getters instead if fields * All parseMode-specific functions in `StringFormatting` now are not `infix` +* Now will not be thrown exception when there is income unknown type of `MessageEntity`. Instead of this will be +created `RegularTextMessageEntity` with the same text * Removed constructor of `TextMentionMessageEntity`, which was deprecated previously * Removed constructor of `TextMentionTextSource`, which was deprecated previously * All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt index b732fcb1dc..1d5ceac9a7 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt @@ -32,7 +32,7 @@ internal data class RawMessageEntity( "text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention")) "underline" -> UnderlineMessageEntity(offset, length, sourceSubstring) "strikethrough" -> StrikethroughMessageEntity(offset, length, sourceSubstring) - else -> throw IllegalArgumentException("Unknown type of message entity") + else -> RegularTextMessageEntity(offset, length, sourceSubstring) } } } From dbef69ffac2e8957ae021356037a5fce0b4a0370 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Jan 2020 00:04:49 +0600 Subject: [PATCH 11/20] fixed problem that usually string formatting did not trigger escaping of control characters --- CHANGELOG.md | 1 + .../textsources/RegularTextSource.kt | 8 ++--- .../TelegramBotAPI/utils/StringFormatting.kt | 30 +++++++++++-------- .../TelegramBotAPI/utils/extensions/String.kt | 3 -- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43da309d6c..32eb165f59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * All parseMode-specific functions in `StringFormatting` now are not `infix` * Now will not be thrown exception when there is income unknown type of `MessageEntity`. Instead of this will be created `RegularTextMessageEntity` with the same text +* Fixed problem that usually string formatting did not trigger escaping of control characters * Removed constructor of `TextMentionMessageEntity`, which was deprecated previously * Removed constructor of `TextMentionTextSource`, which was deprecated previously * All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt index e35c6bd53b..b8e236ea9d 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt @@ -1,14 +1,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.utils.regularHtml +import com.github.insanusmokrassar.TelegramBotAPI.utils.regularMarkdown class RegularTextSource( override val rawSource: String ) : TextSource { override val asMarkdownSource: String - get() = rawSource.toMarkdown() + get() = rawSource.regularMarkdown() override val asHtmlSource: String - get() = rawSource.toHtml() + get() = rawSource.regularHtml() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt index d9c5266817..b9caf45f3f 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt @@ -17,11 +17,11 @@ const val htmlPreControl = "pre" const val htmlUnderlineControl = "u" const val htmlStrikethroughControl = "s" -private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol$this$controlSymbol" -private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>$this" +private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol${toMarkdown()}$controlSymbol" +private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>${toHtml()}" -fun String.linkMarkdown(link: String): String = "[$this]($link)" -fun String.linkHTML(link: String): String = "$this" +fun String.linkMarkdown(link: String): String = "[${toMarkdown()}]($link)" +fun String.linkHTML(link: String): String = "${toHtml()}" fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl) @@ -40,8 +40,8 @@ fun String.preMarkdown(): String = markdownDefault(markdownPreControl) fun String.preHTML(): String = htmlDefault(htmlPreControl) -fun String.emailMarkdown(): String = linkMarkdown("mailto://$this") -fun String.emailHTML(): String = linkHTML("mailto://$this") +fun String.emailMarkdown(): String = linkMarkdown("mailto://$${toMarkdown()}") +fun String.emailHTML(): String = linkHTML("mailto://$${toHtml()}") /** * Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct @@ -58,14 +58,14 @@ fun String.underlineHTML(): String = htmlDefault(htmlUnderlineControl) private inline fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) { - this + adapt() } else { "@${adapt()}" } private inline fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) { - this + adapt() } else { "#${adapt()}" } @@ -87,14 +87,18 @@ fun String.phoneMarkdown(): String = toMarkdown() fun String.phoneHTML(): String = toHtml() -fun String.command(): String = if (startsWith("/")) { - this +fun String.command(adapt: String.() -> String): String = if (startsWith("/")) { + adapt() } else { - "/$this" + "/${adapt()}" } -fun String.commandMarkdown(): String = command() -fun String.commandHTML(): String = command() +fun String.commandMarkdown(): String = command(String::toMarkdown) +fun String.commandHTML(): String = command(String::toHtml) + + +fun String.regularMarkdown(): String = toMarkdown() +fun String.regularHtml(): String = toHtml() infix fun String.bold(parseMode: ParseMode): String = when (parseMode) { diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt index 040c6aa846..7d5457953c 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt @@ -25,7 +25,4 @@ fun String.toHtml(): String = replace( ).replace( "&", "&" -).replace( - "\"", - """ ) From 398adf06ff0f312829f7f73ae3c251d8181a6fca Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Jan 2020 00:27:24 +0600 Subject: [PATCH 12/20] Actualized work with pre type of text - now it is possible to use language for formatting of text --- CHANGELOG.md | 1 + .../TelegramBotAPI/utils/StringFormatting.kt | 29 ++++++++++++--- .../utils/StringFormattingTests.kt | 36 +++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormattingTests.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 32eb165f59..ffd979eea4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Now will not be thrown exception when there is income unknown type of `MessageEntity`. Instead of this will be created `RegularTextMessageEntity` with the same text * Fixed problem that usually string formatting did not trigger escaping of control characters +* Actualized work with `pre` type of text - now it is possible to use `language` for formatting of text * Removed constructor of `TextMentionMessageEntity`, which was deprecated previously * Removed constructor of `TextMentionTextSource`, which was deprecated previously * All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt index b9caf45f3f..175fcf0569 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt @@ -17,8 +17,14 @@ const val htmlPreControl = "pre" const val htmlUnderlineControl = "u" const val htmlStrikethroughControl = "s" -private fun String.markdownDefault(controlSymbol: String) = "$controlSymbol${toMarkdown()}$controlSymbol" -private fun String.htmlDefault(controlSymbol: String) = "<$controlSymbol>${toHtml()}" +private fun String.markdownDefault( + openControlSymbol: String, + closeControlSymbol: String = openControlSymbol +) = "$openControlSymbol${toMarkdown()}$closeControlSymbol" +private fun String.htmlDefault( + openControlSymbol: String, + closeControlSymbol: String = openControlSymbol +) = "<$openControlSymbol>${toHtml()}" fun String.linkMarkdown(link: String): String = "[${toMarkdown()}]($link)" fun String.linkHTML(link: String): String = "${toHtml()}" @@ -36,8 +42,18 @@ fun String.codeMarkdown(): String = markdownDefault(markdownCodeControl) fun String.codeHTML(): String = htmlDefault(htmlCodeControl) -fun String.preMarkdown(): String = markdownDefault(markdownPreControl) -fun String.preHTML(): String = htmlDefault(htmlPreControl) +fun String.preMarkdown(language: String? = null): String = markdownDefault( + "$markdownPreControl${language ?.let { "$it\n" } ?: "\n"}", + "\n$markdownPreControl" +) +fun String.preHTML(language: String? = null): String = htmlDefault( + language ?.let { _ -> + "$htmlPreControl><$htmlCodeControl class=\"language-$language\"" + } ?: htmlPreControl, + language ?.let { _ -> + "$htmlCodeControl> preMarkdown() } +fun String.pre(parseMode: ParseMode, language: String? = null): String = when (parseMode) { + is HTML -> preHTML(language) + is Markdown -> preMarkdown(language) +} + infix fun String.email(parseMode: ParseMode): String = when (parseMode) { is HTML -> emailHTML() is Markdown -> emailMarkdown() diff --git a/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormattingTests.kt b/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormattingTests.kt new file mode 100644 index 0000000000..48a7db2ccf --- /dev/null +++ b/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormattingTests.kt @@ -0,0 +1,36 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import kotlin.test.Test +import kotlin.test.assertEquals + +class StringFormattingTests { + @Test + fun testThatPreEscapingWorksCorrectly() { + val originalHelloWorld = """ + fun main() { + println("Hello world") + } + """.replace(" ", "") + val helloWorldLanguage = "kotlin" + assertEquals( + "
$originalHelloWorld
", + originalHelloWorld.preHTML( + helloWorldLanguage + ) + ) + assertEquals( + "
$originalHelloWorld
", + originalHelloWorld.preHTML() + ) + assertEquals( + "```$helloWorldLanguage\n$originalHelloWorld\n```", + originalHelloWorld.preMarkdown( + helloWorldLanguage + ) + ) + assertEquals( + "```\n$originalHelloWorld\n```", + originalHelloWorld.preMarkdown() + ) + } +} From 72127ce13397f6cacfb523a004a24cd997d57e1f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 5 Jan 2020 19:24:17 +0600 Subject: [PATCH 13/20] rework in part of text message entities --- .../CommonAbstracts/Captioned.kt | 6 +- .../CommonAbstracts/TextSource.kt | 14 +- .../MessageEntity/BoldTextMessageEntity.kt | 12 -- .../MessageEntity/BotCommandMessageEntity.kt | 16 -- .../MessageEntity/CodeTextMessageEntity.kt | 12 -- .../types/MessageEntity/EMailMessageEntity.kt | 12 -- .../MessageEntity/HashTagMessageEntity.kt | 12 -- .../MessageEntity/ItalicTextMessageEntity.kt | 12 -- .../MessageEntity/MentionMessageEntity.kt | 12 -- .../types/MessageEntity/MessageEntity.kt | 11 -- .../MessageEntity/PhoneNumberMessageEntity.kt | 12 -- .../MessageEntity/PreTextMessageEntity.kt | 12 -- .../types/MessageEntity/RawMessageEntity.kt | 97 ++++++++---- .../MessageEntity/RegularTextMessageEntity.kt | 12 -- .../StrikethroughMessageEntity.kt | 10 -- .../MessageEntity/TextLinkMessageEntity.kt | 13 -- .../MessageEntity/TextMentionMessageEntity.kt | 15 -- .../types/MessageEntity/URLMessageEntity.kt | 15 -- .../MessageEntity/UnderlineMessageEntity.kt | 10 -- .../textsources/BoldTextSource.kt | 19 +-- .../textsources/BotCommandTextSource.kt | 27 ++-- .../textsources/CodeTextSource.kt | 13 +- .../textsources/EMailTextSource.kt | 18 +-- .../textsources/HashTagTextSource.kt | 27 ++-- .../textsources/ItalicTextSource.kt | 19 +-- .../textsources/MentionTextSource.kt | 29 ++-- .../textsources/PhoneNumberTextSource.kt | 18 +-- .../textsources/PreTextSource.kt | 13 +- .../textsources/RegularTextSource.kt | 12 +- .../textsources/StrikethroughTextSource.kt | 19 +-- .../textsources/TextLinkTextSource.kt | 10 +- .../textsources/TextMentionTextSource.kt | 19 +-- .../textsources/URLTextSource.kt | 14 +- .../textsources/UnderlineTextSource.kt | 19 +-- .../types/ParseMode/ParseMode.kt | 7 + .../TelegramBotAPI/types/games/Game.kt | 6 +- .../TelegramBotAPI/types/games/RawGame.kt | 3 +- .../types/message/RawMessage.kt | 7 +- .../types/message/content/TextContent.kt | 10 +- .../message/content/media/AnimationContent.kt | 4 +- .../message/content/media/AudioContent.kt | 4 +- .../message/content/media/DocumentContent.kt | 4 +- .../message/content/media/PhotoContent.kt | 4 +- .../message/content/media/VideoContent.kt | 4 +- .../message/content/media/VoiceContent.kt | 4 +- .../utils/CaptionAndTextSourcesToText.kt | 98 ++++++++++++ .../TelegramBotAPI/utils/Captions.kt | 77 ---------- .../utils/HtmlCaptionSourcer.kt | 24 --- .../utils/MarkdownCaptionSourcer.kt | 24 --- .../utils/MultilevelTextSourceFormatting.kt | 137 +++++++++++++++++ .../TelegramBotAPI/utils/StringFormatting.kt | 123 ++++++++++----- .../TelegramBotAPI/utils/extensions/String.kt | 27 ++++ .../MessageEntity/TextPartsCreatingTests.kt | 142 ++++++++++++++++++ 53 files changed, 760 insertions(+), 540 deletions(-) delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt create mode 100644 src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextPartsCreatingTests.kt diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/Captioned.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/Captioned.kt index 7a80c885a1..b7755a370f 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/Captioned.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/Captioned.kt @@ -1,7 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode +import com.github.insanusmokrassar.TelegramBotAPI.utils.fullListOfSubSource interface Captioned { val caption: String? @@ -12,5 +12,7 @@ interface CaptionedOutput : Captioned { } interface CaptionedInput : Captioned { - val captionEntities: List + val captionEntities: List } + +fun CaptionedInput.fullEntitiesList() = caption ?.fullListOfSubSource(captionEntities) ?.map { it.source } ?: emptyList() diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt index 0369f0dbc8..be70f1032e 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/CommonAbstracts/TextSource.kt @@ -1,7 +1,17 @@ package com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts interface TextSource { - val rawSource: String val asMarkdownSource: String + val asMarkdownV2Source: String val asHtmlSource: String -} \ No newline at end of file +} + + +interface MultilevelTextSource : TextSource { + val textParts: List +} + +data class TextPart( + val range: IntRange, + val source: TextSource +) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt deleted file mode 100644 index ef5b0dbc96..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BoldTextMessageEntity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BoldTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.boldHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown - -data class BoldTextMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by BoldTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt deleted file mode 100644 index 8e755225db..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/BotCommandMessageEntity.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BotCommandTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.commandHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.commandMarkdown - -data class BotCommandMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String, - private val botCommandTextSource: BotCommandTextSource = BotCommandTextSource(rawSource) -) : MessageEntity, TextSource by botCommandTextSource { - val command: String - get() = botCommandTextSource.command -} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt deleted file mode 100644 index 6b65c2e0a7..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/CodeTextMessageEntity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.CodeTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.codeHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown - -data class CodeTextMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by CodeTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt deleted file mode 100644 index 342bd7b5d4..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/EMailMessageEntity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.EMailTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.emailHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown - -data class EMailMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by EMailTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt deleted file mode 100644 index 65dd2c2ac1..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/HashTagMessageEntity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.HashTagTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown - -data class HashTagMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by HashTagTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt deleted file mode 100644 index 38d9510bb3..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/ItalicTextMessageEntity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.ItalicTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.italicHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown - -data class ItalicTextMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by ItalicTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt deleted file mode 100644 index de484c6925..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MentionMessageEntity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.MentionTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown - -class MentionMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by MentionTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt deleted file mode 100644 index 0021ec72a1..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/MessageEntity.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource - -interface MessageEntity : TextSource { - val offset: Int - val length: Int - @Deprecated("Due to opportunity to get the same string from rawSource const", ReplaceWith("rawSource")) - val sourceString: String - get() = rawSource -} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt deleted file mode 100644 index dfc3710bab..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PhoneNumberMessageEntity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.PhoneNumberTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown - -data class PhoneNumberMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by PhoneNumberTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt deleted file mode 100644 index b28c189c26..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/PreTextMessageEntity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.PreTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.preHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown - -data class PreTextMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by PreTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt index 1d5ceac9a7..bdbfbb3f77 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt @@ -1,10 +1,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.* import com.github.insanusmokrassar.TelegramBotAPI.types.User -import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat -import kotlinx.serialization.KSerializer +import com.github.insanusmokrassar.TelegramBotAPI.utils.shiftSourcesToTheLeft import kotlinx.serialization.Serializable -import kotlinx.serialization.internal.ArrayListSerializer @Serializable internal data class RawMessageEntity( @@ -13,32 +14,74 @@ internal data class RawMessageEntity( val length: Int, val url: String? = null, val user: User? = null -) { - fun asMessageEntity(source: String): MessageEntity { - val sourceSubstring = source.substring(offset, offset + length) - return when (type) { - "mention" -> MentionMessageEntity(offset, length, sourceSubstring) - "hashtag" -> HashTagMessageEntity(offset, length, sourceSubstring) - "cashtag" -> TODO() - "bot_command" -> BotCommandMessageEntity(offset, length, sourceSubstring) - "url" -> URLMessageEntity(offset, length, sourceSubstring) - "email" -> EMailMessageEntity(offset, length, sourceSubstring) - "phone_number" -> PhoneNumberMessageEntity(offset, length, sourceSubstring) - "bold" -> BoldTextMessageEntity(offset, length, sourceSubstring) - "italic" -> ItalicTextMessageEntity(offset, length, sourceSubstring) - "code" -> CodeTextMessageEntity(offset, length, sourceSubstring) - "pre" -> PreTextMessageEntity(offset, length, sourceSubstring) - "text_link" -> TextLinkMessageEntity(offset, length, sourceSubstring, url ?: throw IllegalStateException("URL must not be null for text link")) - "text_mention" -> TextMentionMessageEntity(offset, length, sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention")) - "underline" -> UnderlineMessageEntity(offset, length, sourceSubstring) - "strikethrough" -> StrikethroughMessageEntity(offset, length, sourceSubstring) - else -> RegularTextMessageEntity(offset, length, sourceSubstring) +) + +internal fun RawMessageEntity.asTextParts(source: String, subParts: List): List { + val sourceSubstring = source.substring(offset, offset + length) + val range = offset until (offset + length) + val shiftedSubParts = subParts.shiftSourcesToTheLeft(offset) + return when (type) { + "mention" -> MentionTextSource(sourceSubstring, shiftedSubParts) + "hashtag" -> HashTagTextSource(sourceSubstring, shiftedSubParts) + "cashtag" -> TODO() + "bot_command" -> BotCommandTextSource(sourceSubstring, shiftedSubParts) + "url" -> URLTextSource(sourceSubstring) + "email" -> EMailTextSource(sourceSubstring, shiftedSubParts) + "phone_number" -> PhoneNumberTextSource(sourceSubstring, shiftedSubParts) + "bold" -> BoldTextSource(sourceSubstring, shiftedSubParts) + "italic" -> ItalicTextSource(sourceSubstring, shiftedSubParts) + "code" -> CodeTextSource(sourceSubstring) + "pre" -> PreTextSource(sourceSubstring) + "text_link" -> TextLinkTextSource(sourceSubstring, url ?: throw IllegalStateException("URL must not be null for text link")) + "text_mention" -> TextMentionTextSource(sourceSubstring, user ?: throw IllegalStateException("User must not be null for text mention"), shiftedSubParts) + "underline" -> UnderlineTextSource(sourceSubstring, shiftedSubParts) + "strikethrough" -> StrikethroughTextSource(sourceSubstring, shiftedSubParts) + else -> RegularTextSource(sourceSubstring) + }.let { + val part = TextPart(range, it) + if (it !is MultilevelTextSource) { + (subParts + part).sortedBy { currentPart -> currentPart.range.first } + } else { + listOf(part) } } } -internal typealias RawMessageEntities = List +internal fun createTextPart(from: String, entities: RawMessageEntities): List { + val mutableEntities = entities.toMutableList() + mutableEntities.sortBy { it.offset } + val resultList = mutableListOf() -internal object RawMessageEntitiesSerializer : KSerializer> by ArrayListSerializer( - RawMessageEntity.serializer() -) + while (mutableEntities.isNotEmpty()) { + val currentFirst = mutableEntities.removeAt(0) + val subEntities = if (mutableEntities.isNotEmpty()) { + val lastIndex = currentFirst.offset + currentFirst.length + val subEntities = mutableListOf() + while (mutableEntities.isNotEmpty()) { + val currentPossibleSubEntity = mutableEntities.first() + if (currentPossibleSubEntity.offset < lastIndex) { + subEntities.add(currentPossibleSubEntity) + mutableEntities.removeAt(0) + } else { + break + } + } + subEntities + } else { + emptyList() + } + + resultList.addAll( + currentFirst.asTextParts( + from, + createTextPart(from, subEntities) + ) + ) + } + + return resultList +} + +internal fun RawMessageEntities.asTextParts(sourceString: String): List = createTextPart(sourceString, this) + +internal typealias RawMessageEntities = List diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt deleted file mode 100644 index b9834832d0..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RegularTextMessageEntity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.RegularTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown - -data class RegularTextMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by RegularTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt deleted file mode 100644 index 315abd4309..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/StrikethroughMessageEntity.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.StrikethroughTextSource - -class StrikethroughMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by StrikethroughTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt deleted file mode 100644 index 4a8f704ded..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextLinkMessageEntity.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.TextLinkTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown - -data class TextLinkMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String, - val url: String -) : MessageEntity, TextSource by TextLinkTextSource(rawSource, url) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt deleted file mode 100644 index 7a4f8aaa89..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextMentionMessageEntity.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.TextMentionTextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.User -import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat -import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown - -class TextMentionMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String, - val privateChat: PrivateChat -) : MessageEntity, TextSource by TextMentionTextSource(rawSource, privateChat) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt deleted file mode 100644 index 61df04059b..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/URLMessageEntity.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.URLTextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown - -data class URLMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by URLTextSource(rawSource) { - val url: String - get() = rawSource -} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt deleted file mode 100644 index 29519fdf64..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/UnderlineMessageEntity.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.UnderlineTextSource - -class UnderlineMessageEntity( - override val offset: Int, - override val length: Int, - override val rawSource: String -) : MessageEntity, TextSource by UnderlineTextSource(rawSource) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt index fda182dc83..45f308f77b 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt @@ -1,14 +1,15 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.boldHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.boldMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class BoldTextSource( - override val rawSource: String -) : TextSource { - override val asMarkdownSource: String - get() = rawSource.boldMarkdown() - override val asHtmlSource: String - get() = rawSource.boldHTML() + source: String, + textParts: List +) : MultilevelTextSource { + override val textParts: List = source.fullListOfSubSource(textParts) + override val asMarkdownSource: String by lazy { source.boldMarkdown() } + override val asMarkdownV2Source: String by lazy { boldMarkdownV2() } + override val asHtmlSource: String by lazy { boldHTML() } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt index 0eb749548f..a8dfaa4c52 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BotCommandTextSource.kt @@ -1,20 +1,25 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.commandHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.commandMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart +import com.github.insanusmokrassar.TelegramBotAPI.utils.* private val commandRegex = Regex("[/!][^@\\s]*") class BotCommandTextSource( - override val rawSource: String -) : TextSource { - override val asMarkdownSource: String - get() = rawSource.commandMarkdown() - override val asHtmlSource: String - get() = rawSource.commandHTML() - + source: String, + textParts: List +) : MultilevelTextSource { val command: String by lazy { - commandRegex.find(rawSource) ?.value ?.substring(1) ?: rawSource.substring(1)// skip first symbol like "/" or "!" + commandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!" } + + override val textParts: List by lazy { + command.fullListOfSubSource( + textParts.shiftSourcesToTheLeft(1) + ) + } + override val asMarkdownSource: String by lazy { source.commandMarkdown() } + override val asMarkdownV2Source: String by lazy { commandMarkdownV2() } + override val asHtmlSource: String by lazy { commandHTML() } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt index 0ff5d3334a..fb74f58ae8 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt @@ -1,14 +1,13 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.codeHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.codeMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.utils.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.escapeMarkdownV2PreAndCode class CodeTextSource( - override val rawSource: String + source: String ) : TextSource { - override val asMarkdownSource: String - get() = rawSource.codeMarkdown() - override val asHtmlSource: String - get() = rawSource.codeHTML() + override val asMarkdownSource: String by lazy { source.codeMarkdown() } + override val asMarkdownV2Source: String by lazy { source.codeMarkdownV2() } + override val asHtmlSource: String by lazy { source.codeHTML() } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt index 135b31df98..8c89bc9774 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt @@ -1,14 +1,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.emailHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.emailMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class EMailTextSource( - override val rawSource: String -) : TextSource { - override val asMarkdownSource: String - get() = rawSource.emailMarkdown() - override val asHtmlSource: String - get() = rawSource.emailHTML() + source: String, + textParts: List +) : MultilevelTextSource { + override val textParts: List by lazy { source.fullListOfSubSource(textParts) } + override val asMarkdownSource: String by lazy { source.emailMarkdown() } + override val asMarkdownV2Source: String by lazy { emailMarkdownV2(source) } + override val asHtmlSource: String by lazy { emailHTML(source) } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt index fb5302d769..45a3eaf4a3 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt @@ -1,14 +1,23 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.hashTagMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.* + +private val String.withoutSharp + get() = if (startsWith("#")){ + substring(1) + } else { + this + } class HashTagTextSource( - override val rawSource: String -) : TextSource { - override val asMarkdownSource: String - get() = rawSource.hashTagMarkdown() - override val asHtmlSource: String - get() = rawSource.hashTagHTML() + source: String, + textParts: List +) : MultilevelTextSource { + override val textParts: List = source.withoutSharp.fullListOfSubSource( + textParts.shiftSourcesToTheLeft(1) + ) + override val asMarkdownSource: String by lazy { source.hashTagMarkdown() } + override val asMarkdownV2Source: String by lazy { hashTagMarkdownV2() } + override val asHtmlSource: String by lazy { hashTagHTML() } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt index 2027728f64..44e7b083a3 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt @@ -1,14 +1,15 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.italicHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.italicMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class ItalicTextSource( - override val rawSource: String -) : TextSource { - override val asMarkdownSource: String - get() = rawSource.italicMarkdown() - override val asHtmlSource: String - get() = rawSource.italicHTML() + source: String, + textParts: List +) : MultilevelTextSource { + override val textParts: List = source.fullListOfSubSource(textParts) + override val asMarkdownSource: String by lazy { source.italicMarkdown() } + override val asMarkdownV2Source: String by lazy { italicMarkdownV2() } + override val asHtmlSource: String by lazy { italicHTML() } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt index a865ef6df4..9e171a0a14 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt @@ -1,14 +1,25 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.* + +private val String.withoutCommercialAt + get() = if (startsWith("@")) { + substring(1) + } else { + this + } class MentionTextSource( - override val rawSource: String -) : TextSource { - override val asMarkdownSource: String - get() = rawSource.mentionMarkdown() - override val asHtmlSource: String - get() = rawSource.mentionHTML() + source: String, + textParts: List +) : MultilevelTextSource { + override val textParts: List by lazy { + source.withoutCommercialAt.fullListOfSubSource( + textParts.shiftSourcesToTheLeft(1) + ) + } + override val asMarkdownSource: String by lazy { source.mentionMarkdown() } + override val asMarkdownV2Source: String by lazy { mentionMarkdownV2() } + override val asHtmlSource: String by lazy { mentionHTML() } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt index d9948a3ea2..72e584268a 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt @@ -1,14 +1,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.phoneMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class PhoneNumberTextSource( - override val rawSource: String -) : TextSource { - override val asMarkdownSource: String - get() = rawSource.phoneMarkdown() - override val asHtmlSource: String - get() = rawSource.phoneHTML() + source: String, + textParts: List +) : MultilevelTextSource { + override val textParts: List = source.fullListOfSubSource(textParts) + override val asMarkdownSource: String by lazy { source.phoneMarkdown() } + override val asMarkdownV2Source: String by lazy { phoneMarkdownV2() } + override val asHtmlSource: String by lazy { phoneHTML() } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt index dd8abe9637..d83896ba3f 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PreTextSource.kt @@ -1,14 +1,13 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.preHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.preMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class PreTextSource( - override val rawSource: String + source: String, + val language: String? = null ) : TextSource { - override val asMarkdownSource: String - get() = rawSource.preMarkdown() - override val asHtmlSource: String - get() = rawSource.preHTML() + override val asMarkdownSource: String by lazy { source.preMarkdown(language) } + override val asMarkdownV2Source: String by lazy { source.preMarkdownV2(language) } + override val asHtmlSource: String by lazy { source.preHTML(language) } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt index b8e236ea9d..dca8f5aedc 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/RegularTextSource.kt @@ -1,14 +1,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.regularHtml -import com.github.insanusmokrassar.TelegramBotAPI.utils.regularMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class RegularTextSource( - override val rawSource: String + source: String ) : TextSource { - override val asMarkdownSource: String - get() = rawSource.regularMarkdown() - override val asHtmlSource: String - get() = rawSource.regularHtml() + override val asMarkdownSource: String by lazy { source.regularMarkdown() } + override val asMarkdownV2Source: String by lazy { source.regularMarkdownV2() } + override val asHtmlSource: String by lazy { source.regularHtml() } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt index fa7d3b74cd..274e32f5c7 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt @@ -1,14 +1,15 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.strikethroughMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class StrikethroughTextSource( - override val rawSource: String -) : TextSource { - override val asHtmlSource: String - get() = rawSource.strikethroughHTML() - override val asMarkdownSource: String - get() = rawSource.strikethroughMarkdown() + source: String, + textParts: List +) : MultilevelTextSource { + override val textParts: List = source.fullListOfSubSource(textParts) + override val asHtmlSource: String by lazy { strikethroughHTML() } + override val asMarkdownV2Source: String by lazy { strikethroughMarkdownV2() } + override val asMarkdownSource: String by lazy { source.strikethroughMarkdown() } } \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt index 7356ac0b31..73d3ff8803 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextLinkTextSource.kt @@ -1,13 +1,13 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class TextLinkTextSource( - override val rawSource: String, + source: String, url: String ) : TextSource { - override val asMarkdownSource: String = rawSource.linkMarkdown(url) - override val asHtmlSource: String = rawSource.linkHTML(url) + override val asMarkdownSource: String by lazy { source.linkMarkdown(url) } + override val asMarkdownV2Source: String by lazy { source.linkMarkdownV2(url) } + override val asHtmlSource: String by lazy { source.linkHTML(url) } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt index 3cc4643d55..d4c7f92403 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt @@ -1,15 +1,16 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.User +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat -import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.mentionMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class TextMentionTextSource( - override val rawSource: String, - privateChat: PrivateChat -) : TextSource { - override val asMarkdownSource: String = rawSource.mentionMarkdown(privateChat.id) - override val asHtmlSource: String = rawSource.mentionHTML(privateChat.id) + source: String, + privateChat: PrivateChat, + textParts: List +) : MultilevelTextSource { + override val textParts: List by lazy { source.fullListOfSubSource(textParts) } + override val asMarkdownSource: String by lazy { source.textMentionMarkdown(privateChat.id) } + override val asMarkdownV2Source: String by lazy { textMentionMarkdownV2(privateChat.id) } + override val asHtmlSource: String by lazy { textMentionHTML(privateChat.id) } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt index f96a169373..bd572b097b 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/URLTextSource.kt @@ -1,14 +1,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.linkHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.linkMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class URLTextSource( - override val rawSource: String -) : TextSource{ - override val asMarkdownSource: String - get() = rawSource.linkMarkdown(rawSource) - override val asHtmlSource: String - get() = rawSource.linkHTML(rawSource) + source: String +) : TextSource { + override val asMarkdownSource: String by lazy { source.linkMarkdown(source) } + override val asMarkdownV2Source: String by lazy { source.linkMarkdownV2(source) } + override val asHtmlSource: String by lazy { source.linkHTML(source) } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt index d7cef33cdc..92965f33d6 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt @@ -1,14 +1,15 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineHTML -import com.github.insanusmokrassar.TelegramBotAPI.utils.underlineMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart +import com.github.insanusmokrassar.TelegramBotAPI.utils.* class UnderlineTextSource( - override val rawSource: String -) : TextSource { - override val asMarkdownSource: String - get() = rawSource.underlineMarkdown() - override val asHtmlSource: String - get() = rawSource.underlineHTML() + source: String, + textParts: List +) : MultilevelTextSource { + override val textParts: List = source.fullListOfSubSource(textParts) + override val asMarkdownSource: String by lazy { source.underlineMarkdown() } + override val asMarkdownV2Source: String by lazy { underlineMarkdownV2() } + override val asHtmlSource: String by lazy { underlineHTML() } } \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt index 7a94983458..e3558593d7 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt @@ -16,6 +16,12 @@ object MarkdownParseMode : ParseMode() { override val parseModeName: String = "Markdown" } +@Serializable(ParseModeSerializerObject::class) +object MarkdownV2ParseMode : ParseMode() { + @Serializable + @SerialName(parseModeField) + override val parseModeName: String = "MarkdownV2" +} @Serializable(ParseModeSerializerObject::class) object HTMLParseMode : ParseMode() { @Serializable @@ -24,6 +30,7 @@ object HTMLParseMode : ParseMode() { } typealias Markdown = MarkdownParseMode +typealias MarkdownV2 = MarkdownV2ParseMode typealias HTML = HTMLParseMode @Serializer(ParseMode::class) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/Game.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/Game.kt index f35b549081..6fef99c5f6 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/Game.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/Game.kt @@ -1,8 +1,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.games -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.Titled -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.files.AnimationFile import com.github.insanusmokrassar.TelegramBotAPI.types.files.Photo @@ -11,6 +9,6 @@ data class Game( val description: String, val photo: Photo, override val caption: String? = null, - override val captionEntities: List = emptyList(), + override val captionEntities: List = emptyList(), val animation: AnimationFile? = null ) : Titled, CaptionedInput diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/RawGame.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/RawGame.kt index 76054a81ea..5e740529d7 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/RawGame.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/RawGame.kt @@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.games import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessageEntities +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.asTextParts import com.github.insanusmokrassar.TelegramBotAPI.types.files.* import kotlinx.serialization.* @@ -27,7 +28,7 @@ internal data class RawGame( description, photo, caption, - caption ?.let { _ -> captionEntities.map { it.asMessageEntity(caption) } } ?: emptyList(), + caption ?.let { _ -> captionEntities.asTextParts(caption) } ?: emptyList(), animation ) } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt index 14edb09d08..1a48869f16 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt @@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessageEntities +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.asTextParts import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.files.* @@ -85,13 +86,11 @@ internal data class RawMessage( ) { private val content: MessageContent? by lazy { val adaptedCaptionEntities = caption ?.let { - caption_entities ?.map { - it.asMessageEntity(caption) - } + caption_entities ?.asTextParts(caption) } ?: emptyList() when { - text != null -> TextContent(text, entities ?.map { it.asMessageEntity(text) } ?: emptyList()) + text != null -> TextContent(text, entities ?.asTextParts(text) ?: emptyList()) audio != null -> AudioContent( audio, caption, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/TextContent.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/TextContent.kt index 0dfa2f56f4..0fe6e3296c 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/TextContent.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/TextContent.kt @@ -1,20 +1,19 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.send.SendMessage import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent -import com.github.insanusmokrassar.TelegramBotAPI.utils.toHtmlTexts -import com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownTexts +import com.github.insanusmokrassar.TelegramBotAPI.utils.* data class TextContent( val text: String, - val entities: List = emptyList() + val entities: List = emptyList() ) : MessageContent { override fun createResend( chatId: ChatIdentifier, @@ -52,6 +51,7 @@ data class TextContent( parseMode: ParseMode = HTMLParseMode ): List> = when (parseMode) { is MarkdownParseMode -> toMarkdownTexts() + is MarkdownV2ParseMode -> toMarkdownV2Texts() is HTMLParseMode -> toHtmlTexts() }.map { SendMessage( @@ -65,3 +65,5 @@ data class TextContent( ) } } + +fun TextContent.fullEntitiesList() = text.fullListOfSubSource(entities).map { it.source } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/AnimationContent.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/AnimationContent.kt index a47d4d773e..04227b8c05 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/AnimationContent.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/AnimationContent.kt @@ -1,10 +1,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.SendAnimation import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup @@ -18,7 +18,7 @@ data class AnimationContent( override val media: AnimationFile, val includedDocument: DocumentFile?, override val caption: String?, - override val captionEntities: List + override val captionEntities: List ) : MediaContent, CaptionedInput { override fun createResend( chatId: ChatIdentifier, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/AudioContent.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/AudioContent.kt index 346826b0b1..ff78e9c9a1 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/AudioContent.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/AudioContent.kt @@ -1,10 +1,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.SendAudio import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup @@ -16,7 +16,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.toHtmlCaptions data class AudioContent( override val media: AudioFile, override val caption: String? = null, - override val captionEntities: List = emptyList() + override val captionEntities: List = emptyList() ) : MediaContent, CaptionedInput { override fun createResend( chatId: ChatIdentifier, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/DocumentContent.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/DocumentContent.kt index 2b6b31cb8b..219859ce03 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/DocumentContent.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/DocumentContent.kt @@ -1,10 +1,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.SendDocument import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup @@ -16,7 +16,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.toHtmlCaptions data class DocumentContent( override val media: DocumentFile, override val caption: String? = null, - override val captionEntities: List = emptyList() + override val captionEntities: List = emptyList() ) : MediaContent, CaptionedInput { override fun createResend( chatId: ChatIdentifier, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/PhotoContent.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/PhotoContent.kt index d358b88ebe..8a03bd47c0 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/PhotoContent.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/PhotoContent.kt @@ -1,11 +1,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.SendPhoto import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia.InputMediaPhoto import com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia.MediaGroupMemberInputMedia -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup @@ -19,7 +19,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.toHtmlCaptions data class PhotoContent( override val mediaCollection: List, override val caption: String? = null, - override val captionEntities: List = emptyList() + override val captionEntities: List = emptyList() ) : MediaCollectionContent, MediaGroupContent { override val media: PhotoSize = mediaCollection.biggest() ?: throw IllegalStateException("Can't locate any photo size for this content") diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/VideoContent.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/VideoContent.kt index 718845cafb..ff7210e201 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/VideoContent.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/VideoContent.kt @@ -1,11 +1,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.SendVideo import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia.InputMediaVideo import com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia.MediaGroupMemberInputMedia -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup @@ -17,7 +17,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.toHtmlCaptions data class VideoContent( override val media: VideoFile, override val caption: String? = null, - override val captionEntities: List = emptyList() + override val captionEntities: List = emptyList() ) : MediaGroupContent { override fun createResend( chatId: ChatIdentifier, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/VoiceContent.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/VoiceContent.kt index 63c07dbb5c..61ab4290b6 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/VoiceContent.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/media/VoiceContent.kt @@ -1,10 +1,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.SendVoice import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup @@ -16,7 +16,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.toHtmlCaptions data class VoiceContent( override val media: VoiceFile, override val caption: String? = null, - override val captionEntities: List = emptyList() + override val captionEntities: List = emptyList() ) : MediaContent, CaptionedInput { override fun createResend( chatId: ChatIdentifier, diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt new file mode 100644 index 0000000000..7705a6c832 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt @@ -0,0 +1,98 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* +import com.github.insanusmokrassar.TelegramBotAPI.types.captionLength +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList +import com.github.insanusmokrassar.TelegramBotAPI.types.textLength + +fun createFormattedText( + entities: List, + partLength: Int = 4096, + mode: ParseMode = MarkdownParseMode +): List { + val texts = mutableListOf() + val textBuilder = StringBuilder(partLength) + for (entity in entities) { + val string = when (mode) { + is MarkdownParseMode -> entity.asMarkdownSource + is MarkdownV2ParseMode -> entity.asMarkdownV2Source + is HTMLParseMode -> entity.asHtmlSource + } + 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 +} + + +fun createMarkdownText( + entities: List, + partLength: Int = 4096 +): List = createFormattedText(entities, partLength, MarkdownParseMode) + +fun CaptionedInput.toMarkdownCaptions(): List = createMarkdownText( + fullEntitiesList(), + captionLength.last + 1 +) + +fun TextContent.toMarkdownTexts(): List = createMarkdownText( + fullEntitiesList(), + textLength.last + 1 +) + + +fun createMarkdownV2Text( + entities: List, + partLength: Int = 4096 +): List = createFormattedText(entities, partLength, MarkdownV2ParseMode) + +fun CaptionedInput.toMarkdownV2Captions(): List = createMarkdownV2Text( + fullEntitiesList(), + captionLength.last + 1 +) + +fun TextContent.toMarkdownV2Texts(): List = createMarkdownV2Text( + fullEntitiesList(), + textLength.last + 1 +) + + +fun createHtmlText( + entities: List, + partLength: Int = 4096 +): List = createFormattedText(entities, partLength, HTMLParseMode) + +fun CaptionedInput.toHtmlCaptions(): List = createHtmlText( + fullEntitiesList(), + captionLength.last + 1 +) + +fun TextContent.toHtmlTexts(): List = createHtmlText( + fullEntitiesList(), + textLength.last + 1 +) + + diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt deleted file mode 100644 index 5eca85548f..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Captions.kt +++ /dev/null @@ -1,77 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.utils - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RegularTextMessageEntity -import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* -import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent - -fun CaptionedInput.fullEntitiesList(): List = caption ?.let { - convertToFullMessageEntityList(it, captionEntities) -} ?: emptyList() - -fun TextContent.fullEntitiesList(): List = convertToFullMessageEntityList(text, entities) - -fun convertToFullMessageEntityList( - text: String, - messageEntities: List -): List { - val result = mutableListOf() - - var offset = 0 - for (entity in messageEntities) { - val newEntitySize = entity.offset - offset - if (newEntitySize > 0) { - val regularEntity = RegularTextMessageEntity(offset, newEntitySize, text.substring(offset, entity.offset)) - result.add(regularEntity) - offset += regularEntity.length - } - result.add(entity) - offset += entity.length - } - val newEntitySize = text.length - offset - if (newEntitySize > 0) { - result.add(RegularTextMessageEntity(offset, newEntitySize, text.substring(offset, text.length))) - } - return result -} - -fun createFormattedText( - entities: List, - partLength: Int = 4096, - mode: ParseMode = MarkdownParseMode -): List { - val texts = mutableListOf() - val textBuilder = StringBuilder(partLength) - for (entity in entities) { - val string = when (mode) { - is MarkdownParseMode -> entity.asMarkdownSource - is HTMLParseMode -> entity.asHtmlSource - } - 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 -} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt deleted file mode 100644 index a36b52bee5..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HtmlCaptionSourcer.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.utils - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity -import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.HTMLParseMode -import com.github.insanusmokrassar.TelegramBotAPI.types.captionLength -import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent -import com.github.insanusmokrassar.TelegramBotAPI.types.textLength - -fun createHtmlText( - entities: List, - partLength: Int = 4096 -): List = createFormattedText(entities, partLength, HTMLParseMode) - -fun CaptionedInput.toHtmlCaptions(): List = createHtmlText( - fullEntitiesList(), - captionLength.endInclusive + 1 -) - -fun TextContent.toHtmlTexts(): List = createHtmlText( - fullEntitiesList(), - textLength.endInclusive + 1 -) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt deleted file mode 100644 index c506670702..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MarkdownCaptionSourcer.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.utils - -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity -import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownParseMode -import com.github.insanusmokrassar.TelegramBotAPI.types.captionLength -import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent -import com.github.insanusmokrassar.TelegramBotAPI.types.textLength - -fun createMarkdownText( - entities: List, - partLength: Int = 4096 -): List = createFormattedText(entities, partLength, MarkdownParseMode) - -fun CaptionedInput.toMarkdownCaptions(): List = createMarkdownText( - fullEntitiesList(), - captionLength.endInclusive + 1 -) - -fun TextContent.toMarkdownTexts(): List = createMarkdownText( - fullEntitiesList(), - textLength.endInclusive + 1 -) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt new file mode 100644 index 0000000000..15b0138bcb --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt @@ -0,0 +1,137 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.* +import com.github.insanusmokrassar.TelegramBotAPI.types.UserId +import com.github.insanusmokrassar.TelegramBotAPI.types.link +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.* + +internal fun String.fullListOfSubSource(sourceList: List): List { + val sortedSourceList = sourceList.sortedBy { it.range.first }.toMutableList() + + var previousLastIndex = 0 + + val newSubSources = mutableListOf() + + while (sortedSourceList.isNotEmpty()) { + val topSource = sortedSourceList.removeAt(0) + if (topSource.range.first - previousLastIndex > 0) { + val range = previousLastIndex until topSource.range.first + newSubSources.add( + TextPart( + range, + RegularTextSource( + substring(range) + ) + ) + ) + } + newSubSources.add(topSource) + previousLastIndex = topSource.range.last + 1 + } + + if (length > previousLastIndex) { + val range = previousLastIndex until length + newSubSources.add( + TextPart( + range, + RegularTextSource( + substring(range) + ) + ) + ) + } + + return newSubSources +} + +internal fun List.shiftSourcesToTheLeft(shiftCount: Int = 1): List { + return mapNotNull { + val first = (it.range.first - shiftCount).let { firstCalculated -> + if (firstCalculated < 0) { + 0 + } else { + firstCalculated + } + } + val last = (it.range.last - shiftCount).let { lastCalculated -> + if (lastCalculated < 0) { + 0 + } else { + lastCalculated + } + } + it.copy(range = first .. last).let { newSubSource -> + if (newSubSource.range.isEmpty()) { + null + } else { + newSubSource + } + } + } +} + +private fun List.joinSubSourcesMarkdownV2() = joinToString("") { + it.source.asMarkdownV2Source +} + +private fun List.joinSubSourcesHtml() = joinToString("") { + it.source.asHtmlSource +} + +internal fun MultilevelTextSource.markdownV2Default( + openControlSymbol: String, + closeControlSymbol: String = openControlSymbol +) = "$openControlSymbol${textParts.joinSubSourcesMarkdownV2()}$closeControlSymbol" +internal fun MultilevelTextSource.htmlDefault( + openControlSymbol: String, + closeControlSymbol: String = openControlSymbol +) = "<$openControlSymbol>${textParts.joinSubSourcesHtml()}" + + +internal fun MultilevelTextSource.linkMarkdownV2( + link: String +) = "[${textParts.joinSubSourcesMarkdownV2()}](${link.escapeMarkdownV2Link()})" +internal fun MultilevelTextSource.linkHTML( + link: String +) = "${textParts.joinSubSourcesHtml()}" + + +internal fun MultilevelTextSource.emailMarkdownV2(address: String): String = linkMarkdownV2("mailto://$address") +internal fun MultilevelTextSource.emailHTML(address: String): String = linkHTML("mailto://$address}") + + +internal fun MultilevelTextSource.boldMarkdownV2(): String = markdownV2Default(markdownBoldControl) +internal fun MultilevelTextSource.boldHTML(): String = htmlDefault(htmlBoldControl) + + +internal fun MultilevelTextSource.italicMarkdownV2(): String = markdownV2Default(markdownItalicControl) +internal fun MultilevelTextSource.italicHTML(): String = htmlDefault(htmlItalicControl) + + +internal fun MultilevelTextSource.strikethroughMarkdownV2(): String = markdownV2Default(markdownV2StrikethroughControl) +internal fun MultilevelTextSource.strikethroughHTML(): String = htmlDefault(htmlStrikethroughControl) + + +internal fun MultilevelTextSource.underlineMarkdownV2(): String = markdownV2Default(markdownV2UnderlineControl) +internal fun MultilevelTextSource.underlineHTML(): String = htmlDefault(htmlUnderlineControl) + + +internal fun MultilevelTextSource.textMentionMarkdownV2(userId: UserId): String = linkMarkdownV2(userId.link) +internal fun MultilevelTextSource.textMentionHTML(userId: UserId): String = linkHTML(userId.link) + +internal fun MultilevelTextSource.mentionMarkdownV2(): String = "@${textParts.joinSubSourcesMarkdownV2()}" +internal fun MultilevelTextSource.mentionHTML(): String = "@${textParts.joinSubSourcesHtml()}" + + +internal fun MultilevelTextSource.hashTagMarkdownV2(): String = "#${textParts.joinSubSourcesMarkdownV2()}" +internal fun MultilevelTextSource.hashTagHTML(): String = "#${textParts.joinSubSourcesHtml()}" + + +internal fun MultilevelTextSource.phoneMarkdownV2(): String = textParts.joinSubSourcesMarkdownV2() +internal fun MultilevelTextSource.phoneHTML(): String = textParts.joinSubSourcesHtml() + + +internal fun MultilevelTextSource.commandMarkdownV2(): String = "/${textParts.joinSubSourcesMarkdownV2()}" +internal fun MultilevelTextSource.commandHTML(): String = "/${textParts.joinSubSourcesHtml()}" + diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt index 175fcf0569..bf500dabf1 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt @@ -2,14 +2,19 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toMarkdown +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.* const val markdownBoldControl = "*" const val markdownItalicControl = "_" const val markdownCodeControl = "`" const val markdownPreControl = "```" +const val markdownV2ItalicUnderlineDelimiter = "\u0013" +const val markdownV2StrikethroughControl = "~" +const val markdownV2UnderlineControl = "__" +const val markdownV2UnderlineEndControl = "$markdownV2UnderlineControl$markdownV2ItalicUnderlineDelimiter" +const val markdownV2ItalicEndControl = "$markdownItalicControl$markdownV2ItalicUnderlineDelimiter" + const val htmlBoldControl = "b" const val htmlItalicControl = "i" const val htmlCodeControl = "code" @@ -21,48 +26,35 @@ private fun String.markdownDefault( openControlSymbol: String, closeControlSymbol: String = openControlSymbol ) = "$openControlSymbol${toMarkdown()}$closeControlSymbol" +private fun String.markdownV2Default( + openControlSymbol: String, + closeControlSymbol: String = openControlSymbol, + escapeFun: String.() -> String = String::escapeMarkdownV2Common +) = "$openControlSymbol${escapeFun()}$closeControlSymbol" private fun String.htmlDefault( openControlSymbol: String, closeControlSymbol: String = openControlSymbol ) = "<$openControlSymbol>${toHtml()}" -fun String.linkMarkdown(link: String): String = "[${toMarkdown()}]($link)" +fun String.linkMarkdown(link: String): String = "[${toMarkdown()}](${link.toMarkdown()})" +fun String.linkMarkdownV2(link: String): String = "[${escapeMarkdownV2Common()}](${link.escapeMarkdownV2Link()})" fun String.linkHTML(link: String): String = "${toHtml()}" fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl) +fun String.boldMarkdownV2(): String = markdownV2Default(markdownBoldControl) fun String.boldHTML(): String = htmlDefault(htmlBoldControl) fun String.italicMarkdown(): String = markdownDefault(markdownItalicControl) -fun String.italicHTML(): String =htmlDefault(htmlItalicControl) - - -fun String.codeMarkdown(): String = markdownDefault(markdownCodeControl) -fun String.codeHTML(): String = htmlDefault(htmlCodeControl) - - -fun String.preMarkdown(language: String? = null): String = markdownDefault( - "$markdownPreControl${language ?.let { "$it\n" } ?: "\n"}", - "\n$markdownPreControl" -) -fun String.preHTML(language: String? = null): String = htmlDefault( - language ?.let { _ -> - "$htmlPreControl><$htmlCodeControl class=\"language-$language\"" - } ?: htmlPreControl, - language ?.let { _ -> - "$htmlCodeControl><$htmlCodeControl class=\"language-$language\"" + } ?: htmlPreControl, + language ?.let { + "$htmlCodeControl> String): String = if (startsWith("@")) { adapt() } else { @@ -87,19 +109,23 @@ private inline fun String.hashTag(adapt: String.() -> String): String = if (star } -fun String.mentionMarkdown(userId: UserId): String = linkMarkdown(userId.link) -fun String.mentionHTML(userId: UserId): String = linkHTML(userId.link) +fun String.textMentionMarkdown(userId: UserId): String = linkMarkdown(userId.link) +fun String.textMentionMarkdownV2(userId: UserId): String = linkMarkdownV2(userId.link) +fun String.textMentionHTML(userId: UserId): String = linkHTML(userId.link) fun String.mentionMarkdown(): String = mention(String::toMarkdown) +fun String.mentionMarkdownV2(): String = mention(String::escapeMarkdownV2Common) fun String.mentionHTML(): String = mention(String::toHtml) fun String.hashTagMarkdown(): String = hashTag(String::toMarkdown) +fun String.hashTagMarkdownV2(): String = hashTag(String::escapeMarkdownV2Common) fun String.hashTagHTML(): String = hashTag(String::toHtml) fun String.phoneMarkdown(): String = toMarkdown() +fun String.phoneMarkdownV2(): String = escapeMarkdownV2Common() fun String.phoneHTML(): String = toHtml() @@ -110,70 +136,97 @@ fun String.command(adapt: String.() -> String): String = if (startsWith("/")) { } fun String.commandMarkdown(): String = command(String::toMarkdown) +fun String.commandMarkdownV2(): String = command(String::escapeMarkdownV2Common) fun String.commandHTML(): String = command(String::toHtml) fun String.regularMarkdown(): String = toMarkdown() +fun String.regularMarkdownV2(): String = escapeMarkdownV2Common() fun String.regularHtml(): String = toHtml() infix fun String.bold(parseMode: ParseMode): String = when (parseMode) { is HTML -> boldHTML() is Markdown -> boldMarkdown() + is MarkdownV2 -> boldMarkdownV2() } infix fun String.italic(parseMode: ParseMode): String = when (parseMode) { is HTML -> italicHTML() is Markdown -> italicMarkdown() + is MarkdownV2 -> italicMarkdownV2() } infix fun String.hashTag(parseMode: ParseMode): String = when (parseMode) { is HTML -> hashTagHTML() is Markdown -> hashTagMarkdown() + is MarkdownV2 -> hashTagMarkdownV2() } infix fun String.code(parseMode: ParseMode): String = when (parseMode) { is HTML -> codeHTML() is Markdown -> codeMarkdown() -} - -infix fun String.pre(parseMode: ParseMode): String = when (parseMode) { - is HTML -> preHTML() - is Markdown -> preMarkdown() + is MarkdownV2 -> codeMarkdownV2() } fun String.pre(parseMode: ParseMode, language: String? = null): String = when (parseMode) { is HTML -> preHTML(language) is Markdown -> preMarkdown(language) + is MarkdownV2 -> preMarkdownV2(language) } +infix fun String.pre(parseMode: ParseMode): String = pre(parseMode, null) infix fun String.email(parseMode: ParseMode): String = when (parseMode) { is HTML -> emailHTML() is Markdown -> emailMarkdown() + is MarkdownV2 -> emailMarkdownV2() } infix fun Pair.link(parseMode: ParseMode): String = when (parseMode) { is HTML -> first.linkHTML(second) is Markdown -> first.linkMarkdown(second) + is MarkdownV2 -> first.linkMarkdownV2(second) } infix fun String.mention(parseMode: ParseMode): String = when (parseMode) { is HTML -> mentionHTML() is Markdown -> mentionMarkdown() + is MarkdownV2 -> mentionMarkdownV2() } infix fun Pair.mention(parseMode: ParseMode): String = when (parseMode) { - is HTML -> first.mentionHTML(second) - is Markdown -> first.mentionMarkdown(second) + is HTML -> first.textMentionHTML(second) + is Markdown -> first.textMentionMarkdown(second) + is MarkdownV2 -> first.textMentionMarkdownV2(second) } infix fun String.phone(parseMode: ParseMode): String = when (parseMode) { is HTML -> phoneHTML() is Markdown -> phoneMarkdown() + is MarkdownV2 -> phoneMarkdownV2() } infix fun String.command(parseMode: ParseMode): String = when (parseMode) { is HTML -> commandHTML() is Markdown -> commandMarkdown() + is MarkdownV2 -> commandMarkdownV2() +} + +infix fun String.underline(parseMode: ParseMode): String = when (parseMode) { + is HTML -> underlineHTML() + is Markdown -> underlineMarkdown() + is MarkdownV2 -> underlineMarkdownV2() +} + +infix fun String.strikethrough(parseMode: ParseMode): String = when (parseMode) { + is HTML -> strikethroughHTML() + is Markdown -> strikethroughMarkdown() + is MarkdownV2 -> strikethroughMarkdownV2() +} + +infix fun String.regular(parseMode: ParseMode): String = when (parseMode) { + is HTML -> regularHtml() + is Markdown -> regularMarkdown() + is MarkdownV2 -> regularMarkdownV2() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt index 7d5457953c..349d644f83 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/String.kt @@ -16,6 +16,33 @@ fun String.toMarkdown(): String { ) } +private val markdownV2LinkEscapes = mutableSetOf(')', '\\') +private val markdownV2PreAndCodeEscapes = mutableSetOf('`', '\\') +private val markdownV2CommonEscapes = mutableSetOf( + '_', + '*', + '[', ']', + '(', ')', + '~', + '`', + '>', + '#', + '+', '-', '=', + '|', + '{', '}', + '.', '!' +) +private fun String.escapeMarkdownV2(escapeCharacters: Iterable): String = map { + if (it in escapeCharacters) { + "\\$it" + } else { + "$it" + } +}.joinToString("") +fun String.escapeMarkdownV2Link() = escapeMarkdownV2(markdownV2LinkEscapes) +fun String.escapeMarkdownV2PreAndCode() = escapeMarkdownV2(markdownV2PreAndCodeEscapes) +fun String.escapeMarkdownV2Common() = escapeMarkdownV2(markdownV2CommonEscapes) + fun String.toHtml(): String = replace( "<", "<" diff --git a/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextPartsCreatingTests.kt b/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextPartsCreatingTests.kt new file mode 100644 index 0000000000..7a2024523e --- /dev/null +++ b/src/commonTest/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/TextPartsCreatingTests.kt @@ -0,0 +1,142 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity + +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.* +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownV2 +import com.github.insanusmokrassar.TelegramBotAPI.utils.* +import kotlin.test.* + +class TextPartsCreatingTests { + @Test + fun testThatTextWithMultilevelPartsCorrectlyCreating() { + val text = "It is simple hello world" + val formattedV2Text = "It *_is_ ~__simple__~* hello world" + val entities = listOf( + RawMessageEntity( + "bold", + 3, + 9 + ), + RawMessageEntity( + "italic", + 3, + 2 + ), + RawMessageEntity( + "strikethrough", + 6, + 6 + ), + RawMessageEntity( + "underline", + 6, + 6 + ) + ) + + val textParts = createTextPart(text, entities) + + + assertTrue ( + textParts.first().source is BoldTextSource + ) + + val boldSource = textParts.first().source as BoldTextSource + assertTrue ( + boldSource.textParts.first().source is ItalicTextSource + ) + assertTrue ( + boldSource.textParts[1].source is RegularTextSource + ) + assertTrue ( + boldSource.textParts[2].source is StrikethroughTextSource + ) + assertTrue ( + (boldSource.textParts[2].source as StrikethroughTextSource).textParts.first().source is UnderlineTextSource + ) + + + val fullTextParts = text.fullListOfSubSource(textParts) + + assertTrue( + fullTextParts.first().source is RegularTextSource + ) + assertTrue( + fullTextParts[1].source is BoldTextSource + ) + assertTrue( + fullTextParts[2].source is RegularTextSource + ) + + assertEquals( + formattedV2Text, + createMarkdownV2Text(fullTextParts.map { it.source }).first() + ) + } + + @Test + fun testThatTextWithMultilevelPartsCorrectlyCreatingInHtml() { + val text = "It is simple hello world" + val formattedHtmlText = "It is simple hello world" + val entities = listOf( + RawMessageEntity( + "bold", + 3, + 9 + ), + RawMessageEntity( + "italic", + 3, + 2 + ), + RawMessageEntity( + "strikethrough", + 6, + 6 + ), + RawMessageEntity( + "underline", + 6, + 6 + ) + ) + + val textParts = createTextPart(text, entities) + + + assertTrue ( + textParts.first().source is BoldTextSource + ) + + val boldSource = textParts.first().source as BoldTextSource + assertTrue ( + boldSource.textParts.first().source is ItalicTextSource + ) + assertTrue ( + boldSource.textParts[1].source is RegularTextSource + ) + assertTrue ( + boldSource.textParts[2].source is StrikethroughTextSource + ) + assertTrue ( + (boldSource.textParts[2].source as StrikethroughTextSource).textParts.first().source is UnderlineTextSource + ) + + + val fullTextParts = text.fullListOfSubSource(textParts) + + assertTrue( + fullTextParts.first().source is RegularTextSource + ) + assertTrue( + fullTextParts[1].source is BoldTextSource + ) + assertTrue( + fullTextParts[2].source is RegularTextSource + ) + + assertEquals( + formattedHtmlText, + createHtmlText(fullTextParts.map { it.source }).first() + ) + } +} From b095f07b9c75deb30ee7ef5b475bfe3ee776bff9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 5 Jan 2020 19:50:04 +0600 Subject: [PATCH 14/20] add subcondition for adding subparts in raw message entities --- .../TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt index bdbfbb3f77..d33b743ab1 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/RawMessageEntity.kt @@ -39,7 +39,7 @@ internal fun RawMessageEntity.asTextParts(source: String, subParts: List RegularTextSource(sourceSubstring) }.let { val part = TextPart(range, it) - if (it !is MultilevelTextSource) { + if (it !is MultilevelTextSource && subParts.isNotEmpty()) { (subParts + part).sortedBy { currentPart -> currentPart.range.first } } else { listOf(part) From 7694b25d334156ae64939111243a920318d6f33f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 5 Jan 2020 19:51:32 +0600 Subject: [PATCH 15/20] optimize imports --- .../bot/Ktor/base/AbstractRequestCallFactory.kt | 2 -- .../types/MessageEntity/textsources/CodeTextSource.kt | 1 - .../types/MessageEntity/textsources/EMailTextSource.kt | 3 ++- .../types/MessageEntity/textsources/HashTagTextSource.kt | 3 ++- .../types/MessageEntity/textsources/MentionTextSource.kt | 3 ++- .../MessageEntity/textsources/PhoneNumberTextSource.kt | 3 ++- .../MessageEntity/textsources/TextMentionTextSource.kt | 3 ++- .../utils/MultilevelTextSourceFormatting.kt | 8 +++++--- 8 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/AbstractRequestCallFactory.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/AbstractRequestCallFactory.kt index 0658c692f3..4e8fb1f428 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/AbstractRequestCallFactory.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/AbstractRequestCallFactory.kt @@ -9,8 +9,6 @@ import io.ktor.client.request.accept import io.ktor.client.request.url import io.ktor.http.ContentType import io.ktor.http.HttpMethod -import kotlin.collections.MutableMap -import kotlin.collections.mutableMapOf import kotlin.collections.set abstract class AbstractRequestCallFactory : KtorCallFactory { diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt index fb74f58ae8..5b2359a458 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/CodeTextSource.kt @@ -2,7 +2,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsourc import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.utils.* -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.escapeMarkdownV2PreAndCode class CodeTextSource( source: String diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt index 8c89bc9774..f87d899953 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/EMailTextSource.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* class EMailTextSource( diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt index 45a3eaf4a3..3c3267c052 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* private val String.withoutSharp diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt index 9e171a0a14..398ae8c388 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/MentionTextSource.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* private val String.withoutCommercialAt diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt index 72e584268a..0da3735933 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.utils.* class PhoneNumberTextSource( diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt index d4c7f92403..b335806c6f 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/TextMentionTextSource.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat import com.github.insanusmokrassar.TelegramBotAPI.utils.* diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt index 15b0138bcb..d510337afe 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt @@ -1,10 +1,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils -import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.* +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.RegularTextSource import com.github.insanusmokrassar.TelegramBotAPI.types.UserId import com.github.insanusmokrassar.TelegramBotAPI.types.link -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.escapeMarkdownV2Link +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.toHtml internal fun String.fullListOfSubSource(sourceList: List): List { val sortedSourceList = sourceList.sortedBy { it.range.first }.toMutableList() From bb3ebb455e94897e7eb4adabf112b50c77f7be03 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 5 Jan 2020 19:58:29 +0600 Subject: [PATCH 16/20] update CHANGELOG --- CHANGELOG.md | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffd979eea4..6bd147543b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,20 +2,16 @@ ## 0.21.0 TelegramBotAPI 4.5 +* _**All `MessageEntity`'es now are replaced with `TextPart`**_ * Added support of strikethrough and underline - * Added `UnderlineTextSource` and `UnderlineMessageEntity` - * Added `StrikethroughTextSource` and `StrikethroughMessageEntity` + * Added `UnderlineTextSource` + * Added `StrikethroughTextSource` * Added support in `RawMessageEntity` -* Now `TextSource` have its `rawSource` const - * `sourceString` in `MessageEntity` now is deprecated - * All `MessageEntity` classes now have no income parameter `sourceString` - * In most part of `TextSource` classes `asMarkdownSource` and `asHtmlSource` now are getters instead if fields - * All parseMode-specific functions in `StringFormatting` now are not `infix` -* Now will not be thrown exception when there is income unknown type of `MessageEntity`. Instead of this will be -created `RegularTextMessageEntity` with the same text +* Added support of `MarkdownV2` +* Now will not be thrown exception when there is income unknown type of `RawMessageEntity`. Instead of this will be +created `RegularTextSource` with the same text * Fixed problem that usually string formatting did not trigger escaping of control characters * Actualized work with `pre` type of text - now it is possible to use `language` for formatting of text -* Removed constructor of `TextMentionMessageEntity`, which was deprecated previously * Removed constructor of `TextMentionTextSource`, which was deprecated previously * All `TelegramMediaFile` instances now have field `fileUniqueId`, which represents `file_unique_id` field from API * Now `ChatPhoto` have two additional fields: `smallFileUniqueId` and `bigFileUniqueId` From 4ac01d53314a72d083403d52db934daf948dfb8a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 5 Jan 2020 20:07:51 +0600 Subject: [PATCH 17/20] all fields in text sources are lazy --- .../types/MessageEntity/textsources/BoldTextSource.kt | 2 +- .../types/MessageEntity/textsources/HashTagTextSource.kt | 8 +++++--- .../types/MessageEntity/textsources/ItalicTextSource.kt | 2 +- .../MessageEntity/textsources/PhoneNumberTextSource.kt | 2 +- .../MessageEntity/textsources/StrikethroughTextSource.kt | 2 +- .../MessageEntity/textsources/UnderlineTextSource.kt | 2 +- 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt index 45f308f77b..a491b5953f 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/BoldTextSource.kt @@ -8,7 +8,7 @@ class BoldTextSource( source: String, textParts: List ) : MultilevelTextSource { - override val textParts: List = source.fullListOfSubSource(textParts) + override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.boldMarkdown() } override val asMarkdownV2Source: String by lazy { boldMarkdownV2() } override val asHtmlSource: String by lazy { boldHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt index 3c3267c052..1daabab4d7 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/HashTagTextSource.kt @@ -15,9 +15,11 @@ class HashTagTextSource( source: String, textParts: List ) : MultilevelTextSource { - override val textParts: List = source.withoutSharp.fullListOfSubSource( - textParts.shiftSourcesToTheLeft(1) - ) + override val textParts: List by lazy { + source.withoutSharp.fullListOfSubSource( + textParts.shiftSourcesToTheLeft(1) + ) + } override val asMarkdownSource: String by lazy { source.hashTagMarkdown() } override val asMarkdownV2Source: String by lazy { hashTagMarkdownV2() } override val asHtmlSource: String by lazy { hashTagHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt index 44e7b083a3..5043cd19d8 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/ItalicTextSource.kt @@ -8,7 +8,7 @@ class ItalicTextSource( source: String, textParts: List ) : MultilevelTextSource { - override val textParts: List = source.fullListOfSubSource(textParts) + override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.italicMarkdown() } override val asMarkdownV2Source: String by lazy { italicMarkdownV2() } override val asHtmlSource: String by lazy { italicHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt index 0da3735933..f83e41be12 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/PhoneNumberTextSource.kt @@ -8,7 +8,7 @@ class PhoneNumberTextSource( source: String, textParts: List ) : MultilevelTextSource { - override val textParts: List = source.fullListOfSubSource(textParts) + override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.phoneMarkdown() } override val asMarkdownV2Source: String by lazy { phoneMarkdownV2() } override val asHtmlSource: String by lazy { phoneHTML() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt index 274e32f5c7..17c79618bf 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/StrikethroughTextSource.kt @@ -8,7 +8,7 @@ class StrikethroughTextSource( source: String, textParts: List ) : MultilevelTextSource { - override val textParts: List = source.fullListOfSubSource(textParts) + override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asHtmlSource: String by lazy { strikethroughHTML() } override val asMarkdownV2Source: String by lazy { strikethroughMarkdownV2() } override val asMarkdownSource: String by lazy { source.strikethroughMarkdown() } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt index 92965f33d6..6cc142ec25 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/MessageEntity/textsources/UnderlineTextSource.kt @@ -8,7 +8,7 @@ class UnderlineTextSource( source: String, textParts: List ) : MultilevelTextSource { - override val textParts: List = source.fullListOfSubSource(textParts) + override val textParts: List by lazy { source.fullListOfSubSource(textParts) } override val asMarkdownSource: String by lazy { source.underlineMarkdown() } override val asMarkdownV2Source: String by lazy { underlineMarkdownV2() } override val asHtmlSource: String by lazy { underlineHTML() } From f4c11cacc63716312040cc118d42dae9156062b0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 5 Jan 2020 20:18:05 +0600 Subject: [PATCH 18/20] fix in formatting of hashtag text --- .../TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt index d510337afe..9c83aac3d7 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt @@ -126,8 +126,8 @@ internal fun MultilevelTextSource.mentionMarkdownV2(): String = "@${textParts.jo internal fun MultilevelTextSource.mentionHTML(): String = "@${textParts.joinSubSourcesHtml()}" -internal fun MultilevelTextSource.hashTagMarkdownV2(): String = "#${textParts.joinSubSourcesMarkdownV2()}" -internal fun MultilevelTextSource.hashTagHTML(): String = "#${textParts.joinSubSourcesHtml()}" +internal fun MultilevelTextSource.hashTagMarkdownV2(): String = "\\#${textParts.joinSubSourcesMarkdownV2()}" +internal fun MultilevelTextSource.hashTagHTML(): String = "\\#${textParts.joinSubSourcesHtml()}" internal fun MultilevelTextSource.phoneMarkdownV2(): String = textParts.joinSubSourcesMarkdownV2() From 2f09504f0875fec030cef136da2ba0d2bcf0fdac Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 5 Jan 2020 20:28:44 +0600 Subject: [PATCH 19/20] fix in formatting of hashtag text --- .../TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt index 9c83aac3d7..f54265fc05 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MultilevelTextSourceFormatting.kt @@ -127,7 +127,7 @@ internal fun MultilevelTextSource.mentionHTML(): String = "@${textParts.joinSubS internal fun MultilevelTextSource.hashTagMarkdownV2(): String = "\\#${textParts.joinSubSourcesMarkdownV2()}" -internal fun MultilevelTextSource.hashTagHTML(): String = "\\#${textParts.joinSubSourcesHtml()}" +internal fun MultilevelTextSource.hashTagHTML(): String = "#${textParts.joinSubSourcesHtml()}" internal fun MultilevelTextSource.phoneMarkdownV2(): String = textParts.joinSubSourcesMarkdownV2() From 33d8dcf97704f0c6ca540e33eb46d26bd22a6248 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 5 Jan 2020 20:30:42 +0600 Subject: [PATCH 20/20] update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bd147543b..f67426b952 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,11 @@ created `RegularTextSource` with the same text bot. * Added the field `slowModeDelay` to the `ExtendedSupergroupChat` objects. +* `CaptionedInput` now have extension `fullEntitiesList` which will return list of `TextPart` with `RegularSource`'s +* `TextPart` added - it will be used as part of some text and can be not related to telegram bot +* `MultilevelTextSource` was added - it is type of `TextSource`, which can have subsources as parts of this text +* In all `TextSource`s all fields now are lazy for avoiding of potential risk for performance issues + ## 0.20.0 MPP Migration * Time library change: `joda-time` -> `com.soywiz.korlibs.klock:klock`