[2.x] Fix hasUpvoted/hasDownvoted reflecting another user's vote (#135)#152
Merged
Conversation
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
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.
Fixes #135
Problem
hasUpvoted/hasDownvotedon a discussion'sfirstPost(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.actualvotesis eager-loaded with awhere('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 reportshasUpvoted=false, hasDownvoted=true.This is the same class of bug as #134; that fix was lost when
AddPostDatawas 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 bothDiscussionResourceFieldsandPostResourceFields, so correctness no longer depends on howactualvoteswas 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.