fix: presence not updating after app resumes from background#7120
fix: presence not updating after app resumes from background#7120
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughAdds DM-specific presence refresh: queries local DB for open, unarchived direct-message subscriptions to collect user IDs, short-circuits presence requests when Changes
Sequence Diagram(s)sequenceDiagram
participant LoginSaga as Login Saga
participant PresenceLib as Presence Library
participant DB as WatermelonDB
participant SDK as Server SDK
LoginSaga->>PresenceLib: subscribeUsersPresence()
PresenceLib-->>LoginSaga: subscribed
LoginSaga->>PresenceLib: refreshDmUsersPresence()
PresenceLib->>DB: query subscriptions (t='d', open=true, archived=false)
DB-->>PresenceLib: DM subscription records
PresenceLib->>PresenceLib: extract, filter, dedupe user IDs
alt user IDs non-empty
PresenceLib->>SDK: getUsersPresence(ids)
SDK-->>PresenceLib: presence data
end
PresenceLib-->>LoginSaga: refresh complete
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Warning Review ran into problems🔥 ProblemsErrors were encountered while retrieving linked issues. Errors (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@app/containers/Status/index.tsx`:
- Around line 23-28: The effect currently calls getUserPresence(id) on every
mount when connected which causes duplicate enqueues during virtualized
remounts; restore a guard that only fetches on actual reconnects (e.g., re-add
the statusState === 'loading' check used before) or add deduping inside
getUserPresence/batching logic so repeated id enqueues are ignored; update the
useEffect (referencing useEffect, connected, id, status/statusState) to only
call getUserPresence when connection state transitions to connected or
statusState indicates an initial load, or modify getUserPresence to check an
in-flight set and skip enqueueing duplicate ids.
In `@app/lib/methods/getUsersPresence.ts`:
- Around line 159-167: The helper refreshDmUsersPresence currently swallows all
errors, preventing callers in app/sagas/login.js and app/sagas/state.js from
seeing failures; change it to either rethrow the caught error or log it before
returning — specifically update refreshDmUsersPresence (which calls
getDirectMessageUserIds() and getUsersPresence(dmUserIds)) so that in the catch
block you either throw the error (allowing the saga callers to handle/log it) or
call the shared logger (e.g., processLogger or the project's logging utility)
with the error and contextual message, rather than silently ignoring it.
- Around line 145-153: getDirectMessageUserIds currently picks sub.uids?.[0]
which may be the logged-in user; change getDirectMessageUserIds so for each
subscription (subscriptions / sub.uids) you find the peer id by excluding the
current user's id (e.g. obtain currentUserId via Meteor.userId() or the existing
auth helper) and selecting the remaining uid (for DMs, the other participant),
then collect and dedupe those peer ids instead of using uids[0]; ensure to
handle cases where uids may have multiple entries or missing values by filtering
falsy values before deduping.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d76acab2-b30d-45c0-8569-84d7dad3fa1a
📒 Files selected for processing (4)
app/containers/Status/index.tsxapp/lib/methods/getUsersPresence.tsapp/sagas/login.jsapp/sagas/state.js
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: ESLint and Test / run-eslint-and-test
🧰 Additional context used
📓 Path-based instructions (5)
**/*.{js,jsx,ts,tsx,json}
📄 CodeRabbit inference engine (CLAUDE.md)
Configure Prettier with tabs, single quotes, 130 character width, no trailing commas, arrow parens avoid, and bracket same line
Files:
app/containers/Status/index.tsxapp/sagas/login.jsapp/sagas/state.jsapp/lib/methods/getUsersPresence.ts
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit inference engine (CLAUDE.md)
Use ESLint with
@rocket.chat/eslint-configbase configuration including React, React Native, TypeScript, and Jest plugins
Files:
app/containers/Status/index.tsxapp/sagas/login.jsapp/sagas/state.jsapp/lib/methods/getUsersPresence.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (CLAUDE.md)
Use TypeScript with strict mode enabled and configure baseUrl to app/ for import resolution
**/*.{ts,tsx}: Use TypeScript for type safety; add explicit type annotations to function parameters and return types
Prefer interfaces over type aliases for defining object shapes in TypeScript
Use enums for sets of related constants rather than magic strings or numbers
Files:
app/containers/Status/index.tsxapp/lib/methods/getUsersPresence.ts
app/containers/**/*.{ts,tsx}
📄 CodeRabbit inference engine (CLAUDE.md)
Place reusable UI components in app/containers/ directory
Files:
app/containers/Status/index.tsx
**/*.{js,ts,jsx,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
**/*.{js,ts,jsx,tsx}: Use descriptive names for functions, variables, and classes that clearly convey their purpose
Write comments that explain the 'why' behind code decisions, not the 'what'
Keep functions small and focused on a single responsibility
Use const by default, let when reassignment is needed, and avoid var
Prefer async/await over .then() chains for handling asynchronous operations
Use explicit error handling with try/catch blocks for async operations
Avoid deeply nested code; refactor complex logic into helper functions
Files:
app/containers/Status/index.tsxapp/sagas/login.jsapp/sagas/state.jsapp/lib/methods/getUsersPresence.ts
🧠 Learnings (5)
📚 Learning: 2026-04-07T17:49:17.538Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat.ReactNative PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-04-07T17:49:17.538Z
Learning: Applies to app/AppContainer.tsx : Implement root navigation container logic in app/AppContainer.tsx to switch between authentication states
Applied to files:
app/containers/Status/index.tsx
📚 Learning: 2026-03-10T15:21:45.098Z
Learnt from: Rohit3523
Repo: RocketChat/Rocket.Chat.ReactNative PR: 7046
File: app/containers/InAppNotification/NotifierComponent.stories.tsx:46-75
Timestamp: 2026-03-10T15:21:45.098Z
Learning: In `app/containers/InAppNotification/NotifierComponent.tsx` (React Native, Rocket.Chat), `NotifierComponent` is exported as a Redux-connected component via `connect(mapStateToProps)`. The `isMasterDetail` prop is automatically injected from `state.app.isMasterDetail` and does not need to be passed explicitly at call sites or in Storybook stories that use the default (connected) export.
Applied to files:
app/containers/Status/index.tsxapp/lib/methods/getUsersPresence.ts
📚 Learning: 2026-04-07T17:49:17.538Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat.ReactNative PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-04-07T17:49:17.538Z
Learning: Applies to app/sagas/**/*.{ts,tsx} : Place Redux sagas in app/sagas/ directory with separate files for init, login, rooms, messages, encryption, deepLinking, and videoConf side effects
Applied to files:
app/sagas/login.jsapp/sagas/state.js
📚 Learning: 2026-04-07T17:49:17.538Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat.ReactNative PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-04-07T17:49:17.538Z
Learning: Applies to app/lib/store/**/*.{ts,tsx} : Configure Redux store in app/lib/store/ with middleware for saga, app state, and internet state
Applied to files:
app/sagas/state.js
📚 Learning: 2026-04-07T17:49:17.538Z
Learnt from: CR
Repo: RocketChat/Rocket.Chat.ReactNative PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-04-07T17:49:17.538Z
Learning: Applies to @(app/sagas/videoConf.ts|app/lib/methods/videoConf.ts) : Manage video conferencing via Redux actions/reducers/sagas in app/sagas/videoConf.ts and app/lib/methods/videoConf.ts using server-managed Jitsi integration; do not conflate with VoIP
Applied to files:
app/sagas/state.js
🔇 Additional comments (1)
app/sagas/state.js (1)
75-84:⚠️ Potential issue | 🟠 MajorGuard the reconnect resubscribe path against duplicate subscriptions.
handleMeteorConnect()runs on everyMETEOR.SUCCESS(app/sagas/state.js:92) and unconditionally callssubscribeUsersPresence(), which issues multiplesdk.subscribe()calls without any guard or unsubscribe logic. Reconnect flaps will accumulate duplicate presence subscriptions. Add a flag to track subscription state or explicitly unsubscribe before resubscribing to prevent duplicate stream events.⛔ Skipped due to learnings
Learnt from: CR Repo: RocketChat/Rocket.Chat.ReactNative PR: 0 File: CLAUDE.md:0-0 Timestamp: 2026-04-07T17:49:17.538Z Learning: Applies to app/lib/services/sdk.ts : Use Rocket.Chat JS SDK in app/lib/services/sdk.ts for WebSocket real-time subscriptions
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
♻️ Duplicate comments (1)
app/lib/methods/getUsersPresence.ts (1)
158-160:⚠️ Potential issue | 🟠 MajorDon’t silently swallow DB/query failures in DM ID lookup.
Line 159 returns
[]on errors, which hides failures and bypasses the logging path inrefreshDmUsersPresence(Line 170). Let the error bubble or log it here with context.Suggested fix (bubble to caller logger)
} catch (e) { - return []; + throw e; } };🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/lib/methods/getUsersPresence.ts` around lines 158 - 160, In getUsersPresence.ts the catch block that returns [] silently swallows DB/query failures; change the catch in the async function (the block around "} catch (e) { return []; }") to either log the error with context (including the query/DM id) and then rethrow, or simply rethrow the original error so refreshDmUsersPresence can handle/log it; do not return an empty array on exceptions—use throw e or throw new Error(`getUsersPresence failed for ${dmId}: ${e.message}`) so refreshDmUsersPresence's logger sees the failure.
🧹 Nitpick comments (1)
app/lib/methods/getUsersPresence.ts (1)
149-156: Guard early whenloggedUserIdis unavailable.If
loggedUserIdis missing on Line 149, Line 155 can pick an arbitrary UID and request the wrong presence target. Consider short-circuiting before mapping subscriptions.Suggested tweak
export const getDirectMessageUserIds = async (): Promise<string[]> => { try { const db = database.active; const loggedUserId = reduxStore.getState().login.user?.id; + if (!loggedUserId) { + return []; + } const subscriptionsCollection = db.get('subscriptions'); const subscriptions = await subscriptionsCollection .query(Q.where('t', 'd'), Q.where('open', true), Q.where('archived', false)) .fetch();🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/lib/methods/getUsersPresence.ts` around lines 149 - 156, Guard against a missing loggedUserId by short-circuiting before processing subscriptions: check reduxStore.getState().login.user?.id (loggedUserId) and if undefined/null return an empty userIds result or skip the mapping logic that builds userIds from subscriptionsCollection/subscriptions; this prevents the map in subscriptions.map(...).find(...) from selecting arbitrary UIDs when loggedUserId is unavailable and ensures functions that consume userIds receive a safe empty array.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@app/lib/methods/getUsersPresence.ts`:
- Around line 158-160: In getUsersPresence.ts the catch block that returns []
silently swallows DB/query failures; change the catch in the async function (the
block around "} catch (e) { return []; }") to either log the error with context
(including the query/DM id) and then rethrow, or simply rethrow the original
error so refreshDmUsersPresence can handle/log it; do not return an empty array
on exceptions—use throw e or throw new Error(`getUsersPresence failed for
${dmId}: ${e.message}`) so refreshDmUsersPresence's logger sees the failure.
---
Nitpick comments:
In `@app/lib/methods/getUsersPresence.ts`:
- Around line 149-156: Guard against a missing loggedUserId by short-circuiting
before processing subscriptions: check reduxStore.getState().login.user?.id
(loggedUserId) and if undefined/null return an empty userIds result or skip the
mapping logic that builds userIds from subscriptionsCollection/subscriptions;
this prevents the map in subscriptions.map(...).find(...) from selecting
arbitrary UIDs when loggedUserId is unavailable and ensures functions that
consume userIds receive a safe empty array.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 09aa515c-81ae-4e56-a0ec-afde1f01e8b1
📒 Files selected for processing (1)
app/lib/methods/getUsersPresence.ts
📜 Review details
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{js,jsx,ts,tsx,json}
📄 CodeRabbit inference engine (CLAUDE.md)
Configure Prettier with tabs, single quotes, 130 character width, no trailing commas, arrow parens avoid, and bracket same line
Files:
app/lib/methods/getUsersPresence.ts
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit inference engine (CLAUDE.md)
Use ESLint with
@rocket.chat/eslint-configbase configuration including React, React Native, TypeScript, and Jest plugins
Files:
app/lib/methods/getUsersPresence.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (CLAUDE.md)
Use TypeScript with strict mode enabled and configure baseUrl to app/ for import resolution
**/*.{ts,tsx}: Use TypeScript for type safety; add explicit type annotations to function parameters and return types
Prefer interfaces over type aliases for defining object shapes in TypeScript
Use enums for sets of related constants rather than magic strings or numbers
Files:
app/lib/methods/getUsersPresence.ts
**/*.{js,ts,jsx,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
**/*.{js,ts,jsx,tsx}: Use descriptive names for functions, variables, and classes that clearly convey their purpose
Write comments that explain the 'why' behind code decisions, not the 'what'
Keep functions small and focused on a single responsibility
Use const by default, let when reassignment is needed, and avoid var
Prefer async/await over .then() chains for handling asynchronous operations
Use explicit error handling with try/catch blocks for async operations
Avoid deeply nested code; refactor complex logic into helper functions
Files:
app/lib/methods/getUsersPresence.ts
🧠 Learnings (1)
📚 Learning: 2026-03-10T15:21:45.098Z
Learnt from: Rohit3523
Repo: RocketChat/Rocket.Chat.ReactNative PR: 7046
File: app/containers/InAppNotification/NotifierComponent.stories.tsx:46-75
Timestamp: 2026-03-10T15:21:45.098Z
Learning: In `app/containers/InAppNotification/NotifierComponent.tsx` (React Native, Rocket.Chat), `NotifierComponent` is exported as a Redux-connected component via `connect(mapStateToProps)`. The `isMasterDetail` prop is automatically injected from `state.app.isMasterDetail` and does not need to be passed explicitly at call sites or in Storybook stories that use the default (connected) export.
Applied to files:
app/lib/methods/getUsersPresence.ts
🔇 Additional comments (2)
app/lib/methods/getUsersPresence.ts (2)
60-61: Good cache-busting addition for on-demand presence refresh.Adding
_t: Date.now()on Line 61 is a solid way to avoid staleusers.presenceresponses for supported server versions.
163-171: Nice improvement adding error logging in foreground/login refresh flow.The
log(e)on Line 170 makes refresh failures diagnosable instead of fully silent.
There was a problem hiding this comment.
♻️ Duplicate comments (2)
app/lib/methods/getUsersPresence.ts (2)
160-163:⚠️ Potential issue | 🟡 MinorCollect all non-self DM user IDs, not just the first one.
finddrops additional non-selfuids, so multi-user direct rooms can still show stale presence for skipped peers after resume.Suggested fix
const userIds = subscriptions - .map((sub: { uids?: string[] }) => sub.uids?.find(uid => uid && uid !== loggedUserId)) - .filter((uid): uid is string => Boolean(uid)); + .flatMap((sub: { uids?: string[] }) => sub.uids ?? []) + .filter((uid): uid is string => Boolean(uid) && uid !== loggedUserId);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/lib/methods/getUsersPresence.ts` around lines 160 - 163, The current mapping in getUsersPresence.ts uses .map(... .find(...)) which picks only the first non-self uid per subscription, dropping other peers in multi-user DMs; replace that logic so each subscription's uids array is iterated to collect all uids !== loggedUserId (e.g., use .flatMap or map+flat and .filter(uid => uid && uid !== loggedUserId)) and then dedupe with new Set before returning; update the userIds creation (the variable named userIds and the mapping over subscriptions) to collect all non-self ids rather than the first match.
164-166:⚠️ Potential issue | 🟡 MinorDon’t hide subscription query failures from the refresh logger.
Returning
[]here makesrefreshDmUsersPresencelook successful, so DB/query failures become invisible and the presence refresh silently does nothing.Suggested fix
} catch (e) { - return []; + log(e); + return []; }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/lib/methods/getUsersPresence.ts` around lines 164 - 166, The catch in getUsersPresence currently swallows all errors by returning [], which hides subscription/query failures from refreshDmUsersPresence; instead, update getUsersPresence's catch to log the caught error with context using the module's logger (e.g., refreshLogger or the existing logger) and then rethrow the error (or propagate it) so refreshDmUsersPresence can observe the failure and handle/retry accordingly; ensure the log message includes the function name "getUsersPresence" and the caught error details.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@app/lib/methods/getUsersPresence.ts`:
- Around line 160-163: The current mapping in getUsersPresence.ts uses .map(...
.find(...)) which picks only the first non-self uid per subscription, dropping
other peers in multi-user DMs; replace that logic so each subscription's uids
array is iterated to collect all uids !== loggedUserId (e.g., use .flatMap or
map+flat and .filter(uid => uid && uid !== loggedUserId)) and then dedupe with
new Set before returning; update the userIds creation (the variable named
userIds and the mapping over subscriptions) to collect all non-self ids rather
than the first match.
- Around line 164-166: The catch in getUsersPresence currently swallows all
errors by returning [], which hides subscription/query failures from
refreshDmUsersPresence; instead, update getUsersPresence's catch to log the
caught error with context using the module's logger (e.g., refreshLogger or the
existing logger) and then rethrow the error (or propagate it) so
refreshDmUsersPresence can observe the failure and handle/retry accordingly;
ensure the log message includes the function name "getUsersPresence" and the
caught error details.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 59606381-d5e6-48ae-89ed-8bcf51444af4
📒 Files selected for processing (1)
app/lib/methods/getUsersPresence.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: ESLint and Test / run-eslint-and-test
- GitHub Check: format
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{js,ts,jsx,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
**/*.{js,ts,jsx,tsx}: Use descriptive names for functions, variables, and classes that clearly convey their purpose
Write comments that explain the 'why' behind code decisions, not the 'what'
Keep functions small and focused on a single responsibility
Use const by default, let when reassignment is needed, and avoid var
Prefer async/await over .then() chains for handling asynchronous operations
Use explicit error handling with try/catch blocks for async operations
Avoid deeply nested code; refactor complex logic into helper functions
Files:
app/lib/methods/getUsersPresence.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
**/*.{ts,tsx}: Use TypeScript for type safety; add explicit type annotations to function parameters and return types
Prefer interfaces over type aliases for defining object shapes in TypeScript
Use enums for sets of related constants rather than magic strings or numbers
**/*.{ts,tsx}: Use TypeScript with strict mode enabled and baseUrl set to app/ for module imports
Support iOS 13.4+ and Android 6.0+ as minimum target platforms
Files:
app/lib/methods/getUsersPresence.ts
**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.{ts,tsx,js,jsx}: Use tabs for indentation with single quotes, 130 character line width, no trailing commas, and avoid arrow function parentheses when possible
Use ESLint with@rocket.chat/eslint-configbase including React, React Native, TypeScript, and Jest plugins
Files:
app/lib/methods/getUsersPresence.ts
🧠 Learnings (1)
📚 Learning: 2026-03-10T15:21:45.098Z
Learnt from: Rohit3523
Repo: RocketChat/Rocket.Chat.ReactNative PR: 7046
File: app/containers/InAppNotification/NotifierComponent.stories.tsx:46-75
Timestamp: 2026-03-10T15:21:45.098Z
Learning: In `app/containers/InAppNotification/NotifierComponent.tsx` (React Native, Rocket.Chat), `NotifierComponent` is exported as a Redux-connected component via `connect(mapStateToProps)`. The `isMasterDetail` prop is automatically injected from `state.app.isMasterDetail` and does not need to be passed explicitly at call sites or in Storybook stories that use the default (connected) export.
Applied to files:
app/lib/methods/getUsersPresence.ts
OtavioStasiak
left a comment
There was a problem hiding this comment.
LGTM!
Run e2e tests and you can merge
Proposed changes
The app fails to fetch the latest user presence after returning from the background, resulting in outdated status being displayed.
Issue(s)
https://rocketchat.atlassian.net/browse/PRES-13
How to test or reproduce
User Aon the web and set the status to OnlineUser Bon mobileUser Astatus to BusyUser Astatus as BusyScreenshots
Types of changes
Checklist
Further comments
Summary by CodeRabbit
New Features
Bug Fixes
Reliability