Skip to content

chore: changes to foreground and background logic#6958

Open
Rohit3523 wants to merge 3 commits intodevelopfrom
background-foreground-logic
Open

chore: changes to foreground and background logic#6958
Rohit3523 wants to merge 3 commits intodevelopfrom
background-foreground-logic

Conversation

@Rohit3523
Copy link
Contributor

@Rohit3523 Rohit3523 commented Feb 1, 2026

Proposed changes

Fix app state handling in sagas/state.js by ensuring logic only runs when appRoot is available, extracting shared background/foreground logic into a reusable function, and gating it on isAuthenticated && meteor.connected. Update presence handling so authentication/connection logic also runs in background, and add safeguards to mark user as away if the app goes to background during connection.

Issue(s)

https://rocketchat.atlassian.net/browse/CORE-1781

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

    • Automatic user presence updates: status now switches to "away" when the app goes to background and to "online" when it returns to foreground.
  • Improvements

    • More reliable background/foreground handling with consolidated authentication and connection readiness checks.
    • Improved error handling and logging around presence updates and session saving.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 1, 2026

Walkthrough

Added background-presence handling during login and consolidated authentication+connection readiness checks into a shared helper used by foreground/background sagas. A new saga forks on login success to set user presence to away when the app is backgrounded.

Changes

Cohort / File(s) Summary
Background Presence Handling
app/sagas/login.js
Imported setUserPresenceAway, added checkBackgroundAndSetAway generator, and forked it from handleLoginSuccess to set presence-away when login completes while app is backgrounded.
Auth & Connection Refactoring
app/sagas/state.js
Introduced isAuthAndConnected helper saga; refactored appHasComeBackToForeground and appHasComeBackToBackground to use the helper, added early-exit guards, and returned presence-online result for foreground.

Sequence Diagram

sequenceDiagram
    participant App as App Instance
    participant LoginSaga as Login Saga
    participant StateSaga as State Saga
    participant RestAPI as REST API
    participant Redux as Redux State

    App->>LoginSaga: Login success event
    LoginSaga->>Redux: fetch user-related data
    LoginSaga->>LoginSaga: fork checkBackgroundAndSetAway
    alt App in background
        LoginSaga->>RestAPI: setUserPresenceAway()
    end

    App->>StateSaga: App -> Foreground
    StateSaga->>Redux: isAuthAndConnected?
    alt Authenticated & connected
        StateSaga->>Redux: fetch server
        StateSaga->>RestAPI: setUserPresenceOnline()
    end

    App->>StateSaga: App -> Background
    StateSaga->>Redux: isAuthAndConnected?
    alt Authenticated & connected
        StateSaga->>Redux: save last auth session
        StateSaga->>RestAPI: setUserPresenceAway()
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested reviewers

  • diegolmello

Poem

🐰 When login hits and the screen is away,

I hop and I check if the app wants to stay.
If quiet and hidden, I whisper "away"—
else hop online, ready to play! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'chore: changes to foreground and background logic' is overly vague and generic, using non-descriptive phrasing that lacks specificity about the actual implementation changes. Revise the title to be more specific and descriptive. For example: 'refactor: extract and consolidate foreground/background logic with presence updates' would better convey the changes.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR addresses all primary objectives from CORE-1781: consolidates foreground/background logic via isAuthAndConnected helper, ensures appRoot checks, gates logic on authentication/connection, and adds background presence handling with forked saga logic.
Out of Scope Changes check ✅ Passed All changes directly align with CORE-1781 requirements: refactoring state.js logic, adding checkBackgroundAndSetAway saga, and implementing presence handling for background events with no unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch background-foreground-logic

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
Copy link
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 2, 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
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: 1

🤖 Fix all issues with AI agents
In `@app/sagas/login.js`:
- Around line 226-235: checkBackgroundAndSetAway currently sets presence based
only on background state and can run before the app/root/auth/connection are
ready; update the generator to first guard that the app is inside the root and
that auth and connection are ready before calling setUserPresenceAway.
Concretely, inside checkBackgroundAndSetAway use select to verify
RootEnum.ROOT_INSIDE (e.g. select(state => state.root.current ===
RootEnum.ROOT_INSIDE)) and also select auth/connection readiness flags (e.g.
select(state => state.auth.ready) and select(state => state.connection.ready) or
existing selectors like isAuthReady/isConnectionReady) and only if all are true
proceed to check the background flag and call setUserPresenceAway; keep the
try/catch and error logging as-is.

@Rohit3523
Copy link
Contributor Author

https://github.com/coderabbitai review

@Rohit3523 Rohit3523 marked this pull request as ready for review February 4, 2026 17:50
@Rohit3523 Rohit3523 requested a deployment to approve_e2e_testing February 4, 2026 17:50 — with GitHub Actions Waiting
@Rohit3523
Copy link
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 4, 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.

@Rohit3523 Rohit3523 requested a deployment to experimental_android_build February 4, 2026 17:52 — with GitHub Actions Waiting
@Rohit3523 Rohit3523 requested a deployment to official_android_build February 4, 2026 17:52 — with GitHub Actions Waiting
@Rohit3523 Rohit3523 requested a deployment to experimental_ios_build February 4, 2026 17:52 — with GitHub Actions Waiting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant