1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-23 00:57:13 +00:00

update ChatIdentifierWithThreadId and kdocs

This commit is contained in:
InsanusMokrassar 2022-11-10 12:05:59 +06:00
parent f2f15d7173
commit 236e47478f

View File

@ -2,15 +2,24 @@ package dev.inmo.tgbotapi.utils.extensions
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.utils.extensions.ChatIdWithThreadId.ByPair
import kotlinx.serialization.Serializable
import kotlin.Pair
import dev.inmo.tgbotapi.types.message.abstract.Message
import kotlin.jvm.JvmInline
/**
* Union to keep both [ChatId] and optionally [MessageThreadId] as an identifier of target for messages sending and
* other information
*/
sealed interface ChatIdWithThreadId {
val chatId: ChatId
val threadId: MessageThreadId?
// Light weight due to absence of any conversations
/**
* Lightweight variant of [ChatIdWithThreadId] due to absence of any conversations
*/
@JvmInline
value class ByMessage(
val sourceMessage: Message
) : ChatIdWithThreadId {
@ -19,7 +28,15 @@ sealed interface ChatIdWithThreadId {
override val threadId: MessageThreadId?
get() = sourceMessage.threadIdOrNull
}
/**
* [Serializable] variant of [ChatIdWithThreadId] based on [Pair] of target [ChatId] and [MessageThreadId]
*
* @see invoke
* @see serializable
*/
@Serializable
@JvmInline
value class ByPair(
val pair: Pair<ChatId, MessageThreadId?>
) : ChatIdWithThreadId {
@ -29,14 +46,32 @@ sealed interface ChatIdWithThreadId {
get() = pair.second
}
companion {
companion object {
/**
* Creates lightweight [ByMessage] variant of [ChatIdWithThreadId]
*/
inline operator fun invoke(message: Message) = ByMessage(message)
inline fun serializable(message: Message) = ByPair(message.chatId.id to message.threadIdOrNull)
/**
* Creates [ByPair] variant of [ChatIdWithThreadId] using incoming [message] [Message.chat] and extension
* [Message.threadIdOrNull]
*/
inline fun serializable(message: Message) = ByPair(message.chat.id to message.threadIdOrNull)
/**
* Creates [ByPair] variant of [ChatIdWithThreadId] using incoming [pair]
*/
inline fun serializable(pair: Pair<ChatId, MessageThreadId?>) = ByPair(pair)
}
}
/**
* Creates [ChatIdWithThreadId.ByMessage] variant of [ChatIdWithThreadId] using [ChatIdWithThreadId.serializable]
*/
val Message.chatIdWithThreadId
get() = ChatIdWithThreadId(this)
/**
* Creates [ChatIdWithThreadId.ByPair] variant of [ChatIdWithThreadId] using [ChatIdWithThreadId.serializable]
*/
val Message.serializableChatIdWithThreadId
get() = ChatIdWithThreadId.serializable(this)