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
import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.extensions.makeString
import dev.inmo.tgbotapi.utils.internal.*
@ -13,6 +14,12 @@ data class CashTagTextSource @RiskFeature(DirectInvocationOfTextSourceConstructo
override val source: String,
override val subsources: TextSourcesList
) : 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 markdownV2: String by lazy { cashTagMarkdownV2() }
override val html: String by lazy { cashTagHTML() }

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.message.textsources
import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.extensions.makeString
import dev.inmo.tgbotapi.utils.internal.*
@ -13,6 +14,13 @@ data class HashTagTextSource @RiskFeature(DirectInvocationOfTextSourceConstructo
override val source: String,
override val subsources: TextSourcesList
) : 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 markdownV2: String by lazy { hashTagMarkdownV2() }
override val html: String by lazy { hashTagHTML() }

View File

@ -1,5 +1,6 @@
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.asTextSources
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.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" +
"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 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 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@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(
RawMessageEntity(
"bold",
@ -45,21 +46,21 @@ internal val testTextEntities = listOf(
RawMessageEntity(
"hashtag",
36,
4
11
),
RawMessageEntity(
"mention",
45,
52,
8
),
RawMessageEntity(
"blockquote",
76,
83,
86
),
RawMessageEntity(
"expandable_blockquote",
197,
204,
86
)
)
@ -71,6 +72,7 @@ fun TextSourcesList.testTextSources() {
assertTrue (get(3) is SpoilerTextSource)
assertTrue (get(4) is RegularTextSource)
assertTrue (get(5) is HashTagTextSource)
assertEquals(Username("@sample"), (get(5) as HashTagTextSource).username)
assertTrue (get(6) is RegularTextSource)
assertTrue (get(7) is MentionTextSource)
assertTrue (get(8) is RegularTextSource)

View File

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