Skip to content

fix: fetch user presence when app come to foreground#7120

Draft
Rohit3523 wants to merge 10 commits intodevelopfrom
presence-fix
Draft

fix: fetch user presence when app come to foreground#7120
Rohit3523 wants to merge 10 commits intodevelopfrom
presence-fix

Conversation

@Rohit3523
Copy link
Copy Markdown
Member

@Rohit3523 Rohit3523 commented Apr 9, 2026

Proposed changes

Issue(s)

https://rocketchat.atlassian.net/browse/PRES-13

How to test or reproduce

Screenshots

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • Improvement (non-breaking change which improves a current function)
  • New feature (non-breaking change which adds functionality)
  • Documentation update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works (if applicable)
  • I have added necessary documentation (if applicable)
  • Any dependent changes have been merged and published in downstream modules

Further comments

Summary by CodeRabbit

  • New Features

    • Direct message contacts' online status is now automatically kept up to date.
  • Improvements

    • Enhanced connection stability with automatic retry logic for network reconnection.
    • Online status information now refreshes after login, when the app returns to foreground, and when connection is restored.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 9, 2026

Walkthrough

The pull request adds automatic refresh of direct-message user presence at key lifecycle points: after login, when the app returns to foreground, and when the Meteor connection succeeds. For server versions >= 3.0.0, presence requests now include a timestamp parameter. Connection readiness checks are implemented before presence operations.

Changes

Cohort / File(s) Summary
Core DM Presence Logic
app/lib/methods/getUsersPresence.ts
Added getDirectMessageUserIds() to query open, non-archived DM subscriptions and extract user IDs from WatermelonDB. Added refreshDmUsersPresence() to invoke presence updates only when DM user list is non-empty. Modified getUsersPresence() to include _t: Date.now() timestamp in requests for servers >= 3.0.0.
Login Presence Refresh
app/sagas/login.js
Imported refreshDmUsersPresence and created fetchDmUsersPresenceFork saga generator. Extended handleLoginSuccess to fork DM presence refresh after successful login.
Connection & State Management
app/sagas/state.js
Added waitForConnection generator to poll for authenticated and connected state. Modified appHasComeBackToForeground to wait for connection readiness before presence operations and call refreshDmUsersPresence after setting user online. Added handleMeteorConnect saga triggered on connection success to re-subscribe to presence stream and refresh DM presence. Updated root saga to register connection-triggered handler.

Sequence Diagram(s)

sequenceDiagram
    participant App as App Lifecycle
    participant Login as Login Saga
    participant DM as DM Presence<br/>Refresh
    participant DB as WatermelonDB<br/>Subscriptions
    participant API as Presence API
    
    App->>Login: User logs in
    Login->>DM: call(refreshDmUsersPresence)
    DM->>DB: getDirectMessageUserIds()
    DB-->>DM: [dmUserIds...]
    alt Non-empty DM list
        DM->>API: getUsersPresence(dmUserIds)
        API-->>DM: presence data
    else Empty list
        DM-->>DM: abort (no DMs)
    end
    DM-->>Login: success/error
    
    App->>App: Foreground event
    App->>App: waitForConnection()
    alt Connection ready
        App->>API: setUserPresenceOnline()
        App->>DM: call(refreshDmUsersPresence)
        DM->>DB: getDirectMessageUserIds()
        DB-->>DM: [dmUserIds...]
        DM->>API: getUsersPresence(dmUserIds)
    else Not ready
        App-->>App: abort foreground tasks
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR adds refresh logic for DM user presence on app foreground and Meteor reconnection, but does not address the core issue: updating presence from online to away after idle timeout expires. The changes fetch presence on foreground/reconnect but do not implement idle timeout detection or automatic presence status transitions needed to meet PRES-13 requirements.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly reflects the main change: fetching user presence when the app comes to the foreground, which aligns with the primary modification in the code changes.
Out of Scope Changes check ✅ Passed All changes are focused on refreshing DM user presence at key lifecycle moments; no unrelated modifications detected beyond the stated objectives.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Rohit3523 Rohit3523 requested a deployment to experimental_android_build April 9, 2026 20:49 — with GitHub Actions Waiting
@Rohit3523 Rohit3523 had a problem deploying to experimental_ios_build April 9, 2026 20:49 — with GitHub Actions Error
@Rohit3523 Rohit3523 had a problem deploying to official_android_build April 9, 2026 20:49 — with GitHub Actions Error
@Rohit3523 Rohit3523 had a problem deploying to official_android_build April 9, 2026 23:27 — with GitHub Actions Error
@Rohit3523 Rohit3523 temporarily deployed to experimental_android_build April 9, 2026 23:27 — with GitHub Actions Inactive
@Rohit3523 Rohit3523 had a problem deploying to experimental_ios_build April 9, 2026 23:27 — with GitHub Actions Error
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between cb0feee and fc95fa4.

📒 Files selected for processing (4)
  • app/containers/Status/index.tsx
  • app/lib/methods/getUsersPresence.ts
  • app/sagas/login.js
  • app/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.tsx
  • app/sagas/login.js
  • app/sagas/state.js
  • app/lib/methods/getUsersPresence.ts
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

Use ESLint with @rocket.chat/eslint-config base configuration including React, React Native, TypeScript, and Jest plugins

Files:

  • app/containers/Status/index.tsx
  • app/sagas/login.js
  • app/sagas/state.js
  • 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/containers/Status/index.tsx
  • app/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.tsx
  • app/sagas/login.js
  • app/sagas/state.js
  • app/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.tsx
  • app/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.js
  • 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/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 | 🟠 Major

Guard the reconnect resubscribe path against duplicate subscriptions.

handleMeteorConnect() runs on every METEOR.SUCCESS (app/sagas/state.js:92) and unconditionally calls subscribeUsersPresence(), which issues multiple sdk.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

@Rohit3523
Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 9, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
app/lib/methods/getUsersPresence.ts (1)

158-160: ⚠️ Potential issue | 🟠 Major

Don’t silently swallow DB/query failures in DM ID lookup.

Line 159 returns [] on errors, which hides failures and bypasses the logging path in refreshDmUsersPresence (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 when loggedUserId is unavailable.

If loggedUserId is 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

📥 Commits

Reviewing files that changed from the base of the PR and between fc95fa4 and baed9ed.

📒 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-config base 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 stale users.presence responses 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.

@Rohit3523 Rohit3523 requested a deployment to approve_e2e_testing April 10, 2026 00:09 — with GitHub Actions Waiting
@Rohit3523 Rohit3523 temporarily deployed to experimental_android_build April 10, 2026 00:12 — with GitHub Actions Inactive
@Rohit3523 Rohit3523 requested a deployment to experimental_ios_build April 10, 2026 00:12 — with GitHub Actions Waiting
@Rohit3523 Rohit3523 requested a deployment to official_android_build April 10, 2026 00:12 — with GitHub Actions Waiting
@Rohit3523 Rohit3523 requested a deployment to official_ios_build April 10, 2026 00:12 — with GitHub Actions Waiting
@Rohit3523 Rohit3523 requested a deployment to upload_experimental_android April 10, 2026 00:45 — with GitHub Actions Waiting
@github-actions
Copy link
Copy Markdown

Android Build Available

Rocket.Chat Experimental 4.72.0.108522

Internal App Sharing: https://play.google.com/apps/test/RQVpXLytHNc/ahAO29uNSoNf01ZWHmg0FgRbh7Abupghc0Noz_-XxEDgz8CA9PZQktD7d17JnOA6UBNdXu-RxJyUHI_3Rz2dM7RLM0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant