Skip to content
Draft
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, the people they follow, their mutuals, and the followers you know, 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()));
}
}
27 changes: 27 additions & 0 deletions app/Http/Controllers/Api/UserFollowersYouKnowController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Http\Requests\ListApiUserFollowersYouKnowRequest;
use App\Http\Resources\UserConnectionCollection;
use App\Models\User;
use App\Queries\BioProfilesQuery;
use App\Queries\FollowersYouKnowQuery;
use App\Queries\UserConnectionsQuery;
use Illuminate\Container\Attributes\CurrentUser;

final readonly class UserFollowersYouKnowController
{
/**
* A user's followers whom the signed-in user follows too, 30 a page as on the web, with the same flags and bio
* profiles as the followers list.
*/
public function index(ListApiUserFollowersYouKnowRequest $request, #[CurrentUser] User $currentUser, User $user, FollowersYouKnowQuery $followersYouKnow, UserConnectionsQuery $connections, BioProfilesQuery $bioProfiles): UserConnectionCollection
{
$page = $connections->builder($followersYouKnow->builder($user, $currentUser), $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()));
}
}
26 changes: 26 additions & 0 deletions app/Http/Controllers/Api/UserMutualsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Http\Requests\ListApiUserMutualsRequest;
use App\Http\Resources\UserConnectionCollection;
use App\Models\User;
use App\Queries\BioProfilesQuery;
use App\Queries\UserConnectionsQuery;
use Illuminate\Container\Attributes\CurrentUser;

final readonly class UserMutualsController
{
/**
* The people a user follows who follow them back, 30 a page as on the web, with the same flags and bio profiles as
* the followers list.
*/
public function index(ListApiUserMutualsRequest $request, #[CurrentUser] User $currentUser, User $user, UserConnectionsQuery $connections, BioProfilesQuery $bioProfiles): UserConnectionCollection
{
$page = $connections->builder($connections->mutuals($user), $currentUser)->cursorPaginate(30);

return new UserConnectionCollection($page, $bioProfiles->getMany($page->items()));
}
}
10 changes: 2 additions & 8 deletions app/Http/Controllers/UserFollowersYouKnowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use App\Queries\FollowersYouKnowQuery;
use App\Queries\UserConnectionsQuery;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Database\Eloquent\Builder;
use Inertia\Inertia;
use Inertia\Response;

Expand All @@ -21,13 +20,8 @@ public function index(ListUserFollowersYouKnowRequest $request, #[CurrentUser] U
return Inertia::render('user/connections', [
'user' => $user,
'type' => ConnectionType::FollowersYouKnow,
'loadedDigest' => Inertia::optional(fn (): array => ['users' => $connections->loadedDigest($followersYouKnow->builder($user, $currentUser), $request->loaded('users'))]),
'users' => Inertia::scroll(fn () => $followersYouKnow->builder($user, $currentUser)
->withExists([
'followers as is_followed' => fn (Builder $query): Builder => $query->whereKey($currentUser->id),
'following as follows_you' => fn (Builder $query): Builder => $query->whereKey($currentUser->id),
])
->cursorPaginate(30)),
'loadedDigest' => Inertia::optional(fn (): array => ['users' => $connections->loadedDigest($connections->builder($followersYouKnow->builder($user, $currentUser), $currentUser), $request->loaded('users'))]),
'users' => Inertia::scroll(fn () => $connections->builder($followersYouKnow->builder($user, $currentUser), $currentUser)->cursorPaginate(30)),
]);
}
}
17 changes: 2 additions & 15 deletions app/Http/Controllers/UserMutualsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use App\Models\User;
use App\Queries\UserConnectionsQuery;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Inertia\Inertia;
use Inertia\Response;

Expand All @@ -24,19 +22,8 @@ public function index(ListUserMutualsRequest $request, #[CurrentUser] User $curr
return Inertia::render('user/connections', [
'user' => $user,
'type' => ConnectionType::Mutuals,
'loadedDigest' => Inertia::optional(fn (): array => ['users' => $connections->loadedDigest($this->mutuals($user, $currentUser, $connections), $request->loaded('users'))]),
'users' => Inertia::scroll(fn () => $this->mutuals($user, $currentUser, $connections)->cursorPaginate(30)),
'loadedDigest' => Inertia::optional(fn (): array => ['users' => $connections->loadedDigest($connections->builder($connections->mutuals($user), $currentUser), $request->loaded('users'))]),
'users' => Inertia::scroll(fn () => $connections->builder($connections->mutuals($user), $currentUser)->cursorPaginate(30)),
]);
}

