Repository navigation
feat: let API clients look up GitHub profiles and wait to follow them #1557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WendellAdriel
wants to merge
5
commits into
dev-null-sh:production
Choose a base branch
from
WendellAdriel:feat/api-profile-lookups
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
407c265
refactor: move pending follow queries into their own class
WendellAdriel 9890d4e
feat: let API clients look up GitHub profiles and wait to follow them
WendellAdriel e9919fd
docs: mention profile lookups and pending follows in the signed API
WendellAdriel 3c5359f
fix: answer 422 when a profile lookup handle isn't a string
WendellAdriel 59daf7b
feat: tell API clients how many commits mention an unclaimed developer
WendellAdriel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers\Api; | ||
|
|
||
| use App\Actions\CreatePendingFollow; | ||
| use App\Actions\DeletePendingFollow; | ||
| use App\Http\Requests\CreateApiPendingFollowRequest; | ||
| use App\Http\Requests\DeleteApiPendingFollowRequest; | ||
| use App\Models\Profile; | ||
| use App\Models\User; | ||
| use Illuminate\Container\Attributes\CurrentUser; | ||
| use Illuminate\Http\Response; | ||
|
|
||
| /** | ||
| * Following a developer who isn't on /dev/null yet waits for them to sign up. | ||
| */ | ||
| final readonly class PendingFollowController | ||
| { | ||
| public function store(CreateApiPendingFollowRequest $request, #[CurrentUser] User $user, Profile $profile, CreatePendingFollow $action): Response | ||
| { | ||
| $action->handle($user, $profile); | ||
|
|
||
| return response()->noContent(); | ||
| } | ||
|
|
||
| public function destroy(DeleteApiPendingFollowRequest $request, #[CurrentUser] User $user, Profile $profile, DeletePendingFollow $action): Response | ||
| { | ||
| $action->handle($user, $profile); | ||
|
|
||
| return response()->noContent(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers\Api; | ||
|
|
||
| use App\Actions\CreateProfileLookup; | ||
| use App\Http\Requests\CreateApiProfileLookupRequest; | ||
| use App\Http\Requests\ShowApiProfileLookupRequest; | ||
| use App\Queries\ProfileLookupQuery; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Response; | ||
|
|
||
| /** | ||
| * A handle nobody on /dev/null has may still be someone's on GitHub. The app looks it up, then polls where the lookup | ||
| * stands until GitHub answers, as the web's lookup page does. | ||
| */ | ||
| final readonly class ProfileLookupController | ||
| { | ||
| public function show(ShowApiProfileLookupRequest $request, ProfileLookupQuery $lookups): JsonResponse | ||
| { | ||
| $lookup = $lookups->get($request->handle()); | ||
|
|
||
| return response()->json(['status' => $lookup['status'], 'resumes_at' => $lookup['resumesAt']]); | ||
| } | ||
|
|
||
| public function store(CreateApiProfileLookupRequest $request, CreateProfileLookup $action): Response | ||
| { | ||
| $action->handle($request->handle()); | ||
|
|
||
| return response()->noContent(202); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers\Api; | ||
|
|
||
| use App\Http\Requests\ShowApiUnclaimedProfileRequest; | ||
| use App\Models\Profile; | ||
| use App\Models\User; | ||
| use App\Queries\PendingFollowsQuery; | ||
| use App\Queries\UnclaimedProfileMentionCountQuery; | ||
| use Illuminate\Container\Attributes\CurrentUser; | ||
| use Illuminate\Http\JsonResponse; | ||
|
|
||
| final readonly class UnclaimedProfileController | ||
| { | ||
| /** | ||
| * A developer looked up from GitHub who isn't on /dev/null yet, as their page on the web shows them, with how many | ||
| * members wait to follow them and how many commits mention them. A signed request also gets whether its user waits, | ||
| * and who they follow that does. | ||
| */ | ||
| public function show(ShowApiUnclaimedProfileRequest $request, #[CurrentUser] ?User $user, PendingFollowsQuery $pendingFollows, UnclaimedProfileMentionCountQuery $mentions): JsonResponse | ||
| { | ||
| $profile = $request->profile(); | ||
| assert($profile instanceof Profile); | ||
|
|
||
| return response()->json([ | ||
| 'data' => $profile->only(['id', 'handle', 'name', 'avatar_url', 'bio']), | ||
| 'waiting_count' => $pendingFollows->count($profile), | ||
| 'mention_count' => $mentions->get($profile), | ||
| 'is_waiting' => $user instanceof User && $pendingFollows->isWaiting($profile, $user), | ||
| 'waiting_you_follow' => $user instanceof User ? $pendingFollows->youFollow($profile, $user) : [], | ||
| ]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Models\Profile; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class CreateApiPendingFollowRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Only a developer who isn't on /dev/null yet is waited for: once they are, members follow them. | ||
| */ | ||
| public function authorize(): Response | ||
| { | ||
| $profile = $this->route('profile'); | ||
| assert($profile instanceof Profile); | ||
|
|
||
| return $profile->isUnclaimed() ? Response::allow() : Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return []; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use Closure; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
| use Illuminate\Support\Facades\Cache; | ||
| use Illuminate\Support\Str; | ||
| use Illuminate\Validation\Rule; | ||
|
|
||
| final class CreateApiProfileLookupRequest extends FormRequest | ||
| { | ||
| /** | ||
| * @return array<string, array<int, mixed>> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| 'handle' => [ | ||
| 'bail', | ||
| 'required', | ||
| 'string', | ||
| 'regex:/^[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38}$/', | ||
| Rule::unique('profiles', 'handle'), | ||
| // GitHub said there's no such account a moment ago, so it isn't asked again for a day... | ||
| function (string $attribute, string $value, Closure $fail): void { | ||
| if (Cache::has('github-profiles:not-found:'.Str::lower($value))) { | ||
| $fail(__('GitHub has no @:handle.', ['handle' => $value])); | ||
| } | ||
| }, | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function handle(): string | ||
| { | ||
| return $this->string('handle')->value(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Models\Profile; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class DeleteApiPendingFollowRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Only a developer who isn't on /dev/null yet is waited for: once they are, members follow them. | ||
| */ | ||
| public function authorize(): Response | ||
| { | ||
| $profile = $this->route('profile'); | ||
| assert($profile instanceof Profile); | ||
|
|
||
| return $profile->isUnclaimed() ? Response::allow() : Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return []; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Models\Profile; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
| use Illuminate\Support\Facades\Cache; | ||
| use Illuminate\Support\Str; | ||
|
|
||
| final class ShowApiProfileLookupRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Only a handle GitHub could have can be looked up. Someone who deleted their account here left for good, so their | ||
| * handle is missing, like one GitHub could not have. So is a handle a profile has, as a lookup that found it does. | ||
| */ | ||
| public function authorize(): Response | ||
| { | ||
| $handle = Str::lower($this->handle()); | ||
|
|
||
| return preg_match('/^[a-z0-9](?:[a-z0-9]|-(?=[a-z0-9])){0,38}$/', $handle) === 1 | ||
| && Cache::get("github-profiles:not-found:{$handle}") !== 'deleted' | ||
| && Profile::query()->where('handle', $this->handle())->doesntExist() | ||
| ? Response::allow() | ||
| : Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| public function handle(): string | ||
| { | ||
| $handle = $this->route('handle'); | ||
| assert(is_string($handle)); | ||
|
|
||
| return $handle; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Models\Profile; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class ShowApiUnclaimedProfileRequest extends FormRequest | ||
| { | ||
| private ?Profile $profile = null; | ||
|
|
||
| public function authorize(): Response | ||
| { | ||
| return $this->profile()?->isUnclaimed() === true ? Response::allow() : Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * Only developers who already have a page, whatever the case of the handle, so looking one up never asks GitHub. | ||
| */ | ||
| public function profile(): ?Profile | ||
| { | ||
| $handle = $this->route('handle'); | ||
| assert(is_string($handle)); | ||
|
|
||
| return $this->profile ??= Profile::query()->firstWhere('handle', $handle); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.