Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ An app can use the same `/api/v1` for you with signed requests. It keeps an SSH

A signed request sees what the web shows you, such as your own pending commits and your organizations'. It can also, with the web's validation, moderation, and rate limits:

- read your account, logs, replies, notifications (including grouped activity), and starred commits;
- read your account, logs, replies, notifications (including grouped activity), and starred commits, and read, in order, a thread someone wrote as replies to themselves;
- read the organizations you are a member of, your role in each, and whether posting as an organization is switched on;
- commit, reply, and quote, with images, and delete your commits, with the same undo window as the web;
- preview commit drafts without publishing them;
Expand Down
32 changes: 32 additions & 0 deletions app/Http/Controllers/Api/CommitContinuationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Http\Requests\ListApiCommitContinuationRequest;
use App\Http\Resources\CommitResource;
use App\Models\Commit;
use App\Models\User;
use App\Queries\CommitContinuationQuery;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

final readonly class CommitContinuationController
{
/**
* The author's thread below the commit, as the web shows it, a page at a time. The next page starts from the last
* commit in data, as next_id only names the commit that follows it.
*/
public function index(ListApiCommitContinuationRequest $request, #[CurrentUser] User $user, Commit $commit, CommitContinuationQuery $continuation): AnonymousResourceCollection
{
$thread = $continuation->get($commit, $user);

return CommitResource::collection($thread->commits)->additional([
'position' => $thread->position,
'length' => $thread->length,
'is_capped' => $thread->isCapped,
'next_id' => $thread->nextId,
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/CommitReplyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
{
public function index(ListApiCommitRepliesRequest $request, #[CurrentUser] User $user, Commit $commit, CommitRepliesQuery $replies): AnonymousResourceCollection
{
return CommitResource::collection($replies->paginate($commit, $user, $request->boolean('newest'))->withQueryString());
return CommitResource::collection($replies->paginate($commit, $user, $request->boolean('newest'), except: $request->exceptedReplyId())->withQueryString());
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/ListApiCommitContinuationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Models\Commit;
use App\Models\User;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Http\FormRequest;

final class ListApiCommitContinuationRequest extends FormRequest
{
public function authorize(): Response
{
$user = $this->user();
assert($user instanceof User);

$commit = $this->route('commit');
assert($commit instanceof Commit);

return $commit->isVisibleTo($user) ? Response::allow() : Response::denyAsNotFound();
}

/**
* @return array{}
*/
public function rules(): array
{
return [];
}
}
9 changes: 8 additions & 1 deletion app/Http/Requests/ListApiCommitRepliesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@ public function authorize(): Response
return $commit->isVisibleTo($user) ? Response::allow() : Response::denyAsNotFound();
}

public function exceptedReplyId(): ?string
{
return $this->string('except')->value() ?: null;
}

/**
* Newest first continues the next_cursor of a reply preview, as on the web.
* Newest first continues the next_cursor of a reply preview, as on the web. Except leaves out the reply that
* continues the author's thread, which the web shows on its own.
*
* @return array<string, list<string>>
*/
public function rules(): array
{
return [
'newest' => ['sometimes', 'boolean'],
'except' => ['sometimes', 'nullable', 'uuid'],
];
}
}
12 changes: 7 additions & 5 deletions app/Http/Resources/CommitResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
* and how many pastéis it was given, which the app only shows in pastéis mode, as the web does. Like the web's cards,
* it also has preview_segments, the excerpt a list shows of a long commit, the poll, with what the user may see of
* its votes, and the commit it quotes, or null once the user may no longer see that one, while quoted_commit_id still
* says it quoted something. In a list, reply_preview is the newest reply or quote the web shows under it. As on the
* web, the counts, link_preview, images, starred, parent, poll, quoted_commit and reply_preview are only
* there when they were loaded, so ancestors and the commits of notifications come without some of them. The images of
* a commit that isn't published yet have temporary URLs, since the app has no web session to load them with. For the
* same reason, the guild's icon_url is signed for the user, or a guest, and stays the same all day.
* says it quoted something. In a list, reply_preview is the newest reply or quote the web shows under it, and
* continuation_id the author's reply that continues their thread. As on the web, the counts, link_preview, images,
* starred, parent, poll, quoted_commit, continuation_id and reply_preview are only there when they were loaded, so
* ancestors and the commits of notifications come without some of them. The images of a commit that isn't published
* yet have temporary URLs, since the app has no web session to load them with. For the same reason, the guild's
* icon_url is signed for the user, or a guest, and stays the same all day.
*
* A deleted commit that still has replies stays in its thread as a tombstone: its id, parent_id, deleted_at and
* replies_count, but never its author or body. Only signed requests are ever sent one.
Expand Down Expand Up @@ -101,6 +102,7 @@ public function toArray(Request $request): array
'quoted_commit_id' => $this->quoted_commit_id,
'quoted_commit' => $this->whenLoaded('quotedCommit', fn (Commit $quoted): self => self::make($quoted)),
'poll' => $this->whenLoaded('poll', fn (Poll $poll): PollResource => PollResource::make($poll)),
'continuation_id' => $this->whenHas('continuation_id'),
'reply_preview' => $this->whenLoaded('replyPreview', fn (Collection $replies): array => [
'data' => self::collection($replies),
'next_cursor' => data_get($this->getAttribute('reply_preview'), 'next_cursor'),
Expand Down
5 changes: 5 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Exceptions\ApiException;
use App\Http\Controllers\Api\BroadcastAuthorizationController;
use App\Http\Controllers\Api\CallController;
use App\Http\Controllers\Api\CommitContinuationController;
use App\Http\Controllers\Api\CommitController as ApiCommitController;
use App\Http\Controllers\Api\CommitPastelController;
use App\Http\Controllers\Api\CommitPinController;
Expand Down Expand Up @@ -323,6 +324,10 @@
->withTrashed()
->name('api-commit-reply.index');

// Commit Continuation: the author's thread below a commit, continued from the last commit shown...
Route::get('commits/{commit}/continuation', [CommitContinuationController::class, 'index'])
->name('api-commit-continuation.index');

// Commit Report: a commit that doesn't exist or was deleted gets the same answer as one the user may not see...
Route::post('commits/{commit}/reports', [CommitReportController::class, 'store'])
->middleware('throttle:commit-reports')
Expand Down
93 changes: 93 additions & 0 deletions tests/Feature/Controllers/Api/CommitContinuationControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

use App\Jobs\DetectCommitLanguage;
use App\Models\Commit;
use App\Models\User;
use App\Queries\CommitContinuationQuery;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;

beforeEach(function (): void {
$this->user = User::factory()->create();
$this->keyPair = apiKeyPairFor($this->user);
});

/**
* @return list<Commit>
*/
function selfThread(int $replies): array
{
$author = User::factory()->create();
$commits = [Commit::factory()->for($author)->create()];

foreach (range(1, $replies) as $ignored) {
$commits[] = Commit::factory()->for(end($commits), 'parent')->for($author)->create();
}

return $commits;
}

it('lists the author thread below a commit in order, as the web does', function (): void {
$commits = selfThread(3);
Commit::factory()->for($commits[0], 'parent')->create();

$response = signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commits[0]));

$response->assertOk()
->assertJsonPath('data.*.id', [$commits[1]->id, $commits[2]->id, $commits[3]->id])
->assertJsonPath('data.0.author.handle', $commits[0]->user->handle)
->assertJsonPath('data.0.status', 'approved')
->assertJsonPath('position', 1)
->assertJsonPath('length', 3)
->assertJsonPath('is_capped', false)
->assertJsonPath('next_id', null);
});

it('lists the thread a page at a time, going on from the last commit shown', function (): void {
$commits = selfThread(CommitContinuationQuery::PER_PAGE + 2);

$first = signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commits[0]));

$first->assertOk()
->assertJsonCount(CommitContinuationQuery::PER_PAGE, 'data')
->assertJsonPath('next_id', $commits[CommitContinuationQuery::PER_PAGE + 1]->id);

$second = signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $first->json('data.9.id')));

$second->assertOk()
->assertJsonPath('data.*.id', [$commits[11]->id, $commits[12]->id])
->assertJsonPath('position', CommitContinuationQuery::PER_PAGE + 1)
->assertJsonPath('next_id', null);
});

it('does not query each commit of the thread', function (): void {
Queue::fake([DetectCommitLanguage::class]);
$short = selfThread(2);
$long = selfThread(8);

$queries = function (Commit $commit): int {
DB::flushQueryLog();
DB::enableQueryLog();

signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commit))->assertOk();

return count(DB::getQueryLog());
};

// The first signed request also syncs the user's keys with GitHub...
$queries($short[0]);

expect($queries($long[0]))->toBe($queries($short[0]));
});

