1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +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.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.abstracts.*
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.message.ParseMode
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.update.media_group.SentMediaGroupUpdate

View File

@ -1,7 +1,6 @@
package dev.inmo.tgbotapi.extensions.api.utils
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.Update
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.requests.abstracts.Request
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.update.abstracts.BaseEditMessageUpdate
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.utils.SimpleFilter
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.utils.RiskFeature
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.withContent
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.utils.RiskFeature
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.MarkerFactory
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.update.abstracts.Update
import dev.inmo.tgbotapi.utils.PreviewFeature

View File

@ -1,7 +1,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories
import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
object ByChatMediaGroupMarkerFactory : MarkerFactory<List<MediaGroupMessage<*>>, Any> {
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.types.*
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.TelegramBotAPIMessageDeserializeOnlySerializerClass
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.MediaGroupContent
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.builtins.ListSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.jsonPrimitive
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"
@ -35,7 +37,7 @@ fun <T : MediaGroupPartContent> SendMediaGroup(
protectContent: Boolean = false,
replyToMessageId: MessageId? = null,
allowSendingWithoutReply: Boolean? = null
): Request<List<MediaGroupMessage<T>>> {
): Request<PossiblySentViaBotCommonMessage<T>> {
if (media.size !in mediaCountInMediaGroup) {
throwRangeError("Count of members in media group", mediaCountInMediaGroup, media.size)
}
@ -68,7 +70,7 @@ fun <T : MediaGroupPartContent> SendMediaGroup(
data,
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> {
val messages = serializer.deserialize(decoder)
return messages.asMediaGroupContent()
return messages.asMediaGroupMessage()
}
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 messageIdField = "message_id"
const val messageThreadIdField = "message_thread_id"
const val mediaGroupIdField = "media_group_id"
const val updateIdField = "update_id"
const val fromChatIdField = "from_chat_id"
const val disableWebPagePreviewField = "disable_web_page_preview"

View File

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

View File

@ -21,5 +21,6 @@ data class PrivateContentMessageImpl<T: MessageContent>(
override val forwardInfo: ForwardInfo?,
override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?,
override val senderBot: CommonBot?
override val senderBot: CommonBot?,
override val mediaGroupId: MediaGroupIdentifier?,
) : 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)")
}
} ?: content?.let { content ->
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) {
} ?: content?.let { content -> when (chat) {
is PublicChat -> when (chat) {
is ChannelChat -> ChannelContentMessageImpl(
messageId,
@ -320,7 +282,8 @@ internal data class RawMessage(
reply_to_message?.asMessage,
reply_markup,
via_bot,
author_signature
author_signature,
media_group_id
)
is ForumChat -> if (messageThreadId != null) {
when (sender_chat) {
@ -337,7 +300,8 @@ internal data class RawMessage(
reply_markup,
content,
via_bot,
author_signature
author_signature,
media_group_id
)
is GroupChat -> AnonymousForumContentMessageImpl(
chat,
@ -351,7 +315,8 @@ internal data class RawMessage(
reply_markup,
content,
via_bot,
author_signature
author_signature,
media_group_id
)
null -> CommonForumContentMessageImpl(
chat,
@ -365,7 +330,8 @@ internal data class RawMessage(
reply_to_message ?.asMessage,
reply_markup,
content,
via_bot
via_bot,
media_group_id
)
}
} else {
@ -383,7 +349,8 @@ internal data class RawMessage(
reply_markup,
content,
via_bot,
author_signature
author_signature,
media_group_id
)
} else {
UnconnectedFromChannelGroupContentMessageImpl(
@ -398,7 +365,8 @@ internal data class RawMessage(
reply_markup,
content,
via_bot,
author_signature
author_signature,
media_group_id
)
}
is GroupChat -> AnonymousGroupContentMessageImpl(
@ -412,7 +380,8 @@ internal data class RawMessage(
reply_markup,
content,
via_bot,
author_signature
author_signature,
media_group_id
)
null -> CommonGroupContentMessageImpl(
chat,
@ -425,7 +394,8 @@ internal data class RawMessage(
reply_to_message ?.asMessage,
reply_markup,
content,
via_bot
via_bot,
media_group_id
)
}
}
@ -443,7 +413,8 @@ internal data class RawMessage(
reply_markup,
content,
via_bot,
author_signature
author_signature,
media_group_id
)
} else {
UnconnectedFromChannelGroupContentMessageImpl(
@ -458,7 +429,8 @@ internal data class RawMessage(
reply_markup,
content,
via_bot,
author_signature
author_signature,
media_group_id
)
}
is GroupChat -> AnonymousGroupContentMessageImpl(
@ -472,7 +444,8 @@ internal data class RawMessage(
reply_markup,
content,
via_bot,
author_signature
author_signature,
media_group_id
)
null -> CommonGroupContentMessageImpl(
chat,
@ -485,7 +458,8 @@ internal data class RawMessage(
reply_to_message ?.asMessage,
reply_markup,
content,
via_bot
via_bot,
media_group_id
)
}
}
@ -500,7 +474,8 @@ internal data class RawMessage(
forwarded,
reply_to_message?.asMessage,
reply_markup,
via_bot
via_bot,
media_group_id
)
else -> error("Unknown type of chat: $chat")
}

View File

@ -7,4 +7,5 @@ sealed interface CommonMessage<T: MessageContent> : Message,
PossiblyEditedMessage,
PossiblyReplyMessage,
PossiblyMarkedUp,
PossiblyMediaGroupMessage<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(
override val updateId: UpdateIdentifier,
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(
override val updateId: UpdateIdentifier,
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
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.ConnectedFromChannelGroupContentMessage
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
@ -28,15 +27,13 @@ import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
import dev.inmo.tgbotapi.utils.RiskFeature
@RiskFeature("This API is experimental and can be changed without any notice, use with caution")
fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
mediaGroupIdentifier: MediaGroupIdentifier
): PossiblySentViaBotCommonMessage<MediaGroupContent> {
fun List<PossiblySentViaBotCommonMessage<MediaGroupPartContent>>.asMediaGroupMessage(): PossiblySentViaBotCommonMessage<MediaGroupContent> {
val sourceMessage = first()
val content = MediaGroupContent(
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()) {
is MediaGroupMessage -> TODO()
return when (sourceMessage) {
is ChannelContentMessage -> ChannelContentMessageImpl(
sourceMessage.messageId,
sourceMessage.chat,
@ -48,7 +45,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyTo,
sourceMessage.replyMarkup,
sourceMessage.senderBot,
sourceMessage.authorSignature
sourceMessage.authorSignature,
sourceMessage.mediaGroupId
)
is PrivateContentMessage -> PrivateContentMessageImpl(
sourceMessage.messageId,
@ -61,7 +59,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.forwardInfo,
sourceMessage.replyTo,
sourceMessage.replyMarkup,
sourceMessage.senderBot
sourceMessage.senderBot,
sourceMessage.mediaGroupId
)
is AnonymousGroupContentMessage -> AnonymousGroupContentMessageImpl(
sourceMessage.chat,
@ -74,7 +73,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyMarkup,
content,
sourceMessage.senderBot,
sourceMessage.authorSignature
sourceMessage.authorSignature,
sourceMessage.mediaGroupId
)
is CommonGroupContentMessage -> CommonGroupContentMessageImpl(
sourceMessage.chat,
@ -87,7 +87,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyTo,
sourceMessage.replyMarkup,
content,
sourceMessage.senderBot
sourceMessage.senderBot,
sourceMessage.mediaGroupId
)
is ConnectedFromChannelGroupContentMessage -> ConnectedFromChannelGroupContentMessageImpl(
sourceMessage.chat,
@ -101,7 +102,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyMarkup,
content,
sourceMessage.senderBot,
sourceMessage.authorSignature
sourceMessage.authorSignature,
sourceMessage.mediaGroupId
)
is UnconnectedFromChannelGroupContentMessage -> UnconnectedFromChannelGroupContentMessageImpl(
sourceMessage.chat,
@ -115,7 +117,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyMarkup,
content,
sourceMessage.senderBot,
sourceMessage.authorSignature
sourceMessage.authorSignature,
sourceMessage.mediaGroupId
)
is AnonymousForumContentMessage -> AnonymousForumContentMessageImpl(
sourceMessage.chat,
@ -129,7 +132,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyMarkup,
content,
sourceMessage.senderBot,
sourceMessage.authorSignature
sourceMessage.authorSignature,
sourceMessage.mediaGroupId
)
is CommonForumContentMessage -> CommonForumContentMessageImpl(
sourceMessage.chat,
@ -143,7 +147,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyTo,
sourceMessage.replyMarkup,
content,
sourceMessage.senderBot
sourceMessage.senderBot,
sourceMessage.mediaGroupId
)
is FromChannelForumContentMessage -> FromChannelForumContentMessageImpl(
sourceMessage.chat,
@ -158,7 +163,8 @@ fun List<CommonMessage<MediaGroupPartContent>>.asMediaGroupContent(
sourceMessage.replyMarkup,
content,
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.voice.*
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.payments.SuccessfulPaymentEvent
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.ChannelContentMessageImpl
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.DeleteChatPhoto
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.CommonGroupContentMessageImpl
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.ConnectedFromChannelGroupContentMessageImpl
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.GroupContentMessage
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.PossiblyEditedMessage
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<*>.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
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
import dev.inmo.tgbotapi.types.MediaGroupIdentifier
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.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]
*/
@Deprecated("Redundant", ReplaceWith("updateId"))
fun Update.lastUpdateIdentifier(): UpdateIdentifier {
return if (this is SentMediaGroupUpdate) {
origins.last().updateId
} else {
updateId
}
return updateId
}
/**
@ -24,7 +23,7 @@ fun Update.lastUpdateIdentifier(): UpdateIdentifier {
* @see [Update.lastUpdateIdentifier]
*/
fun List<Update>.lastUpdateIdentifier(): UpdateIdentifier? {
return maxByOrNull { it.updateId } ?.lastUpdateIdentifier()
return maxByOrNull { it.updateId } ?.updateId
}
/**
@ -32,62 +31,40 @@ fun List<Update>.lastUpdateIdentifier(): UpdateIdentifier? {
*/
fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
val resultUpdates = mutableListOf<Update>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseSentMessageUpdate>>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<Pair<BaseSentMessageUpdate, PossiblySentViaBotCommonMessage<MediaGroupPartContent>>>>()
for (update in this) {
val data = (update.data as? MediaGroupMessage<*>)
if (data == null) {
val message = (update.data as? PossiblySentViaBotCommonMessage<*>) ?.withContentOrNull<MediaGroupPartContent>()
val mediaGroupId = message ?.mediaGroupId
if (message == null || mediaGroupId == null) {
resultUpdates.add(update)
continue
}
when (update) {
is BaseEditMessageUpdate -> resultUpdates.add(
update.toEditMediaGroupUpdate()
)
is BaseSentMessageUpdate -> {
mediaGroups.getOrPut(data.mediaGroupId) {
mediaGroups.getOrPut(mediaGroupId) {
mutableListOf()
}.add(update)
}.add(update to message)
}
else -> resultUpdates.add(update)
}
}
mediaGroups.values.map {
it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate ->
resultUpdates.add(mediaGroupUpdate)
}
mediaGroups.map { (_, updatesWithMessages) ->
val update = updatesWithMessages.maxBy { it.first.updateId }.first
resultUpdates.add(
update.copy(updatesWithMessages.map { it.second }.asMediaGroupMessage())
)
}
resultUpdates.sortBy { it.updateId }
return resultUpdates
}
/**
* @receiver List of [BaseSentMessageUpdate] where [BaseSentMessageUpdate.data] is [MediaGroupMessage] and all messages
* 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
}
}
/**
* @return [EditMessageMediaGroupUpdate] in case if [this] is [EditMessageUpdate]. When [this] object is
* [EditChannelPostUpdate] instance - will return [EditChannelPostMediaGroupUpdate]
*
* @throws IllegalStateException
*/
fun BaseEditMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate = (this as? EditMediaGroupUpdate) ?: let {
when (this) {
is EditMessageUpdate -> EditMessageMediaGroupUpdate(this)
is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this)
else -> error("Unsupported type of ${BaseEditMessageUpdate::class.simpleName}")
}
}
fun BaseEditMessageUpdate.toEditMediaGroupUpdate() = this

View File

@ -1,7 +1,7 @@
package dev.inmo.tgbotapi.extensions.utils.updates.retrieving
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.Update
import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver
@ -32,7 +32,7 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
launch {
for (update in updatesChannel) {
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)
}
}