1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

improvements in Username

This commit is contained in:
InsanusMokrassar 2024-01-07 21:56:31 +06:00
parent e28f3492ef
commit 60e50f9492
2 changed files with 14 additions and 10 deletions

View File

@ -87,12 +87,16 @@ fun Byte.toChatId(): IdChatIdentifier = toLong().toChatId()
@Serializable(ChatIdentifierSerializer::class) @Serializable(ChatIdentifierSerializer::class)
@JvmInline @JvmInline
value class Username( value class Username(
@Deprecated("Renamed", ReplaceWith("full"))
val username: String val username: String
) : ChatIdentifier { ) : ChatIdentifier {
val usernameWithoutAt
get() = username.dropWhile { it == '@' }
val full: String val full: String
get() = username get() = username
val withoutAt
get() = full.dropWhile { it == '@' }
@Deprecated("Renamed", ReplaceWith("withoutAt"))
val usernameWithoutAt
get() = withoutAt
init { init {
if (!username.startsWith("@")) { if (!username.startsWith("@")) {

View File

@ -21,24 +21,24 @@ fun makeInternalTgUsernameDeepLinkPrefix(username: String) = "${makeInternalTgUs
fun makeUsernameStartattachPrefix(username: String) = "$internalLinkBeginning/$username?startattach" fun makeUsernameStartattachPrefix(username: String) = "$internalLinkBeginning/$username?startattach"
fun makeUsernameStartattachLink(username: String, data: String? = null) = "${makeUsernameStartattachPrefix(username)}${data?.let { "=$it" } ?: ""}" fun makeUsernameStartattachLink(username: String, data: String? = null) = "${makeUsernameStartattachPrefix(username)}${data?.let { "=$it" } ?: ""}"
inline val Username.usernameLink inline val Username.usernameLink
get() = makeUsernameLink(usernameWithoutAt) get() = makeUsernameLink(withoutAt)
val IdChatIdentifier.chatLink: String val IdChatIdentifier.chatLink: String
get() = makeChatLink(chatId, threadId) get() = makeChatLink(chatId, threadId)
fun ChatId.link(threadId: MessageThreadId?) = makeChatLink(chatId, threadId) fun ChatId.link(threadId: MessageThreadId?) = makeChatLink(chatId, threadId)
inline fun Username.link(threadId: MessageThreadId?) = makeUsernameLink(usernameWithoutAt, threadId) inline fun Username.link(threadId: MessageThreadId?) = makeUsernameLink(withoutAt, threadId)
inline val Username.deepLinkPrefix inline val Username.deepLinkPrefix
get() = makeUsernameDeepLinkPrefix(usernameWithoutAt) get() = makeUsernameDeepLinkPrefix(withoutAt)
inline val Username.startattachPrefix inline val Username.startattachPrefix
get() = makeUsernameStartattachPrefix(usernameWithoutAt) get() = makeUsernameStartattachPrefix(withoutAt)
inline fun makeLink(username: Username, threadId: MessageThreadId? = null) = username.link(threadId) inline fun makeLink(username: Username, threadId: MessageThreadId? = null) = username.link(threadId)
inline fun makeTelegramDeepLink(username: String, startParameter: String) = "${makeUsernameDeepLinkPrefix(username)}$startParameter".encodeURLQueryComponent() inline fun makeTelegramDeepLink(username: String, startParameter: String) = "${makeUsernameDeepLinkPrefix(username)}$startParameter".encodeURLQueryComponent()
inline fun makeInternalTgDeepLink(username: String, startParameter: String) = "${makeInternalTgUsernameDeepLinkPrefix(username)}$startParameter".encodeURLQueryComponent() inline fun makeInternalTgDeepLink(username: String, startParameter: String) = "${makeInternalTgUsernameDeepLinkPrefix(username)}$startParameter".encodeURLQueryComponent()
inline fun makeInternalTgDeepLink(username: Username, startParameter: String) = inline fun makeInternalTgDeepLink(username: Username, startParameter: String) =
makeInternalTgDeepLink(username.usernameWithoutAt, startParameter) makeInternalTgDeepLink(username.withoutAt, startParameter)
inline fun makeTelegramStartattach(username: String, data: String? = null) = makeUsernameStartattachLink(username, data) inline fun makeTelegramStartattach(username: String, data: String? = null) = makeUsernameStartattachLink(username, data)
inline fun makeDeepLink(username: Username, startParameter: String) = makeTelegramDeepLink(username.usernameWithoutAt, startParameter) inline fun makeDeepLink(username: Username, startParameter: String) = makeTelegramDeepLink(username.withoutAt, startParameter)
inline fun makeTelegramDeepLink(username: Username, startParameter: String) = makeDeepLink(username, startParameter) inline fun makeTelegramDeepLink(username: Username, startParameter: String) = makeDeepLink(username, startParameter)
inline fun makeTelegramStartattach(username: Username, data: String? = null) = makeTelegramStartattach(username.usernameWithoutAt, data) inline fun makeTelegramStartattach(username: Username, data: String? = null) = makeTelegramStartattach(username.withoutAt, data)
private val linkIdRedundantPartRegex = Regex("^-100") private val linkIdRedundantPartRegex = Regex("^-100")
private val usernameBeginSymbolRegex = Regex("^@") private val usernameBeginSymbolRegex = Regex("^@")
@ -52,7 +52,7 @@ fun makeLinkToMessage(
username: Username, username: Username,
messageId: MessageId, messageId: MessageId,
threadId: MessageThreadId? = null threadId: MessageThreadId? = null
): String = makeLinkToMessage(username.usernameWithoutAt, messageId, threadId) ): String = makeLinkToMessage(username.withoutAt, messageId, threadId)
fun makeLinkToMessage( fun makeLinkToMessage(
chatId: Identifier, chatId: Identifier,
messageId: MessageId, messageId: MessageId,