-
Notifications
You must be signed in to change notification settings - Fork 382
fix(llc): Don't upsert out-of-window messages on message.updated/message.deleted events
#2794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VelikovPetar
wants to merge
1
commit into
master
Choose a base branch
from
bug/FLU-560_fix_message_updated_event_inserting_messages
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+378
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3194,7 +3194,7 @@ class ChannelClientState { | |
| final message = event.message; | ||
| if (message == null) return; | ||
|
|
||
| return updateMessage(message); | ||
| return _updateMessages([message], upsert: false); | ||
| }), | ||
| ); | ||
| } | ||
|
|
@@ -3209,7 +3209,9 @@ class ChannelClientState { | |
| deletedForMe: event.deletedForMe, | ||
| ); | ||
|
|
||
| return deleteMessage(message, hardDelete: hardDelete); | ||
| if (hardDelete) return deleteMessage(message, hardDelete: true); | ||
| // Soft delete, update the message only if loaded (upsert: false) | ||
| return _updateMessages([message], upsert: false); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use |
||
| }), | ||
| ); | ||
| } | ||
|
|
@@ -3930,18 +3932,20 @@ class ChannelClientState { | |
| void _updateMessages( | ||
| Iterable<Message> messages, { | ||
| Message Function(Message original, Message updated) update = _mergeUpdate, | ||
| bool upsert = true, | ||
| }) { | ||
| if (messages.isEmpty) return; | ||
|
|
||
| _updateThreadMessages(messages, update: update); | ||
| _updateChannelMessages(messages, update: update); | ||
| _updateThreadMessages(messages, update: update, upsert: upsert); | ||
| _updateChannelMessages(messages, update: update, upsert: upsert); | ||
| _updatePinnedMessages(messages, update: update); | ||
| _updateActiveLiveLocations(messages); | ||
| } | ||
|
|
||
| void _updateThreadMessages( | ||
| Iterable<Message> messages, { | ||
| Message Function(Message original, Message updated) update = _mergeUpdate, | ||
| bool upsert = true, | ||
| }) { | ||
| if (messages.isEmpty) return; | ||
|
|
||
|
|
@@ -3963,6 +3967,7 @@ class ChannelClientState { | |
| existing: threadMessages, | ||
| toMerge: value, | ||
| update: update, | ||
| upsert: upsert, | ||
| ); | ||
|
|
||
| // Update the thread with the modified message list. | ||
|
|
@@ -3976,6 +3981,7 @@ class ChannelClientState { | |
| void _updateChannelMessages( | ||
| Iterable<Message> messages, { | ||
| Message Function(Message original, Message updated) update = _mergeUpdate, | ||
| bool upsert = true, | ||
| }) { | ||
| if (messages.isEmpty) return; | ||
|
|
||
|
|
@@ -3996,6 +4002,7 @@ class ChannelClientState { | |
| existing: channelMessages, | ||
| toMerge: affectedMessages, | ||
| update: update, | ||
| upsert: upsert, | ||
| ); | ||
|
|
||
| // Calculate the new last message at time. | ||
|
|
@@ -4092,14 +4099,20 @@ class ChannelClientState { | |
| required Iterable<Message> existing, | ||
| required Iterable<Message> toMerge, | ||
| Message Function(Message original, Message updated) update = _mergeUpdate, | ||
| bool upsert = true, | ||
| }) { | ||
| if (toMerge.isEmpty) return existing; | ||
|
|
||
| // [update] decides whether each pair is reconciled (default — see | ||
| // `_mergeUpdate`) or replaced (`_replaceUpdate`, used by local rollback | ||
| // paths that don't want enrichment fallback to keep optimistic values). | ||
| // | ||
| // [upsert] controls whether ids not already in [existing] are inserted. | ||
| // Event-driven paths (`message.updated`, `message.deleted` soft) pass | ||
| // `upsert: false` so an out-of-window message isn't dropped into a gap | ||
| // between the loaded slice and history the client hasn't paged in yet. | ||
| final existingList = existing is List<Message> ? existing : existing.toList(); | ||
| final toMergeList = toMerge is List<Message> ? toMerge : toMerge.toList(); | ||
| var toMergeList = toMerge is List<Message> ? toMerge : toMerge.toList(); | ||
|
|
||
| // Single-message fast path. The hot ingest path (server echoes, edits, | ||
| // reactions, read receipts) always lands here, and `lastIndexWhere` + | ||
|
|
@@ -4108,6 +4121,10 @@ class ChannelClientState { | |
| if (toMergeList.length == 1) { | ||
| final message = toMergeList.first; | ||
| final oldIndex = existingList.lastIndexWhere((it) => it.id == message.id); | ||
|
|
||
| // upsert: false — skip update if message is not loaded | ||
| if (oldIndex == -1 && !upsert) return existingList; | ||
|
|
||
| final resolved = oldIndex == -1 ? message : update(existingList[oldIndex], message); | ||
|
|
||
| final mergedMessages = existingList.sortedUpsertAt( | ||
|
|
@@ -4127,6 +4144,13 @@ class ChannelClientState { | |
| ); | ||
| } | ||
|
|
||
| // upsert: false - skip messages not loaded in the window | ||
| if (!upsert) { | ||
| final existingIds = {for (final m in existingList) m.id}; | ||
| toMergeList = toMergeList.where((m) => existingIds.contains(m.id)).toList(); | ||
| if (toMergeList.isEmpty) return existingList; | ||
| } | ||
|
|
||
| // Batch path: receiver (`existingList`) is maintained sorted as a | ||
| // state invariant; `mergeSorted` sorts `toMergeList` internally and | ||
| // returns a sorted result. | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add the upsert flag to
updateMessagetoo and use it?