Skip to content

fix(social): realtime correctness — duplicate bubbles, unscoped reaction reloads, has_more off-by-one#205

Merged
Jose-Gael-Cruz-Lopez merged 2 commits into
mainfrom
fix/social-realtime-correctness
Jun 12, 2026
Merged

fix(social): realtime correctness — duplicate bubbles, unscoped reaction reloads, has_more off-by-one#205
Jose-Gael-Cruz-Lopez merged 2 commits into
mainfrom
fix/social-realtime-correctness

Conversation

@Jose-Gael-Cruz-Lopez

Copy link
Copy Markdown
Member

What

Fixes the three realtime correctness bugs in the Social study-rooms chat tracked in #131:

Why

These are the concrete root causes behind the Social page misbehaving (#85):

  • send() appended a plaintext temp row and the realtime dedup kept rows where m.text !== row.text. Plaintext never equals ciphertext, so the temp row was never removed → duplicate bubble. The realtime row also hardcoded reply_to: null / reactions: [] and the UPDATE handler overwrote edited plaintext with ciphertext.
  • The room_reactions INSERT/DELETE subscriptions had no room scope (the table has no room_id column), so every reaction anywhere triggered load() for the open room.
  • has_more = len(rows) == limit is true whenever a page is exactly full, even when no older messages exist.

How

Frontend (Social.tsx)

  • Reconcile the optimistic temp row against the decrypted POST response (matched by real id) — the authoritative dedup. Skip the sender's own realtime INSERT/UPDATE echoes (they carry ciphertext); other users' messages still flow through realtime. Preserve reply_to/reactions instead of hardcoding.
  • handleImage appends from the POST response so the uploader still sees their own image despite the self-echo skip.
  • Scope reaction reloads: only load() when the affected message_id is currently loaded. DELETE payloads omit message_id without REPLICA IDENTITY FULL, so that case falls back to a reload.

Backend (routes/social.py)

  • Over-fetch limit + 1, set has_more = len(rows) > limit, and drop the probe row before reversing. Pagination tests updated to the corrected contract.

How verified

  • Backend: python -m pytest tests/test_social_messages.py -q8 passed (boundary cases: exact-page → has_more=False, probe-row present → has_more=True with truncation, limit clamp/floor account for the +1).
  • Frontend: npx tsc --noEmit → clean; npx vitest run37 passed (5 files).

Caveats

Closes #131

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Jose-Gael-Cruz-Lopez, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 51 minutes and 58 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ef0a3b19-71fd-4113-8f5e-14b18fb9194f

📥 Commits

Reviewing files that changed from the base of the PR and between 824d7e4 and a3d05c1.

📒 Files selected for processing (3)
  • backend/routes/social.py
  • backend/tests/test_social_messages.py
  • frontend/src/components/screens/Social.tsx
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/social-realtime-correctness

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.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 12, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
frontend a3d05c1 Commit Preview URL

Branch Preview URL
Jun 12 2026, 05:01 AM

Fetch limit+1 and set has_more = len(rows) > limit, dropping the probe
row before returning. The previous len(rows) == limit was true at exact
page boundaries, surfacing a phantom "load earlier" that fetched an empty
page. Update pagination tests to the corrected contract.
Message dedup (#7): reconcile the optimistic temp row against the
decrypted POST response (matched by real id) and skip the sender's own
realtime INSERT/UPDATE echoes, which carry ciphertext and never matched
the plaintext temp row. This stops the sender seeing each message twice
(one readable, one ciphertext) and preserves reply_to/reactions instead
of hardcoding them. handleImage appends from the POST response so the
uploader still sees their own image despite the self-echo skip.

Reaction scope (#31): the room_reactions INSERT/DELETE subscriptions had
no room scope, so a reaction in any room triggered a reload of the open
room. Only reload when the affected message_id is currently loaded.
@Jose-Gael-Cruz-Lopez Jose-Gael-Cruz-Lopez force-pushed the fix/social-realtime-correctness branch from c606f1a to a3d05c1 Compare June 12, 2026 04:46
@Jose-Gael-Cruz-Lopez Jose-Gael-Cruz-Lopez merged commit 9844c3a into main Jun 12, 2026
5 checks passed
@Jose-Gael-Cruz-Lopez Jose-Gael-Cruz-Lopez deleted the fix/social-realtime-correctness branch June 12, 2026 04:54
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.

[P1] Social realtime correctness: duplicate bubbles, unscoped reaction reloads, has_more off-by-one

1 participant