From f41233afa25982e0122d106669851da6218f0618 Mon Sep 17 00:00:00 2001 From: Rui Maduro Date: Sat, 28 Nov 2026 14:00:51 +0000 Subject: [PATCH] feat: add Atom feeds for organizations --- KNOWLEDGE_BASE.md | 4 +- ...eateUserFeed.php => CreateProfileFeed.php} | 37 +++++------ .../Controllers/ProfileFeedController.php | 19 ++++++ app/Http/Controllers/UserFeedController.php | 19 ------ app/Services/PageMetadata.php | 17 +++--- routes/web.php | 4 +- .../OrganizationControllerTest.php | 1 + ...Test.php => ProfileFeedControllerTest.php} | 61 +++++++++++++++++++ tests/Unit/Services/PageMetadataTest.php | 11 +++- 9 files changed, 122 insertions(+), 51 deletions(-) rename app/Actions/{CreateUserFeed.php => CreateProfileFeed.php} (62%) create mode 100644 app/Http/Controllers/ProfileFeedController.php delete mode 100644 app/Http/Controllers/UserFeedController.php rename tests/Feature/Controllers/{UserFeedControllerTest.php => ProfileFeedControllerTest.php} (74%) diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index 4cf327f296..cd4a134321 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -576,9 +576,9 @@ Sources: [GitHub action](.github/actions/commit/action.yml). The read-only JSON API at `/api/v1` requires no authentication or token, except for who follows whom. Read a public profile at `/api/v1/users/{handle}`, that user's commits at `/api/v1/users/{handle}/commits`, or one commit at `/api/v1/commits/{commit}`. `/api/v1/users/{handle}/followers` and `/following` list people the way the profile's people lists do, newest follow first, with each person's lane and whether you follow each other, and `/api/v1/users/{handle}/following/organizations` lists the organizations they follow the same way, with whether you follow each one; like on the web, they are for members only and need a [signed request](#signed-api-and-devices). Add `?replies=1` to the user-commits endpoint to read their replies instead of top-level commits. Lists use cursor pagination: 20 commits, or 30 people or organizations, per page. Only public approved, non-deleted content is returned, not private profile data such as email. Unsigned requests are limited to **60 requests per minute per IP** and cannot create posts, follow, or star; terminal posting and the [signed API](#signed-api-and-devices) are separate signed interfaces. -Subscribe in a feed reader to `/@{handle}/feed.atom` for a profile's latest **50 approved top-level commits**, including image enclosures. Feeds are public and can be cached for five minutes. +Subscribe in a feed reader to `/@{handle}/feed.atom` for the latest **50 approved top-level commits** of a developer or an organization, including image enclosures. Feeds are public and can be cached for five minutes. -Sources: [API routes](routes/api.php), [API controllers](app/Http/Controllers/Api), [feed generation](app/Actions/CreateUserFeed.php), [rate limits](app/Providers/AppServiceProvider.php). +Sources: [API routes](routes/api.php), [API controllers](app/Http/Controllers/Api), [feed generation](app/Actions/CreateProfileFeed.php), [rate limits](app/Providers/AppServiceProvider.php). ### Signed API and devices diff --git a/app/Actions/CreateUserFeed.php b/app/Actions/CreateProfileFeed.php similarity index 62% rename from app/Actions/CreateUserFeed.php rename to app/Actions/CreateProfileFeed.php index 1527a1d7db..a2a5595d24 100644 --- a/app/Actions/CreateUserFeed.php +++ b/app/Actions/CreateProfileFeed.php @@ -5,20 +5,20 @@ namespace App\Actions; use App\Models\Commit; -use App\Models\User; +use App\Models\Profile; use Illuminate\Support\Str; use XMLWriter; -final readonly class CreateUserFeed +final readonly class CreateProfileFeed { public const int LIMIT = 50; /** - * Renders the user's latest public top-level commits as an Atom feed. + * Renders the latest public top-level commits of a user or an organization as an Atom feed. */ - public function handle(User $user): string + public function handle(Profile $profile): string { - $commits = $user->commits() + $commits = $profile->commits() ->whereNull('guild_id') ->approved() ->topLevel() @@ -34,25 +34,26 @@ public function handle(User $user): string $xml->startDocument('1.0', 'UTF-8'); $xml->startElementNs(null, 'feed', 'http://www.w3.org/2005/Atom'); - $xml->writeElement('id', "urn:uuid:{$user->id}"); - $xml->writeElement('title', $this->text("{$user->nameWithHandle()} on /dev/null")); + $xml->writeElement('id', "urn:uuid:{$profile->id}"); + $xml->writeElement('title', $this->text("{$profile->nameWithHandle()} on /dev/null")); - if ($user->bio !== null) { - $xml->writeElement('subtitle', $this->text($user->bio)); + if ($profile->bio !== null) { + $xml->writeElement('subtitle', $this->text($profile->bio)); } - $lastUpdatedAt = $commits->first()->created_at ?? $user->created_at; - $xml->writeElement('updated', $lastUpdatedAt->max($user->updated_at)->toAtomString()); - $this->link($xml, 'self', route('user-feed.show', $user), 'application/atom+xml'); - $this->link($xml, 'alternate', route('user.show', $user), 'text/html'); + $owner = $profile->owner() ?? $profile; + $lastUpdatedAt = $commits->first()->created_at ?? $owner->created_at; + $xml->writeElement('updated', $lastUpdatedAt->max($owner->updated_at)->toAtomString()); + $this->link($xml, 'self', route('user-feed.show', $profile), 'application/atom+xml'); + $this->link($xml, 'alternate', route('user.show', $profile), 'text/html'); $xml->startElement('author'); - $xml->writeElement('name', $this->text($user->name)); - $xml->writeElement('uri', route('user.show', $user)); + $xml->writeElement('name', $this->text($profile->name)); + $xml->writeElement('uri', route('user.show', $profile)); $xml->endElement(); foreach ($commits as $commit) { - $this->entry($xml, $user, $commit); + $this->entry($xml, $profile, $commit); } $xml->endElement(); @@ -61,12 +62,12 @@ public function handle(User $user): string return $xml->outputMemory(); } - private function entry(XMLWriter $xml, User $user, Commit $commit): void + private function entry(XMLWriter $xml, Profile $profile, Commit $commit): void { $xml->startElement('entry'); $xml->writeElement('id', "urn:uuid:{$commit->id}"); $xml->writeElement('title', $this->text(Str::limit(Str::squish($commit->body), 72, '…', preserveWords: true))); - $this->link($xml, 'alternate', route('commit.show', [$user, $commit]), 'text/html'); + $this->link($xml, 'alternate', route('commit.show', [$profile, $commit]), 'text/html'); foreach ($commit->images as $image) { $this->link($xml, 'enclosure', $image->url(), 'image/webp'); diff --git a/app/Http/Controllers/ProfileFeedController.php b/app/Http/Controllers/ProfileFeedController.php new file mode 100644 index 0000000000..e07f00954a --- /dev/null +++ b/app/Http/Controllers/ProfileFeedController.php @@ -0,0 +1,19 @@ +handle($profile), headers: [ + 'Content-Type' => 'application/atom+xml; charset=UTF-8', + ]); + } +} diff --git a/app/Http/Controllers/UserFeedController.php b/app/Http/Controllers/UserFeedController.php deleted file mode 100644 index b5665099c5..0000000000 --- a/app/Http/Controllers/UserFeedController.php +++ /dev/null @@ -1,19 +0,0 @@ -handle($user), headers: [ - 'Content-Type' => 'application/atom+xml; charset=UTF-8', - ]); - } -} diff --git a/app/Services/PageMetadata.php b/app/Services/PageMetadata.php index 8d6b49c7ba..1c7ad0c145 100644 --- a/app/Services/PageMetadata.php +++ b/app/Services/PageMetadata.php @@ -18,9 +18,9 @@ * @phpstan-type Image array{url: string, width?: int, height?: int, type?: string, alt: string} * @phpstan-type Feed array{url: string, title: string} * @phpstan-type WebsiteMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list, structuredData: array} - * @phpstan-type CommitMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list, publishedAt: string, author: string, feed?: Feed, structuredData: array} + * @phpstan-type CommitMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list, publishedAt: string, author: string, feed: Feed, structuredData: array} * @phpstan-type UserMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list, feed: Feed, handle: string, structuredData: array} - * @phpstan-type OrganizationMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list, handle: string, structuredData: array} + * @phpstan-type OrganizationMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list, feed: Feed, handle: string, structuredData: array} * @phpstan-type LinkMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list, structuredData: array} * @phpstan-type UnclaimedProfileMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list, handle: string} */ @@ -219,7 +219,7 @@ public function commit(Commit $commit, array $replies = []): ?array 'images' => $images, 'publishedAt' => $commit->created_at->toIso8601String(), 'author' => route('user.show', $profile), - ...$profile->user instanceof User ? ['feed' => $this->feed($profile->user)] : [], + 'feed' => $this->feed($profile), 'structuredData' => [ '@context' => 'https://schema.org', '@type' => 'SocialMediaPosting', @@ -261,7 +261,7 @@ public function user(User $user): array 'url' => route('user.show', $user), 'robots' => self::INDEXABLE, 'images' => [$this->profileImage($user, $title)], - 'feed' => $this->feed($user), + 'feed' => $this->feed($user->profile), 'handle' => $user->handle, 'structuredData' => [ '@context' => 'https://schema.org', @@ -309,6 +309,7 @@ public function organization(Organization $organization): array 'type' => 'image/png', 'alt' => $title, ]], + 'feed' => $this->feed($organization->profile), 'handle' => $organization->handle, 'structuredData' => [ '@context' => 'https://schema.org', @@ -428,15 +429,15 @@ private function commitImages(Commit $commit, Profile $profile): array } /** - * The Atom feed of the user's commits. + * The Atom feed of the commits of a user or an organization. * * @return Feed */ - private function feed(User $user): array + private function feed(Profile $profile): array { return [ - 'url' => route('user-feed.show', $user), - 'title' => "{$user->nameWithHandle()} on /dev/null", + 'url' => route('user-feed.show', $profile), + 'title' => "{$profile->nameWithHandle()} on /dev/null", ]; } diff --git a/routes/web.php b/routes/web.php index e4b8c6961f..b58858e690 100644 --- a/routes/web.php +++ b/routes/web.php @@ -123,6 +123,7 @@ use App\Http\Controllers\PendingFollowController; use App\Http\Controllers\PrivacyController; use App\Http\Controllers\ProductMetricsController; +use App\Http\Controllers\ProfileFeedController; use App\Http\Controllers\ProfileLookupController; use App\Http\Controllers\ProfileOpenGraphImageController; use App\Http\Controllers\ProfileSearchController; @@ -151,7 +152,6 @@ use App\Http\Controllers\UserBanController; use App\Http\Controllers\UserBlockController; use App\Http\Controllers\UserController; -use App\Http\Controllers\UserFeedController; use App\Http\Controllers\UserFollowController; use App\Http\Controllers\UserFollowersController; use App\Http\Controllers\UserFollowersYouKnowController; @@ -327,7 +327,7 @@ ->name('user-preview.show'); Route::get('@{profile:handle}/og.png', [ProfileOpenGraphImageController::class, 'show']) ->name('user-open-graph-image.show'); -Route::get('@{user:handle}/feed.atom', [UserFeedController::class, 'show']) +Route::get('@{profile:handle}/feed.atom', [ProfileFeedController::class, 'show']) ->middleware('cache.headers:public;max_age=300;etag') ->name('user-feed.show'); diff --git a/tests/Feature/Controllers/OrganizationControllerTest.php b/tests/Feature/Controllers/OrganizationControllerTest.php index 737bc08d22..a82aa35778 100644 --- a/tests/Feature/Controllers/OrganizationControllerTest.php +++ b/tests/Feature/Controllers/OrganizationControllerTest.php @@ -116,6 +116,7 @@ ->assertSee('', false) ->assertSee('', false) ->assertSee('', false) + ->assertSee('', false) ->assertSee('application/ld+json', false); }); diff --git a/tests/Feature/Controllers/UserFeedControllerTest.php b/tests/Feature/Controllers/ProfileFeedControllerTest.php similarity index 74% rename from tests/Feature/Controllers/UserFeedControllerTest.php rename to tests/Feature/Controllers/ProfileFeedControllerTest.php index 7488013a62..68f014c2cd 100644 --- a/tests/Feature/Controllers/UserFeedControllerTest.php +++ b/tests/Feature/Controllers/ProfileFeedControllerTest.php @@ -5,6 +5,8 @@ use App\Models\Commit; use App\Models\CommitImage; use App\Models\Guild; +use App\Models\Organization; +use App\Models\Profile; use App\Models\User; use Illuminate\Support\Facades\DB; use Illuminate\Testing\TestResponse; @@ -211,6 +213,65 @@ function atomFeed(TestResponse $response): SimpleXMLElement ->and((string) $feed->updated)->toBe(now()->subYear()->toAtomString()); }); +it('serves the public top-level commits of an organization as an atom feed', function (): void { + $organization = Organization::factory()->create([ + 'handle' => 'laravel', + 'name' => 'Laravel', + 'bio' => 'The PHP framework for web artisans.', + ]); + $member = User::factory()->create(['handle' => 'taylorotwell']); + + $older = Commit::factory()->for($member)->create([ + 'author_id' => $organization->id, + 'body' => 'Laravel 13 is out', + 'created_at' => now()->subDay(), + ]); + $newer = Commit::factory()->for($member)->create(['author_id' => $organization->id, 'body' => 'Laravel Cloud is out']); + Commit::factory()->for($member)->for(Guild::factory())->create(['author_id' => $organization->id, 'body' => 'Only on the guild board']); + Commit::factory()->for($member)->pending()->create(['author_id' => $organization->id, 'body' => 'Pending commit']); + Commit::factory()->for($member)->create(['body' => 'From the member']); + + $response = $this->get('/@laravel/feed.atom'); + + $response->assertOk() + ->assertHeader('Content-Type', 'application/atom+xml; charset=UTF-8') + ->assertDontSee('Only on the guild board') + ->assertDontSee('Pending commit') + ->assertDontSee('From the member'); + + $feed = atomFeed($response); + + expect((string) $feed->id)->toBe("urn:uuid:{$organization->id}") + ->and((string) $feed->title)->toBe('Laravel (@laravel) on /dev/null') + ->and((string) $feed->subtitle)->toBe('The PHP framework for web artisans.') + ->and((string) $feed->author->name)->toBe('Laravel') + ->and((string) $feed->author->uri)->toBe(route('user.show', 'laravel')) + ->and((string) $feed->link[0]['href'])->toBe(route('user-feed.show', ['profile' => 'laravel'])) + ->and((string) $feed->link[1]['href'])->toBe(route('user.show', 'laravel')) + ->and($feed->entry)->toHaveCount(2) + ->and((string) $feed->entry[0]->id)->toBe("urn:uuid:{$newer->id}") + ->and((string) $feed->entry[1]->id)->toBe("urn:uuid:{$older->id}") + ->and((string) $feed->entry[1]->link['href'])->toBe(route('commit.show', ['profile' => 'laravel', 'commit' => $older])); +}); + +it('leaves the commits posted as an organization out of the feed of the member who posted them', function (): void { + $organization = Organization::factory()->create(['handle' => 'laravel']); + $member = User::factory()->create(['handle' => 'taylorotwell']); + Commit::factory()->for($member)->create(['author_id' => $organization->id, 'body' => 'Posted as Laravel']); + + $this->get('/@taylorotwell/feed.atom') + ->assertOk() + ->assertDontSee('Posted as Laravel'); +}); + +it('serves an empty feed for developers who have not joined', function (): void { + Profile::factory()->create(['handle' => 'octocat']); + + $feed = atomFeed($this->get('/@octocat/feed.atom')); + + expect($feed->entry)->toHaveCount(0); +}); + it('returns not found for unknown handles', function (): void { $this->get('/@missing/feed.atom')->assertNotFound(); }); diff --git a/tests/Unit/Services/PageMetadataTest.php b/tests/Unit/Services/PageMetadataTest.php index a9a0dea44b..c0486ec089 100644 --- a/tests/Unit/Services/PageMetadataTest.php +++ b/tests/Unit/Services/PageMetadataTest.php @@ -60,7 +60,7 @@ expect($metadata['structuredData'])->not->toHaveKey('isPartOf'); }); -it('describes a commit from an organization, which has no feed', function (): void { +it('describes a commit from an organization, with its feed', function (): void { $organization = Organization::factory()->create(['handle' => 'laravel', 'name' => 'Laravel']); $commit = Commit::factory()->create(['author_id' => $organization->id])->loadPublicPreview()->loadCount('stars'); @@ -68,7 +68,10 @@ expect($metadata['title'])->toStartWith('Laravel (@laravel) on /dev/null: “') ->and($metadata['url'])->toBe(route('commit.show', ['profile' => 'laravel', 'commit' => $commit])) - ->and($metadata)->not->toHaveKey('feed') + ->and($metadata['feed'])->toBe([ + 'url' => route('user-feed.show', ['profile' => 'laravel']), + 'title' => 'Laravel (@laravel) on /dev/null', + ]) ->and($metadata['structuredData']['author'])->toBe([ '@type' => 'Organization', 'name' => 'Laravel', @@ -104,6 +107,10 @@ 'title' => 'Laravel (@laravel) on /dev/null', 'description' => 'The PHP framework for web artisans.', 'url' => route('user.show', 'laravel'), + 'feed' => [ + 'url' => route('user-feed.show', ['profile' => 'laravel']), + 'title' => 'Laravel (@laravel) on /dev/null', + ], 'handle' => 'laravel', ]) ->and($metadata['images'][0]['url'])->toBe(route('user-open-graph-image.show', ['profile' => 'laravel', 'v' => $organization->openGraphImageVersion()]))