1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 03:28:44 +00:00
This commit is contained in:
InsanusMokrassar 2019-05-16 14:58:19 +08:00
parent 1e0bf9e66d
commit 84a086e7e4
11 changed files with 85 additions and 34 deletions

View File

@ -26,6 +26,12 @@ some default library
* `UpdatesFilter` now have additional callback for polls
* `StopPoll#replyMarkup` now is optional
### 0.14.2 MediaGroups edit hotfixes
* `convertWithMediaGroupUpdates` extension added
* All media group converting extensions are internal for now
* Fixes according to updates in converting of updates to media group updates
## 0.13.0 Telegram Polls
* Type `PollOption` and `AnonymousPollOption` added

View File

@ -1,4 +1,4 @@
project.version = "0.14.1"
project.version = "0.14.2"
project.group = "com.github.insanusmokrassar"
buildscript {

View File

@ -6,7 +6,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMes
data class ChannelPostMediaGroupUpdate(
override val origins: List<BaseMessageUpdate>
) : MediaGroupUpdate {
) : SentMediaGroupUpdate {
override val updateId: UpdateIdentifier = origins.last().updateId
override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
}

View File

@ -2,11 +2,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.EditChannelPostUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
data class EditChannelPostMediaGroupUpdate(
override val origins: List<BaseMessageUpdate>
) : MediaGroupUpdate {
override val updateId: UpdateIdentifier = origins.last().updateId
override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
override val origin: EditChannelPostUpdate
) : EditMediaGroupUpdate {
override val updateId: UpdateIdentifier = origin.updateId
override val data: MediaGroupMessage = origin.data as MediaGroupMessage
}

View File

@ -2,11 +2,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.EditMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
data class EditMessageMediaGroupUpdate(
override val origins: List<BaseMessageUpdate>
) : MediaGroupUpdate {
override val updateId: UpdateIdentifier = origins.last().updateId
override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
override val origin: EditMessageUpdate
) : EditMediaGroupUpdate {
override val updateId: UpdateIdentifier = origin.updateId
override val data: MediaGroupMessage = origin.data as MediaGroupMessage
}

View File

@ -4,7 +4,14 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaG
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
interface MediaGroupUpdate: Update {
interface MediaGroupUpdate : Update
interface SentMediaGroupUpdate: MediaGroupUpdate {
override val data: List<MediaGroupMessage>
val origins: List<BaseMessageUpdate>
}
interface EditMediaGroupUpdate : MediaGroupUpdate {
override val data: MediaGroupMessage
val origin: BaseMessageUpdate
}

View File

@ -6,7 +6,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMes
data class MessageMediaGroupUpdate(
override val origins: List<BaseMessageUpdate>
) : MediaGroupUpdate {
) : SentMediaGroupUpdate {
override val updateId: UpdateIdentifier = origins.last().updateId
override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
}

View File

@ -50,9 +50,7 @@ data class UpdatesFilter(
is EditMessageMediaGroupUpdate -> editedMessageMediaGroupCallback ?.also { receiver ->
receiver(update)
} ?: editedMessageCallback ?.also { receiver ->
update.origins.mapNotNull { it as? EditMessageUpdate }.forEach {
receiver(it)
}
receiver(update.origin)
}
is ChannelPostUpdate -> channelPostCallback ?.invoke(update)
is ChannelPostMediaGroupUpdate -> channelPostMediaGroupCallback ?.also { receiver ->
@ -66,9 +64,7 @@ data class UpdatesFilter(
is EditChannelPostMediaGroupUpdate -> editedChannelPostMediaGroupCallback ?.also { receiver ->
receiver(update)
} ?: editedChannelPostCallback ?.also { receiver ->
update.origins.mapNotNull { it as? EditChannelPostUpdate }.forEach {
receiver(it)
}
receiver(update.origin)
}
is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(update)
is InlineQueryUpdate -> inlineQueryCallback ?.invoke(update)

View File

@ -7,10 +7,9 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeUnsafe
import com.github.insanusmokrassar.TelegramBotAPI.utils.mediaGroupId
import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate
import kotlinx.coroutines.*
import java.util.concurrent.Executors
@ -35,9 +34,7 @@ class UpdatesPoller(
val inputMediaGroupId = (update ?.data as? MediaGroupMessage) ?.mediaGroupId
if (mediaGroup.isNotEmpty() && inputMediaGroupId ?.equals(mediaGroup.mediaGroupId) != true) {
mediaGroup.sortBy { it.updateId }
mediaGroup.toMediaGroupUpdate() ?.let {
sendToBlock(it)
} ?: mediaGroup.forEach {
mediaGroup.convertWithMediaGroupUpdates().forEach {
sendToBlock(it)
}
mediaGroup.clear()

View File

@ -1,16 +1,55 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils
import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
fun List<BaseMessageUpdate>.toMediaGroupUpdate(): MediaGroupUpdate? = (this as? MediaGroupUpdate) ?: let {
private inline val Pair<MediaGroupMessage, *>.message
get() = first
internal fun List<BaseMessageUpdate>.convertWithMediaGroupUpdates(): List<Update> {
val resultUpdates = mutableListOf<Update>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseMessageUpdate>>()
for (update in this) {
val asEditMediaGroupMessage = update.toEditMediaGroupUpdate()
if (asEditMediaGroupMessage != null) {
resultUpdates.add(asEditMediaGroupMessage)
} else {
val data = update.data
if (data is MediaGroupMessage) {
(mediaGroups[data.mediaGroupId] ?: mutableListOf<BaseMessageUpdate>().also { mediaGroups[data.mediaGroupId] = it }).add(update)
} else {
resultUpdates.add(update)
}
}
}
mediaGroups.values.map {
it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate ->
resultUpdates.add(mediaGroupUpdate)
}
}
return resultUpdates.sortedBy { it.updateId }
}
internal fun List<BaseMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let {
if (isEmpty()) {
return@let null
}
val resultList = sortedBy { it.updateId }
when (first()) {
is MessageUpdate -> MessageMediaGroupUpdate(resultList)
is EditMessageUpdate -> EditMessageMediaGroupUpdate(resultList)
is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(resultList)
is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(resultList)
else -> null
}
}
internal fun BaseMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate? = (this as? EditMediaGroupUpdate) ?: let {
when (this) {
is EditMessageUpdate -> EditMessageMediaGroupUpdate(this)
is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this)
else -> null
}
}

View File

@ -3,14 +3,15 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile
import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook
import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig
import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate
import com.github.insanusmokrassar.TelegramBotAPI.utils.convertWithMediaGroupUpdates
import com.github.insanusmokrassar.TelegramBotAPI.utils.toSentMediaGroupUpdate
import io.ktor.application.call
import io.ktor.request.receiveText
import io.ktor.response.respond
@ -61,7 +62,7 @@ suspend fun RequestsExecutor.setWebhook(
)
)
val updatesChannel = Channel<Update>(Channel.UNLIMITED)
val mediaGroupChannel = Channel<Pair<MediaGroupIdentifier, BaseMessageUpdate>>(Channel.UNLIMITED)
val mediaGroupChannel = Channel<Pair<String, BaseMessageUpdate>>(Channel.UNLIMITED)
val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey(
1000L,
scope = scope
@ -110,17 +111,20 @@ suspend fun RequestsExecutor.setWebhook(
launch {
for (update in updatesChannel) {
val data = update.data
if (data is MediaGroupUpdate) {
} else {
}
when (data) {
is MediaGroupMessage -> mediaGroupChannel.send(data.mediaGroupId to update as BaseMessageUpdate)
is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate)
else -> block(update)
}
}
}
launch {
for (mediaGroupUpdate in mediaGroupAccumulatedChannel) {
mediaGroupUpdate.second.toMediaGroupUpdate() ?.let {
block(it)
} ?: mediaGroupUpdate.second.forEach {
for ((_, mediaGroup) in mediaGroupAccumulatedChannel) {
mediaGroup.convertWithMediaGroupUpdates().forEach {
block(it)
}
}