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
|
|
|
|
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
|
2018-12-26 08:07:24 +00:00
|
|
|
import kotlinx.serialization.*
|
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
|
|
|
|
|
|
|
@Serializable(ChatIdentifierSerializer::class)
|
|
|
|
sealed class ChatIdentifier
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Also used as User Identifier
|
|
|
|
*/
|
|
|
|
@Serializable(ChatIdentifierSerializer::class)
|
2019-01-21 00:23:27 +00:00
|
|
|
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")
|
|
|
|
val Identifier.link: String
|
|
|
|
get() = "tg://user?id=$this"
|
|
|
|
/**
|
|
|
|
* https://core.telegram.org/bots/api#formatting-options
|
|
|
|
*/
|
|
|
|
@Warning("This API have restrictions in Telegram System")
|
|
|
|
val UserId.link: String
|
|
|
|
get() = chatId.link
|
|
|
|
val User.link: String
|
|
|
|
get() = id.link
|
2019-03-31 03:11:32 +00:00
|
|
|
|
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)
|
2019-01-21 00:23:27 +00:00
|
|
|
data class Username(
|
2019-02-18 06:35:58 +00:00
|
|
|
val username: String
|
2018-12-26 08:07:24 +00:00
|
|
|
) : ChatIdentifier() {
|
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
|
|
|
|
|
|
|
@Serializer(ChatIdentifier::class)
|
2019-11-29 06:25:22 +00:00
|
|
|
internal object ChatIdentifierSerializer : KSerializer<ChatIdentifier> {
|
2019-02-21 06:21:33 +00:00
|
|
|
override fun deserialize(decoder: Decoder): ChatIdentifier {
|
2020-08-18 06:50:11 +00:00
|
|
|
val id = JsonPrimitive.serializer().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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|