it('returns not found for a commit the user may not see', function (): void {
$commit = Commit::factory()->pending()->create();

signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commit))->assertNotFound();
});

it('rejects requests that are not signed', function (): void {
$this->getJson(route('api-commit-continuation.index', Commit::factory()->create()))->assertUnauthorized();
});
14 changes: 14 additions & 0 deletions tests/Feature/Controllers/Api/CommitControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
'quoted_commit_id' => null,
'quoted_commit' => null,
'poll' => null,
'continuation_id' => null,
],
'ancestors' => [[
'id' => $root->id,
Expand Down Expand Up @@ -384,6 +385,19 @@
]);
});

it('shows a signed request the reply that continues the author thread', function (): void {
$keyPair = apiKeyPairFor(User::factory()->create());
$commit = Commit::factory()->create();
Commit::factory()->for($commit, 'parent')->create(['created_at' => now()->subMinutes(2)]);
$next = Commit::factory()->for($commit, 'parent')->for($commit->user)->create(['created_at' => now()->subMinute()]);
Commit::factory()->for($commit, 'parent')->for($commit->user)->create();

signedApiRequest($keyPair, 'GET', route('api-commit.show', $commit))
->assertOk()
->assertJsonPath('data.continuation_id', $next->id)
->assertJsonPath('data.replies_count', 2);
});

