From 99a1a9392c6b83a594c5f7bfff465c939c33e04f Mon Sep 17 00:00:00 2001 From: Ali <> Date: Thu, 26 Oct 2023 21:13:39 +0400 Subject: [PATCH] Update peers in message history views --- .../Postbox/Sources/MessageHistoryView.swift | 5 +++ .../Sources/MessageHistoryViewState.swift | 37 +++++++++++++++++++ .../Sources/ChatHistoryEntry.swift | 12 ++++++ 3 files changed, 54 insertions(+) diff --git a/submodules/Postbox/Sources/MessageHistoryView.swift b/submodules/Postbox/Sources/MessageHistoryView.swift index 9a3c87e073..4611261d2e 100644 --- a/submodules/Postbox/Sources/MessageHistoryView.swift +++ b/submodules/Postbox/Sources/MessageHistoryView.swift @@ -683,6 +683,11 @@ final class MutableMessageHistoryView { hasChanges = true } } + if !transaction.currentUpdatedPeers.isEmpty { + if loadedState.updatePeers(postbox: postbox, updatedPeers: transaction.currentUpdatedPeers) { + hasChanges = true + } + } } if hasChanges { diff --git a/submodules/Postbox/Sources/MessageHistoryViewState.swift b/submodules/Postbox/Sources/MessageHistoryViewState.swift index cec31265a6..b2e937db9e 100644 --- a/submodules/Postbox/Sources/MessageHistoryViewState.swift +++ b/submodules/Postbox/Sources/MessageHistoryViewState.swift @@ -1514,6 +1514,43 @@ final class HistoryViewLoadedState { return updated } + func updatePeers(postbox: PostboxImpl, updatedPeers: [PeerId: Peer]) -> Bool { + var updated = false + for space in self.orderedEntriesBySpace.keys { + let spaceUpdated = self.orderedEntriesBySpace[space]!.mutableScan({ entry in + switch entry { + case let .MessageEntry(value, reloadAssociatedMessages, reloadPeers): + let message = value.message + var rebuild = false + var peers = message.peers + var author = message.author + for (peerId, _) in message.peers { + if let updatedPeer = updatedPeers[peerId] { + peers[peerId] = updatedPeer + rebuild = true + } + } + if let authorValue = author, let updatedAuthor = updatedPeers[authorValue.id] { + author = updatedAuthor + rebuild = true + } + + if rebuild { + let updatedMessage = message.withUpdatedPeers(peers).withUpdatedAuthor(author) + return .MessageEntry(MessageHistoryMessageEntry(message: updatedMessage, location: value.location, monthLocation: value.monthLocation, attributes: value.attributes), reloadAssociatedMessages: reloadAssociatedMessages, reloadPeers: reloadPeers) + } + case .IntermediateMessageEntry: + break + } + return nil + }) + if spaceUpdated { + updated = true + } + } + return updated + } + func add(entry: MutableMessageHistoryEntry) -> Bool { if let ignoreMessagesInTimestampRange = self.ignoreMessagesInTimestampRange { if ignoreMessagesInTimestampRange.contains(entry.index.timestamp) { diff --git a/submodules/TelegramUI/Components/Chat/ChatHistoryEntry/Sources/ChatHistoryEntry.swift b/submodules/TelegramUI/Components/Chat/ChatHistoryEntry/Sources/ChatHistoryEntry.swift index d402913f9e..ee01ff53fd 100644 --- a/submodules/TelegramUI/Components/Chat/ChatHistoryEntry/Sources/ChatHistoryEntry.swift +++ b/submodules/TelegramUI/Components/Chat/ChatHistoryEntry/Sources/ChatHistoryEntry.swift @@ -120,6 +120,18 @@ public enum ChatHistoryEntry: Identifiable, Comparable { if lhsMessage.stableVersion != rhsMessage.stableVersion { return false } + + if lhsMessage.peers.count != rhsMessage.peers.count { + return false + } + for (id, peer) in lhsMessage.peers { + if let otherPeer = rhsMessage.peers[id] { + if !peer.isEqual(otherPeer) { + return false + } + } + } + if lhsMessage.media.count != rhsMessage.media.count { return false }