Skip to content

[2.x] Fix hasUpvoted/hasDownvoted reflecting another user's vote (#135)#152

Merged
imorland merged 1 commit into
2.xfrom
fix/first-post-vote-actor
Jun 7, 2026
Merged

[2.x] Fix hasUpvoted/hasDownvoted reflecting another user's vote (#135)#152
imorland merged 1 commit into
2.xfrom
fix/first-post-vote-actor

Conversation

@imorland

@imorland imorland commented Jun 7, 2026

Copy link
Copy Markdown
Member

Fixes #135

Problem

hasUpvoted / hasDownvoted on a discussion's firstPost (and on posts) are derived from $post->actualvotes->first(), which assumes the only vote present is the current actor's.

That holds on the discussion list, where firstPost.actualvotes is eager-loaded with a where('user_id', actor) constraint. But on the single-discussion (show) endpoint the relation is unconstrained, so ->first() returns whichever row sorts first by the (user_id, post_id) index — i.e. another user's vote. The actor then sees someone else's vote state.

Example: the actor upvotes a first post that another user (with a lower user_id) downvoted → the show endpoint reports hasUpvoted=false, hasDownvoted=true.

This is the same class of bug as #134; that fix was lost when AddPostData was refactored into the resource-field classes for 2.x.

Fix

Derive the value from the actor's own vote with firstWhere('user_id', $actor->id) in both DiscussionResourceFields and PostResourceFields, so correctness no longer depends on how actualvotes was loaded.

Tests

Adds integration tests asserting that, with multiple users' votes on the first post, both the discussion list and show endpoints reflect only the actor's own vote. The show test fails without this change.

On the single-discussion (show) endpoint the firstPost's `actualvotes`
relation is not constrained to the current actor, so `->first()`
returned whichever vote sorts first by the (user_id, post_id) index --
another user's vote. The actor could see someone else's vote state on
both the discussion's firstPost and on posts.

Use `firstWhere('user_id', $actor->id)` so the value is derived from the
actor's own vote regardless of how the relation was loaded, restoring
the behaviour from #134 that was lost when AddPostData was refactored
into the resource field classes.

Add integration tests covering the discussion list and show endpoints
with multiple users' votes on the first post.

Fixes #135
@imorland imorland requested a review from a team as a code owner June 7, 2026 16:28
@imorland imorland changed the title Fix hasUpvoted/hasDownvoted reflecting another user's vote (#135) [2.x] Fix hasUpvoted/hasDownvoted reflecting another user's vote (#135) Jun 7, 2026
@imorland imorland merged commit d2d1ef9 into 2.x Jun 7, 2026
20 checks passed
@imorland imorland deleted the fix/first-post-vote-actor branch June 7, 2026 16:30
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.

[2.x] Investigate if hasUpvoted / hasDownvoted attribute on firstPost relationship of ListDiscussionController is correct

1 participant