add opportunity to be called between resender sends

This commit is contained in:
InsanusMokrassar 2023-04-22 22:16:14 +06:00
parent 240ee3de6f
commit b8601d4c90

View File

@ -16,7 +16,8 @@ class MessagesResender(
) { ) {
suspend fun resend( suspend fun resend(
targetChatId: IdChatIdentifier, targetChatId: IdChatIdentifier,
messagesInfo: List<MessageMetaInfo> messagesInfo: List<MessageMetaInfo>,
onBetweenMessages: suspend (sent: List<MessageMetaInfo>, toBeSent: List<MessageMetaInfo>) -> Unit
): List<Pair<MessageMetaInfo, MessageMetaInfo>> { ): List<Pair<MessageMetaInfo, MessageMetaInfo>> {
val messagesWithOrders = messagesInfo.mapIndexed { i, messageInfo -> messageInfo to i }.toMap() val messagesWithOrders = messagesInfo.mapIndexed { i, messageInfo -> messageInfo to i }.toMap()
val ordersWithMessagesGroups = messagesInfo.groupBy { it.group }.flatMap { (group, list) -> val ordersWithMessagesGroups = messagesInfo.groupBy { it.group }.flatMap { (group, list) ->
@ -29,9 +30,14 @@ class MessagesResender(
} }
}.sortedBy { it.first } }.sortedBy { it.first }
val sent = mutableListOf<MessageMetaInfo>()
val leftToSend = ordersWithMessagesGroups.map { it.second }.toMutableList()
return ordersWithMessagesGroups.flatMap { (_, contents) -> return ordersWithMessagesGroups.flatMap { (_, contents) ->
val sourceMessagesToSentMessages = mutableListOf<Pair<MessageMetaInfo, MessageMetaInfo>>() val sourceMessagesToSentMessages = mutableListOf<Pair<MessageMetaInfo, MessageMetaInfo>>()
onBetweenMessages(sent.toList(), leftToSend.flatten())
when { when {
contents.size == 1 -> { contents.size == 1 -> {
val messageInfo = contents.first() val messageInfo = contents.first()
@ -137,8 +143,15 @@ class MessagesResender(
} }
} }
leftToSend.takeIf { it.isNotEmpty() } ?.removeAt(0) ?.also {
sent.addAll(it)
}
sourceMessagesToSentMessages.toList() sourceMessagesToSentMessages.toList()
} }
}
} suspend fun resend(
targetChatId: IdChatIdentifier,
messagesInfo: List<MessageMetaInfo>
): List<Pair<MessageMetaInfo, MessageMetaInfo>> = resend(targetChatId, messagesInfo) { _, _ -> }
} }