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 { if !self.holeLater, let typingDraft = mutableView.typingDraft {
entries.append(MessageHistoryEntry( let newEntry = MessageHistoryEntry(
message: typingDraft, message: typingDraft,
isRead: false, isRead: false,
location: nil, location: nil,
monthLocation: nil, monthLocation: nil,
attributes: MutableMessageHistoryEntryAttributes(authorIsContact: false) attributes: MutableMessageHistoryEntryAttributes(authorIsContact: false)
)) )
entries.append(newEntry)
entries.sort()
} }
self.entries = entries self.entries = entries

View file

@ -4018,7 +4018,7 @@ func replayFinalState(
} }
case update(Update) case update(Update)
case cancel case cancel(updatedTimestamp: Int32)
} }
var liveTypingDraftUpdates: [PeerAndThreadId: [LiveTypingDraftUpdate]] = [:] var liveTypingDraftUpdates: [PeerAndThreadId: [LiveTypingDraftUpdate]] = [:]
@ -4236,11 +4236,11 @@ func replayFinalState(
let allKey = PeerAndThreadId(peerId: chatPeerId, threadId: nil) let allKey = PeerAndThreadId(peerId: chatPeerId, threadId: nil)
if liveTypingDraftUpdates[key] != nil { if liveTypingDraftUpdates[key] != nil {
liveTypingDraftUpdates[key] = [.cancel] liveTypingDraftUpdates[key] = [.cancel(updatedTimestamp: message.timestamp)]
liveTypingDraftUpdates[allKey] = [.cancel] liveTypingDraftUpdates[allKey] = [.cancel(updatedTimestamp: message.timestamp)]
} else if let currentDraft = transaction.getCurrentTypingDraft(location: key) { } else if let currentDraft = transaction.getCurrentTypingDraft(location: key) {
liveTypingDraftUpdates[key] = [.cancel] liveTypingDraftUpdates[key] = [.cancel(updatedTimestamp: message.timestamp)]
liveTypingDraftUpdates[allKey] = [.cancel] liveTypingDraftUpdates[allKey] = [.cancel(updatedTimestamp: message.timestamp)]
messages[i] = messages[i].withUpdatedCustomStableId(currentDraft.stableId) messages[i] = messages[i].withUpdatedCustomStableId(currentDraft.stableId)
} }
} }
@ -6080,6 +6080,23 @@ func replayFinalState(
} }
if !liveTypingDraftUpdates.isEmpty { 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 transaction.combineTypingDrafts(locations: Set(liveTypingDraftUpdates.keys), update: { key, current in
guard let update = liveTypingDraftUpdates[key]?.max(by: { lhs, rhs in guard let update = liveTypingDraftUpdates[key]?.max(by: { lhs, rhs in
switch lhs { switch lhs {
@ -6105,6 +6122,11 @@ func replayFinalState(
if let current, current.id == update.id { if let current, current.id == update.id {
timestamp = current.timestamp timestamp = current.timestamp
} }
if current == nil {
if let index = transaction.getTopPeerMessageIndex(peerId: key.peerId) {
timestamp = max(timestamp, index.timestamp)
}
}
return ( return (
update.id, update.id,
Namespaces.Message.Cloud, Namespaces.Message.Cloud,