From 6bbd48cc6c6e91ef4ff0217f4e0955cd46df31ba Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Wed, 18 Nov 2026 17:18:05 +0000 Subject: [PATCH 1/2] test: cover pinning commits to a guild board through the API --- .../Api/GuildCommitPinControllerTest.php | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tests/Feature/Controllers/Api/GuildCommitPinControllerTest.php diff --git a/tests/Feature/Controllers/Api/GuildCommitPinControllerTest.php b/tests/Feature/Controllers/Api/GuildCommitPinControllerTest.php new file mode 100644 index 000000000..db13d5610 --- /dev/null +++ b/tests/Feature/Controllers/Api/GuildCommitPinControllerTest.php @@ -0,0 +1,134 @@ +update(Feature::Guilds, true, adminOnly: false); + + $this->user = User::factory()->create(); + $this->keyPair = apiKeyPairFor($this->user); + $this->rust = Guild::factory()->create(['handle' => 'rust', 'user_id' => $this->user->id]); +}); + +it('pins and unpins board commits for the moderator, keeping their profile pin', function (): void { + $first = Commit::factory()->for($this->rust)->create(); + $next = Commit::factory()->create(['body' => 'Learning #rust']); + $profilePin = Commit::factory()->for($this->user)->create(); + $this->user->update(['pinned_commit_id' => $profilePin->id]); + + signedApiRequest($this->keyPair, 'PUT', route('api-guild-commit-pin.update', ['rust', $first]))->assertNoContent(); + signedApiRequest($this->keyPair, 'GET', route('api-guild.show', 'rust'))->assertJsonPath('pinned_commit.id', $first->id); + + signedApiRequest($this->keyPair, 'PUT', route('api-guild-commit-pin.update', ['rust', $next]))->assertNoContent(); + signedApiRequest($this->keyPair, 'DELETE', route('api-guild-commit-pin.destroy', ['rust', $first]))->assertNoContent(); + signedApiRequest($this->keyPair, 'GET', route('api-guild.show', 'rust'))->assertJsonPath('pinned_commit.id', $next->id); + + signedApiRequest($this->keyPair, 'DELETE', route('api-guild-commit-pin.destroy', ['rust', $next]))->assertNoContent(); + + expect($this->rust->fresh()->pinned_commit_id)->toBeNull() + ->and($this->user->fresh()->pinned_commit_id)->toBe($profilePin->id); + signedApiRequest($this->keyPair, 'GET', route('api-guild.show', 'rust'))->assertJsonPath('pinned_commit', null); +}); + +it('clears a pin the board no longer shows, but does not pin it again', function (string $change): void { + $commit = Commit::factory()->create(['body' => '#rust']); + $this->rust->update(['pinned_commit_id' => $commit->id]); + $change === 'deleted' ? $commit->delete() : $commit->update(['body' => 'No guild hashtag']); + + signedApiRequest($this->keyPair, 'PUT', route('api-guild-commit-pin.update', ['rust', $commit]))->assertNotFound(); + expect($this->rust->fresh()->pinned_commit_id)->toBe($commit->id); + + signedApiRequest($this->keyPair, 'DELETE', route('api-guild-commit-pin.destroy', ['rust', $commit]))->assertNoContent(); + + expect($this->rust->fresh()->pinned_commit_id)->toBeNull(); +})->with(['deleted', 'edited']); + +it('does not find the pin for anyone but the moderator, or for commits the board does not show', function (string $case): void { + $commit = match ($case) { + 'reply' => Commit::factory()->for(Commit::factory()->for($this->rust), 'parent')->create(['body' => '#rust']), + 'other board' => Commit::factory()->for(Guild::factory())->create(['body' => '#rust']), + 'unrelated' => Commit::factory()->create(['body' => '`#rust` #rustaceans']), + default => Commit::factory()->for($this->rust)->create(), + }; + if ($case === 'admin') { + config(['app.admins' => [$this->user->handle]]); + $this->rust->update(['user_id' => User::factory()->create()->id]); + } + + if ($case === 'guilds off') { + resolve(Features::class)->update(Feature::Guilds, false); + } + + signedApiRequest($this->keyPair, 'PUT', route('api-guild-commit-pin.update', ['rust', $commit]))->assertNotFound(); + signedApiRequest($this->keyPair, 'DELETE', route('api-guild-commit-pin.destroy', ['rust', $commit]))->assertNotFound(); + + expect($this->rust->fresh()->pinned_commit_id)->toBeNull(); +})->with(['admin', 'reply', 'other board', 'unrelated', 'guilds off']); + +it('requires a signed request', function (string $method, string $route): void { + $commit = Commit::factory()->for($this->rust)->create(); + + $this->json($method, route($route, ['rust', $commit]))->assertUnauthorized(); +})->with([ + 'pin' => ['PUT', 'api-guild-commit-pin.update'], + 'unpin' => ['DELETE', 'api-guild-commit-pin.destroy'], +]); + +it('does not find the guild for a moderator removed after authorization', function (string $method, string $route, bool $pinned): void { + $commit = Commit::factory()->for($this->rust)->create(); + $this->rust->update(['pinned_commit_id' => $pinned ? $commit->id : null]); + $this->app->beforeResolving(UpdateGuildCommitPin::class, fn () => $this->rust->update(['user_id' => null])); + + signedApiRequest($this->keyPair, $method, route($route, ['rust', $commit]))->assertNotFound(); + + expect($this->rust->fresh()->pinned_commit_id)->toBe($pinned ? $commit->id : null); +})->with([ + 'pin' => ['PUT', 'api-guild-commit-pin.update', false], + 'unpin' => ['DELETE', 'api-guild-commit-pin.destroy', true], +]); + +it('keeps the current pin when the commit becomes unavailable after authorization', function (string $change): void { + $old = Commit::factory()->for($this->rust)->create(); + $this->rust->update(['pinned_commit_id' => $old->id]); + $target = Commit::factory()->create(['body' => '#rust']); + $this->app->beforeResolving(UpdateGuildCommitPin::class, fn () => match ($change) { + 'erased' => $target->forceDelete(), + 'deleted' => $target->delete(), + 'edited' => $target->update(['body' => 'No guild hashtag']), + }); + + signedApiRequest($this->keyPair, 'PUT', route('api-guild-commit-pin.update', ['rust', $target])) + ->assertNotFound()->assertJsonPath('code', 'commit_unavailable'); + + expect($this->rust->fresh()->pinned_commit_id)->toBe($old->id); +})->with(['erased', 'deleted', 'edited']); + +it('refuses pin changes while the moderator is in timeout', function (string $method, string $route): void { + $commit = Commit::factory()->for($this->rust)->create(); + $this->rust->update(['pinned_commit_id' => $commit->id]); + UserTimeout::factory()->for($this->user)->create(); + + signedApiRequest($this->keyPair, $method, route($route, ['rust', $commit])) + ->assertForbidden()->assertJsonPath('code', 'timed_out'); + + expect($this->rust->fresh()->pinned_commit_id)->toBe($commit->id); +})->with([ + 'pin' => ['PUT', 'api-guild-commit-pin.update'], + 'unpin' => ['DELETE', 'api-guild-commit-pin.destroy'], +]); + +it('does not tell a user in timeout who does not moderate the guild about the timeout', function (): void { + $commit = Commit::factory()->for($this->rust)->create(); + $this->rust->update(['user_id' => User::factory()->create()->id]); + UserTimeout::factory()->for($this->user)->create(); + + signedApiRequest($this->keyPair, 'PUT', route('api-guild-commit-pin.update', ['rust', $commit]))->assertNotFound(); +}); From bdd07e6318187ed81881bec1a0a9df992a6b0801 Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Wed, 18 Nov 2026 17:18:05 +0000 Subject: [PATCH 2/2] feat: let API clients pin commits to a guild board --- KNOWLEDGE_BASE.md | 1 + .../Api/GuildCommitPinController.php | 52 ++++++++++++++++++ .../DeleteApiGuildCommitPinRequest.php | 55 +++++++++++++++++++ .../UpdateApiGuildCommitPinRequest.php | 55 +++++++++++++++++++ routes/api.php | 13 +++++ 5 files changed, 176 insertions(+) create mode 100644 app/Http/Controllers/Api/GuildCommitPinController.php create mode 100644 app/Http/Requests/DeleteApiGuildCommitPinRequest.php create mode 100644 app/Http/Requests/UpdateApiGuildCommitPinRequest.php diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index fc2dac0d2..4accc1963 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -601,6 +601,7 @@ A signed request sees what the web shows you, such as your own pending commits a - join or leave a guild, where joining clears your mute of it, as the Join button does, while guilds are available to you; - report a guild you don't moderate to the admins, privately, as the Report button does, where reporting it again keeps your first report until an admin dismisses it, while guilds are available to you; - mute or unmute a guild, which hides its commits from logs and search, Following included, even while you stay a member, while guilds are available to you; +- pin or unpin a commit on the board of a guild you moderate, as Pin to board does, which replaces the previous pin, where a pin the board no longer shows can still be unpinned, while guilds are available to you; - star or unstar, and list who starred your own commit, newest first, 20 a page; - create polls on commits that are neither replies nor quotes, and vote on polls; - change Remotes settings, read and send messages with images, link cards, and voice notes in conversations and orgs, mark them pulled, react to messages and list your reactions, pin and unpin org messages as a maintainer, find people, and start orgs; diff --git a/app/Http/Controllers/Api/GuildCommitPinController.php b/app/Http/Controllers/Api/GuildCommitPinController.php new file mode 100644 index 000000000..f86fa7a53 --- /dev/null +++ b/app/Http/Controllers/Api/GuildCommitPinController.php @@ -0,0 +1,52 @@ +handle($guild, $user, $commit, true); + } catch (GuildModeratorRemoved) { + throw new NotFoundHttpException; + } catch (GuildCommitUnavailable) { + throw ApiException::commitUnavailable(); + } + + return response()->noContent(); + } + + /** + * Unpinning a commit that isn't the pin anymore changes nothing. + */ + public function destroy(DeleteApiGuildCommitPinRequest $request, Guild $guild, Commit $commit, #[CurrentUser] User $user, UpdateGuildCommitPin $action): Response + { + try { + $action->handle($guild, $user, $commit, false); + } catch (GuildModeratorRemoved) { + throw new NotFoundHttpException; + } + + return response()->noContent(); + } +} diff --git a/app/Http/Requests/DeleteApiGuildCommitPinRequest.php b/app/Http/Requests/DeleteApiGuildCommitPinRequest.php new file mode 100644 index 000000000..6ed252163 --- /dev/null +++ b/app/Http/Requests/DeleteApiGuildCommitPinRequest.php @@ -0,0 +1,55 @@ +route('guild'); + assert($guild instanceof Guild); + $commit = $this->route('commit'); + assert($commit instanceof Commit); + $user = $this->user(); + assert($user instanceof User); + + $allowed = $features->available(Feature::Guilds, $user) && $guild->user_id === $user->id && $commit->parent_id === null + && ($guild->pinned_commit_id === $commit->id || $query->rootIds($guild, $user, $commit->id)->isNotEmpty()); + + if (! $allowed) { + return Response::denyAsNotFound(); + } + + $timeout = $user->activeTimeout(); + + if ($timeout instanceof UserTimeout) { + throw new ApiException(ApiErrorCode::TimedOut, $timeout->message()); + } + + return Response::allow(); + } + + /** @return array{} */ + public function rules(): array + { + return []; + } +} diff --git a/app/Http/Requests/UpdateApiGuildCommitPinRequest.php b/app/Http/Requests/UpdateApiGuildCommitPinRequest.php new file mode 100644 index 000000000..d40e7d909 --- /dev/null +++ b/app/Http/Requests/UpdateApiGuildCommitPinRequest.php @@ -0,0 +1,55 @@ +route('guild'); + assert($guild instanceof Guild); + $commit = $this->route('commit'); + assert($commit instanceof Commit); + $user = $this->user(); + assert($user instanceof User); + + $allowed = $features->available(Feature::Guilds, $user) && $guild->user_id === $user->id && $commit->parent_id === null + && $query->rootIds($guild, $user, $commit->id)->isNotEmpty(); + + if (! $allowed) { + return Response::denyAsNotFound(); + } + + $timeout = $user->activeTimeout(); + + if ($timeout instanceof UserTimeout) { + throw new ApiException(ApiErrorCode::TimedOut, $timeout->message()); + } + + return Response::allow(); + } + + /** @return array{} */ + public function rules(): array + { + return []; + } +} diff --git a/routes/api.php b/routes/api.php index 1c674c24e..71117d0ab 100644 --- a/routes/api.php +++ b/routes/api.php @@ -31,6 +31,7 @@ use App\Http\Controllers\Api\CurrentUserController; use App\Http\Controllers\Api\DeviceController; use App\Http\Controllers\Api\DevicePushTokenController; +use App\Http\Controllers\Api\GuildCommitPinController; use App\Http\Controllers\Api\GuildController; use App\Http\Controllers\Api\GuildIconController; use App\Http\Controllers\Api\GuildMembershipController; @@ -388,6 +389,18 @@ ->middleware('throttle:follows') ->name('api-guild-mute.destroy'); + // Guild Pin: the moderator featuring one commit on the guild's board, as the web's Pin to board does... + Route::put('guilds/{guild:handle}/commits/{commit}/pin', [GuildCommitPinController::class, 'update']) + ->whereUuid('commit') + ->withTrashed() + ->middleware('throttle:pins') + ->name('api-guild-commit-pin.update'); + Route::delete('guilds/{guild:handle}/commits/{commit}/pin', [GuildCommitPinController::class, 'destroy']) + ->whereUuid('commit') + ->withTrashed() + ->middleware('throttle:pins') + ->name('api-guild-commit-pin.destroy'); + // Who to Follow... Route::get('me/suggested-users', [SuggestedUserController::class, 'index'])->name('api-suggested-user.index'); Route::post('users/{user:handle}/dismiss', [SuggestionDismissalController::class, 'store'])->name('api-suggestion-dismissal.store');