1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-16 13:49:26 +00:00

RawChatId as value class

This commit is contained in:
2024-03-16 22:40:42 +06:00
parent ffba4e9cef
commit 8ca86b1e14
24 changed files with 153 additions and 55 deletions

View File

@@ -7,7 +7,11 @@ import kotlin.jvm.JvmInline
@JvmInline
value class CallbackQueryId(
val string: String
)
) {
override fun toString(): String {
return string
}
}
@Deprecated("Renamed", ReplaceWith("CallbackQueryId", "dev.inmo.tgbotapi.types.CallbackQueryId"))
typealias CallbackQueryIdentifier = CallbackQueryId

View File

@@ -26,13 +26,13 @@ sealed interface ChatIdentifier
*/
@Serializable(ChatIdentifierSerializer::class)
sealed interface IdChatIdentifier : ChatIdentifier {
abstract val chatId: Identifier
abstract val chatId: RawChatId
val threadId: MessageThreadId?
get() = null
companion object {
operator fun invoke(chatId: Identifier) = ChatId(chatId)
operator fun invoke(chatId: Identifier, threadId: MessageThreadId?) = threadId ?.let {
operator fun invoke(chatId: RawChatId) = ChatId(chatId)
operator fun invoke(chatId: RawChatId, threadId: MessageThreadId?) = threadId ?.let {
ChatIdWithThreadId(chatId, threadId)
} ?: ChatId(chatId)
}
@@ -40,17 +40,17 @@ sealed interface IdChatIdentifier : ChatIdentifier {
@Serializable(ChatIdentifierSerializer::class)
@JvmInline
value class ChatId(override val chatId: Identifier) : IdChatIdentifier
value class ChatId(override val chatId: RawChatId) : IdChatIdentifier
@Serializable(ChatIdentifierSerializer::class)
@JvmInline
value class ChatIdWithThreadId(val chatIdWithThreadId: Pair<Identifier, MessageThreadId>) : IdChatIdentifier {
override val chatId: Identifier
value class ChatIdWithThreadId(val chatIdWithThreadId: Pair<RawChatId, MessageThreadId>) : IdChatIdentifier {
override val chatId: RawChatId
get() = chatIdWithThreadId.first
override val threadId: MessageThreadId
get() = chatIdWithThreadId.second
constructor(chatId: Identifier, threadId: MessageThreadId): this(chatId to threadId)
constructor(chatId: RawChatId, threadId: MessageThreadId): this(chatId to threadId)
}
val ChatIdentifier.threadId: MessageThreadId?
@@ -67,7 +67,7 @@ fun IdChatIdentifier.toChatWithThreadId(threadId: MessageThreadId) = IdChatIdent
* https://core.telegram.org/bots/api#formatting-options
*/
@Warning("This API have restrictions in Telegram System")
val Identifier.userLink: String
val RawChatId.userLink: String
get() = "$internalUserLinkBeginning$this"
/**
* https://core.telegram.org/bots/api#formatting-options
@@ -80,9 +80,10 @@ val User.userLink: String
typealias UserId = ChatId
fun Identifier.toChatId(): ChatId = ChatId(this)
fun Int.toChatId(): IdChatIdentifier = toLong().toChatId()
fun Byte.toChatId(): IdChatIdentifier = toLong().toChatId()
fun RawChatId.toChatId(): ChatId = ChatId(this)
fun Long.toChatId(): ChatId = ChatId(RawChatId(this))
fun Int.toChatId(): IdChatIdentifier = RawChatId(toLong()).toChatId()
fun Byte.toChatId(): IdChatIdentifier = RawChatId(toLong()).toChatId()
@Serializable(ChatIdentifierSerializer::class)
@JvmInline
@@ -115,7 +116,7 @@ object ChatIdentifierSerializer : KSerializer<ChatIdentifier> {
val id = internalSerializer.deserialize(decoder)
return id.longOrNull ?.let {
ChatId(it)
ChatId(RawChatId(it))
} ?: id.content.let {
if (!it.startsWith("@")) {
Username("@$it")
@@ -127,7 +128,7 @@ object ChatIdentifierSerializer : KSerializer<ChatIdentifier> {
override fun serialize(encoder: Encoder, value: ChatIdentifier) {
when (value) {
is IdChatIdentifier -> encoder.encodeLong(value.chatId)
is IdChatIdentifier -> encoder.encodeLong(value.chatId.long)
is Username -> encoder.encodeString(value.full)
}
}
@@ -141,13 +142,13 @@ object FullChatIdentifierSerializer : KSerializer<ChatIdentifier> {
val id = internalSerializer.deserialize(decoder)
return id.longOrNull ?.let {
ChatId(it)
ChatId(RawChatId(it))
} ?:let {
val splitted = id.content.split("/")
if (splitted.size == 2) {
val (chatId, threadId) = splitted
ChatIdWithThreadId(
chatId.toLongOrNull() ?: return@let null,
chatId.toLongOrNull() ?.let(::RawChatId) ?: return@let null,
threadId.toLongOrNull() ?.let(::MessageThreadId) ?: return@let null
)
} else {
@@ -164,7 +165,7 @@ object FullChatIdentifierSerializer : KSerializer<ChatIdentifier> {
override fun serialize(encoder: Encoder, value: ChatIdentifier) {
when (value) {
is ChatId -> encoder.encodeLong(value.chatId)
is ChatId -> encoder.encodeLong(value.chatId.long)
is ChatIdWithThreadId -> encoder.encodeString("${value.chatId}/${value.threadId}")
is Username -> encoder.encodeString(value.full)
}

View File

@@ -9,7 +9,6 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.jvm.JvmInline
typealias Identifier = Long
typealias ForwardSignature = String
typealias ForwardSenderName = String
typealias AuthorSignature = ForwardSignature

View File

@@ -7,6 +7,10 @@ import kotlin.jvm.JvmInline
@JvmInline
value class InlineMessageId(
val string: String
)
) {
override fun toString(): String {
return string
}
}
@Deprecated("Renamed", ReplaceWith("InlineMessageId", "dev.inmo.tgbotapi.types.InlineMessageId"))
typealias InlineMessageIdentifier = InlineMessageId

View File

@@ -7,7 +7,11 @@ import kotlin.jvm.JvmInline
@JvmInline
value class InlineQueryId(
val string: String
)
) {
override fun toString(): String {
return string
}
}
@Deprecated("Renamed", ReplaceWith("InlineQueryId", "dev.inmo.tgbotapi.types.InlineQueryId"))
typealias InlineQueryIdentifier = InlineQueryId

View File

@@ -7,7 +7,11 @@ import kotlin.jvm.JvmInline
@JvmInline
value class MediaGroupId(
val string: String
)
) {
override fun toString(): String {
return string
}
}
@Deprecated("Renamed", ReplaceWith("MediaGroupId", "dev.inmo.tgbotapi.types.MediaGroupId"))
typealias MediaGroupIdentifier = MediaGroupId

View File

@@ -29,7 +29,11 @@ object MessageIdSerializer : KSerializer<MessageId> {
@JvmInline
value class MessageId(
val long: Long
)
) {
override fun toString(): String {
return long.toString()
}
}
fun Long.asTelegramMessageId() = MessageId(this)

View File

@@ -7,4 +7,8 @@ import kotlin.jvm.JvmInline
@JvmInline
value class MessageThreadId(
val long: Long
)
) {
override fun toString(): String {
return long.toString()
}
}

View File

@@ -7,4 +7,8 @@ import kotlin.jvm.JvmInline
@JvmInline
value class PreCheckoutQueryId(
val string: String
)
) {
override fun toString(): String {
return string
}
}

View File

@@ -0,0 +1,20 @@
package dev.inmo.tgbotapi.types
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline
@Serializable
@JvmInline
value class RawChatId(
val long: Long
) {
companion object {
val DefaultUserId = RawChatId(136817688L) // I do not know why, it is Telegram crutch
}
override fun toString(): String {
return long.toString()
}
}
@Deprecated("Renamed", ReplaceWith("RawChatId", "dev.inmo.tgbotapi.types.RawChatId"))
typealias Identifier = RawChatId

View File

@@ -7,7 +7,11 @@ import kotlin.jvm.JvmInline
@JvmInline
value class ShippingQueryId(
val string: String
)
) {
override fun toString(): String {
return string
}
}
@Deprecated("Renamed", ReplaceWith("ShippingQueryId", "dev.inmo.tgbotapi.types.ShippingQueryId"))
typealias ShippingQueryIdentifier = ShippingQueryId

View File

@@ -7,7 +7,11 @@ import kotlin.jvm.JvmInline
@JvmInline
value class TgFileUniqueId(
val string: String
)
) {
override fun toString(): String {
return string
}
}
@Deprecated(
"Renamed",
ReplaceWith("TgFileUniqueId", "dev.inmo.tgbotapi.types.TgFileUniqueId")

View File

@@ -14,6 +14,10 @@ value class UpdateId(
override fun compareTo(other: UpdateId): Int {
return long.compareTo(other.long)
}
override fun toString(): String {
return long.toString()
}
}
@Deprecated("Renamed", ReplaceWith("UpdateId", "dev.inmo.tgbotapi.types.UpdateId"))

View File

@@ -7,4 +7,8 @@ import kotlin.jvm.JvmInline
@JvmInline
value class WebAppQueryId(
val string: String
)
) {
override fun toString(): String {
return string
}
}

View File

@@ -31,9 +31,9 @@ inline fun mention(parts: TextSourcesList, userId: UserId) = mention(parts, Comm
@Suppress("NOTHING_TO_INLINE")
inline fun UserId.mention(parts: TextSourcesList) = mention(parts, this)
@Suppress("NOTHING_TO_INLINE")
inline fun mention(parts: TextSourcesList, id: Identifier) = mention(parts, UserId(id))
inline fun mention(parts: TextSourcesList, id: RawChatId) = mention(parts, UserId(id))
@Suppress("NOTHING_TO_INLINE")
inline fun Identifier.mention(parts: TextSourcesList) = mention(parts, this)
inline fun RawChatId.mention(parts: TextSourcesList) = mention(parts, this)
@Suppress("NOTHING_TO_INLINE")
inline fun mention(user: User, vararg parts: TextSource) = mention(
textSourcesOrElseTextSource(parts.toList()) {
@@ -50,6 +50,6 @@ inline fun mention(text: String, userId: UserId) = mention(text, CommonUser(user
@Suppress("NOTHING_TO_INLINE")
inline fun UserId.mention(text: String) = mention(text, this)
@Suppress("NOTHING_TO_INLINE")
inline fun mention(text: String, id: Identifier) = mention(text, UserId(id))
inline fun mention(text: String, id: RawChatId) = mention(text, UserId(id))
@Suppress("NOTHING_TO_INLINE")
inline fun Identifier.mention(text: String) = mention(text, this)
inline fun RawChatId.mention(text: String) = mention(text, this)

View File

@@ -43,7 +43,7 @@ sealed interface PollAnswer: FromUser {
companion object {
val defaultUser = CommonBot(
UserId(136817688L),
UserId(RawChatId.DefaultUserId),
"",
"",
Username("@Channel_Bot")

View File

@@ -4,19 +4,19 @@ import dev.inmo.tgbotapi.TestsJsonFormat
import kotlinx.serialization.Serializable
import kotlin.test.*
private const val chatIdentifierChatId: Identifier = 123L
private const val chatIdentifierLink = "tg://user?id=$chatIdentifierChatId"
private val chatIdentifierChatId: RawChatId = RawChatId(123L)
private val chatIdentifierLink = "tg://user?id=$chatIdentifierChatId"
private const val testUsername = "@Example"
class ChatIdentifierTests {
@Test
fun `Cast_from_Int_to_ChatId_is_working_correctly`() {
val chatId = chatIdentifierChatId.toInt().toChatId()
val chatId = chatIdentifierChatId.long.toInt().toChatId()
assertEquals(chatIdentifierChatId, chatId.chatId)
}
@Test
fun `Cast_from_Byte_to_ChatId_is_working_correctly`() {
val chatId = chatIdentifierChatId.toByte().toChatId()
val chatId = chatIdentifierChatId.long.toByte().toChatId()
assertEquals(chatIdentifierChatId, chatId.chatId)
}
@Test

View File

@@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.types.message.ChatEvents
import dev.inmo.tgbotapi.TestsJsonFormat
import dev.inmo.tgbotapi.extensions.utils.*
import dev.inmo.tgbotapi.types.IdChatIdentifier
import dev.inmo.tgbotapi.types.RawChatId
import dev.inmo.tgbotapi.types.update.abstracts.UpdateDeserializationStrategy
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -43,6 +44,6 @@ class MigratedToSupergroupTest {
val data = message.data.supergroupEventMessageOrThrow()
val event = data.chatEvent.migratedToSupergroupOrThrow()
assertEquals(IdChatIdentifier(57005), event.migratedFrom)
assertEquals(IdChatIdentifier(RawChatId(57005)), event.migratedFrom)
}
}