1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-21 15:53:47 +00:00

potentially, add support of mentions in usernames

This commit is contained in:
InsanusMokrassar 2024-11-01 14:40:08 +06:00
parent 45bbcb5ec1
commit 50b2db8859
4 changed files with 25 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.message.textsources package dev.inmo.tgbotapi.types.message.textsources
import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.extensions.makeString import dev.inmo.tgbotapi.utils.extensions.makeString
import dev.inmo.tgbotapi.utils.internal.* import dev.inmo.tgbotapi.utils.internal.*
@ -13,6 +14,12 @@ data class CashTagTextSource @RiskFeature(DirectInvocationOfTextSourceConstructo
override val source: String, override val source: String,
override val subsources: TextSourcesList override val subsources: TextSourcesList
) : MultilevelTextSource { ) : MultilevelTextSource {
val username: Username? by lazy {
val potentialUsername = source.dropWhile { it != '@' }
if (potentialUsername.isEmpty()) return@lazy null
Username(potentialUsername)
}
override val markdown: String by lazy { source.cashTagMarkdown() } override val markdown: String by lazy { source.cashTagMarkdown() }
override val markdownV2: String by lazy { cashTagMarkdownV2() } override val markdownV2: String by lazy { cashTagMarkdownV2() }
override val html: String by lazy { cashTagHTML() } override val html: String by lazy { cashTagHTML() }

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.message.textsources package dev.inmo.tgbotapi.types.message.textsources
import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.extensions.makeString import dev.inmo.tgbotapi.utils.extensions.makeString
import dev.inmo.tgbotapi.utils.internal.* import dev.inmo.tgbotapi.utils.internal.*
@ -13,6 +14,13 @@ data class HashTagTextSource @RiskFeature(DirectInvocationOfTextSourceConstructo
override val source: String, override val source: String,
override val subsources: TextSourcesList override val subsources: TextSourcesList
) : MultilevelTextSource { ) : MultilevelTextSource {
val username: Username? by lazy {
val potentialUsername = source.dropWhile { it != '@' }
if (potentialUsername.isEmpty()) return@lazy null
Username(potentialUsername)
}
override val markdown: String by lazy { source.hashTagMarkdown() } override val markdown: String by lazy { source.hashTagMarkdown() }
override val markdownV2: String by lazy { hashTagMarkdownV2() } override val markdownV2: String by lazy { hashTagMarkdownV2() }
override val html: String by lazy { hashTagHTML() } override val html: String by lazy { hashTagHTML() }

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.MessageEntity package dev.inmo.tgbotapi.types.MessageEntity
import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.types.message.RawMessageEntity import dev.inmo.tgbotapi.types.message.RawMessageEntity
import dev.inmo.tgbotapi.types.message.asTextSources import dev.inmo.tgbotapi.types.message.asTextSources
import dev.inmo.tgbotapi.types.message.textsources.* import dev.inmo.tgbotapi.types.message.textsources.*
@ -8,14 +9,14 @@ import dev.inmo.tgbotapi.utils.extensions.makeSourceString
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertTrue import kotlin.test.assertTrue
const val testText = "It (is?) is simple hello world with #tag and @mention. Start of blockquote: Block quotation started\n" + const val testText = "It (is?) is simple hello world with #tag@sample and @mention. Start of blockquote: Block quotation started\n" +
"Block quotation continued\n" + "Block quotation continued\n" +
"The last line of the block quotation\n" + "The last line of the block quotation\n" +
". Start of expandable blockquote: Block quotation started\n" + ". Start of expandable blockquote: Block quotation started\n" +
"Block quotation continued\n" + "Block quotation continued\n" +
"The last line of the block quotation" "The last line of the block quotation"
const val formattedV2Text = "It \\(is?\\) *_is_ ~__simple__~* ||hello world|| with \\#tag and @mention\\. Start of blockquote: >Block quotation started\n>Block quotation continued\n>The last line of the block quotation\n\\. Start of expandable blockquote: **>Block quotation started\n>Block quotation continued\n>The last line of the block quotation||" const val formattedV2Text = "It \\(is?\\) *_is_ ~__simple__~* ||hello world|| with \\#tag@sample and @mention\\. Start of blockquote: >Block quotation started\n>Block quotation continued\n>The last line of the block quotation\n\\. Start of expandable blockquote: **>Block quotation started\n>Block quotation continued\n>The last line of the block quotation||"
const val formattedHtmlText = "It (is?) <b><i>is</i> <s><u>simple</u></s></b> <span class=\"tg-spoiler\">hello world</span> with #tag and @mention. Start of blockquote: <blockquote>Block quotation started\nBlock quotation continued\nThe last line of the block quotation</blockquote>\n. Start of expandable blockquote: <blockquote expandable>Block quotation started\nBlock quotation continued\nThe last line of the block quotation</blockquote>" const val formattedHtmlText = "It (is?) <b><i>is</i> <s><u>simple</u></s></b> <span class=\"tg-spoiler\">hello world</span> with #tag@sample and @mention. Start of blockquote: <blockquote>Block quotation started\nBlock quotation continued\nThe last line of the block quotation</blockquote>\n. Start of expandable blockquote: <blockquote expandable>Block quotation started\nBlock quotation continued\nThe last line of the block quotation</blockquote>"
internal val testTextEntities = listOf( internal val testTextEntities = listOf(
RawMessageEntity( RawMessageEntity(
"bold", "bold",
@ -45,21 +46,21 @@ internal val testTextEntities = listOf(
RawMessageEntity( RawMessageEntity(
"hashtag", "hashtag",
36, 36,
4 11
), ),
RawMessageEntity( RawMessageEntity(
"mention", "mention",
45, 52,
8 8
), ),
RawMessageEntity( RawMessageEntity(
"blockquote", "blockquote",
76, 83,
86 86
), ),
RawMessageEntity( RawMessageEntity(
"expandable_blockquote", "expandable_blockquote",
197, 204,
86 86
) )
) )
@ -71,6 +72,7 @@ fun TextSourcesList.testTextSources() {
assertTrue (get(3) is SpoilerTextSource) assertTrue (get(3) is SpoilerTextSource)
assertTrue (get(4) is RegularTextSource) assertTrue (get(4) is RegularTextSource)
assertTrue (get(5) is HashTagTextSource) assertTrue (get(5) is HashTagTextSource)
assertEquals(Username("@sample"), (get(5) as HashTagTextSource).username)
assertTrue (get(6) is RegularTextSource) assertTrue (get(6) is RegularTextSource)
assertTrue (get(7) is MentionTextSource) assertTrue (get(7) is MentionTextSource)
assertTrue (get(8) is RegularTextSource) assertTrue (get(8) is RegularTextSource)

View File

@ -46,7 +46,7 @@ class StringFormattingTests {
" " + " " +
spoiler("hello world") + spoiler("hello world") +
" with " + " with " +
hashtag("tag") + hashtag("tag@sample") +
" and " + " and " +
mention("mention") + mention("mention") +
". Start of blockquote: " + ". Start of blockquote: " +