Skip to content

Commit 1ca6538

Browse files
authored
feat: vote on polls from the API (#663)
Answers with the commit as its page shows it, so the app gets the poll's results once the vote is in. Votes are final and authors can't vote, as on the web.
1 parent 2a98849 commit 1ca6538

5 files changed

Lines changed: 238 additions & 0 deletions

File tree

‎KNOWLEDGE_BASE.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ A signed request sees what the web shows you, such as your own pending commits.
439439
- commit, reply, and quote, with images, and delete your commits;
440440
- follow or unfollow, mute or unmute, and block or unblock, and list who you muted and blocked;
441441
- star or unstar, and save or unsave;
442+
- vote on polls;
442443
- switch Pastéis mode and give pastéis;
443444
- report commits;
444445
- sync from GitHub and save your lane color;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers\Api;
6+
7+
use App\Actions\CreateCommitPollVote;
8+
use App\Http\Requests\CreateApiCommitPollVoteRequest;
9+
use App\Http\Resources\CommitResource;
10+
use App\Models\Commit;
11+
use App\Models\User;
12+
use App\Queries\CommitPageQuery;
13+
use Illuminate\Container\Attributes\CurrentUser;
14+
15+
final readonly class CommitPollVoteController
16+
{
17+
/**
18+
* Votes on the commit's poll, as the web's Vote button does. It answers with the commit as its page shows it, so
19+
* the app gets the poll with its results, which show once the user voted.
20+
*/
21+
public function store(CreateApiCommitPollVoteRequest $request, #[CurrentUser] User $user, Commit $commit, CreateCommitPollVote $action, CommitPageQuery $page): CommitResource
22+
{
23+
$action->handle($user, $request->option());
24+
25+
return CommitResource::make($page->get($commit, $user));
26+
}
27+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Requests;
6+
7+
use App\Models\Commit;
8+
use App\Models\PollOption;
9+
use App\Models\User;
10+
use App\Rules\OpenPoll;
11+
use Illuminate\Auth\Access\Response;
12+
use Illuminate\Contracts\Validation\ValidationRule;
13+
use Illuminate\Foundation\Http\FormRequest;
14+
use Illuminate\Validation\Rule;
15+
use Illuminate\Validation\Rules\Exists;
16+
17+
final class CreateApiCommitPollVoteRequest extends FormRequest
18+
{
19+
/**
20+
* Like on the web, the user may vote on the poll of any commit they may see. One they may not see looks like it does
21+
* not exist.
22+
*/
23+
public function authorize(): Response
24+
{
25+
return $this->commit()->isVisibleTo($this->voter()) ? Response::allow() : Response::denyAsNotFound();
26+
}
27+
28+
/**
29+
* Votes are final, and the author sees the results instead of voting, as on the web (see OpenPoll).
30+
*
31+
* @return array<string, array<int, string|Exists|ValidationRule>>
32+
*/
33+
public function rules(): array
34+
{
35+
return [
36+
'option' => [
37+
'required',
38+
'uuid',
39+
Rule::exists(PollOption::class, 'id')->where('poll_id', $this->commit()->poll?->id),
40+
new OpenPoll($this->commit(), $this->voter()),
41+
],
42+
];
43+
}
44+
45+
public function option(): PollOption
46+
{
47+
return PollOption::query()->findOrFail($this->string('option')->value());
48+
}
49+
50+
private function commit(): Commit
51+
{
52+
$commit = $this->route('commit');
53+
assert($commit instanceof Commit);
54+
55+
return $commit;
56+
}
57+
58+
private function voter(): User
59+
{
60+
$user = $this->user();
61+
assert($user instanceof User);
62+
63+
return $user;
64+
}
65+
}

‎routes/api.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Exceptions\ApiException;
66
use App\Http\Controllers\Api\CommitController as ApiCommitController;
77
use App\Http\Controllers\Api\CommitPastelController;
8+
use App\Http\Controllers\Api\CommitPollVoteController;
89
use App\Http\Controllers\Api\CommitReplyController;
910
use App\Http\Controllers\Api\CommitReportController;
1011
use App\Http\Controllers\Api\CommitStarController;
@@ -114,6 +115,9 @@
114115
->middleware('throttle:60,1')
115116
->name('api-commit-pastel.store');
116117

118+
// Commit Poll Vote...
119+
Route::post('commits/{commit}/poll/votes', [CommitPollVoteController::class, 'store'])->name('api-commit-poll-vote.store');
120+
117121
// Private starred commits...
118122
Route::get('starred', [StarredCommitController::class, 'index'])->name('api-starred.index');
119123

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\Models\Commit;
6+
use App\Models\Poll;
7+
use App\Models\PollVote;
8+
use App\Models\User;
9+
10+
beforeEach(function (): void {
11+
$this->user = User::factory()->create();
12+
$this->keyPair = apiKeyPairFor($this->user);
13+
});
14+
15+
it('votes and answers with the commit, whose poll now shows the results', function (): void {
16+
$poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create();
17+
PollVote::factory()->for($poll->options[0], 'option')->count(2)->create();
18+
19+
$response = signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[1]->id]);
20+
21+
$response->assertOk()
22+
->assertJsonPath('data.id', $poll->commit_id)
23+
->assertJsonPath('data.poll.has_voted', true)
24+
->assertJsonPath('data.poll.total_votes', 3)
25+
->assertJsonPath('data.poll.options.0.votes_count', 2)
26+
->assertJsonPath('data.poll.options.0.voted', false)
27+
->assertJsonPath('data.poll.options.0.percentage', 67)
28+
->assertJsonPath('data.poll.options.1.votes_count', 1)
29+
->assertJsonPath('data.poll.options.1.voted', true)
30+
->assertJsonPath('data.poll.options.1.percentage', 33);
31+
32+
expect($this->user->pollVotes()->sole()->poll_option_id)->toBe($poll->options[1]->id);
33+
});
34+
35+
it('answers with the commit as the commit endpoint shows it', function (): void {
36+
$poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create();
37+
38+
$response = signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id]);
39+
40+
$response->assertOk()
41+
->assertExactJson(['data' => signedApiRequest($this->keyPair, 'GET', route('api-commit.show', $poll->commit))->json('data')]);
42+
});
43+
44+
it('requires an option of the poll', function (Closure $option, string $message): void {
45+
$poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create();
46+
47+
signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $option()])
48+
->assertUnprocessable()
49+
->assertJsonValidationErrors(['option' => $message]);
50+
51+
expect(PollVote::query()->exists())->toBeFalse();
52+
})->with([
53+
'missing' => [fn (): null => null, 'The option field is required.'],
54+
'not a uuid' => [fn (): string => 'tabs', 'The option field must be a valid UUID.'],
55+
'of another poll' => [fn (): string => Poll::factory()->withOptions('Vim', 'Emacs')->create()->options[0]->id, 'The selected option is invalid.'],
56+
]);
57+
58+
it('may not vote on a commit without a poll', function (): void {
59+
$commit = Commit::factory()->create();
60+
61+
signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $commit), ['option' => Poll::factory()->withOptions('Vim', 'Emacs')->create()->options[0]->id])
62+
->assertUnprocessable()
63+
->assertJsonValidationErrors(['option' => 'The selected option is invalid.']);
64+
});
65+
66+
it('explains why a vote is refused, as the web does', function (Closure $poll, string $message): void {
67+
$poll = $poll($this->user);
68+
69+
signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id])
70+
->assertUnprocessable()
71+
->assertJsonValidationErrors(['option' => $message]);
72+
73+
expect(PollVote::query()->exists())->toBeFalse();
74+
})->with([
75+
'own poll' => [fn (User $user): Poll => Poll::factory()->for(Commit::factory()->for($user))->withOptions('Tabs', 'Spaces')->create(), "You can't vote on your own poll."],
76+
'own pending poll' => [fn (User $user): Poll => Poll::factory()->for(Commit::factory()->for($user)->pending())->withOptions('Tabs', 'Spaces')->unopened()->create(), 'This poll is not open for votes.'],
77+
'unopened poll' => [fn (User $user): Poll => Poll::factory()->withOptions('Tabs', 'Spaces')->unopened()->create(), 'This poll is not open for votes.'],
78+
'closed poll' => [fn (User $user): Poll => Poll::factory()->withOptions('Tabs', 'Spaces')->closed()->create(), 'This poll has closed.'],
79+
]);
80+
81+
it('refuses a second vote, since votes are final', function (): void {
82+
$poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create();
83+
PollVote::factory()->for($poll->options[0], 'option')->for($this->user)->create();
84+
85+
signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[1]->id])
86+
->assertUnprocessable()
87+
->assertJsonValidationErrors(['option' => 'You have already voted on this poll.']);
88+
89+
expect($this->user->pollVotes()->sole()->poll_option_id)->toBe($poll->options[0]->id);
90+
});
91+
92+
it('may not vote on a poll the user may not see', function (string $state): void {
93+
$poll = Poll::factory()->for(Commit::factory()->{$state}())->withOptions('Tabs', 'Spaces')->create();
94+
95+
signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id])
96+
->assertNotFound()
97+
->assertDontSee($poll->commit->body);
98+
99+
expect(PollVote::query()->exists())->toBeFalse();
100+
})->with(['pending', 'rejected']);
101+
102+
it('may not vote across a block in either direction', function (bool $voterBlocks): void {
103+
$poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create();
104+
$author = $poll->commit->user;
105+
106+
$voterBlocks ? $this->user->blocking()->attach($author) : $author->blocking()->attach($this->user);
107+
108+
signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id])
109+
->assertNotFound();
110+
111+
expect(PollVote::query()->exists())->toBeFalse();
112+
})->with([true, false]);
113+
114+
it('may still vote across a mute, as on the web', function (): void {
115+
$poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create();
116+
$this->user->muting()->attach($poll->commit->user);
117+
118+
signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id])
119+
->assertOk()
120+
->assertJsonPath('data.poll.has_voted', true);
121+
});
122+
123+
it('may not vote on a deleted or unknown commit', function (): void {
124+
$poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create();
125+
$poll->commit->delete();
126+
127+
signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit_id), ['option' => $poll->options[0]->id])
128+
->assertNotFound();
129+
130+
signedApiRequest($this->keyPair, 'POST', 'api/v1/commits/0199e3a4-0000-7000-8000-000000000000/poll/votes', ['option' => $poll->options[0]->id])
131+
->assertNotFound();
132+
});
133+
134+
it('requires a signed request', function (): void {
135+
$poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create();
136+
137+
$this->postJson(route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id])
138+
->assertUnauthorized();
139+
140+
expect(PollVote::query()->exists())->toBeFalse();
141+
});

0 commit comments

Comments
 (0)