1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00
This commit is contained in:
InsanusMokrassar 2022-11-08 01:51:27 +06:00
parent 47f514a635
commit b32ce88a97
33 changed files with 120 additions and 307 deletions

View File

@ -2,31 +2,6 @@ package dev.inmo.tgbotapi.extensions.api.InternalUtils
import dev.inmo.tgbotapi.types.MediaGroupIdentifier import dev.inmo.tgbotapi.types.MediaGroupIdentifier
import dev.inmo.tgbotapi.types.UpdateIdentifier import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.message.AnonymousForumContentMessageImpl
import dev.inmo.tgbotapi.types.message.AnonymousGroupContentMessageImpl
import dev.inmo.tgbotapi.types.message.ChannelContentMessageImpl
import dev.inmo.tgbotapi.types.message.CommonForumContentMessageImpl
import dev.inmo.tgbotapi.types.message.CommonGroupContentMessageImpl
import dev.inmo.tgbotapi.types.message.ConnectedFromChannelGroupContentMessageImpl
import dev.inmo.tgbotapi.types.message.FromChannelForumContentMessageImpl
import dev.inmo.tgbotapi.types.message.PrivateContentMessageImpl
import dev.inmo.tgbotapi.types.message.UnconnectedFromChannelGroupContentMessageImpl
import dev.inmo.tgbotapi.types.message.abstracts.AnonymousForumContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.AnonymousGroupContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.ChannelContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.CommonForumContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.abstracts.ConnectedFromChannelGroupContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.FromChannelForumContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage
import dev.inmo.tgbotapi.types.message.abstracts.PrivateContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.UnconnectedFromChannelGroupContentMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent
import dev.inmo.tgbotapi.types.message.content.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.types.update.* import dev.inmo.tgbotapi.types.update.*
import dev.inmo.tgbotapi.types.update.abstracts.* import dev.inmo.tgbotapi.types.update.abstracts.*
import dev.inmo.tgbotapi.types.update.media_group.* import dev.inmo.tgbotapi.types.update.media_group.*

View File

@ -9,7 +9,6 @@ import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.MessageThreadId import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.message.ParseMode import dev.inmo.tgbotapi.types.message.ParseMode
import dev.inmo.tgbotapi.types.chat.Chat import dev.inmo.tgbotapi.types.chat.Chat
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate

View File

@ -1,7 +1,6 @@
package dev.inmo.tgbotapi.extensions.api.utils package dev.inmo.tgbotapi.extensions.api.utils
import dev.inmo.tgbotapi.extensions.api.InternalUtils.convertWithMediaGroupUpdates import dev.inmo.tgbotapi.extensions.api.InternalUtils.convertWithMediaGroupUpdates
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver

View File

@ -7,7 +7,6 @@ import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull
import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.extensions.utils.withContent
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.content.*
import dev.inmo.tgbotapi.types.update.abstracts.BaseEditMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.BaseEditMessageUpdate
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature

View File

@ -3,7 +3,6 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.content.*
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage

View File

@ -4,7 +4,6 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull
import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.extensions.utils.withContent
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.content.*
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage

View File

@ -8,7 +8,6 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMediaGroupMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMediaGroupMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.content.*
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.utils.PreviewFeature import dev.inmo.tgbotapi.utils.PreviewFeature

View File

