fix(insights): keep private and unshared accounts out of the shared feed
Before: the insights feed is shared by the whole family, but the idle cash nudge picked any untouched cash account, including one private to a single member, and showed its name and balance to everyone. And when an owner revoked a share, an insight already written about that account stayed in every member's feed (and in the assistant's answers) until the next nightly run.
After: idle cash only names accounts every active member can see. Any insight that names an account (metadata.account_id) disappears from the feed as soon as that account is no longer shared with every active member, is hidden, or is deleted, and comes back unchanged if access is restored.
This is the central fix discussed in the review of #3573: the access-revocation concern there applies to every insight that names an account, not only the balance discrepancy one, so it is handled once at read time instead of per generator. The balance discrepancy insight stores account_id in its metadata and is covered automatically once both land.
How
- New
Account.accessible_by_all_active_membersscope: one SQLNOT EXISTS, the same rule asaccessible_by(owner or share), applied to every active member. Inactive users do not count. IdleCashGeneratordraws only from that scope.Insight.about_shared_accountsis a read-time check: an insight withoutaccount_idalways passes; one with it passes only while the account is visible (draft/active) and in the scope above.Insight.visiblenow includes it, so the web feed, dashboard widget, broadcasts, API v1 and the assistant all get it. The id is cast to uuid inside aCASE(primary-key probe; a malformed value just hides the row).- The same check guards the remaining paths: the unread badge, mark-read on
/insights, acknowledge/unacknowledge by id, the assistant'sinclude_acknowledged, and push delivery. GenerateInsightsJob#expire_stale_insightsnow selects by status instead ofvisible, so a hidden row whose condition cleared still expires and can't reappear stale when access returns.
Why a read-time filter instead of callbacks on AccountShare/User: callbacks would miss bulk writes (delete_all, insert_all) and account deletion, and would lose a member's acknowledgement on unshare/reshare. Filtering on read covers every way access can change and writes nothing.
Paths checked
| Path | Covered by | Test |
|---|---|---|
| Idle cash generator | generator scope | idle_cash_generator_test (private skipped, shared kept, inactive member ignored) |
Web feed /insights, dashboard widget, broadcasts | Insight.visible | insight_test, insights_controller_test |
Mark read on /insights | visible.active.update_all | insights_controller_test |
| Acknowledge / unacknowledge by id | about_shared_accounts.find (404) | insights_controller_test |
| Unread badge in layout | visible.active.count | existing badge tests |
API v1 GET /insights | Insight.visible | existing API tests (unchanged path) |
Assistant get_insights (incl. acknowledged) | visible / about_shared_accounts | get_insights_test |
Push notification (DeliverInsightNotificationJob) | check before delivery | deliver_insight_notification_job_test |
Share revoked (form, unshare_with!, delete_all) | read-time check | insight_test (revoke) |
| Member joins / reactivated | read-time check | insight_test (join; inactive member ignored) |
| Account moved to another family, pending deletion, deleted | read-time check | insight_test |
| Nightly stale expiry | status-based selection | generate_insights_job_test |
Not in this PR
Cash flow warning, subscription audit, spending anomaly and maintained-goal insights aggregate over all family accounts (no single account_id), so private accounts still feed their totals. That is a separate topic.
Validation
- New tests fail without the fix and pass with it.
- Full suite, Rubocop, ERB lint, Biome and Brakeman green.
- No UI layout change (an insight simply isn't listed), so no screenshots.
Summary by CodeRabbit
- Bug Fixes
- Insights tied to accounts are now shown only when those accounts are accessible to all active family members. Insights without an associated account remain visible.
- The insights badge count and read status now reflect only visible insights. Hidden insights can’t be acknowledged or trigger push notifications.
- Idle-cash insights are limited to accounts accessible to all active family members, and stale insights are expired when their generator succeeds without producing a match.