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
1 change: 1 addition & 0 deletions KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions app/Http/Controllers/Api/UserFollowersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
14 changes: 8 additions & 6 deletions app/Http/Controllers/Api/UserFollowingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
37 changes: 37 additions & 0 deletions app/Http/Resources/UserConnectionCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use App\Models\User;
use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

/**
* A page of someone's followers or following, each with the profiles their bio and company mention, as their profile
* links them.
*/
final class UserConnectionCollection extends ResourceCollection
{
/**
* @param CursorPaginator<int, covariant User> $page
* @param array<int|string, array<string, string>> $bioProfiles
*/
public function __construct(private readonly CursorPaginator $page, private readonly array $bioProfiles)
{
parent::__construct($page);
}

/**
* @return array<int, array<string, mixed>>
*/
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());
}
}
31 changes: 30 additions & 1 deletion app/Queries/BioProfilesQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, User> $users
* @return array<int|string, array<string, string>>
*/
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<string>
*/
private function handles(User $user): array
{
return $this->mentions->handles("{$user->bio}\n{$user->company}");
}

/**
* @param list<string> $handles
* @return array<string, string>
*/
private function profiles(array $handles): array
{
if ($handles === []) {
return [];
}
Expand Down
20 changes: 18 additions & 2 deletions tests/Feature/Controllers/Api/UserFollowersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.']);
Expand Down Expand Up @@ -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);
});
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/Controllers/Api/UserFollowingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Queries/BioProfilesQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
});