it('shows a signed request the poll of a commit as the web does, with each share once the votes show', function (): void {
$user = User::factory()->create();
$keyPair = apiKeyPairFor($user);
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/Controllers/Api/CommitReplyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@
->assertJsonPath('meta.next_cursor', null);
});

it('tells which reply continues the thread of each reply', function (): void {
$parent = Commit::factory()->create();
$reply = Commit::factory()->for($parent, 'parent')->create();
$next = Commit::factory()->for($reply, 'parent')->for($reply->user)->create();

signedApiRequest($this->keyPair, 'GET', route('api-commit-reply.index', $parent))
->assertOk()
->assertJsonPath('data.0.continuation_id', $next->id);
});

it('leaves out the reply it is asked to, as the web does', function (): void {
$parent = Commit::factory()->create();
$continuation = Commit::factory()->for($parent, 'parent')->for($parent->user)->create(['created_at' => now()->subMinute()]);
$other = Commit::factory()->for($parent, 'parent')->create();

signedApiRequest($this->keyPair, 'GET', route('api-commit-reply.index', [$parent, 'except' => $continuation->id]))
->assertOk()
->assertJsonPath('data.*.id', [$other->id]);
});

it('validates the reply to leave out', function (): void {
signedApiRequest($this->keyPair, 'GET', route('api-commit-reply.index', [Commit::factory()->create(), 'except' => 'not-a-uuid']))
->assertUnprocessable()
->assertJsonValidationErrors('except');
});

it('shows the stars and saves of each reply as the user sees them', function (): void {
$parent = Commit::factory()->create();
$starred = Commit::factory()->for($parent, 'parent')->create(['created_at' => now()->subMinutes(2)]);
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Controllers/Api/LogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
'quoted_commit_id' => null,
'quoted_commit' => null,
'poll' => null,
'continuation_id' => null,
'guild_mentions' => [],
]);

Expand Down