small refactor

This commit is contained in:
InsanusMokrassar 2023-01-14 23:42:16 +06:00
parent 88a89ff1e7
commit 617b8091db

View File

@ -18,19 +18,19 @@ class MessagesResender(
targetChatId: IdChatIdentifier, targetChatId: IdChatIdentifier,
messagesInfo: List<MessageMetaInfo> messagesInfo: List<MessageMetaInfo>
): List<Pair<MessageMetaInfo, MessageMetaInfo>> { ): List<Pair<MessageMetaInfo, MessageMetaInfo>> {
val orders = messagesInfo.mapIndexed { i, messageInfo -> messageInfo to i }.toMap() val messagesWithOrders = messagesInfo.mapIndexed { i, messageInfo -> messageInfo to i }.toMap()
val sortedMessagesContents = messagesInfo.groupBy { it.group }.flatMap { (group, list) -> val ordersWithMessagesGroups = messagesInfo.groupBy { it.group }.flatMap { (group, list) ->
if (group == null) { if (group == null) {
list.map { list.map {
orders.getValue(it) to listOf(it) messagesWithOrders.getValue(it) to listOf(it)
} }
} else { } else {
listOf(orders.getValue(list.first()) to list) listOf(messagesWithOrders.getValue(list.first()) to list)
} }
}.sortedBy { it.first } }.sortedBy { it.first }
return sortedMessagesContents.flatMap { (_, contents) -> return ordersWithMessagesGroups.flatMap { (_, contents) ->
val result = mutableListOf<Pair<MessageMetaInfo, MessageMetaInfo>>() val sourceMessagesToSentMessages = mutableListOf<Pair<MessageMetaInfo, MessageMetaInfo>>()
when { when {
contents.size == 1 -> { contents.size == 1 -> {
@ -88,7 +88,7 @@ class MessagesResender(
it as ContentMessage<MediaGroupPartContent> it as ContentMessage<MediaGroupPartContent>
} }
src to (forwardedMessageAsMediaPartMessage ?: null.also { _ -> src to (forwardedMessageAsMediaPartMessage ?: null.also { _ ->
result.add( sourceMessagesToSentMessages.add(
src to MessageMetaInfo( src to MessageMetaInfo(
targetChatId, targetChatId,
bot.execute( bot.execute(
@ -104,7 +104,7 @@ class MessagesResender(
} }
resultContents.singleOrNull() ?.also { (src, it) -> resultContents.singleOrNull() ?.also { (src, it) ->
result.add( sourceMessagesToSentMessages.add(
src to MessageMetaInfo( src to MessageMetaInfo(
targetChatId, targetChatId,
bot.execute( bot.execute(
@ -124,7 +124,7 @@ class MessagesResender(
) )
).content.group.mapIndexed { i, partWrapper -> ).content.group.mapIndexed { i, partWrapper ->
it.getOrNull(i) ?.let { it.getOrNull(i) ?.let {
result.add( sourceMessagesToSentMessages.add(
it.first to MessageMetaInfo( it.first to MessageMetaInfo(
partWrapper.sourceMessage.chat.id, partWrapper.sourceMessage.chat.id,
partWrapper.sourceMessage.messageId, partWrapper.sourceMessage.messageId,
@ -137,7 +137,7 @@ class MessagesResender(
} }
} }
result.toList() sourceMessagesToSentMessages.toList()
} }
} }