@ -1,7 +1,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories
import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
object ByChatMediaGroupMarkerFactory : MarkerFactory<List<MediaGroupMessage<*>>, Any> { object ByChatMediaGroupMarkerFactory : MarkerFactory<List<MediaGroupMessage<*>>, Any> {
override suspend fun invoke(data: List<MediaGroupMessage<*>>) = data.chat ?: error("Data must not be empty") override suspend fun invoke(data: List<MediaGroupMessage<*>>) = data.chat ?: error("Data must not be empty")

View File

@ -6,7 +6,6 @@ import dev.inmo.tgbotapi.requests.send.abstracts.SendMessageRequest
import dev.inmo.tgbotapi.requests.send.media.base.* import dev.inmo.tgbotapi.requests.send.media.base.*
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.media.* import dev.inmo.tgbotapi.types.media.*
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage
import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializerClass import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializerClass
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
@ -15,13 +14,16 @@ import dev.inmo.tgbotapi.types.message.content.AudioContent
import dev.inmo.tgbotapi.types.message.content.DocumentContent import dev.inmo.tgbotapi.types.message.content.DocumentContent
import dev.inmo.tgbotapi.types.message.content.MediaGroupContent import dev.inmo.tgbotapi.types.message.content.MediaGroupContent
import dev.inmo.tgbotapi.utils.* import dev.inmo.tgbotapi.utils.*
import dev.inmo.tgbotapi.utils.extensions.asMediaGroupContent import dev.inmo.tgbotapi.utils.extensions.asMediaGroupMessage
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonArray import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.jsonPrimitive
const val rawSendingMediaGroupsWarning = "Media groups contains restrictions related to combinations of media" + const val rawSendingMediaGroupsWarning = "Media groups contains restrictions related to combinations of media" +
" types. Currently it is possible to combine photo + video OR audio OR documents" " types. Currently it is possible to combine photo + video OR audio OR documents"
@ -35,7 +37,7 @@ fun <T : MediaGroupPartContent> SendMediaGroup(
protectContent: Boolean = false, protectContent: Boolean = false,
replyToMessageId: MessageId? = null, replyToMessageId: MessageId? = null,
allowSendingWithoutReply: Boolean? = null allowSendingWithoutReply: Boolean? = null
): Request<List<MediaGroupMessage<T>>> { ): Request<PossiblySentViaBotCommonMessage<T>> {
if (media.size !in mediaCountInMediaGroup) { if (media.size !in mediaCountInMediaGroup) {
throwRangeError("Count of members in media group", mediaCountInMediaGroup, media.size) throwRangeError("Count of members in media group", mediaCountInMediaGroup, media.size)
} }
@ -68,7 +70,7 @@ fun <T : MediaGroupPartContent> SendMediaGroup(
data, data,
SendMediaGroupFiles(files) SendMediaGroupFiles(files)
) )
}) as Request<List<MediaGroupMessage<T>>> }) as Request<PossiblySentViaBotCommonMessage<T>>
} }
/** /**
@ -126,7 +128,7 @@ private object MessagesListSerializer: KSerializer<PossiblySentViaBotCommonMessa
override fun deserialize(decoder: Decoder): PossiblySentViaBotCommonMessage<MediaGroupContent> { override fun deserialize(decoder: Decoder): PossiblySentViaBotCommonMessage<MediaGroupContent> {
val messages = serializer.deserialize(decoder) val messages = serializer.deserialize(decoder)
return messages.asMediaGroupContent() return messages.asMediaGroupMessage()
} }
override fun serialize(encoder: Encoder, value: PossiblySentViaBotCommonMessage<MediaGroupContent>) { override fun serialize(encoder: Encoder, value: PossiblySentViaBotCommonMessage<MediaGroupContent>) {

View File

@ -151,6 +151,7 @@ const val chatIdField = "chat_id"
const val senderChatIdField = "sender_chat_id" const val senderChatIdField = "sender_chat_id"
const val messageIdField = "message_id" const val messageIdField = "message_id"
const val messageThreadIdField = "message_thread_id" const val messageThreadIdField = "message_thread_id"
const val mediaGroupIdField = "media_group_id"
const val updateIdField = "update_id" const val updateIdField = "update_id"
const val fromChatIdField = "from_chat_id" const val fromChatIdField = "from_chat_id"
const val disableWebPagePreviewField = "disable_web_page_preview" const val disableWebPagePreviewField = "disable_web_page_preview"

View File

@ -19,5 +19,6 @@ data class ChannelContentMessageImpl<T: MessageContent>(
override val replyTo: Message?, override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?, override val replyMarkup: InlineKeyboardMarkup?,
override val senderBot: CommonBot?, override val senderBot: CommonBot?,
override val authorSignature: AuthorSignature? override val authorSignature: AuthorSignature?,
override val mediaGroupId: MediaGroupIdentifier?,
) : ChannelContentMessage<T> ) : ChannelContentMessage<T>

View File

@ -1,25 +0,0 @@
package dev.inmo.tgbotapi.types.message
import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.types.MediaGroupIdentifier
import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.types.chat.Chat
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
data class ChannelMediaGroupMessage<T : MediaGroupPartContent>(
override val messageId: MessageId,
override val threadId: MessageThreadId?,
override val chat: Chat,
override val date: DateTime,
override val mediaGroupId: MediaGroupIdentifier,
override val content: T,
override val editDate: DateTime?,
override val hasProtectedContent: Boolean,
override val forwardInfo: ForwardInfo?,
override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?
) : MediaGroupMessage<T>

View File

@ -1,24 +0,0 @@
package dev.inmo.tgbotapi.types.message
import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.types.chat.Chat
import dev.inmo.tgbotapi.types.chat.User
import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
data class CommonMediaGroupMessage<T : MediaGroupPartContent>(
override val messageId: MessageId,
override val threadId: MessageThreadId?,
override val from: User,
override val chat: Chat,
override val date: DateTime,
override val mediaGroupId: MediaGroupIdentifier,
override val content: T,
override val editDate: DateTime?,
override val hasProtectedContent: Boolean,
override val forwardInfo: ForwardInfo?,
override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?
) : MediaGroupMessage<T>, FromUserMessage

View File

@ -21,7 +21,8 @@ data class ConnectedFromChannelGroupContentMessageImpl<T : MessageContent>(
override val replyMarkup: InlineKeyboardMarkup?, override val replyMarkup: InlineKeyboardMarkup?,
override val content: T, override val content: T,
override val senderBot: CommonBot?, override val senderBot: CommonBot?,
override val authorSignature: AuthorSignature? override val authorSignature: AuthorSignature?,
override val mediaGroupId: MediaGroupIdentifier?,
) : ConnectedFromChannelGroupContentMessage<T> ) : ConnectedFromChannelGroupContentMessage<T>
data class UnconnectedFromChannelGroupContentMessageImpl<T: MessageContent>( data class UnconnectedFromChannelGroupContentMessageImpl<T: MessageContent>(
@ -36,7 +37,8 @@ data class UnconnectedFromChannelGroupContentMessageImpl<T: MessageContent>(
override val replyMarkup: InlineKeyboardMarkup?, override val replyMarkup: InlineKeyboardMarkup?,
override val content: T, override val content: T,
override val senderBot: CommonBot?, override val senderBot: CommonBot?,
override val authorSignature: AuthorSignature? override val authorSignature: AuthorSignature?,
override val mediaGroupId: MediaGroupIdentifier?,
) : UnconnectedFromChannelGroupContentMessage<T> ) : UnconnectedFromChannelGroupContentMessage<T>
data class AnonymousGroupContentMessageImpl<T : MessageContent>( data class AnonymousGroupContentMessageImpl<T : MessageContent>(
@ -50,7 +52,8 @@ data class AnonymousGroupContentMessageImpl<T : MessageContent>(
override val replyMarkup: InlineKeyboardMarkup?, override val replyMarkup: InlineKeyboardMarkup?,
override val content: T, override val content: T,
override val senderBot: CommonBot?, override val senderBot: CommonBot?,
override val authorSignature: AuthorSignature? override val authorSignature: AuthorSignature?,
override val mediaGroupId: MediaGroupIdentifier?,
) : AnonymousGroupContentMessage<T> ) : AnonymousGroupContentMessage<T>
data class CommonGroupContentMessageImpl<T : MessageContent>( data class CommonGroupContentMessageImpl<T : MessageContent>(
@ -64,7 +67,8 @@ data class CommonGroupContentMessageImpl<T : MessageContent>(
override val replyTo: Message?, override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?, override val replyMarkup: InlineKeyboardMarkup?,
override val content: T, override val content: T,
override val senderBot: CommonBot? override val senderBot: CommonBot?,
override val mediaGroupId: MediaGroupIdentifier?,
) : CommonGroupContentMessage<T> ) : CommonGroupContentMessage<T>
data class FromChannelForumContentMessageImpl<T: MessageContent>( data class FromChannelForumContentMessageImpl<T: MessageContent>(
@ -80,7 +84,8 @@ data class FromChannelForumContentMessageImpl<T: MessageContent>(
override val replyMarkup: InlineKeyboardMarkup?, override val replyMarkup: InlineKeyboardMarkup?,
override val content: T, override val content: T,
override val senderBot: CommonBot?, override val senderBot: CommonBot?,
override val authorSignature: AuthorSignature? override val authorSignature: AuthorSignature?,
override val mediaGroupId: MediaGroupIdentifier?,
) : FromChannelForumContentMessage<T> ) : FromChannelForumContentMessage<T>
data class AnonymousForumContentMessageImpl<T : MessageContent>( data class AnonymousForumContentMessageImpl<T : MessageContent>(
@ -95,7 +100,8 @@ data class AnonymousForumContentMessageImpl<T : MessageContent>(
override val replyMarkup: InlineKeyboardMarkup?, override val replyMarkup: InlineKeyboardMarkup?,
override val content: T, override val content: T,
override val senderBot: CommonBot?, override val senderBot: CommonBot?,
override val authorSignature: AuthorSignature? override val authorSignature: AuthorSignature?,
override val mediaGroupId: MediaGroupIdentifier?,
) : AnonymousForumContentMessage<T> ) : AnonymousForumContentMessage<T>
data class CommonForumContentMessageImpl<T : MessageContent>( data class CommonForumContentMessageImpl<T : MessageContent>(
@ -110,5 +116,6 @@ data class CommonForumContentMessageImpl<T : MessageContent>(
override val replyTo: Message?, override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?, override val replyMarkup: InlineKeyboardMarkup?,
override val content: T, override val content: T,
override val senderBot: CommonBot? override val senderBot: CommonBot?,
override val mediaGroupId: MediaGroupIdentifier?,
) : CommonForumContentMessage<T> ) : CommonForumContentMessage<T>

View File

@ -21,5 +21,6 @@ data class PrivateContentMessageImpl<T: MessageContent>(
override val forwardInfo: ForwardInfo?, override val forwardInfo: ForwardInfo?,
override val replyTo: Message?, override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?, override val replyMarkup: InlineKeyboardMarkup?,
override val senderBot: CommonBot? override val senderBot: CommonBot?,
override val mediaGroupId: MediaGroupIdentifier?,
) : PrivateContentMessage<T> ) : PrivateContentMessage<T>

View File

@ -269,45 +269,7 @@ internal data class RawMessage(
) )
else -> error("Expected one of the public chats, but was $chat (in extracting of chat event message)") else -> error("Expected one of the public chats, but was $chat (in extracting of chat event message)")
} }
} ?: content?.let { content -> } ?: content?.let { content -> when (chat) {
media_group_id?.let {
val checkedContent = when (content) {
is PhotoContent -> content
is VideoContent -> content
is AudioContent -> content
is DocumentContent -> content
else -> error("Unsupported content for media group")
}
when (from) {
null -> ChannelMediaGroupMessage(
messageId,
messageThreadId,
chat,
date.asDate,
it,
checkedContent,
edit_date?.asDate,
has_protected_content == true,
forwarded,
reply_to_message?.asMessage,
reply_markup
)
else -> CommonMediaGroupMessage(
messageId,
messageThreadId,
from,
chat,
date.asDate,
it,
checkedContent,
edit_date?.asDate,
has_protected_content == true,
forwarded,
reply_to_message?.asMessage,
reply_markup
)
}
} ?: when (chat) {
is PublicChat -> when (chat) { is PublicChat -> when (chat) {
is ChannelChat -> ChannelContentMessageImpl( is ChannelChat -> ChannelContentMessageImpl(
messageId, messageId,
@ -320,7 +282,8 @@ internal data class RawMessage(
reply_to_message?.asMessage, reply_to_message?.asMessage,
reply_markup, reply_markup,
via_bot, via_bot,
author_signature author_signature,
media_group_id
) )
is ForumChat -> if (messageThreadId != null) { is ForumChat -> if (messageThreadId != null) {
when (sender_chat) { when (sender_chat) {
@ -337,7 +300,8 @@ internal data class RawMessage(
reply_markup, reply_markup,
content, content,
via_bot, via_bot,
author_signature author_signature,
media_group_id
) )
is GroupChat -> AnonymousForumContentMessageImpl( is GroupChat -> AnonymousForumContentMessageImpl(
chat, chat,
@ -351,7 +315,8 @@ internal data class RawMessage(
reply_markup, reply_markup,
content, content,
via_bot, via_bot,
author_signature author_signature,
media_group_id
) )
null -> CommonForumContentMessageImpl( null -> CommonForumContentMessageImpl(
chat, chat,
@ -365,7 +330,8 @@ internal data class RawMessage(
reply_to_message ?.asMessage, reply_to_message ?.asMessage,
reply_markup, reply_markup,
content, content,
via_bot via_bot,
media_group_id
) )
} }
} else { } else {
@ -383,7 +349,8 @@ internal data class RawMessage(
reply_markup, reply_markup,
content, content,
via_bot, via_bot,
author_signature author_signature,
media_group_id
) )
} else { } else {
UnconnectedFromChannelGroupContentMessageImpl( UnconnectedFromChannelGroupContentMessageImpl(
@ -398,7 +365,8 @@ internal data class RawMessage(
reply_markup, reply_markup,
content, content,
via_bot, via_bot,
author_signature author_signature,
media_group_id
) )
} }
is GroupChat -> AnonymousGroupContentMessageImpl( is GroupChat -> AnonymousGroupContentMessageImpl(
@ -412,7 +380,8 @@ internal data class RawMessage(
reply_markup, reply_markup,
content, content,
via_bot, via_bot,
author_signature author_signature,
media_group_id
) )
null -> CommonGroupContentMessageImpl( null -> CommonGroupContentMessageImpl(
chat, chat,
@ -425,7 +394,8 @@ internal data class RawMessage(
reply_to_message ?.asMessage, reply_to_message ?.asMessage,
reply_markup, reply_markup,
content, content,
via_bot via_bot,
media_group_id
) )
} }
} }
@ -443,7 +413,8 @@ internal data class RawMessage(
reply_markup, reply_markup,
content, content,
via_bot, via_bot,
author_signature author_signature,
media_group_id
) )
} else { } else {
UnconnectedFromChannelGroupContentMessageImpl( UnconnectedFromChannelGroupContentMessageImpl(
@ -458,7 +429,8 @@ internal data class RawMessage(
reply_markup, reply_markup,
content, content,
via_bot, via_bot,
author_signature author_signature,
media_group_id
) )
} }
is GroupChat -> AnonymousGroupContentMessageImpl( is GroupChat -> AnonymousGroupContentMessageImpl(
@ -472,7 +444,8 @@ internal data class RawMessage(
reply_markup, reply_markup,
content, content,
via_bot, via_bot,
author_signature author_signature,
media_group_id
) )
null -> CommonGroupContentMessageImpl( null -> CommonGroupContentMessageImpl(
chat, chat,
@ -485,7 +458,8 @@ internal data class RawMessage(
reply_to_message ?.asMessage, reply_to_message ?.asMessage,
reply_markup, reply_markup,
content, content,
via_bot via_bot,
media_group_id
) )
} }
} }
@ -500,7 +474,8 @@ internal data class RawMessage(
forwarded, forwarded,
reply_to_message?.asMessage, reply_to_message?.asMessage,
reply_markup, reply_markup,
via_bot via_bot,
media_group_id
) )
else -> error("Unknown type of chat: $chat") else -> error("Unknown type of chat: $chat")
} }

View File

@ -7,4 +7,5 @@ sealed interface CommonMessage<T: MessageContent> : Message,
PossiblyEditedMessage, PossiblyEditedMessage,
PossiblyReplyMessage, PossiblyReplyMessage,
PossiblyMarkedUp, PossiblyMarkedUp,
PossiblyMediaGroupMessage<T>,
ContentMessage<T> ContentMessage<T>

View File

@ -1,10 +0,0 @@
package dev.inmo.tgbotapi.types.message.abstracts
import dev.inmo.tgbotapi.types.MediaGroupIdentifier
import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
interface MediaGroupMessage<T : MediaGroupPartContent> : CommonMessage<T> {
val mediaGroupId: MediaGroupIdentifier
val threadId: MessageThreadId?
}

View File

@ -0,0 +1,8 @@
package dev.inmo.tgbotapi.types.message.abstracts
import dev.inmo.tgbotapi.types.MediaGroupIdentifier
import dev.inmo.tgbotapi.types.message.content.MessageContent
interface PossiblyMediaGroupMessage<T : MessageContent> : ContentMessage<T> {
val mediaGroupId: MediaGroupIdentifier?
}

View File

@ -7,4 +7,6 @@ import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
data class ChannelPostUpdate( data class ChannelPostUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: Message override val data: Message
) : BaseSentMessageUpdate ) : BaseSentMessageUpdate {
override fun copy(newData: Message): BaseSentMessageUpdate = copy(updateId, newData)
}

View File

@ -7,4 +7,6 @@ import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
data class MessageUpdate( data class MessageUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: Message override val data: Message
) : BaseSentMessageUpdate ) : BaseSentMessageUpdate {
override fun copy(newData: Message) = copy(updateId, newData)
}

View File

@ -1,3 +1,7 @@
package dev.inmo.tgbotapi.types.update.abstracts package dev.inmo.tgbotapi.types.update.abstracts
interface BaseSentMessageUpdate : BaseMessageUpdate import dev.inmo.tgbotapi.types.message.abstracts.Message
interface BaseSentMessageUpdate : BaseMessageUpdate {
fun copy(newData: Message): BaseSentMessageUpdate
}

View File

@ -1,13 +0,0 @@
package dev.inmo.tgbotapi.types.update.media_group
import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
data class ChannelPostMediaGroupUpdate(
override val origins: List<BaseMessageUpdate>
) : SentMediaGroupUpdate {
override val updateId: UpdateIdentifier = origins.last().updateId
override val data: List<MediaGroupMessage<MediaGroupPartContent>> = origins.mapNotNull { it.data as? MediaGroupMessage<MediaGroupPartContent> }
}

View File

@ -1,13 +0,0 @@
package dev.inmo.tgbotapi.types.update.media_group
import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.types.update.EditChannelPostUpdate
data class EditChannelPostMediaGroupUpdate(
override val origin: EditChannelPostUpdate
) : EditMediaGroupUpdate {
override val updateId: UpdateIdentifier = origin.updateId
override val data: MediaGroupMessage<MediaGroupPartContent> = origin.data as MediaGroupMessage<MediaGroupPartContent>
}

View File

@ -1,13 +0,0 @@
package dev.inmo.tgbotapi.types.update.media_group
import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.types.update.EditMessageUpdate
data class EditMessageMediaGroupUpdate(
override val origin: EditMessageUpdate
) : EditMediaGroupUpdate {
override val updateId: UpdateIdentifier = origin.updateId
override val data: MediaGroupMessage<MediaGroupPartContent> = origin.data as MediaGroupMessage<MediaGroupPartContent>
}

View File

@ -1,24 +0,0 @@
package dev.inmo.tgbotapi.types.update.media_group
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.types.update.abstracts.*
/**
* By default there is no instances of objects which could be deserialized from raw updates. If you want to get objects
* with this type, you should use something like [dev.inmo.tgbotapi.extensions.api.SetWebhookKt.includeWebhookInRoute]
*
* @see dev.inmo.tgbotapi.extensions.api.SetWebhookKt.includeWebhookInRoute
* @see dev.inmo.tgbotapi.extensions.api.updates.UpdatesPollingKt.startGettingOfUpdates
*/
sealed interface MediaGroupUpdate : Update
sealed interface SentMediaGroupUpdate: MediaGroupUpdate {
override val data: List<MediaGroupMessage<MediaGroupPartContent>>
val origins: List<BaseMessageUpdate>
}
sealed interface EditMediaGroupUpdate : BaseEditMessageUpdate, MediaGroupUpdate {
override val data: MediaGroupMessage<MediaGroupPartContent>
val origin: BaseMessageUpdate
}

