fix: return properly sorted messages from offline DB #2996
Merged
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.
🎯 Goal
This PR fixes a regression that we introduced about a month ago which causes a specific chain of events to break pagination in the
MessageList
whenever offline support is enabled.Steps to reproduce:
ChannelList
componentWith a recent bugfix to pagination, we pessimistically set the pagination cursors to
false
as an initial value. The bugfix relies on the fact that whenever we query for new messages, we should heuristically be able to figure out whether we have earlier (or later) messages to load through the sorting of the messages. However, when offline support is enabled we do not in fact do a query first as we optimistically fetch whatever's in the DB and only then query. This causes the pagination state to be irreparably broken and unable to recover (even with closing and reopening the app).The culprit was the fact that we were not properly sorting messages from our local
SQLite
instance and they arrived in reverse order, breaking the heuristics. After the messages are consumed, sorting is still applied so it was not noticeable at all. This "issue" has been dormant for a very long time, however due to the fact that we always had the pagination cursors set totrue
initially the issue has not surfaced until now.The reason why the sorting is wrong is because
SQLite
has a hard time parsingISOString
instances of a date. This means that it will lose precision and sometimes even fallback to alternative sorting if it cannot figure the input out. In our case, it caused the order of the messages to be reversed as it falls back to sorting lexicographically (andISOString
s can be sorted like so) causing the bug.To fix this, we resort to converting the date into a UNIX timestamp and sorting that way, as it's:
Linear ticket: https://linear.app/stream/issue/REACT-306/pagination-sometimes-breaks-when-offline-support-is-enabled
Possibly also fixes this GH issue: #2994
🛠 Implementation details
🎨 UI Changes
iOS
Android
🧪 Testing
☑️ Checklist
develop
branch