From 64244a60fefd7038203de609451610332884fcaf Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 31 Aug 2023 19:20:42 +0600 Subject: [PATCH] potential fix of incorrect parsing in 'RawMessageEntity' --- CHANGELOG.md | 3 ++ .../types/message/RawMessageEntity.kt | 28 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d97b803cc..d74864db7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 9.1.1 +* `Core`: + * Potential fix of incorrect parsing in `RawMessageEntity` + ## 9.1.0 **This update contains adding of [Telegram Bot API 6.8](https://core.telegram.org/bots/api-changelog#august-18-2023) support** diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessageEntity.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessageEntity.kt index 8d4e49fe0a..71a2e300cb 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessageEntity.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessageEntity.kt @@ -18,6 +18,29 @@ internal data class RawMessageEntity( internal val range by lazy { offset until (offset + length) } + + val weight by lazy { + when (type) { + "mention" -> 2 + "hashtag" -> 2 + "cashtag" -> 2 + "bot_command" -> 1 + "url" -> 1 + "email" -> 2 + "phone_number" -> 2 + "bold" -> 2 + "italic" -> 2 + "code" -> 1 + "pre" -> 1 + "text_link" -> 1 + "text_mention" -> 2 + "underline" -> 2 + "strikethrough" -> 2 + "spoiler" -> 2 + "custom_emoji" -> 2 + else -> 1 + } + } } internal fun RawMessageEntity.asTextSource( @@ -85,7 +108,10 @@ private fun createTextSources( originalFullString: String, entities: RawMessageEntities ): List> { - val mutableEntities = entities.toMutableList().apply { sortBy { it.offset } } + val mutableEntities = entities.toMutableList().apply { + sortByDescending { it.weight } + sortBy { it.offset } + } val resultList = mutableListOf>() while (mutableEntities.isNotEmpty()) {