Skip to content

Commit 8de2e37

Browse files
committed
feat: list, edit and cancel scheduled commits from the API
1 parent 109e171 commit 8de2e37

12 files changed

Lines changed: 515 additions & 4 deletions

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ A signed profile also says `is_muting` and has the web's `achievements`, with th
9393

9494
`POST /api/v1/commits` takes a `body`, and either a `parent_id` to reply to a commit or a `quoted_commit_id` to quote one, but not both. Returns a `201` on successful commit creation.
9595

96+
`GET /api/v1/scheduled-commits` lists the commits the user scheduled, as the web's Scheduled commits page does, which only they see: the ones still waiting first, then the submitted ones, each by when it was due, 20 a page. Canceled ones aren't listed. Each has its `id`, `body`, `body_segments` and `preview_segments` as a commit has them, its `poll_options` (or `null`) and `image_count`, `scheduled_at` in UTC, `submitted_at`, `status` (`scheduled` until it is submitted, then its commit's status, or `deleted`) and `commit_id`. Once submitted, the body is the commit's, so it is `null` here. `PUT /api/v1/scheduled-commits/{id}` gives a waiting one a new `body` and `scheduled_at`, with the rules it was scheduled with, and answers with it. Like posting, it is a `403` with `timed_out` while the user is in a timeout, though they can still cancel. Its images and poll stay, and its link preview becomes the first link in the new body. `DELETE /api/v1/scheduled-commits/{id}` cancels it, as the web's Cancel does, and answers `204`. Once it was submitted or canceled, both are a `409` with `scheduled_commit_submitted`, and someone else's is a `404`.
97+
9698
`POST /api/v1/commits/{id}/reports` reports a commit to the moderators, privately, as the web's Report button does. It takes a `reason`, one of `spam`, `harassment`, `unsafe` or `other`, and optional `details` of up to 500 characters, and answers `204`. Reporting doesn't hide the commit. Only someone else's published commit can be reported: the user's own is a `403` with `own_commit`, and one that is pending, rejected, deleted, missing or across a block is a `404` with `commit_unavailable`. Reporting a commit again also answers `204` and keeps the first report. Each user can send 5 reports a minute.
9799

98100
`GET /api/v1/me` also has what only the user sees in the web's profile settings: `preferred_lane` (a lane, a custom `#rrggbb` color, or `null` for Automatic), the `automatic_lane` Automatic picks for them, their `sponsors_url` (or `null`), and `github_synced_at`, when their profile was last synced from GitHub (`null` if it only was when they signed in). `PUT /api/v1/me/github` syncs it from GitHub again, like Sync from GitHub on the web, and shares its limit of one sync a minute. `PUT /api/v1/me/lane` saves their color, with a `lane` from `0` to `5`, or `null` for Automatic, or a custom `color` as lowercase `#rrggbb` with a `null` lane, as the web's color picker does. Both answer like `GET /api/v1/me`.
@@ -138,6 +140,7 @@ An error is JSON with a `message` to show the user. The errors the app has to te
138140
| `403` | `user_unavailable` | The profile isn't available to the user, because it blocked them. |
139141
| `403` | `own_commit` | The user tried to report their own commit. |
140142
| `404` | `commit_unavailable` | The commit to report doesn't exist, was deleted, isn't published, or is across a block. |
143+
| `409` | `scheduled_commit_submitted` | `PUT` and `DELETE /api/v1/scheduled-commits/{id}` only: the scheduled commit was already submitted or canceled. Nothing changed. |
141144
| `422` | `pasteis_exhausted` | `POST /api/v1/commits/{id}/pasteis` only: the user gave all 10 of today's pastéis. They come back at 00:00 UTC. |
142145
| `429` | `rate_limited` | Over 120 signed requests a minute for the user, 10 commits a minute, 30 follows and unfollows a minute, 60 stars and unstars a minute, 60 saves and unsaves a minute, 5 reports a minute, 60 pastéis a minute, one profile sync a minute, or 60 failed signatures a minute from one IP. `Retry-After` says when to try again. |
143146
| `503` | `github_sync_failed` | `PUT /api/v1/me/github` only: GitHub couldn't be reached, or didn't answer as expected. Nothing changed. |
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions;
6+
7+
use App\Models\ScheduledCommit;
8+
use App\Models\User;
9+
use App\Services\Urls;
10+
use Carbon\CarbonImmutable;
11+
12+
final readonly class UpdateScheduledCommit
13+
{
14+
public function __construct(private Urls $urls)
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Changes the body and the time of a scheduled commit that is still waiting, and gives it the link preview of the
21+
* first URL in the new body, as when it was scheduled. Its images and poll stay. It returns false, and changes
22+
* nothing, once the commit was submitted or canceled.
23+
*/
24+
public function handle(User $user, ScheduledCommit $scheduled, string $body, CarbonImmutable $scheduledAt): bool
25+
{
26+
$body = mb_trim($body);
27+
28+
return ScheduledCommit::query()->whereKey($scheduled->id)->where('user_id', $user->id)
29+
->whereNull('submitted_at')->whereNull('canceled_at')
30+
->update(['body' => $body, 'link_preview_url' => $this->urls->first($body), 'scheduled_at' => $scheduledAt->utc()]) === 1;
31+
}
32+
}

‎app/Enums/ApiErrorCode.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum ApiErrorCode: string
2323
case TimedOut = 'timed_out';
2424
case Banned = 'banned';
2525
case CommitRulesRequired = 'commit_rules_required';
26+
case ScheduledCommitSubmitted = 'scheduled_commit_submitted';
2627

2728
public function status(): int
2829
{
@@ -31,6 +32,7 @@ public function status(): int
3132
self::InvalidSignature, self::DeviceNotApproved => 401,
3233
self::UserBlocked, self::UserUnavailable, self::OwnCommit, self::TimedOut, self::Banned, self::CommitRulesRequired => 403,
3334
self::CommitUnavailable => 404,
35+
self::ScheduledCommitSubmitted => 409,
3436
self::PasteisExhausted => 422,
3537
self::RateLimited => 429,
3638
self::GitHubSyncFailed => 503,

‎app/Exceptions/ApiException.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public static function banned(): self
7979
return new self(ApiErrorCode::Banned, __('Your account is banned from /dev/null. All you can do is delete it.'));
8080
}
8181

82+
/**
83+
* The scheduled commit was submitted, or canceled, before it could be changed or canceled. The web's own words.
84+
*/
85+
public static function scheduledCommitSubmitted(): self
86+
{
87+
return new self(ApiErrorCode::ScheduledCommitSubmitted, __('This commit was already submitted or canceled. Refresh to see its status.'));
88+
}
89+
8290
public function render(): JsonResponse
8391
{
8492
return response()->json([
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers\Api;
6+
7+
use App\Actions\DeleteScheduledCommit;
8+
use App\Actions\UpdateScheduledCommit;
9+
use App\Exceptions\ApiException;
10+
use App\Http\Requests\DeleteApiScheduledCommitRequest;
11+
use App\Http\Requests\UpdateApiScheduledCommitRequest;
12+
use App\Http\Resources\ScheduledCommitResource;
13+
use App\Models\ScheduledCommit;
14+
use App\Models\User;
15+
use App\Queries\ScheduledCommitsQuery;
16+
use Illuminate\Container\Attributes\CurrentUser;
17+
use Illuminate\Http\JsonResponse;
18+
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
19+
use Illuminate\Http\Response;
20+
21+
final readonly class ScheduledCommitController
22+
{
23+
/**
24+
* The user's scheduled commits, as the web's Scheduled commits page lists them: the ones still waiting first, then
25+
* the submitted ones, 20 a page.
26+
*/
27+
public function index(#[CurrentUser] User $user, ScheduledCommitsQuery $scheduledCommits): AnonymousResourceCollection
28+
{
29+
return ScheduledCommitResource::collection($scheduledCommits->builder($user)->paginate(20));
30+
}
31+
32+
/**
33+
* Answers with the scheduled commit as it now is. Once it was submitted or canceled, it is a
34+
* scheduled_commit_submitted error, and nothing changes.
35+
*/
36+
public function update(UpdateApiScheduledCommitRequest $request, #[CurrentUser] User $user, ScheduledCommit $scheduledCommit, UpdateScheduledCommit $action): ScheduledCommitResource|JsonResponse
37+
{
38+
if (! $action->handle($user, $scheduledCommit, $request->string('body')->value(), $request->scheduledAt())) {
39+
return ApiException::scheduledCommitSubmitted()->render();
40+
}
41+
42+
return ScheduledCommitResource::make($scheduledCommit->refresh());
43+
}
44+
45+
/**
46+
* Cancels the scheduled commit, as the web's Cancel does, so it is never submitted. Once it was submitted or
47+
* canceled, it is a scheduled_commit_submitted error.
48+
*/
49+
public function destroy(DeleteApiScheduledCommitRequest $request, #[CurrentUser] User $user, ScheduledCommit $scheduledCommit, DeleteScheduledCommit $action): Response|JsonResponse
50+
{
51+
if (! $action->handle($user, $scheduledCommit)) {
52+
return ApiException::scheduledCommitSubmitted()->render();
53+
}
54+
55+
return response()->noContent();
56+
}
57+
}

‎app/Http/Controllers/ScheduledCommitController.php‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Http\Requests\DeleteScheduledCommitRequest;
1111
use App\Models\ScheduledCommit;
1212
use App\Models\User;
13+
use App\Queries\ScheduledCommitsQuery;
1314
use App\Services\CommitBodies;
1415
use Carbon\CarbonImmutable;
1516
use Illuminate\Container\Attributes\CurrentUser;
@@ -20,12 +21,10 @@
2021

2122
final readonly class ScheduledCommitController
2223
{
23-
public function index(#[CurrentUser] User $user, CommitBodies $bodies): Response
24+
public function index(#[CurrentUser] User $user, CommitBodies $bodies, ScheduledCommitsQuery $scheduledCommits): Response
2425
{
2526
return Inertia::render('scheduled-commits/index', [
26-
'scheduledCommits' => fn (): LengthAwarePaginator => ScheduledCommit::query()->where('user_id', $user->id)
27-
->whereNull('canceled_at')->with('commit')
28-
->orderByRaw('submitted_at IS NOT NULL')->orderBy('scheduled_at')->orderBy('id')
27+
'scheduledCommits' => fn (): LengthAwarePaginator => $scheduledCommits->builder($user)
2928
->paginate(20)->through(fn (ScheduledCommit $scheduled): array => [
3029
'id' => $scheduled->id,
3130
...($scheduled->submitted_at === null && $scheduled->body !== null
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Requests;
6+
7+
use App\Models\ScheduledCommit;
8+
use App\Models\User;
9+
use Illuminate\Auth\Access\Response;
10+
use Illuminate\Foundation\Http\FormRequest;
11+
12+
/**
13+
* Someone else's scheduled commit is a 404, as though it didn't exist.
14+
*/
15+
final class DeleteApiScheduledCommitRequest extends FormRequest
16+
{
17+
public function authorize(): Response
18+
{
19+
$user = $this->user();
20+
assert($user instanceof User);
21+
22+
$scheduled = $this->route('scheduledCommit');
23+
assert($scheduled instanceof ScheduledCommit);
24+
25+
return $scheduled->user_id === $user->id ? Response::allow() : Response::denyAsNotFound();
26+
}
27+
28+
/**
29+
* @return array{}
30+
*/
31+
public function rules(): array
32+
{
33+
return [];
34+
}
35+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Requests;
6+
7+
use App\Models\ScheduledCommit;
8+
use App\Models\User;
9+
use Carbon\CarbonImmutable;
10+
use Illuminate\Auth\Access\Response;
11+
use Illuminate\Contracts\Validation\ValidationRule;
12+
use Illuminate\Foundation\Http\FormRequest;
13+
14+
/**
15+
* A new body and time for a commit the user scheduled, with the rules it was scheduled with. Someone else's scheduled
16+
* commit is a 404, as though it didn't exist.
17+
*/
18+
final class UpdateApiScheduledCommitRequest extends FormRequest
19+
{
20+
public function authorize(): Response
21+
{
22+
$user = $this->user();
23+
assert($user instanceof User);
24+
25+
$scheduled = $this->route('scheduledCommit');
26+
assert($scheduled instanceof ScheduledCommit);
27+
28+
return $scheduled->user_id === $user->id ? Response::allow() : Response::denyAsNotFound();
29+
}
30+
31+
/**
32+
* Blank lines are collapsed before the body is measured, as on the web.
33+
*/
34+
public function prepareForValidation(): void
35+
{
36+
CreateCommitRequest::normalizeBody($this);
37+
}
38+
39+
/**
40+
* @return array<string, list<string|ValidationRule>>
41+
*/
42+
public function rules(): array
43+
{
44+
$scheduled = $this->route('scheduledCommit');
45+
assert($scheduled instanceof ScheduledCommit);
46+
47+
return [
48+
'body' => CreateCommitRequest::bodyRules($scheduled->images !== null),
49+
'scheduled_at' => ['required', 'date_format:Y-m-d\TH:i:sP', 'after:now', 'before_or_equal:'.now()->addDays(30)->toIso8601String()],
50+
];
51+
}
52+
53+
/**
54+
* When the commit is now due, in UTC.
55+
*/
56+
public function scheduledAt(): CarbonImmutable
57+
{
58+
return CarbonImmutable::parse($this->string('scheduled_at')->value())->utc();
59+
}
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Resources;
6+
7+
use App\Models\ScheduledCommit;
8+
use App\Services\CommitBodies;
9+
use Illuminate\Http\Request;
10+
use Illuminate\Http\Resources\Json\JsonResource;
11+
12+
/**
13+
* A commit the user scheduled, as the web's Scheduled commits page shows it, which only they see. Until it is
14+
* submitted it has its body, split like a commit's, the options of its poll and how many images it carries. Once
15+
* submitted, those belong to the commit it became: the body is null, the status is that commit's (or deleted once it
16+
* is), and commit_id is that commit while it exists.
17+
*
18+
* @mixin ScheduledCommit
19+
*/
20+
final class ScheduledCommitResource extends JsonResource
21+
{
22+
/**
23+
* @return array<string, mixed>
24+
*/
25+
public function toArray(Request $request): array
26+
{
27+
$commit = $this->submitted_at === null || $this->commit?->trashed() !== false ? null : $this->commit;
28+
29+
return [
30+
'id' => $this->id,
31+
...($this->submitted_at === null && $this->body !== null
32+
? ['body' => $this->body, ...resolve(CommitBodies::class)->segments($this->body)]
33+
: ['body' => null, 'body_segments' => [], 'preview_segments' => null]),
34+
'poll_options' => $this->poll['options'] ?? null,
35+
'image_count' => count($this->images ?? []),
36+
'scheduled_at' => $this->scheduled_at->toIso8601ZuluString(),
37+
'submitted_at' => $this->submitted_at?->toIso8601ZuluString(),
38+
'status' => $this->submitted_at === null ? 'scheduled' : ($commit?->status->value ?? 'deleted'),
39+
'commit_id' => $commit?->id,
40+
];
41+
}
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Queries;
6+
7+
use App\Models\ScheduledCommit;
8+
use App\Models\User;
9+
use Illuminate\Database\Eloquent\Builder;
10+
11+
final readonly class ScheduledCommitsQuery
12+
{
13+
/**
14+
* The user's scheduled commits that weren't canceled: the ones still waiting first, then the submitted ones, each
15+
* by when they were due.
16+
*
17+
* @return Builder<ScheduledCommit>
18+
*/
19+
public function builder(User $user): Builder
20+
{
21+
return ScheduledCommit::query()->where('user_id', $user->id)
22+
->whereNull('canceled_at')->with('commit')
23+
->orderByRaw('submitted_at IS NOT NULL')->orderBy('scheduled_at')->orderBy('id');
24+
}
25+
}

0 commit comments

Comments
 (0)