-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: offline sync queue - reconcile TEMP messages on restart, auto-r… #6964
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
deepak0x
wants to merge
3
commits into
RocketChat:develop
Choose a base branch
from
deepak0x:feature/offline-sync-queue
base: develop
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { Q } from '@nozbe/watermelondb'; | ||
|
|
||
| import database from '../database'; | ||
| import log from './helpers/log'; | ||
| import { messagesStatus } from '../constants/messagesStatus'; | ||
| import { changeMessageStatus, resendMessage } from './sendMessage'; | ||
| import { getSingleMessage as getSingleMessageService } from '../services/restApi'; | ||
| import type { TMessageModel } from '../../definitions'; | ||
|
|
||
| const TEMP_RECONCILIATION_THRESHOLD_MS = 5 * 60 * 1000; | ||
|
|
||
| const hasMessageNotFoundHint = (value?: string): boolean => | ||
| /message[\s_-]*not[\s_-]*found|error-message-not-found/i.test(value ?? ''); | ||
|
|
||
| const shouldResendAfterLookupFailure = (error: unknown): boolean => { | ||
| if (!error) { | ||
| return false; | ||
| } | ||
|
|
||
| if (typeof error === 'string') { | ||
| return hasMessageNotFoundHint(error); | ||
| } | ||
|
|
||
| if (error instanceof Error) { | ||
| return hasMessageNotFoundHint(error.message); | ||
| } | ||
|
|
||
| const err = error as { | ||
| message?: string; | ||
| error?: string; | ||
| reason?: string; | ||
| data?: { message?: string; error?: string; errorType?: string }; | ||
| }; | ||
|
|
||
| return ( | ||
| hasMessageNotFoundHint(err.message) || | ||
| hasMessageNotFoundHint(err.error) || | ||
| hasMessageNotFoundHint(err.reason) || | ||
| hasMessageNotFoundHint(err.data?.message) || | ||
| hasMessageNotFoundHint(err.data?.error) || | ||
| hasMessageNotFoundHint(err.data?.errorType) | ||
| ); | ||
| }; | ||
|
|
||
| const processSequentially = <T>(items: T[], processItem: (item: T) => Promise<void>) => | ||
| items.reduce<Promise<void>>(async (previous, item) => { | ||
| await previous; | ||
| await processItem(item); | ||
| }, Promise.resolve()); | ||
|
|
||
| export async function reconcileTempMessages(): Promise<void> { | ||
| const db = database.active; | ||
| if (!db) { | ||
| return; | ||
| } | ||
|
|
||
| const msgCollection = db.get('messages'); | ||
| const threshold = Date.now() - TEMP_RECONCILIATION_THRESHOLD_MS; | ||
|
|
||
| try { | ||
| const tempMessages = await msgCollection | ||
| .query(Q.where('status', messagesStatus.TEMP), Q.where('ts', Q.lt(threshold))) | ||
| .fetch(); | ||
| await processSequentially(tempMessages as TMessageModel[], async record => { | ||
| try { | ||
| const result = await getSingleMessageService(record.id); | ||
| if (result?.success && result.message) { | ||
| await changeMessageStatus( | ||
| record.id, | ||
| messagesStatus.SENT, | ||
| record.tmid ?? undefined, | ||
| result.message | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (shouldResendAfterLookupFailure(result)) { | ||
| try { | ||
| await resendMessage(record, record.tmid ?? undefined); | ||
| } catch (e) { | ||
| log(e); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| log(result); | ||
| } catch (e) { | ||
| if (shouldResendAfterLookupFailure(e)) { | ||
| try { | ||
| await resendMessage(record, record.tmid ?? undefined); | ||
| } catch (resendError) { | ||
| log(resendError); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| log(e); | ||
| } | ||
| }); | ||
| } catch (e) { | ||
| log(e); | ||
| } | ||
| } | ||
|
|
||
| export async function retryErrorMessages(): Promise<void> { | ||
| const db = database.active; | ||
| if (!db) { | ||
| return; | ||
| } | ||
|
|
||
| const msgCollection = db.get('messages'); | ||
|
|
||
| try { | ||
| const errorMessages = await msgCollection.query(Q.where('status', messagesStatus.ERROR)).fetch(); | ||
| await processSequentially(errorMessages as TMessageModel[], async record => { | ||
| try { | ||
| await resendMessage(record, record.tmid ?? undefined); | ||
| } catch (e) { | ||
| log(e); | ||
| } | ||
| }); | ||
| } catch (e) { | ||
| log(e); | ||
| } | ||
| } | ||
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
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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { call, select, takeEvery } from 'redux-saga/effects'; | ||
|
|
||
| import { METEOR } from '../actions/actionsTypes'; | ||
| import log from '../lib/methods/helpers/log'; | ||
| import { retryErrorMessages } from '../lib/methods/messageSync'; | ||
|
|
||
| const getUser = state => state.login.user; | ||
|
|
||
| const retryErrorMessagesSaga = function* retryErrorMessagesSaga() { | ||
| const user = yield select(getUser); | ||
| if (!user?.id) { | ||
| return; | ||
| } | ||
| try { | ||
| yield call(retryErrorMessages); | ||
| } catch (e) { | ||
| log(e); | ||
| } | ||
| }; | ||
|
|
||
| const root = function* root() { | ||
| yield takeEvery(METEOR.SUCCESS, retryErrorMessagesSaga); | ||
| }; | ||
|
|
||
| export default root; |
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.