Skip to content
Open
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
4 changes: 2 additions & 2 deletions KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
Expand All @@ -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');
Expand Down
19 changes: 19 additions & 0 deletions app/Http/Controllers/ProfileFeedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Actions\CreateProfileFeed;
use App\Models\Profile;
use Illuminate\Http\Response;

final readonly class ProfileFeedController
{
public function show(Profile $profile, CreateProfileFeed $action): Response
{
return response($action->handle($profile), headers: [
'Content-Type' => 'application/atom+xml; charset=UTF-8',
]);
}
}
19 changes: 0 additions & 19 deletions app/Http/Controllers/UserFeedController.php

This file was deleted.

17 changes: 9 additions & 8 deletions app/Services/PageMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Image>, structuredData: array<string, mixed>}
* @phpstan-type CommitMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list<Image>, publishedAt: string, author: string, feed?: Feed, structuredData: array<string, mixed>}
* @phpstan-type CommitMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list<Image>, publishedAt: string, author: string, feed: Feed, structuredData: array<string, mixed>}
* @phpstan-type UserMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list<Image>, feed: Feed, handle: string, structuredData: array<string, mixed>}
* @phpstan-type OrganizationMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list<Image>, handle: string, structuredData: array<string, mixed>}
* @phpstan-type OrganizationMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list<Image>, feed: Feed, handle: string, structuredData: array<string, mixed>}
* @phpstan-type LinkMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list<Image>, structuredData: array<string, mixed>}
* @phpstan-type UnclaimedProfileMetadata array{type: string, title: string, description: string, url: string, robots: string, images: list<Image>, handle: string}
*/
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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",
];
}

Expand Down
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');

Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Controllers/OrganizationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
->assertSee('<meta property="og:url" content="'.route('user.show', 'laravel').'">', false)
->assertSee('<meta property="og:image" content="'.route('user-open-graph-image.show', ['profile' => 'laravel', 'v' => $laravel->openGraphImageVersion()]).'">', false)
->assertSee('<meta property="profile:username" content="laravel">', false)
->assertSee('<link rel="alternate" type="application/atom+xml" title="Laravel (@laravel) on /dev/null" href="'.route('user-feed.show', ['profile' => 'laravel']).'">', false)
->assertSee('application/ld+json', false);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
Expand Down
11 changes: 9 additions & 2 deletions tests/Unit/Services/PageMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,18 @@
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');

$metadata = resolve(PageMetadata::class)->commit($commit);

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',
Expand Down Expand Up @@ -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()]))
Expand Down