fix(messages): stop DMs landing in the broadcast/group thread - #522
Merged
Conversation
Starting a direct message returned the global broadcast thread instead of a one-to-one conversation, so every DM went to the group discussion. `contains(participant_ids, [me, them])` is a *superset* test: it matches any conversation holding both users. Broadcast threads are gig_id IS NULL and hold the sender plus every recipient, so they matched the DM lookup — and, being the only such row, won every time. Introduced by the broadcast feature (#521). The gig-scoped lookups had the same latent flaw against the per-gig "message all applicants" thread, which predates broadcasts; fixed in the same pass. All four call sites now exclude broadcast threads and require the candidate to hold exactly the two participants: - api/conversations POST (direct + gig-scoped) - api/messages/send - api/gigs/[id]/messages Adds api/conversations/route.test.ts covering the regression: broadcast threads, larger group threads, and the gig message-all thread are all rejected, while genuine one-to-one threads are still reused. Verified these fail against the pre-fix route (4 of 6) and pass after. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
vu1nz Security Review0 finding(s) in PR #? No security issues found. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Regression from #521. Starting a direct message returned the global broadcast thread instead of a one-to-one conversation, so every DM went to the group discussion.
Cause
contains(participant_ids, [me, them])is a superset test — it matches any conversation holding both users, not a conversation of exactly those two.Broadcast threads are
gig_id IS NULLand hold the sender plus every recipient. So a DM lookup, which filters ongig_id IS NULLand then does the superset match, matched the broadcast thread. Being the only such row, it won every time:The gig-scoped lookups had the same latent flaw against the per-gig "message all applicants" group thread. That one predates broadcasts, but it's the identical bug, so it's fixed here too.
Fix
All four call sites now exclude broadcast threads and require the candidate to hold exactly the two participants:
api/conversationsPOST — direct branchapi/conversationsPOST — gig-scoped branchapi/messages/sendapi/gigs/[id]/messagesThe length check is belt-and-braces — it also rejects any ordinary multi-party thread, so this can't recur if another group-thread feature lands later.
Production impact
Checked before fixing: only one broadcast thread exists (242 participants, 2 messages, both from the thread owner). No other user's DM was absorbed into it, so there's no data to clean up.
Tests
New
src/app/api/conversations/route.test.ts(6 cases):I verified these against the pre-fix route: 4 of 6 fail, and the 2 that pass are exactly the reuse guards. They all pass after.
Also updated the
reuses existing conversationmock ingigs/[id]/messages/route.test.ts— it modelled the old.single()shape and had to become a list with a realistic 2-participant row.Verification
vitest run— 202 files, 1883 tests passingtsc --noEmit— cleaneslint— 0 errors on changed pathsnode scripts/build-next.js— compiles🤖 Generated with Claude Code