mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-22 16:47:13 +00:00
resolution of #484
This commit is contained in:
parent
65c1f018ac
commit
91212eaa3a
@ -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`
|
||||
|
@ -2,6 +2,15 @@ package dev.inmo.tgbotapi.CommonAbstracts
|
||||
|
||||
import dev.inmo.tgbotapi.types.User
|
||||
|
||||
interface FromUser {
|
||||
val user: User
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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<ChatMember> {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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?
|
||||
|
@ -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
|
||||
|
@ -9,7 +9,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.GroupEventMessage
|
||||
|
||||
data class CommonGroupEventMessage<T : GroupEvent>(
|
||||
override val messageId: MessageIdentifier,
|
||||
override val user: User,
|
||||
override val from: User,
|
||||
override val chat: GroupChat,
|
||||
override val chatEvent: T,
|
||||
override val date: DateTime
|
||||
|
@ -9,7 +9,7 @@ import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
|
||||
|
||||
data class CommonMediaGroupMessage<T : MediaGroupContent>(
|
||||
override val messageId: MessageIdentifier,
|
||||
override val user: User,
|
||||
override val from: User,
|
||||
override val chat: Chat,
|
||||
override val date: DateTime,
|
||||
override val mediaGroupId: MediaGroupIdentifier,
|
||||
|
@ -9,7 +9,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.SupergroupEventMessage
|
||||
|
||||
data class CommonSupergroupEventMessage<T : SupergroupEvent>(
|
||||
override val messageId: MessageIdentifier,
|
||||
override val user: User,
|
||||
override val from: User,
|
||||
override val chat: SupergroupChat,
|
||||
override val chatEvent: T,
|
||||
override val date: DateTime
|
||||
|
@ -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,
|
||||
|
@ -38,7 +38,7 @@ data class AnonymousGroupContentMessageImpl<T : MessageContent>(
|
||||
data class CommonGroupContentMessageImpl<T : MessageContent>(
|
||||
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?,
|
||||
|
@ -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
|
||||
|
@ -11,7 +11,7 @@ import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentEvent
|
||||
|
||||
data class PrivateContentMessageImpl<T: MessageContent>(
|
||||
override val messageId: MessageIdentifier,
|
||||
override val user: User,
|
||||
override val from: User,
|
||||
override val chat: Chat,
|
||||
override val content: T,
|
||||
override val date: DateTime,
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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<Int>
|
||||
) : FromUser
|
||||
) : FromUser {
|
||||
@Transient
|
||||
override val from: User
|
||||
get() = user
|
||||
}
|
||||
|
@ -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 <T> 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 <T> Any.whenWithOptionalLanguageCode(block: (WithOptionalLanguageCode) -> T) = asWithOptionalLanguageCode() ?.let(block)
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user