diff --git a/CHANGELOG.md b/CHANGELOG.md index aaa9f86432..527eeb9b62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,10 +24,13 @@ * Two new dsl: * `inlineKeyboard` for creating `InlineKeyboardMarkup` * `replyKeyboard` for creating `ReplyKeyboardMarkup` - * Cast helpers for `Message`: + * Cast helpers for `Message` (thanks to [madhead](https://github.com/madhead)): * `asPossiblyReplyMessage`: tries to cast a `Message` to `PossiblyReplyMessage`, returns `null` if the message is not of that type * `requirePossiblyReplyMessage`: casts a `Message` to `PossiblyReplyMessage`, fails if the message is not of that type * `whenPossiblyReplyMessage`: tries to cast a `Message` to `PossiblyReplyMessage` and runs the given block of code with it, if the cast is successful + * New type `WithUser` for unioning of all types with `user` + * `FromUser` now extends `WithUser` + * Cast helpers for type `WithUser`: `asWithUser`, `whenWithUser`, `requireWithUser` * `Behaviour Builder`: * New expecters and waiters: * `waitShippingQueries`/`onShippingQuery` diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/FromUser.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/FromUser.kt index d45e65b52f..698f36c45a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/FromUser.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/FromUser.kt @@ -2,6 +2,15 @@ package dev.inmo.tgbotapi.CommonAbstracts import dev.inmo.tgbotapi.types.User -interface FromUser { - val user: User -} \ No newline at end of file +/** + * Inheritors of this interface have some [User] as a source of data. For example, any [dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery] + * have [User] as the source of that query + */ +interface FromUser : WithUser { + /** + * The source [User] of this type + */ + val from: User + override val user: User + get() = from +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/WithUser.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/WithUser.kt new file mode 100644 index 0000000000..765a8d1e13 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/WithUser.kt @@ -0,0 +1,12 @@ +package dev.inmo.tgbotapi.CommonAbstracts + +import dev.inmo.tgbotapi.types.User + +/** + * All inheritors of this type have [User] in their data as one of the main data + * + * @see FromUser + */ +interface WithUser { + val user: User +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/CallbackQuery.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/CallbackQuery.kt index 3ab043ed28..dbbd8a2699 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/CallbackQuery.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/CallbackQuery.kt @@ -11,7 +11,7 @@ sealed interface CallbackQuery : FromUser { data class UnknownCallbackQueryType( override val id: CallbackQueryIdentifier, - override val user: User, + override val from: User, override val chatInstance: String, val raw: String ) : CallbackQuery diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/InlineMessageIdDataCallbackQuery.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/InlineMessageIdDataCallbackQuery.kt index aa4ecf9f70..bdb013340e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/InlineMessageIdDataCallbackQuery.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/InlineMessageIdDataCallbackQuery.kt @@ -4,7 +4,7 @@ import dev.inmo.tgbotapi.types.* data class InlineMessageIdDataCallbackQuery( override val id: CallbackQueryIdentifier, - override val user: User, + override val from: User, override val chatInstance: String, override val inlineMessageId: InlineMessageIdentifier, override val data: String diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/InlineMessageIdGameShortNameCallbackQuery.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/InlineMessageIdGameShortNameCallbackQuery.kt index f54793fcd4..8369e71332 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/InlineMessageIdGameShortNameCallbackQuery.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/InlineMessageIdGameShortNameCallbackQuery.kt @@ -4,7 +4,7 @@ import dev.inmo.tgbotapi.types.* data class InlineMessageIdGameShortNameCallbackQuery( override val id: CallbackQueryIdentifier, - override val user: User, + override val from: User, override val chatInstance: String, override val inlineMessageId: InlineMessageIdentifier, override val gameShortName: String diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/MessageDataCallbackQuery.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/MessageDataCallbackQuery.kt index 9f6047197f..e276bd219e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/MessageDataCallbackQuery.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/MessageDataCallbackQuery.kt @@ -6,7 +6,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.Message data class MessageDataCallbackQuery( override val id: CallbackQueryIdentifier, - override val user: User, + override val from: User, override val chatInstance: String, override val message: Message, override val data: String diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/MessageGameShortNameCallbackQuery.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/MessageGameShortNameCallbackQuery.kt index f026675b13..208f820179 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/MessageGameShortNameCallbackQuery.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/CallbackQuery/MessageGameShortNameCallbackQuery.kt @@ -6,7 +6,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.Message data class MessageGameShortNameCallbackQuery( override val id: CallbackQueryIdentifier, - override val user: User, + override val from: User, override val chatInstance: String, override val message: Message, override val gameShortName: String diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatMember.kt index 2551a7d017..8b6de401ab 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatMember.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.types.ChatMember.abstracts +import dev.inmo.tgbotapi.CommonAbstracts.WithUser import dev.inmo.tgbotapi.types.ChatMember.* import dev.inmo.tgbotapi.types.User import dev.inmo.tgbotapi.types.statusField @@ -14,9 +15,7 @@ import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonPrimitive @Serializable(ChatMemberSerializer::class) -sealed interface ChatMember { - val user: User -} +sealed interface ChatMember : WithUser @RiskFeature object ChatMemberSerializer : KSerializer { diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/ChosenInlineResult/BaseChosenInlineResult.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/ChosenInlineResult/BaseChosenInlineResult.kt index bf85756e09..a19a61d7a0 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/ChosenInlineResult/BaseChosenInlineResult.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/ChosenInlineResult/BaseChosenInlineResult.kt @@ -8,7 +8,7 @@ import kotlinx.serialization.Serializable data class BaseChosenInlineResult( override val resultId: InlineQueryIdentifier, @SerialName(fromField) - override val user: User, + override val from: User, override val inlineMessageId: InlineMessageIdentifier?, override val query: String ) : ChosenInlineResult diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/ChosenInlineResult/LocationChosenInlineResult.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/ChosenInlineResult/LocationChosenInlineResult.kt index 30388e7e6f..c56bc5e520 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/ChosenInlineResult/LocationChosenInlineResult.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/ChosenInlineResult/LocationChosenInlineResult.kt @@ -9,7 +9,7 @@ import kotlinx.serialization.Serializable data class LocationChosenInlineResult( override val resultId: InlineQueryIdentifier, @SerialName(fromField) - override val user: User, + override val from: User, val location: StaticLocation, override val inlineMessageId: InlineMessageIdentifier?, override val query: String diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/query/InlineQuery.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/query/InlineQuery.kt index 2e7c093d8d..f94e97b18e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/query/InlineQuery.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InlineQueries/query/InlineQuery.kt @@ -1,12 +1,11 @@ package dev.inmo.tgbotapi.types.InlineQueries.query +import dev.inmo.tgbotapi.CommonAbstracts.FromUser import dev.inmo.tgbotapi.types.InlineQueryIdentifier -import dev.inmo.tgbotapi.types.User import dev.inmo.tgbotapi.types.chat.ChatType -sealed interface InlineQuery { +sealed interface InlineQuery : FromUser { val id: InlineQueryIdentifier - val from: User val query: String val offset: String val chatType: ChatType? diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChatEvents/LeftChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChatEvents/LeftChatMember.kt index a8b3bf1668..925cfb35bb 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChatEvents/LeftChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChatEvents/LeftChatMember.kt @@ -1,8 +1,9 @@ package dev.inmo.tgbotapi.types.message.ChatEvents +import dev.inmo.tgbotapi.CommonAbstracts.WithUser import dev.inmo.tgbotapi.types.User import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.PublicChatEvent data class LeftChatMember( - val user: User -) : PublicChatEvent + override val user: User +) : PublicChatEvent, WithUser diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt index 03e228f693..2e9b602668 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt @@ -9,7 +9,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.GroupEventMessage data class CommonGroupEventMessage( override val messageId: MessageIdentifier, - override val user: User, + override val from: User, override val chat: GroupChat, override val chatEvent: T, override val date: DateTime diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt index 49d1118e57..28bbacf8bc 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt @@ -9,7 +9,7 @@ import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent data class CommonMediaGroupMessage( override val messageId: MessageIdentifier, - override val user: User, + override val from: User, override val chat: Chat, override val date: DateTime, override val mediaGroupId: MediaGroupIdentifier, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt index b63a7dbf1b..124fe0cac6 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt @@ -9,7 +9,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.SupergroupEventMessage data class CommonSupergroupEventMessage( override val messageId: MessageIdentifier, - override val user: User, + override val from: User, override val chat: SupergroupChat, override val chatEvent: T, override val date: DateTime diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ForwardInfo.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ForwardInfo.kt index 7ca6f21d87..f239b9d235 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ForwardInfo.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ForwardInfo.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.types.message +import dev.inmo.tgbotapi.CommonAbstracts.FromUser import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.chat.abstracts.ChannelChat import dev.inmo.tgbotapi.types.chat.abstracts.SupergroupChat @@ -15,8 +16,8 @@ data class AnonymousForwardInfo( data class UserForwardInfo( override val dateOfOriginal: TelegramDate, - val from: User -) : ForwardInfo() + override val from: User +) : ForwardInfo(), FromUser data class ForwardFromChannelInfo( override val dateOfOriginal: TelegramDate, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/GroupMessages.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/GroupMessages.kt index e24405ba79..0fee420bf2 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/GroupMessages.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/GroupMessages.kt @@ -38,7 +38,7 @@ data class AnonymousGroupContentMessageImpl( data class CommonGroupContentMessageImpl( override val chat: GroupChat, override val messageId: MessageIdentifier, - override val user: User, + override val from: User, override val date: DateTime, override val forwardInfo: ForwardInfo?, override val editDate: DateTime?, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PassportMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PassportMessage.kt index 55153e159f..b249d704e2 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PassportMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PassportMessage.kt @@ -11,7 +11,7 @@ import dev.inmo.tgbotapi.types.passport.PassportData data class PassportMessage( override val messageId: MessageIdentifier, override val chat: Chat, - override val user: User, + override val from: User, override val date: DateTime, val passportData: PassportData ) : Message, FromUserMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateMessageImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateMessageImpl.kt index 599fce4c76..e08dd2d4ba 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateMessageImpl.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateMessageImpl.kt @@ -11,7 +11,7 @@ import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentEvent data class PrivateContentMessageImpl( override val messageId: MessageIdentifier, - override val user: User, + override val from: User, override val chat: Chat, override val content: T, override val date: DateTime, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/PreCheckoutQuery.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/PreCheckoutQuery.kt index dfdfe4a1a7..d221e408b2 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/PreCheckoutQuery.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/PreCheckoutQuery.kt @@ -12,7 +12,7 @@ data class PreCheckoutQuery( @SerialName(idField) val id: PreCheckoutQueryId, @SerialName(fromField) - override val user: User, + override val from: User, @SerialName(currencyField) override val currency: Currency, @SerialName(totalAmountField) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/ShippingQuery.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/ShippingQuery.kt index 7821211bf6..d72e2fef65 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/ShippingQuery.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/ShippingQuery.kt @@ -10,7 +10,7 @@ data class ShippingQuery( @SerialName(idField) val id: ShippingQueryIdentifier, @SerialName(fromField) - override val user: User, + override val from: User, @SerialName(invoicePayloadField) val invoicePayload: InvoicePayload, @SerialName(shippingAddressField) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/PollAnswer.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/PollAnswer.kt index c4ea74bfa0..0411d5e8ec 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/PollAnswer.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/PollAnswer.kt @@ -2,8 +2,7 @@ package dev.inmo.tgbotapi.types.polls import dev.inmo.tgbotapi.CommonAbstracts.FromUser import dev.inmo.tgbotapi.types.* -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable +import kotlinx.serialization.* @Serializable data class PollAnswer( @@ -13,4 +12,8 @@ data class PollAnswer( override val user: User, @SerialName(optionIdsField) val chosen: List -) : FromUser +) : FromUser { + @Transient + override val from: User + get() = user +} diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt index c89200d275..ae3d35a63c 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt @@ -2,8 +2,7 @@ package dev.inmo.tgbotapi.extensions.utils -import dev.inmo.tgbotapi.CommonAbstracts.CommonSendInvoiceData -import dev.inmo.tgbotapi.CommonAbstracts.FromUser +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.requests.send.payments.SendInvoice import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.CallbackQuery.* @@ -3154,6 +3153,15 @@ inline fun Any.asFromUser(): FromUser? = this as? FromUser @PreviewFeature inline fun Any.requireFromUser(): FromUser = this as FromUser +@PreviewFeature +inline fun Any.whenWithUser(block: (WithUser) -> T) = asWithUser() ?.let(block) + +@PreviewFeature +inline fun Any.asWithUser(): WithUser? = this as? WithUser + +@PreviewFeature +inline fun Any.requireWithUser(): WithUser = this as WithUser + @PreviewFeature inline fun Any.whenWithOptionalLanguageCode(block: (WithOptionalLanguageCode) -> T) = asWithOptionalLanguageCode() ?.let(block) diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/UpdateChatRetriever.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/UpdateChatRetriever.kt index a1f341c16b..9507f7052b 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/UpdateChatRetriever.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/UpdateChatRetriever.kt @@ -1,5 +1,7 @@ package dev.inmo.tgbotapi.extensions.utils.extensions +import dev.inmo.tgbotapi.CommonAbstracts.FromUser +import dev.inmo.tgbotapi.CommonAbstracts.WithUser import dev.inmo.tgbotapi.extensions.utils.asFromUser import dev.inmo.tgbotapi.extensions.utils.asUser import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat @@ -12,20 +14,24 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.utils.PreviewFeature @PreviewFeature -fun Update.sourceChat(): Chat? = when (this) { - is MediaGroupUpdate -> when (this) { +fun Update.sourceChat(): Chat? = when { + this is MediaGroupUpdate -> when (this) { is SentMediaGroupUpdate -> data.chat is EditMediaGroupUpdate -> data.chat } - is BaseMessageUpdate -> data.chat - is InlineQueryUpdate -> data.from - is ChosenInlineResultUpdate -> data.user - is CallbackQueryUpdate -> data.user - is PreCheckoutQueryUpdate -> data.user - is PollAnswerUpdate -> data.user - is ShippingQueryUpdate -> data.user - else -> null + this is BaseMessageUpdate -> data.chat + else -> { + when (val data = data) { + is FromUser -> data.from + is WithUser -> data.user + else -> null + } + } } @PreviewFeature -fun Update.sourceUser(): User? = data.asFromUser()?.user ?: sourceChat()?.asUser() +fun Update.sourceUser(): User? = when (val data = data) { + is FromUser -> data.from + is WithUser -> data.user + else -> sourceChat()?.asUser() +}