mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-04 15:49:41 +00:00
updates in Username
This commit is contained in:
@@ -12,5 +12,5 @@ import kotlin.String
|
|||||||
public suspend
|
public suspend
|
||||||
fun TelegramBot.setBusinessAccountUsername(businessConnectionId: BusinessConnectionId,
|
fun TelegramBot.setBusinessAccountUsername(businessConnectionId: BusinessConnectionId,
|
||||||
username: String): Boolean = setBusinessAccountUsername(
|
username: String): Boolean = setBusinessAccountUsername(
|
||||||
businessConnectionId = businessConnectionId, username = with(username) { Username(username) }
|
businessConnectionId = businessConnectionId, username = with(username) { Username.prepare(username) }
|
||||||
)
|
)
|
||||||
|
@@ -13436,6 +13436,7 @@ public final class dev/inmo/tgbotapi/types/Username : dev/inmo/tgbotapi/types/Ch
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final class dev/inmo/tgbotapi/types/Username$Companion {
|
public final class dev/inmo/tgbotapi/types/Username$Companion {
|
||||||
|
public final fun prepare-BnpbnlE (Ljava/lang/String;)Ljava/lang/String;
|
||||||
public final fun serializer ()Lkotlinx/serialization/KSerializer;
|
public final fun serializer ()Lkotlinx/serialization/KSerializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -106,13 +106,34 @@ fun Long.toChatId(): ChatId = ChatId(RawChatId(this))
|
|||||||
fun Int.toChatId(): IdChatIdentifier = RawChatId(toLong()).toChatId()
|
fun Int.toChatId(): IdChatIdentifier = RawChatId(toLong()).toChatId()
|
||||||
fun Byte.toChatId(): IdChatIdentifier = RawChatId(toLong()).toChatId()
|
fun Byte.toChatId(): IdChatIdentifier = RawChatId(toLong()).toChatId()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A value class representing a username that always starts with the "@" symbol.
|
||||||
|
*
|
||||||
|
* This class is used to encapsulate the concept of a username, enforce its format,
|
||||||
|
* and ensure consistency when dealing with usernames throughout the application.
|
||||||
|
*
|
||||||
|
* @property full The full username string, guaranteed to start with "@".
|
||||||
|
* @throws IllegalArgumentException if the provided [full] value doesn't start with "@" during initialization.
|
||||||
|
*/
|
||||||
@Serializable(ChatIdentifierSerializer::class)
|
@Serializable(ChatIdentifierSerializer::class)
|
||||||
@JvmInline
|
@JvmInline
|
||||||
value class Username(
|
value class Username (
|
||||||
val full: String
|
val full: String
|
||||||
) : ChatIdentifier {
|
) : ChatIdentifier {
|
||||||
|
/**
|
||||||
|
* Retrieves the full username as a string.
|
||||||
|
*
|
||||||
|
* This property provides the complete username, which is guaranteed to start with the "@" symbol.
|
||||||
|
* It represents the raw value of the username, ensuring consistency and adherence to the required format.
|
||||||
|
*/
|
||||||
val username: String
|
val username: String
|
||||||
get() = full
|
get() = full
|
||||||
|
/**
|
||||||
|
* A property that returns the username string without the leading "@" symbol.
|
||||||
|
*
|
||||||
|
* This property removes any consecutive "@" symbols at the beginning of the `full` property
|
||||||
|
* and provides the rest of the username as a plain string.
|
||||||
|
*/
|
||||||
val withoutAt
|
val withoutAt
|
||||||
get() = full.dropWhile { it == '@' }
|
get() = full.dropWhile { it == '@' }
|
||||||
|
|
||||||
@@ -125,9 +146,24 @@ value class Username(
|
|||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return full
|
return full
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* Prepares a valid instance of [Username] by ensuring the given string starts with "@".
|
||||||
|
*
|
||||||
|
* @param full The input string representing the username. If the string does not start with "@",
|
||||||
|
* it will be prefixed with "@".
|
||||||
|
* @return A [Username] instance constructed using the provided or modified input string.
|
||||||
|
*/
|
||||||
|
fun prepare(full: String): Username = if (full.startsWith("@")) {
|
||||||
|
Username(full)
|
||||||
|
} else {
|
||||||
|
Username("@$full")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun String.toUsername(): Username = Username(this)
|
fun String.toUsername(): Username = Username.prepare(this)
|
||||||
|
|
||||||
@RiskFeature
|
@RiskFeature
|
||||||
object ChatIdentifierSerializer : KSerializer<ChatIdentifier> {
|
object ChatIdentifierSerializer : KSerializer<ChatIdentifier> {
|
||||||
@@ -139,11 +175,7 @@ object ChatIdentifierSerializer : KSerializer<ChatIdentifier> {
|
|||||||
return id.longOrNull ?.let {
|
return id.longOrNull ?.let {
|
||||||
ChatId(RawChatId(it))
|
ChatId(RawChatId(it))
|
||||||
} ?: id.content.let {
|
} ?: id.content.let {
|
||||||
if (!it.startsWith("@")) {
|
Username.prepare(it)
|
||||||
Username("@$it")
|
|
||||||
} else {
|
|
||||||
Username(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,11 +216,7 @@ object FullChatIdentifierSerializer : KSerializer<ChatIdentifier> {
|
|||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
} ?: id.content.let {
|
} ?: id.content.let {
|
||||||
if (!it.startsWith("@")) {
|
Username.prepare(it)
|
||||||
Username("@$it")
|
|
||||||
} else {
|
|
||||||
Username(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,7 +18,7 @@ data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstru
|
|||||||
CommandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!"
|
CommandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!"
|
||||||
}
|
}
|
||||||
val username: Username? by lazy {
|
val username: Username? by lazy {
|
||||||
Username(usernameRegex.find(source) ?.value ?: return@lazy null)
|
Username.prepare(usernameRegex.find(source) ?.value ?: return@lazy null)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val markdown: String by lazy { source.commandMarkdown() }
|
override val markdown: String by lazy { source.commandMarkdown() }
|
||||||
|
@@ -18,7 +18,7 @@ data class CashTagTextSource @RiskFeature(DirectInvocationOfTextSourceConstructo
|
|||||||
val potentialUsername = source.dropWhile { it != '@' }
|
val potentialUsername = source.dropWhile { it != '@' }
|
||||||
if (potentialUsername.isEmpty()) return@lazy null
|
if (potentialUsername.isEmpty()) return@lazy null
|
||||||
|
|
||||||
Username(potentialUsername)
|
Username.prepare(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() }
|
||||||
|
@@ -18,7 +18,7 @@ data class HashTagTextSource @RiskFeature(DirectInvocationOfTextSourceConstructo
|
|||||||
val potentialUsername = source.dropWhile { it != '@' }
|
val potentialUsername = source.dropWhile { it != '@' }
|
||||||
if (potentialUsername.isEmpty()) return@lazy null
|
if (potentialUsername.isEmpty()) return@lazy null
|
||||||
|
|
||||||
Username(potentialUsername)
|
Username.prepare(potentialUsername)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val markdown: String by lazy { source.hashTagMarkdown() }
|
override val markdown: String by lazy { source.hashTagMarkdown() }
|
||||||
|
@@ -24,7 +24,7 @@ data class MentionTextSource @RiskFeature(DirectInvocationOfTextSourceConstructo
|
|||||||
override val markdown: String by lazy { source.mentionMarkdown() }
|
override val markdown: String by lazy { source.mentionMarkdown() }
|
||||||
override val markdownV2: String by lazy { mentionMarkdownV2() }
|
override val markdownV2: String by lazy { mentionMarkdownV2() }
|
||||||
override val html: String by lazy { mentionHTML() }
|
override val html: String by lazy { mentionHTML() }
|
||||||
val username: Username = Username(source)
|
val username: Username = Username.prepare(source)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (!source.startsWith("@")) {
|
if (!source.startsWith("@")) {
|
||||||
|
@@ -34,14 +34,14 @@ fun WebAppUser.asUser() = if (isBot == true) {
|
|||||||
id = UserId(id),
|
id = UserId(id),
|
||||||
firstName = firstName,
|
firstName = firstName,
|
||||||
lastName = lastName ?: "",
|
lastName = lastName ?: "",
|
||||||
username = username ?.let(::Username)
|
username = username ?.let(Username::prepare)
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
CommonUser(
|
CommonUser(
|
||||||
id = UserId(id),
|
id = UserId(id),
|
||||||
firstName = firstName,
|
firstName = firstName,
|
||||||
lastName = lastName ?: "",
|
lastName = lastName ?: "",
|
||||||
username = username ?.let(::Username),
|
username = username ?.let(Username::prepare),
|
||||||
ietfLanguageCode = languageCode ?.let(::IetfLang),
|
ietfLanguageCode = languageCode ?.let(::IetfLang),
|
||||||
isPremium = isPremium
|
isPremium = isPremium
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user