/**
* @return BelongsToMany<User, User>
*/
private function mutuals(User $user, User $viewer, UserConnectionsQuery $connections): BelongsToMany
{
return $connections->builder($user->following()
->whereExists(fn (QueryBuilder $query): QueryBuilder => $query->from('follows', 'follows_back')
->whereColumn('follows_back.follower_id', 'users.id')
->where('follows_back.followed_id', $user->id)), $viewer);
}
}
40 changes: 40 additions & 0 deletions app/Http/Requests/ListApiUserFollowersYouKnowRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Exceptions\ApiException;
use App\Http\Resources\UserResource;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;

final class ListApiUserFollowersYouKnowRequest extends FormRequest
{
/**
* The web shows neither of two people across a block the other's connections, so a signed request across one gets
* a code instead.
*/
public function authorize(): bool
{
$user = $this->route('user');
assert($user instanceof User);

$viewer = $this->user();
assert($viewer instanceof User);

$blockerId = $viewer->blockerIdWith($user);

throw_if($blockerId !== null, fn (): ApiException => ApiException::acrossBlock($user, blockedByViewer: $blockerId === $viewer->id, shown: UserResource::make($user)));

return true;
}

/**
* @return array{}
*/
public function rules(): array
{
return [];
}
}
40 changes: 40 additions & 0 deletions app/Http/Requests/ListApiUserMutualsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Exceptions\ApiException;
use App\Http\Resources\UserResource;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;

final class ListApiUserMutualsRequest extends FormRequest
{
/**
* The web shows neither of two people across a block the other's connections, so a signed request across one gets
* a code instead.
*/
public function authorize(): bool
{
$user = $this->route('user');
assert($user instanceof User);

$viewer = $this->user();
assert($viewer instanceof User);

$blockerId = $viewer->blockerIdWith($user);

throw_if($blockerId !== null, fn (): ApiException => ApiException::acrossBlock($user, blockedByViewer: $blockerId === $viewer->id, shown: UserResource::make($user)));

return true;
}

/**
* @return array{}
*/
public function rules(): array
{
return [];
}
}
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
6 changes: 2 additions & 4 deletions app/Queries/FollowersYouKnowQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public const int PREVIEW_COUNT = 3;

/**
* A user's followers whom the viewer follows too, as the web lists them: the newest follow of the user first.
* A user's followers whom the viewer follows too, for UserConnectionsQuery::builder() to order and flag.
*
* @return BelongsToMany<User, User>
*/
Expand All @@ -22,9 +22,7 @@ public function builder(User $user, User $viewer): BelongsToMany
$following = $viewer->following();

return $user->followers()
->whereIn('users.id', $following->newPivotQuery()->select($following->getRelatedPivotKeyName()))
->latest('follows.created_at')
->latest('users.id');
->whereIn('users.id', $following->newPivotQuery()->select($following->getRelatedPivotKeyName()));
}

/**
Expand Down
16 changes: 15 additions & 1 deletion app/Queries/UserConnectionsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Query\Builder as QueryBuilder;

final readonly class UserConnectionsQuery
{
/**
* A user's followers, or the people they follow, as the web lists them: the newest follow first, with whether the
* Any of a user's connection lists, as the web lists them: the newest follow first, with whether the
* viewer follows each one, and whether each one follows them.
*
* @param BelongsToMany<User, User> $connections
Expand All @@ -29,6 +30,19 @@ public function builder(BelongsToMany $connections, User $viewer): BelongsToMany
->latest('users.id');
}

/**
* The people a user follows who follow them back.
*
* @return BelongsToMany<User, User>
*/
public function mutuals(User $user): BelongsToMany
{
return $user->following()
->whereExists(fn (QueryBuilder $query): QueryBuilder => $query->from('follows', 'follows_back')
->whereColumn('follows_back.follower_id', 'users.id')
->where('follows_back.followed_id', $user->id));
}

/**
* Which of a list a page loaded, as a digest of the sorted ids of as many as it loaded from the start, so a page
* that may have missed follows changing can tell whether its loaded pages still hold the same, even when as many
Expand Down
Loading