Skip to content

Commit c607fc5

Browse files
committed
feat: read the thread an author writes as replies to themselves through the API
The web reads such a thread with commits/{commit}/continuation, but the API had no equivalent: an app had to fetch the thread one commit at a time, with no way to show where a commit stands in it. GET /api/v1/commits/{commit}/continuation lists the commits that follow a commit in its author's thread, with the same contract as its replies: signed requests only, not found for a commit the user may not see, and cursor pagination with meta.next_cursor and links.next. The cursor continues from the last commit of a page, which the user must still see. It reuses the web's query, so the thread stops at the first commit the user may not see and comes 10 commits a page. meta also has position, the place of the commit a page continues from, length, how many commits follow it (counted up to 100, as on the web), and is_capped, so position + length is the size of the thread. Each commit says which one continues it, which explains its replies_count.
1 parent 0da3de7 commit c607fc5

5 files changed

Lines changed: 274 additions & 1 deletion

File tree

‎KNOWLEDGE_BASE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ An app can use the same `/api/v1` for you with signed requests. It keeps an SSH
539539

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

542-
- read your account, logs, replies, notifications (including grouped activity), and starred commits;
542+
- read your account, logs, replies, threads authors write as replies to themselves, notifications (including grouped activity), and starred commits;
543543
- commit, reply, and quote, with images, and delete your commits, with the same undo window as the web;
544544
- preview commit drafts without publishing them;
545545
- follow or unfollow people and organizations, mute or unmute, and block or unblock, and list who you muted and blocked;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers\Api;
6+
7+
use App\Http\Requests\ListApiCommitContinuationRequest;
8+
use App\Http\Resources\CommitResource;
9+
use App\Models\Commit;
10+
use App\Models\User;
11+
use App\Queries\CommitContinuationQuery;
12+
use Illuminate\Container\Attributes\CurrentUser;
13+
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
14+
use Illuminate\Pagination\CursorPaginator;
15+
16+
final readonly class CommitContinuationController
17+
{
18+
/**
19+
* The commits that follow the commit in its author's thread, as the web reads it, paged like its replies: the
20+
* cursor continues from the last commit of a page. position is the place in the thread of the commit a page
21+
* continues from and length how many commits follow it, so position + length is the size of the thread, and
22+
* is_capped says it goes on past that.
23+
*/
24+
public function index(ListApiCommitContinuationRequest $request, #[CurrentUser] User $user, Commit $commit, CommitContinuationQuery $continuation): AnonymousResourceCollection
25+
{
26+
$thread = $continuation->get($request->continuedCommit() ?? $commit, $user);
27+
$commits = $thread->commits->values();
28+
$nextIds = $commits->skip(1)->pluck('id')->push($thread->nextId)->values();
29+
30+
foreach ($commits as $index => $listed) {
31+
$listed->setAttribute('continuation_id', $nextIds[$index]);
32+
}
33+
34+
// The paginator only links to a next page when it is given one item more than a page.
35+
$page = new CursorPaginator(
36+
$thread->nextId === null ? $commits : $commits->concat([new Commit]),
37+
CommitContinuationQuery::PER_PAGE,
38+
options: ['path' => $request->url(), 'parameters' => ['id']],
39+
);
40+
41+
return CommitResource::collection($page)->additional(['meta' => [
42+
'position' => $thread->position,
43+
'length' => $thread->length,
44+
'is_capped' => $thread->isCapped,
45+
]]);
46+
}
47+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Requests;
6+
7+
use App\Models\Commit;
8+
use App\Models\User;
9+
use Illuminate\Auth\Access\Response;
10+
use Illuminate\Foundation\Http\FormRequest;
11+
use Illuminate\Pagination\Cursor;
12+
use Illuminate\Support\Str;
13+
14+
final class ListApiCommitContinuationRequest extends FormRequest
15+
{
16+
private ?Commit $continued = null;
17+
18+
/**
19+
* The cursor of a page after the first continues the thread from the last commit of the page before, so the user
20+
* must still see that commit too.
21+
*/
22+
public function authorize(): Response
23+
{
24+
$user = $this->user();
25+
assert($user instanceof User);
26+
27+
$commit = $this->route('commit');
28+
assert($commit instanceof Commit);
29+
30+
return $commit->isVisibleTo($user) && $this->continuedCommit()?->isVisibleTo($user)
31+
? Response::allow()
32+
: Response::denyAsNotFound();
33+
}
34+
35+
/**
36+
* The commit whose next commits this page lists: the commit itself, or the one the cursor continues from.
37+
*/
38+
public function continuedCommit(): ?Commit
39+
{
40+
if ($this->continued instanceof Commit) {
41+
return $this->continued;
42+
}
43+
44+
$commit = $this->route('commit');
45+
assert($commit instanceof Commit);
46+
47+
$cursor = $this->query('cursor');
48+
$id = is_string($cursor) ? Cursor::fromEncoded($cursor)?->toArray()['id'] ?? null : null;
49+
50+
if ($id === null) {
51+
return $this->continued = $commit;
52+
}
53+
54+
return $this->continued = is_string($id) && Str::isUuid($id) ? Commit::query()->find($id) : null;
55+
}
56+
57+
/**
58+
* @return array{}
59+
*/
60+
public function rules(): array
61+
{
62+
return [];
63+
}
64+
}

‎routes/api.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Exceptions\ApiException;
66
use App\Http\Controllers\Api\CallController;
7+
use App\Http\Controllers\Api\CommitContinuationController;
78
use App\Http\Controllers\Api\CommitController as ApiCommitController;
89
use App\Http\Controllers\Api\CommitPastelController;
910
use App\Http\Controllers\Api\CommitPinController;
@@ -260,6 +261,8 @@
260261
Route::get('commits/{commit}/replies', [CommitReplyController::class, 'index'])
261262
->withTrashed()
262263
->name('api-commit-reply.index');
264+
Route::get('commits/{commit}/continuation', [CommitContinuationController::class, 'index'])
265+
->name('api-commit-continuation.index');
263266

264267
// Commit Report: a commit that doesn't exist or was deleted gets the same answer as one the user may not see...
265268
Route::post('commits/{commit}/reports', [CommitReportController::class, 'store'])
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\Jobs\DetectCommitLanguage;
6+
use App\Models\Commit;
7+
use App\Models\User;
8+
use App\Queries\CommitContinuationQuery;
9+
use Illuminate\Pagination\Cursor;
10+
use Illuminate\Support\Facades\DB;
11+
use Illuminate\Support\Facades\Queue;
12+
13+
beforeEach(function (): void {
14+
$this->user = User::factory()->create();
15+
$this->keyPair = apiKeyPairFor($this->user);
16+
});
17+
18+
it('lists the next commits of the author thread as the API shows commits', function (): void {
19+
$author = User::factory()->create();
20+
$commit = Commit::factory()->for($author)->create(['created_at' => now()->subMinutes(5)]);
21+
$first = Commit::factory()->for($author)->for($commit, 'parent')->create(['created_at' => now()->subMinutes(4)]);
22+
$second = Commit::factory()->for($author)->for($first, 'parent')->create(['created_at' => now()->subMinutes(3)]);
23+
Commit::factory()->for($author)->for($commit, 'parent')->create(['created_at' => now()->subMinutes(2)]);
24+
Commit::factory()->for($first, 'parent')->create(['created_at' => now()->subMinute()]);
25+
26+
signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commit))
27+
->assertOk()
28+
->assertJsonPath('data.*.id', [$first->id, $second->id])
29+
->assertJsonPath('data.0.author.handle', $author->handle)
30+
->assertJsonPath('data.0.continuation_id', $second->id)
31+
->assertJsonPath('data.0.replies_count', 1)
32+
->assertJsonPath('data.1.continuation_id', null)
33+
->assertJsonPath('meta.position', 1)
34+
->assertJsonPath('meta.length', 2)
35+
->assertJsonPath('meta.is_capped', false)
36+
->assertJsonPath('meta.next_cursor', null)
37+
->assertJsonPath('links.next', null);
38+
});
39+
40+
it('lists the thread 10 commits at a time with a cursor to the next page', function (): void {
41+
$author = User::factory()->create();
42+
$commits = [Commit::factory()->for($author)->create()];
43+
44+
foreach (range(1, CommitContinuationQuery::PER_PAGE + 2) as $ignored) {
45+
$commits[] = Commit::factory()->for($author)->for(end($commits), 'parent')->create();
46+
}
47+
48+
$first = signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commits[0]));
49+
50+
$first->assertOk()
51+
->assertJsonPath('data.*.id', array_map(fn (Commit $commit): string => $commit->id, array_slice($commits, 1, 10)))
52+
->assertJsonPath('data.9.continuation_id', $commits[11]->id)
53+
->assertJsonPath('meta.position', 1)
54+
->assertJsonPath('meta.length', 12);
55+
56+
$second = signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', [$commits[0], 'cursor' => $first->json('meta.next_cursor')]));
57+
58+
$second->assertOk()
59+
->assertJsonPath('data.*.id', [$commits[11]->id, $commits[12]->id])
60+
->assertJsonPath('meta.position', 11)
61+
->assertJsonPath('meta.length', 2)
62+
->assertJsonPath('meta.next_cursor', null);
63+
64+
signedApiRequest($this->keyPair, 'GET', (string) $first->json('links.next'))
65+
->assertOk()
66+
->assertJsonPath('data.*.id', [$commits[11]->id, $commits[12]->id]);
67+
});
68+
69+
it('does not query each commit of the thread, its author or its stars', function (): void {
70+
Queue::fake([DetectCommitLanguage::class]);
71+
$author = User::factory()->create();
72+
$commits = [Commit::factory()->for($author)->create()];
73+
74+
$queries = function () use ($commits): int {
75+
DB::flushQueryLog();
76+
DB::enableQueryLog();
77+
78+
signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commits[0]))->assertOk();
79+
80+
return count(DB::getQueryLog());
81+
};
82+
83+
$create = function (int $count) use (&$commits, $author): void {
84+
foreach (range(1, $count) as $ignored) {
85+
$commits[] = $commit = Commit::factory()->for($author)->for(end($commits), 'parent')->create();
86+
$commit->stars()->attach([$this->user->id, User::factory()->create()->id]);
87+
}
88+
};
89+
90+
$create(2);
91+
92+
// The first signed request also syncs the user's keys with GitHub, and the first cards load the features...
93+
$queries();
94+
95+
$few = $queries();
96+
97+
$create(8);
98+
99+
expect($queries())->toBe($few);
100+
});
101+
102+
it('lists the first page for a cursor that is not one', function (mixed $cursor): void {
103+
$commit = Commit::factory()->create();
104+
$next = Commit::factory()->for($commit->user)->for($commit, 'parent')->create();
105+
106+
signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', [$commit, 'cursor' => $cursor]))
107+
->assertOk()
108+
->assertJsonPath('data.*.id', [$next->id]);
109+
})->with([
110+
'malformed' => ['not-a-cursor'],
111+
'array' => [['not-a-cursor']],
112+
]);
113+
114+
it('returns not found for a cursor on something that is not a commit', function (): void {
115+
$commit = Commit::factory()->create();
116+
117+
signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', [$commit, 'cursor' => new Cursor(['id' => 'not-a-uuid'])->encode()]))
118+
->assertNotFound();
119+
});
120+
121+
it('stops the thread at the first commit the user may not see', function (): void {
122+
$author = User::factory()->create();
123+
$commit = Commit::factory()->for($author)->create();
124+
$visible = Commit::factory()->for($author)->for($commit, 'parent')->create();
125+
$pending = Commit::factory()->for($author)->for($visible, 'parent')->pending()->create();
126+
Commit::factory()->for($author)->for($pending, 'parent')->create();
127+
128+
signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commit))
129+
->assertOk()
130+
->assertJsonPath('data.*.id', [$visible->id])
131+
->assertJsonPath('data.0.continuation_id', null)
132+
->assertJsonPath('meta.length', 1);
133+
});
134+
135+
it('returns not found for the thread of a commit the user may not see', function (): void {
136+
$commit = Commit::factory()->pending()->create();
137+
138+
signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commit))->assertNotFound();
139+
});
140+
141+
it('returns not found for the thread of a commit across a block', function (): void {
142+
$commit = Commit::factory()->create();
143+
Commit::factory()->for($commit->user)->for($commit, 'parent')->create();
144+
$this->user->blocking()->attach($commit->user);
145+
146+
signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', $commit))->assertNotFound();
147+
});
148+
149+
it('returns not found for a cursor on a commit the user may not see', function (): void {
150+
$commit = Commit::factory()->create();
151+
$hidden = Commit::factory()->pending()->create();
152+
153+
signedApiRequest($this->keyPair, 'GET', route('api-commit-continuation.index', [$commit, 'cursor' => new Cursor(['id' => $hidden->id])->encode()]))
154+
->assertNotFound();
155+
});
156+
157+
it('rejects requests that are not signed', function (): void {
158+
$this->getJson(route('api-commit-continuation.index', Commit::factory()->create()))->assertUnauthorized();
159+
});

0 commit comments

Comments
 (0)