From 92bfa7170e5781b2936280a5d753ab7c66242fb7 Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Tue, 10 Nov 2026 18:27:53 +0000 Subject: [PATCH 1/4] feat: add an API route for an author's self-thread --- .../Api/CommitContinuationController.php | 32 +++++++ .../ListApiCommitContinuationRequest.php | 32 +++++++ routes/api.php | 5 + .../Api/CommitContinuationControllerTest.php | 93 +++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 app/Http/Controllers/Api/CommitContinuationController.php create mode 100644 app/Http/Requests/ListApiCommitContinuationRequest.php create mode 100644 tests/Feature/Controllers/Api/CommitContinuationControllerTest.php diff --git a/app/Http/Controllers/Api/CommitContinuationController.php b/app/Http/Controllers/Api/CommitContinuationController.php new file mode 100644 index 000000000..4bf46c3ed --- /dev/null +++ b/app/Http/Controllers/Api/CommitContinuationController.php @@ -0,0 +1,32 @@ +get($commit, $user); + + return CommitResource::collection($thread->commits)->additional([ + 'position' => $thread->position, + 'length' => $thread->length, + 'is_capped' => $thread->isCapped, + 'next_id' => $thread->nextId, + ]); + } +} diff --git a/app/Http/Requests/ListApiCommitContinuationRequest.php b/app/Http/Requests/ListApiCommitContinuationRequest.php new file mode 100644 index 000000000..6313b734c --- /dev/null +++ b/app/Http/Requests/ListApiCommitContinuationRequest.php @@ -0,0 +1,32 @@ +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 []; + } +} diff --git a/routes/api.php b/routes/api.php index 9a0422ab6..7a060b07e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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; @@ -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') diff --git a/tests/Feature/Controllers/Api/CommitContinuationControllerTest.php b/tests/Feature/Controllers/Api/CommitContinuationControllerTest.php new file mode 100644 index 000000000..9cf4318b1 --- /dev/null +++ b/tests/Feature/Controllers/Api/CommitContinuationControllerTest.php @@ -0,0 +1,93 @@ +user = User::factory()->create(); + $this->keyPair = apiKeyPairFor($this->user); +}); + +/** + * @return list + */ +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(); +}); From 4b0089175a9f93eed9530ca1d3456a5046db8bfd Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Tue, 10 Nov 2026 18:27:53 +0000 Subject: [PATCH 2/4] feat: show continuation_id on signed API commits --- app/Http/Resources/CommitResource.php | 12 +++++++----- .../Controllers/Api/CommitControllerTest.php | 14 ++++++++++++++ .../Controllers/Api/CommitReplyControllerTest.php | 10 ++++++++++ .../Feature/Controllers/Api/LogControllerTest.php | 1 + 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/Http/Resources/CommitResource.php b/app/Http/Resources/CommitResource.php index b20286609..85573cbef 100644 --- a/app/Http/Resources/CommitResource.php +++ b/app/Http/Resources/CommitResource.php @@ -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. @@ -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'), diff --git a/tests/Feature/Controllers/Api/CommitControllerTest.php b/tests/Feature/Controllers/Api/CommitControllerTest.php index 8863ac514..673bf284c 100644 --- a/tests/Feature/Controllers/Api/CommitControllerTest.php +++ b/tests/Feature/Controllers/Api/CommitControllerTest.php @@ -353,6 +353,7 @@ 'quoted_commit_id' => null, 'quoted_commit' => null, 'poll' => null, + 'continuation_id' => null, ], 'ancestors' => [[ 'id' => $root->id, @@ -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); diff --git a/tests/Feature/Controllers/Api/CommitReplyControllerTest.php b/tests/Feature/Controllers/Api/CommitReplyControllerTest.php index adfc109fc..f9c3672bf 100644 --- a/tests/Feature/Controllers/Api/CommitReplyControllerTest.php +++ b/tests/Feature/Controllers/Api/CommitReplyControllerTest.php @@ -36,6 +36,16 @@ ->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('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)]); diff --git a/tests/Feature/Controllers/Api/LogControllerTest.php b/tests/Feature/Controllers/Api/LogControllerTest.php index 682edb311..ab3a04821 100644 --- a/tests/Feature/Controllers/Api/LogControllerTest.php +++ b/tests/Feature/Controllers/Api/LogControllerTest.php @@ -88,6 +88,7 @@ 'quoted_commit_id' => null, 'quoted_commit' => null, 'poll' => null, + 'continuation_id' => null, 'guild_mentions' => [], ]); From fa6e2eca27887ee158c176b91d07e010c750b27f Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Tue, 10 Nov 2026 18:27:53 +0000 Subject: [PATCH 3/4] feat: let API reply lists leave out one reply --- .../Controllers/Api/CommitReplyController.php | 2 +- .../Requests/ListApiCommitRepliesRequest.php | 9 ++++++++- .../Api/CommitReplyControllerTest.php | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/CommitReplyController.php b/app/Http/Controllers/Api/CommitReplyController.php index 7506a4a07..327595a67 100644 --- a/app/Http/Controllers/Api/CommitReplyController.php +++ b/app/Http/Controllers/Api/CommitReplyController.php @@ -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()); } } diff --git a/app/Http/Requests/ListApiCommitRepliesRequest.php b/app/Http/Requests/ListApiCommitRepliesRequest.php index 359a8f9b0..490c35f0b 100644 --- a/app/Http/Requests/ListApiCommitRepliesRequest.php +++ b/app/Http/Requests/ListApiCommitRepliesRequest.php @@ -25,8 +25,14 @@ 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> */ @@ -34,6 +40,7 @@ public function rules(): array { return [ 'newest' => ['sometimes', 'boolean'], + 'except' => ['sometimes', 'nullable', 'uuid'], ]; } } diff --git a/tests/Feature/Controllers/Api/CommitReplyControllerTest.php b/tests/Feature/Controllers/Api/CommitReplyControllerTest.php index f9c3672bf..ee6933d05 100644 --- a/tests/Feature/Controllers/Api/CommitReplyControllerTest.php +++ b/tests/Feature/Controllers/Api/CommitReplyControllerTest.php @@ -46,6 +46,22 @@ ->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)]); From a4f09011d5f82502f296f8a5580ee0012faafa7b Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Tue, 10 Nov 2026 18:27:53 +0000 Subject: [PATCH 4/4] docs: mention reading self-threads through the signed API --- KNOWLEDGE_BASE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index a3c033a43..6a302b81f 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -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;