View File

@ -1,13 +0,0 @@
package dev.inmo.tgbotapi.types.update.media_group
import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
data class MessageMediaGroupUpdate(
override val origins: List<BaseMessageUpdate>
) : SentMediaGroupUpdate {
override val updateId: UpdateIdentifier = origins.last().updateId
override val data: List<MediaGroupMessage<MediaGroupPartContent>> = origins.mapNotNull { it.data as? MediaGroupMessage<MediaGroupPartContent> }
}

View File

@ -18,7 +18,6 @@ import dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.abstracts.ConnectedFromChannelGroupContentMessage import dev.inmo.tgbotapi.types.message.abstracts.ConnectedFromChannelGroupContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.FromChannelForumContentMessage import dev.inmo.tgbotapi.types.message.abstracts.FromChannelForumContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage
import dev.inmo.tgbotapi.types.message.abstracts.PrivateContentMessage import dev.inmo.tgbotapi.types.message.abstracts.PrivateContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.UnconnectedFromChannelGroupContentMessage import dev.inmo.tgbotapi.types.message.abstracts.UnconnectedFromChannelGroupContentMessage
@ -28,15 +27,13 @@ import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
@RiskFeature("This API is experimental and can be changed without any notice, use with caution") @RiskFeature("This API is experimental and can be changed without any notice, use with caution")
fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent( fun List<PossiblySentViaBotCommonMessage<MediaGroupPartContent>>.asMediaGroupMessage(): PossiblySentViaBotCommonMessage<MediaGroupContent> {
mediaGroupIdentifier: MediaGroupIdentifier val sourceMessage = first()
): PossiblySentViaBotCommonMessage<MediaGroupContent> {
val content = MediaGroupContent( val content = MediaGroupContent(
map { MediaGroupCollectionContent.PartWrapper(it.messageId, it.content, it) }, map { MediaGroupCollectionContent.PartWrapper(it.messageId, it.content, it) },
mediaGroupIdentifier sourceMessage.mediaGroupId ?: error("Can't create media group message with the first message without media group id")
) )
return when (val sourceMessage = first()) { return when (sourceMessage) {
is MediaGroupMessage -> TODO()
is ChannelContentMessage -> ChannelContentMessageImpl( is ChannelContentMessage -> ChannelContentMessageImpl(
sourceMessage.messageId, sourceMessage.messageId,
sourceMessage.chat, sourceMessage.chat,
@ -48,7 +45,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyTo, sourceMessage.replyTo,
sourceMessage.replyMarkup, sourceMessage.replyMarkup,
sourceMessage.senderBot, sourceMessage.senderBot,
sourceMessage.authorSignature sourceMessage.authorSignature,
sourceMessage.mediaGroupId
) )
is PrivateContentMessage -> PrivateContentMessageImpl( is PrivateContentMessage -> PrivateContentMessageImpl(
sourceMessage.messageId, sourceMessage.messageId,
@ -61,7 +59,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.forwardInfo, sourceMessage.forwardInfo,
sourceMessage.replyTo, sourceMessage.replyTo,
sourceMessage.replyMarkup, sourceMessage.replyMarkup,
sourceMessage.senderBot sourceMessage.senderBot,
sourceMessage.mediaGroupId
) )
is AnonymousGroupContentMessage -> AnonymousGroupContentMessageImpl( is AnonymousGroupContentMessage -> AnonymousGroupContentMessageImpl(
sourceMessage.chat, sourceMessage.chat,
@ -74,7 +73,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyMarkup, sourceMessage.replyMarkup,
content, content,
sourceMessage.senderBot, sourceMessage.senderBot,
sourceMessage.authorSignature sourceMessage.authorSignature,
sourceMessage.mediaGroupId
) )
is CommonGroupContentMessage -> CommonGroupContentMessageImpl( is CommonGroupContentMessage -> CommonGroupContentMessageImpl(
sourceMessage.chat, sourceMessage.chat,
@ -87,7 +87,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyTo, sourceMessage.replyTo,
sourceMessage.replyMarkup, sourceMessage.replyMarkup,
content, content,
sourceMessage.senderBot sourceMessage.senderBot,
sourceMessage.mediaGroupId
) )
is ConnectedFromChannelGroupContentMessage -> ConnectedFromChannelGroupContentMessageImpl( is ConnectedFromChannelGroupContentMessage -> ConnectedFromChannelGroupContentMessageImpl(
sourceMessage.chat, sourceMessage.chat,
@ -101,7 +102,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyMarkup, sourceMessage.replyMarkup,
content, content,
sourceMessage.senderBot, sourceMessage.senderBot,
sourceMessage.authorSignature sourceMessage.authorSignature,
sourceMessage.mediaGroupId
) )
is UnconnectedFromChannelGroupContentMessage -> UnconnectedFromChannelGroupContentMessageImpl( is UnconnectedFromChannelGroupContentMessage -> UnconnectedFromChannelGroupContentMessageImpl(
sourceMessage.chat, sourceMessage.chat,
@ -115,7 +117,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyMarkup, sourceMessage.replyMarkup,
content, content,
sourceMessage.senderBot, sourceMessage.senderBot,
sourceMessage.authorSignature sourceMessage.authorSignature,
sourceMessage.mediaGroupId
) )
is AnonymousForumContentMessage -> AnonymousForumContentMessageImpl( is AnonymousForumContentMessage -> AnonymousForumContentMessageImpl(
sourceMessage.chat, sourceMessage.chat,
@ -129,7 +132,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyMarkup, sourceMessage.replyMarkup,
content, content,
sourceMessage.senderBot, sourceMessage.senderBot,
sourceMessage.authorSignature sourceMessage.authorSignature,
sourceMessage.mediaGroupId
) )
is CommonForumContentMessage -> CommonForumContentMessageImpl( is CommonForumContentMessage -> CommonForumContentMessageImpl(
sourceMessage.chat, sourceMessage.chat,
@ -143,7 +147,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyTo, sourceMessage.replyTo,
sourceMessage.replyMarkup, sourceMessage.replyMarkup,
content, content,
sourceMessage.senderBot sourceMessage.senderBot,
sourceMessage.mediaGroupId
) )
is FromChannelForumContentMessage -> FromChannelForumContentMessageImpl( is FromChannelForumContentMessage -> FromChannelForumContentMessageImpl(
sourceMessage.chat, sourceMessage.chat,
@ -158,7 +163,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyMarkup, sourceMessage.replyMarkup,
content, content,
sourceMessage.senderBot, sourceMessage.senderBot,
sourceMessage.authorSignature sourceMessage.authorSignature,
sourceMessage.mediaGroupId
) )
} }
} }

