From 92030fcd5ffdbad9efc59c2fd6715bac11c1fcbd Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Wed, 11 Nov 2026 18:40:56 +0000 Subject: [PATCH 1/2] feat: link the profiles bios mention in the API's follower lists --- .../Api/UserFollowersController.php | 14 ++++--- .../Api/UserFollowingController.php | 14 ++++--- .../Resources/UserConnectionCollection.php | 37 +++++++++++++++++++ app/Queries/BioProfilesQuery.php | 31 +++++++++++++++- .../Api/UserFollowersControllerTest.php | 20 +++++++++- .../Api/UserFollowingControllerTest.php | 13 +++++++ tests/Unit/Queries/BioProfilesQueryTest.php | 25 +++++++++++++ 7 files changed, 139 insertions(+), 15 deletions(-) create mode 100644 app/Http/Resources/UserConnectionCollection.php diff --git a/app/Http/Controllers/Api/UserFollowersController.php b/app/Http/Controllers/Api/UserFollowersController.php index d9b298e874..5e4c1bd9f2 100644 --- a/app/Http/Controllers/Api/UserFollowersController.php +++ b/app/Http/Controllers/Api/UserFollowersController.php @@ -5,20 +5,22 @@ namespace App\Http\Controllers\Api; use App\Http\Requests\ListApiUserFollowersRequest; -use App\Http\Resources\UserResource; +use App\Http\Resources\UserConnectionCollection; use App\Models\User; +use App\Queries\BioProfilesQuery; use App\Queries\UserConnectionsQuery; use Illuminate\Container\Attributes\CurrentUser; -use Illuminate\Http\Resources\Json\AnonymousResourceCollection; final readonly class UserFollowersController { /** - * The people following a user, 30 a page as on the web, with whether the signed-in user follows each one, and whether - * each one follows them. + * The people following a user, 30 a page as on the web, with whether the signed-in user follows each one, whether each + * one follows them, and the profiles each one's bio mentions. */ - public function index(ListApiUserFollowersRequest $request, #[CurrentUser] User $currentUser, User $user, UserConnectionsQuery $connections): AnonymousResourceCollection + public function index(ListApiUserFollowersRequest $request, #[CurrentUser] User $currentUser, User $user, UserConnectionsQuery $connections, BioProfilesQuery $bioProfiles): UserConnectionCollection { - return UserResource::collection($connections->builder($user->followers(), $currentUser)->cursorPaginate(30)); + $page = $connections->builder($user->followers(), $currentUser)->cursorPaginate(30); + + return new UserConnectionCollection($page, $bioProfiles->getMany($page->items())); } } diff --git a/app/Http/Controllers/Api/UserFollowingController.php b/app/Http/Controllers/Api/UserFollowingController.php index 8fe557f287..d68c25bf19 100644 --- a/app/Http/Controllers/Api/UserFollowingController.php +++ b/app/Http/Controllers/Api/UserFollowingController.php @@ -5,20 +5,22 @@ namespace App\Http\Controllers\Api; use App\Http\Requests\ListApiUserFollowingRequest; -use App\Http\Resources\UserResource; +use App\Http\Resources\UserConnectionCollection; use App\Models\User; +use App\Queries\BioProfilesQuery; use App\Queries\UserConnectionsQuery; use Illuminate\Container\Attributes\CurrentUser; -use Illuminate\Http\Resources\Json\AnonymousResourceCollection; final readonly class UserFollowingController { /** - * The people a user follows, 30 a page as on the web, with whether the signed-in user follows each one, and whether - * each one follows them. + * The people a user follows, 30 a page as on the web, with whether the signed-in user follows each one, whether each + * one follows them, and the profiles each one's bio mentions. */ - public function index(ListApiUserFollowingRequest $request, #[CurrentUser] User $currentUser, User $user, UserConnectionsQuery $connections): AnonymousResourceCollection + public function index(ListApiUserFollowingRequest $request, #[CurrentUser] User $currentUser, User $user, UserConnectionsQuery $connections, BioProfilesQuery $bioProfiles): UserConnectionCollection { - return UserResource::collection($connections->builder($user->following(), $currentUser)->cursorPaginate(30)); + $page = $connections->builder($user->following(), $currentUser)->cursorPaginate(30); + + return new UserConnectionCollection($page, $bioProfiles->getMany($page->items())); } } diff --git a/app/Http/Resources/UserConnectionCollection.php b/app/Http/Resources/UserConnectionCollection.php new file mode 100644 index 0000000000..adc4cd3d4a --- /dev/null +++ b/app/Http/Resources/UserConnectionCollection.php @@ -0,0 +1,37 @@ + $page + * @param array> $bioProfiles + */ + public function __construct(private readonly CursorPaginator $page, private readonly array $bioProfiles) + { + parent::__construct($page); + } + + /** + * @return array> + */ + public function toArray(Request $request): array + { + return array_map(fn (User $user): array => [ + ...UserResource::make($user)->toArray($request), + 'bio_profiles' => (object) $this->bioProfiles[$user->id], + ], $this->page->items()); + } +} diff --git a/app/Queries/BioProfilesQuery.php b/app/Queries/BioProfilesQuery.php index 353c04e21d..4833845ab9 100644 --- a/app/Queries/BioProfilesQuery.php +++ b/app/Queries/BioProfilesQuery.php @@ -24,8 +24,37 @@ public function __construct(private Mentions $mentions) */ public function get(User $user): array { - $handles = $this->mentions->handles("{$user->bio}\n{$user->company}"); + return $this->profiles($this->handles($user)); + } + /** + * What get() returns for each user, by user id, with one query for the whole page. + * + * @param iterable $users + * @return array> + */ + public function getMany(iterable $users): array + { + $handles = collect($users)->mapWithKeys(fn (User $user): array => [$user->id => $this->handles($user)]); + $profiles = $this->profiles(array_values(array_unique(array_merge(...$handles->values()->all())))); + + return $handles->map(fn (array $mentioned): array => array_intersect_key($profiles, array_flip($mentioned)))->all(); + } + + /** + * @return list + */ + private function handles(User $user): array + { + return $this->mentions->handles("{$user->bio}\n{$user->company}"); + } + + /** + * @param list $handles + * @return array + */ + private function profiles(array $handles): array + { if ($handles === []) { return []; } diff --git a/tests/Feature/Controllers/Api/UserFollowersControllerTest.php b/tests/Feature/Controllers/Api/UserFollowersControllerTest.php index 483f590d45..91ed103935 100644 --- a/tests/Feature/Controllers/Api/UserFollowersControllerTest.php +++ b/tests/Feature/Controllers/Api/UserFollowersControllerTest.php @@ -12,6 +12,21 @@ $this->getJson('/api/v1/users/octocat/followers')->assertUnauthorized(); }); +it("links the profiles each follower's bio mentions, as their profile does", function (): void { + $user = User::factory()->create(); + User::factory()->create(['handle' => 'TaylorOtwell']); + $linked = User::factory()->create(['bio' => 'Thanks @taylorotwell']); + $plain = User::factory()->create(['bio' => 'Ships on Fridays.', 'company' => null]); + $user->followers()->attach($linked, ['created_at' => now()->subMinute()]); + $user->followers()->attach($plain); + + $response = signedApiRequest(apiKeyPairFor(User::factory()->create()), 'GET', route('api-user-followers.index', $user)) + ->assertOk() + ->assertJsonPath('data.1.bio_profiles', ['taylorotwell' => 'TaylorOtwell']); + + expect(json_decode($response->getContent(), flags: JSON_THROW_ON_ERROR)->data[0]->bio_profiles)->toEqual(new stdClass()); +}); + it('lists the followers of a user, newest follow first', function (): void { $user = User::factory()->create(['handle' => 'octocat']); $earlier = User::factory()->create(['bio' => 'Ships on Fridays.']); @@ -101,10 +116,11 @@ // The first signed request also syncs the user's keys with GitHub... $queries(); - $user->followers()->attach(User::factory()->create()); + User::factory()->create(['handle' => 'taylorotwell']); + $user->followers()->attach(User::factory()->create(['bio' => 'Thanks @taylorotwell'])); $withOne = $queries(); - $user->followers()->attach(User::factory()->count(5)->create()); + $user->followers()->attach(User::factory()->count(5)->create(['bio' => 'Thanks @taylorotwell'])); expect($queries())->toBe($withOne); }); diff --git a/tests/Feature/Controllers/Api/UserFollowingControllerTest.php b/tests/Feature/Controllers/Api/UserFollowingControllerTest.php index 877f6226d4..b94a0da819 100644 --- a/tests/Feature/Controllers/Api/UserFollowingControllerTest.php +++ b/tests/Feature/Controllers/Api/UserFollowingControllerTest.php @@ -32,6 +32,19 @@ ->assertJsonPath('data.0.lane', $mutual->lane()); }); +it('links the profiles the bio of each person followed mentions, as their profile does', function (): void { + $user = User::factory()->create(); + User::factory()->create(['handle' => 'TaylorOtwell']); + $user->following()->attach(User::factory()->create(['bio' => 'Thanks @taylorotwell']), ['created_at' => now()->subMinute()]); + $user->following()->attach(User::factory()->create(['bio' => 'Ships on Fridays.', 'company' => null])); + + $response = signedApiRequest(apiKeyPairFor(User::factory()->create()), 'GET', route('api-user-following.index', $user)) + ->assertOk() + ->assertJsonPath('data.1.bio_profiles', ['taylorotwell' => 'TaylorOtwell']); + + expect(json_decode($response->getContent(), flags: JSON_THROW_ON_ERROR)->data[0]->bio_profiles)->toEqual(new stdClass()); +}); + it('pages through the people a user follows 30 at a time', function (): void { $user = User::factory()->create(); $followed = User::factory()->count(31)->create(); diff --git a/tests/Unit/Queries/BioProfilesQueryTest.php b/tests/Unit/Queries/BioProfilesQueryTest.php index 08d3262912..974ae3a7a3 100644 --- a/tests/Unit/Queries/BioProfilesQueryTest.php +++ b/tests/Unit/Queries/BioProfilesQueryTest.php @@ -23,3 +23,28 @@ expect(resolve(BioProfilesQuery::class)->get($user))->toBe([]); expect(DB::getQueryLog())->toBe([]); }); + +it('finds the profiles each user mentions for a whole page in one query', function (): void { + Organization::factory()->create(['handle' => 'Laravel']); + User::factory()->create(['handle' => 'taylorotwell']); + $first = User::factory()->create(['bio' => 'Thanks @TaylorOtwell', 'company' => null]); + $second = User::factory()->create(['bio' => 'Thanks @taylorotwell', 'company' => '@laravel']); + $quiet = User::factory()->create(['bio' => 'Nothing to see', 'company' => null]); + + DB::enableQueryLog(); + + expect(resolve(BioProfilesQuery::class)->getMany([$first, $second, $quiet]))->toEqual([ + $first->id => ['taylorotwell' => 'taylorotwell'], + $second->id => ['taylorotwell' => 'taylorotwell', 'laravel' => 'Laravel'], + $quiet->id => [], + ])->and(DB::getQueryLog())->toHaveCount(1); +}); + +it('asks nothing when no user on the page mentions anyone', function (): void { + $users = User::factory()->count(2)->create(['bio' => 'Building things', 'company' => null]); + + DB::enableQueryLog(); + + expect(resolve(BioProfilesQuery::class)->getMany($users))->toBe([$users[0]->id => [], $users[1]->id => []]) + ->and(DB::getQueryLog())->toBe([]); +}); From f0cb7a591aa33bc83a5e581dd415744a546f7de6 Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Wed, 11 Nov 2026 18:40:57 +0000 Subject: [PATCH 2/2] docs: mention follower lists with linked bios through the signed API --- KNOWLEDGE_BASE.md | 1 + 1 file changed, 1 insertion(+) diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index 3f5edb7f11..69294e338e 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -583,6 +583,7 @@ A signed request sees what the web shows you, such as your own pending commits a - commit, reply, and quote, with images, and delete your commits, with the same undo window as the web; - preview commit drafts without publishing them; - follow or unfollow people and organizations, mute or unmute, and block or unblock, and list who you muted and blocked; +- list a developer's followers and the people they follow, with the profiles each bio mentions linked as on their profile; - read who to follow, also as it is suggested beside a profile or a commit, and mark a suggestion not interested; - 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;