Improve draft sorting

This commit is contained in:
isaac 2026-04-30 12:14:04 +04:00
parent e7662a3de5
commit 06d744495c
2 changed files with 31 additions and 7 deletions

View file

@ -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

View file

@ -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,