From 733ad342898a6602efd4d178d11b95871b32ec69 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 10 Nov 2022 09:11:13 +0600 Subject: [PATCH] Create ChatIdentifierWithThreadId.kt --- .../extensions/ChatIdentifierWithThreadId.kt | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/ChatIdentifierWithThreadId.kt diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/ChatIdentifierWithThreadId.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/ChatIdentifierWithThreadId.kt new file mode 100644 index 0000000000..e571a47bd5 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/ChatIdentifierWithThreadId.kt @@ -0,0 +1,36 @@ +package dev.inmo.tgbotapi.utils.extensions + +sealed interface ChatIdWithThreadId { + val chatId: ChatId + val threadId: MessageThreadId? + + // Light weight due to absence of any conversations + value class ByMessage( + val sourceMessage: Message + ) : ChatIdWithThreadId { + override val chatId: ChatId + get() = sourceMessage.chat.id + override val threadId: MessageThreadId? + get() = sourceMessage.threadIdOrNull + } + @Serializable + value class ByPair( + val pair: Pair + ) : ChatIdWithThreadId { + override val chatId: ChatId + get() = pair.first + override val threadId: MessageThreadId? + get() = pair.second + } + + companion { + inline operator fun invoke(message: Message) = ByMessage(message) + inline fun serializable(message: Message) = ByPair(message.chatId.id to message.threadIdOrNull) + inline fun serializable(pair: Pair) = ByPair(pair) + } +} + +val Message.chatIdWithThreadId + get() = ChatIdWithThreadId(this) +val Message.serializableChatIdWithThreadId + get() = ChatIdWithThreadId.serializable(this)