fix(messages): clamp conversation list limit#331
Conversation
Greptile SummaryThis PR replaces an unsafe
Confidence Score: 3/5Safe to merge for the limit-clamping fix specifically, but the cursor pagination produces overlapping pages on any multi-page conversation, which should be addressed before this ships to users relying on infinite-scroll. The src/app/api/conversations/[id]/messages/route.ts — specifically the Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[GET /api/conversations/id/messages] --> B{getAuthContext}
B -- null --> C[401 Unauthorized]
B -- auth --> D[Query conversation by id]
D -- not found --> E[404 Not Found]
D -- found --> F{user in participant_ids?}
F -- no --> G[403 Forbidden]
F -- yes --> H[parseLimit from searchParams]
subgraph parseLimit
H --> I{value null or blank?}
I -- yes --> J[use 50]
I -- no --> K[Number coerce value]
K --> L{isFinite?}
L -- no --> M[use 50]
L -- yes --> N[Math.trunc]
N --> O[clamp to 1..100]
J --> O
M --> O
end
O --> P[Query messages DESC limit+1]
P -- error --> Q[400 Bad Request]
P -- ok --> R{messages.length > limit?}
R -- yes hasMore=true --> S[slice to limit, reverse]
R -- no hasMore=false --> S
S --> T[Mark unread messages as read]
T --> U[Return data + hasMore + nextCursor]
|
|
|
||
| it("clamps negative limit values before querying messages", async () => { | ||
| await expectMessageLimit({ limit: "-5" }, 1); |
There was a problem hiding this comment.
parseLimit("0") calls Math.max(0, 1) and returns 1, so a client sending ?limit=0 silently gets the minimum page size. Given that the PR description calls out "clamp negative values to 1" as intentional, a test asserting expectMessageLimit({ limit: "0" }, 1) would make this edge-case explicit and prevent a future reader from accidentally changing the minimum.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Summary
Fixes #330.
Testing