View File

@ -34,7 +34,6 @@ import dev.inmo.tgbotapi.types.message.ChatEvents.LeftChatMember
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.* import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.*
import dev.inmo.tgbotapi.types.message.ChatEvents.voice.* import dev.inmo.tgbotapi.types.message.ChatEvents.voice.*
import dev.inmo.tgbotapi.types.message.abstracts.* import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.content.*
import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentEvent import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentEvent
import dev.inmo.tgbotapi.types.message.textsources.* import dev.inmo.tgbotapi.types.message.textsources.*

View File

@ -221,7 +221,6 @@ import dev.inmo.tgbotapi.types.message.AnonymousForumContentMessageImpl
import dev.inmo.tgbotapi.types.message.AnonymousGroupContentMessageImpl import dev.inmo.tgbotapi.types.message.AnonymousGroupContentMessageImpl
import dev.inmo.tgbotapi.types.message.ChannelContentMessageImpl import dev.inmo.tgbotapi.types.message.ChannelContentMessageImpl
import dev.inmo.tgbotapi.types.message.ChannelEventMessage import dev.inmo.tgbotapi.types.message.ChannelEventMessage
import dev.inmo.tgbotapi.types.message.ChannelMediaGroupMessage
import dev.inmo.tgbotapi.types.message.ChatEvents.ChannelChatCreated import dev.inmo.tgbotapi.types.message.ChatEvents.ChannelChatCreated
import dev.inmo.tgbotapi.types.message.ChatEvents.DeleteChatPhoto import dev.inmo.tgbotapi.types.message.ChatEvents.DeleteChatPhoto
import dev.inmo.tgbotapi.types.message.ChatEvents.GroupChatCreated import dev.inmo.tgbotapi.types.message.ChatEvents.GroupChatCreated
@ -255,7 +254,6 @@ import dev.inmo.tgbotapi.types.message.ChatEvents.voice.VideoChatStarted
import dev.inmo.tgbotapi.types.message.CommonForumContentMessageImpl import dev.inmo.tgbotapi.types.message.CommonForumContentMessageImpl
import dev.inmo.tgbotapi.types.message.CommonGroupContentMessageImpl import dev.inmo.tgbotapi.types.message.CommonGroupContentMessageImpl
import dev.inmo.tgbotapi.types.message.CommonGroupEventMessage import dev.inmo.tgbotapi.types.message.CommonGroupEventMessage
import dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage
import dev.inmo.tgbotapi.types.message.CommonSupergroupEventMessage import dev.inmo.tgbotapi.types.message.CommonSupergroupEventMessage
import dev.inmo.tgbotapi.types.message.ConnectedFromChannelGroupContentMessageImpl import dev.inmo.tgbotapi.types.message.ConnectedFromChannelGroupContentMessageImpl
import dev.inmo.tgbotapi.types.message.ForwardInfo import dev.inmo.tgbotapi.types.message.ForwardInfo
@ -279,7 +277,6 @@ import dev.inmo.tgbotapi.types.message.abstracts.FromChannelGroupContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.FromUserMessage import dev.inmo.tgbotapi.types.message.abstracts.FromUserMessage
import dev.inmo.tgbotapi.types.message.abstracts.GroupContentMessage import dev.inmo.tgbotapi.types.message.abstracts.GroupContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.GroupEventMessage import dev.inmo.tgbotapi.types.message.abstracts.GroupEventMessage
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.types.message.abstracts.PossiblyEditedMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblyEditedMessage
import dev.inmo.tgbotapi.types.message.abstracts.PossiblyForwardedMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblyForwardedMessage

