fix(insights): keep private and unshared accounts out of the shared feed

Merged
GF
2mo ago
+221-13

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_members scope: one SQL NOT EXISTS, the same rule as accessible_by (owner or share), applied to every active member. Inactive users do not count.
  • IdleCashGenerator draws only from that scope.
  • Insight.about_shared_accounts is a read-time check: an insight without account_id always passes; one with it passes only while the account is visible (draft/active) and in the scope above. Insight.visible now includes it, so the web feed, dashboard widget, broadcasts, API v1 and the assistant all get it. The id is cast to uuid inside a CASE (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's include_acknowledged, and push delivery.
  • GenerateInsightsJob#expire_stale_insights now selects by status instead of visible, 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

PathCovered byTest
Idle cash generatorgenerator scopeidle_cash_generator_test (private skipped, shared kept, inactive member ignored)
Web feed /insights, dashboard widget, broadcastsInsight.visibleinsight_test, insights_controller_test
Mark read on /insightsvisible.active.update_allinsights_controller_test
Acknowledge / unacknowledge by idabout_shared_accounts.find (404)insights_controller_test
Unread badge in layoutvisible.active.countexisting badge tests
API v1 GET /insightsInsight.visibleexisting API tests (unchanged path)
Assistant get_insights (incl. acknowledged)visible / about_shared_accountsget_insights_test
Push notification (DeliverInsightNotificationJob)check before deliverydeliver_insight_notification_job_test
Share revoked (form, unshare_with!, delete_all)read-time checkinsight_test (revoke)
Member joins / reactivatedread-time checkinsight_test (join; inactive member ignored)
Account moved to another family, pending deletion, deletedread-time checkinsight_test
Nightly stale expirystatus-based selectiongenerate_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.