diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a6d0e1d45..da7e0d6a38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ work with media groups lists ### 0.10.2 +* Fixes in `Username` + * Now you can create username object using string which is not starting with `@` + * Now `Username` correctly comparing with strings, which are not starting with `@` + ## 0.9.0 * Old extension `OkHttpClient.Builder#useWith` now deprecated and must be replaced by the same in diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt index 651a53c450..36c74899cd 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt @@ -19,12 +19,19 @@ fun Identifier.toChatId(): ChatId = ChatId(this) @Serializable(ChatIdentifierSerializer::class) data class Username( - val username: String + private val baseUsername: String ) : ChatIdentifier() { - init { - if (!username.startsWith("@")) { - throw IllegalArgumentException("Username must starts with `@`") - } + @Transient + val username: String = if (!baseUsername.startsWith("@")) { + "@$baseUsername" + } else { + baseUsername + } + + override fun equals(other: Any?): Boolean { + return super.equals(other) || other ?.let { + super.equals("@$it") + } ?: false } }