diff --git a/submodules/Postbox/Sources/MessageHistoryView.swift b/submodules/Postbox/Sources/MessageHistoryView.swift index c3f80e1ef8..f517f5b3d9 100644 --- a/submodules/Postbox/Sources/MessageHistoryView.swift +++ b/submodules/Postbox/Sources/MessageHistoryView.swift @@ -1560,13 +1560,15 @@ public final class MessageHistoryView: PostboxView { } if !self.holeLater, let typingDraft = mutableView.typingDraft { - entries.append(MessageHistoryEntry( + let newEntry = MessageHistoryEntry( message: typingDraft, isRead: false, location: nil, monthLocation: nil, attributes: MutableMessageHistoryEntryAttributes(authorIsContact: false) - )) + ) + entries.append(newEntry) + entries.sort() } self.entries = entries diff --git a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift index 89eb151625..7fde2c2909 100644 --- a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift +++ b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift @@ -4018,7 +4018,7 @@ func replayFinalState( } case update(Update) - case cancel + case cancel(updatedTimestamp: Int32) } var liveTypingDraftUpdates: [PeerAndThreadId: [LiveTypingDraftUpdate]] = [:] @@ -4236,11 +4236,11 @@ func replayFinalState( let allKey = PeerAndThreadId(peerId: chatPeerId, threadId: nil) if liveTypingDraftUpdates[key] != nil { - liveTypingDraftUpdates[key] = [.cancel] - liveTypingDraftUpdates[allKey] = [.cancel] + liveTypingDraftUpdates[key] = [.cancel(updatedTimestamp: message.timestamp)] + liveTypingDraftUpdates[allKey] = [.cancel(updatedTimestamp: message.timestamp)] } else if let currentDraft = transaction.getCurrentTypingDraft(location: key) { - liveTypingDraftUpdates[key] = [.cancel] - liveTypingDraftUpdates[allKey] = [.cancel] + liveTypingDraftUpdates[key] = [.cancel(updatedTimestamp: message.timestamp)] + liveTypingDraftUpdates[allKey] = [.cancel(updatedTimestamp: message.timestamp)] messages[i] = messages[i].withUpdatedCustomStableId(currentDraft.stableId) } } @@ -6080,6 +6080,23 @@ func replayFinalState( } if !liveTypingDraftUpdates.isEmpty { + for (key, updates) in liveTypingDraftUpdates { + if key.threadId == nil { + var maxCancelledTimestamp: Int32? + for update in updates { + if case let .cancel(updatedTimestamp) = update { + if let current = maxCancelledTimestamp { + maxCancelledTimestamp = max(current, updatedTimestamp) + } else { + maxCancelledTimestamp = updatedTimestamp + } + } + } + if let maxCancelledTimestamp { + transaction.offsetPendingMessagesTimestamps(lowerBound: MessageId(peerId: key.peerId, namespace: Namespaces.Message.Local, id: 1), excludeIds: Set(), timestamp: maxCancelledTimestamp) + } + } + } transaction.combineTypingDrafts(locations: Set(liveTypingDraftUpdates.keys), update: { key, current in guard let update = liveTypingDraftUpdates[key]?.max(by: { lhs, rhs in switch lhs { @@ -6105,6 +6122,11 @@ func replayFinalState( if let current, current.id == update.id { timestamp = current.timestamp } + if current == nil { + if let index = transaction.getTopPeerMessageIndex(peerId: key.peerId) { + timestamp = max(timestamp, index.timestamp) + } + } return ( update.id, Namespaces.Message.Cloud,