tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatIdentifier.kt

88 lines
2.5 KiB
Kotlin
Raw Normal View History

2020-10-04 11:06:30 +00:00
package dev.inmo.tgbotapi.types
2018-12-26 08:07:24 +00:00
2021-02-09 11:15:28 +00:00
import dev.inmo.micro_utils.common.Warning
2022-04-21 18:16:41 +00:00
import dev.inmo.tgbotapi.types.chat.User
2021-05-29 09:34:14 +00:00
import dev.inmo.tgbotapi.utils.RiskFeature
2021-06-28 05:12:51 +00:00
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
2021-05-29 09:34:14 +00:00
import kotlinx.serialization.descriptors.SerialDescriptor
2020-08-18 06:50:11 +00:00
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
2020-11-05 18:12:14 +00:00
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.longOrNull
2018-12-26 08:07:24 +00:00
2022-07-09 16:43:19 +00:00
const val internalLinkBeginning = "https://t.me"
2018-12-26 08:07:24 +00:00
@Serializable(ChatIdentifierSerializer::class)
sealed class ChatIdentifier
/**
* Also used as User Identifier
*/
@Serializable(ChatIdentifierSerializer::class)
data class ChatId(
2018-12-26 08:07:24 +00:00
val chatId: Identifier
) : ChatIdentifier()
2021-02-09 11:15:28 +00:00
/**
* https://core.telegram.org/bots/api#formatting-options
*/
@Warning("This API have restrictions in Telegram System")
2022-07-09 16:43:19 +00:00
val Identifier.userLink: String
2021-02-09 11:15:28 +00:00
get() = "tg://user?id=$this"
/**
* https://core.telegram.org/bots/api#formatting-options
*/
@Warning("This API have restrictions in Telegram System")
2022-07-09 16:43:19 +00:00
val UserId.userLink: String
get() = chatId.userLink
2021-02-09 11:15:28 +00:00
val User.link: String
2022-07-09 16:43:19 +00:00
get() = id.userLink
2018-12-26 08:07:24 +00:00
typealias UserId = ChatId
fun Identifier.toChatId(): ChatId = ChatId(this)
2019-06-02 14:59:24 +00:00
fun Int.toChatId(): ChatId = toLong().toChatId()
fun Byte.toChatId(): ChatId = toLong().toChatId()
2018-12-26 08:07:24 +00:00
@Serializable(ChatIdentifierSerializer::class)
data class Username(
2019-02-18 06:35:58 +00:00
val username: String
2018-12-26 08:07:24 +00:00
) : ChatIdentifier() {
2022-02-07 12:08:06 +00:00
val usernameWithoutAt
get() = username.dropWhile { it == '@' }
2019-02-18 06:35:58 +00:00
init {
if (!username.startsWith("@")) {
throw IllegalArgumentException("Username must starts with `@`")
2019-02-18 04:53:01 +00:00
}
2018-12-26 08:07:24 +00:00
}
}
2019-06-02 14:57:52 +00:00
fun String.toUsername(): Username = Username(this)
2018-12-26 08:07:24 +00:00
2021-05-29 09:34:14 +00:00
@RiskFeature
object ChatIdentifierSerializer : KSerializer<ChatIdentifier> {
private val internalSerializer = JsonPrimitive.serializer()
override val descriptor: SerialDescriptor = internalSerializer.descriptor
2019-02-21 06:21:33 +00:00
override fun deserialize(decoder: Decoder): ChatIdentifier {
2021-05-29 09:34:14 +00:00
val id = internalSerializer.deserialize(decoder)
2020-03-22 09:53:37 +00:00
return id.longOrNull ?.let {
2018-12-26 08:07:24 +00:00
ChatId(it)
2020-03-22 09:53:37 +00:00
} ?: id.content.let {
if (!it.startsWith("@")) {
Username("@$it")
} else {
Username(it)
}
2019-02-18 06:35:58 +00:00
}
2018-12-26 08:07:24 +00:00
}
2020-03-22 07:37:01 +00:00
override fun serialize(encoder: Encoder, value: ChatIdentifier) {
when (value) {
is ChatId -> encoder.encodeLong(value.chatId)
is Username -> encoder.encodeString(value.username)
2018-12-26 08:07:24 +00:00
}
}
2021-05-29 09:34:14 +00:00
}