Skip to content
Merged
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
1 change: 1 addition & 0 deletions KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 52 additions & 0 deletions app/Http/Controllers/Api/GuildCommitPinController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Actions\UpdateGuildCommitPin;
use App\Exceptions\ApiException;
use App\Exceptions\GuildCommitUnavailable;
use App\Exceptions\GuildModeratorRemoved;
use App\Http\Requests\DeleteApiGuildCommitPinRequest;
use App\Http\Requests\UpdateApiGuildCommitPinRequest;
use App\Models\Commit;
use App\Models\Guild;
use App\Models\User;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final readonly class GuildCommitPinController
{
/**
* Pins the commit to the guild's board in place of the one pinned before. A moderator removed meanwhile gets the
* 404 anyone else gets, and a commit the board stopped showing keeps the old pin.
*/
public function update(UpdateApiGuildCommitPinRequest $request, Guild $guild, Commit $commit, #[CurrentUser] User $user, UpdateGuildCommitPin $action): Response
{
try {
$action->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();
}
}
55 changes: 55 additions & 0 deletions app/Http/Requests/DeleteApiGuildCommitPinRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Enums\ApiErrorCode;
use App\Enums\Feature;
use App\Exceptions\ApiException;
use App\Models\Commit;
use App\Models\Guild;
use App\Models\User;
use App\Models\UserTimeout;
use App\Queries\GuildCommitsQuery;
use App\Services\Features;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Http\FormRequest;

final class DeleteApiGuildCommitPinRequest extends FormRequest
{
/**
* Only the guild's moderator unpins, also a pin the board no longer shows, as on the web. Anyone else gets a 404
* before a moderator in timeout is told why they can't, as the web checks the timeout after authorizing.
*/
public function authorize(Features $features, GuildCommitsQuery $query): Response
{
$guild = $this->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 [];
}
}
55 changes: 55 additions & 0 deletions app/Http/Requests/UpdateApiGuildCommitPinRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Enums\ApiErrorCode;
use App\Enums\Feature;
use App\Exceptions\ApiException;
use App\Models\Commit;
use App\Models\Guild;
use App\Models\User;
use App\Models\UserTimeout;
use App\Queries\GuildCommitsQuery;
use App\Services\Features;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Http\FormRequest;

final class UpdateApiGuildCommitPinRequest extends FormRequest
{
/**
* Only the guild's moderator pins a top-level commit its board shows them, as on the web. Anyone else gets a 404
* before a moderator in timeout is told why they can't, as the web checks the timeout after authorizing.
*/
public function authorize(Features $features, GuildCommitsQuery $query): Response
{
$guild = $this->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 [];
}
}
13 changes: 13 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
134 changes: 134 additions & 0 deletions tests/Feature/Controllers/Api/GuildCommitPinControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

use App\Actions\UpdateGuildCommitPin;
use App\Enums\Feature;
use App\Models\Commit;
use App\Models\Guild;
use App\Models\User;
use App\Models\UserTimeout;
use App\Services\Features;

beforeEach(function (): void {
resolve(Features::class)->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();
});
Loading