View File

@ -36,6 +36,3 @@ inline fun <reified T : MessageContent> AnonymousGroupContentMessage<*>.withCont
inline fun <reified T : MessageContent> CommonGroupContentMessage<*>.withContentOrNull() = if (content is T) { this as CommonGroupContentMessage<T> } else { null } inline fun <reified T : MessageContent> CommonGroupContentMessage<*>.withContentOrNull() = if (content is T) { this as CommonGroupContentMessage<T> } else { null }
inline fun <reified T : MessageContent> CommonGroupContentMessage<*>.withContentOrThrow() = withContentOrNull<T>()!! inline fun <reified T : MessageContent> CommonGroupContentMessage<*>.withContentOrThrow() = withContentOrNull<T>()!!
inline fun <reified T : MediaGroupPartContent> MediaGroupMessage<*>.withContentOrNull() = if (content is T) { this as MediaGroupMessage<T> } else { null }
inline fun <reified T : MediaGroupPartContent> MediaGroupMessage<*>.withContentOrThrow() = withContentOrNull<T>()!!

View File

@ -1,21 +1,20 @@
package dev.inmo.tgbotapi.extensions.utils.updates package dev.inmo.tgbotapi.extensions.utils.updates
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
import dev.inmo.tgbotapi.types.MediaGroupIdentifier import dev.inmo.tgbotapi.types.MediaGroupIdentifier
import dev.inmo.tgbotapi.types.UpdateIdentifier import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.types.update.* import dev.inmo.tgbotapi.types.update.*
import dev.inmo.tgbotapi.types.update.abstracts.* import dev.inmo.tgbotapi.types.update.abstracts.*
import dev.inmo.tgbotapi.types.update.media_group.* import dev.inmo.tgbotapi.utils.extensions.asMediaGroupMessage
/** /**
* @return If [this] is [SentMediaGroupUpdate] - [Update.updateId] of [last] element, or its own [Update.updateId] * @return If [this] is [SentMediaGroupUpdate] - [Update.updateId] of [last] element, or its own [Update.updateId]
*/ */
@Deprecated("Redundant", ReplaceWith("updateId"))
fun Update.lastUpdateIdentifier(): UpdateIdentifier { fun Update.lastUpdateIdentifier(): UpdateIdentifier {
return if (this is SentMediaGroupUpdate) { return updateId
origins.last().updateId
} else {
updateId
}
} }
/** /**
@ -24,7 +23,7 @@ fun Update.lastUpdateIdentifier(): UpdateIdentifier {
* @see [Update.lastUpdateIdentifier] * @see [Update.lastUpdateIdentifier]
*/ */
fun List<Update>.lastUpdateIdentifier(): UpdateIdentifier? { fun List<Update>.lastUpdateIdentifier(): UpdateIdentifier? {
return maxByOrNull { it.updateId } ?.lastUpdateIdentifier() return maxByOrNull { it.updateId } ?.updateId
} }
/** /**
@ -32,50 +31,34 @@ fun List<Update>.lastUpdateIdentifier(): UpdateIdentifier? {
*/ */
fun List<Update>.convertWithMediaGroupUpdates(): List<Update> { fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
val resultUpdates = mutableListOf<Update>() val resultUpdates = mutableListOf<Update>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseSentMessageUpdate>>() val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<Pair<BaseSentMessageUpdate, PossiblySentViaBotCommonMessage<MediaGroupPartContent>>>>()
for (update in this) { for (update in this) {
val data = (update.data as? MediaGroupMessage<*>) val message = (update.data as? PossiblySentViaBotCommonMessage<*>) ?.withContentOrNull<MediaGroupPartContent>()
if (data == null) { val mediaGroupId = message ?.mediaGroupId
if (message == null || mediaGroupId == null) {
resultUpdates.add(update) resultUpdates.add(update)
continue continue
} }
when (update) { when (update) {
is BaseEditMessageUpdate -> resultUpdates.add(
update.toEditMediaGroupUpdate()
)
is BaseSentMessageUpdate -> { is BaseSentMessageUpdate -> {
mediaGroups.getOrPut(data.mediaGroupId) { mediaGroups.getOrPut(mediaGroupId) {
mutableListOf() mutableListOf()
}.add(update) }.add(update to message)
} }
else -> resultUpdates.add(update) else -> resultUpdates.add(update)
} }
} }
mediaGroups.values.map {
it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate -> mediaGroups.map { (_, updatesWithMessages) ->
resultUpdates.add(mediaGroupUpdate) val update = updatesWithMessages.maxBy { it.first.updateId }.first
} resultUpdates.add(
} update.copy(updatesWithMessages.map { it.second }.asMediaGroupMessage())
resultUpdates.sortBy { it.updateId } )
return resultUpdates
} }
/** resultUpdates.sortBy { it.updateId }
* @receiver List of [BaseSentMessageUpdate] where [BaseSentMessageUpdate.data] is [MediaGroupMessage] and all messages return resultUpdates
* have the same [MediaGroupMessage.mediaGroupId]
* @return [MessageMediaGroupUpdate] in case if [first] object of [this] is [MessageUpdate]. When [first] object is
* [ChannelPostUpdate] instance - will return [ChannelPostMediaGroupUpdate]. Otherwise will be returned null
*/
fun List<BaseSentMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let {
if (isEmpty()) {
return@let null
}
val resultList = sortedBy { it.updateId }
when (first()) {
is MessageUpdate -> MessageMediaGroupUpdate(resultList)
is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(resultList)
else -> null
}
} }
/** /**
@ -84,10 +67,4 @@ fun List<BaseSentMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpdate?
* *
* @throws IllegalStateException * @throws IllegalStateException
*/ */
fun BaseEditMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate = (this as? EditMediaGroupUpdate) ?: let { fun BaseEditMessageUpdate.toEditMediaGroupUpdate() = this
when (this) {
is EditMessageUpdate -> EditMessageMediaGroupUpdate(this)
is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this)
else -> error("Unsupported type of ${BaseEditMessageUpdate::class.simpleName}")
}
}

View File

@ -1,7 +1,7 @@
package dev.inmo.tgbotapi.extensions.utils.updates.retrieving package dev.inmo.tgbotapi.extensions.utils.updates.retrieving
import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver
@ -32,7 +32,7 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
launch { launch {
for (update in updatesChannel) { for (update in updatesChannel) {
when (val data = update.data) { when (val data = update.data) {
is MediaGroupMessage<*> -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) is PossiblyMediaGroupMessage<*> -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate)
else -> output(update) else -> output(update)
} }
} }