Repository navigation
Expand file tree
/
Copy pathCreateUserGitHubFollows.php
More file actions
117 lines (96 loc) · 5.38 KB
/
Copy pathCreateUserGitHubFollows.php
File metadata and controls
117 lines (96 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Contracts\Services\GitHubProvider;
use App\Enums\ProfileType;
use App\Enums\UserUpdate;
use App\Events\ProfilesUpdated;
use App\Events\UsersUpdated;
use App\Models\Profile;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
/**
* Brings a user's GitHub network over, once: they follow the people and the organizations they follow on GitHub, and
* the people following them on GitHub follow them here. The organizations not on /dev/null yet are kept, so the user
* follows them once they are. It only ever adds follows, and never runs twice, so an unfollow sticks. Organizations are
* marked as imported apart, so the users imported before organizations could be followed get them on another run.
*/
final readonly class CreateUserGitHubFollows
{
/**
* Up to 1,000 accounts each way, so a popular account can't use up the rate limit on its own.
*/
public const int MaxPages = 10;
public function __construct(
private GitHubProvider $github,
private CreateFollowNotification $createFollowNotification,
) {
//
}
public function handle(User $user, bool $notify = true): void
{
$importsPeople = $user->github_follows_imported_at === null;
$importsOrganizations = $user->github_organization_follows_imported_at === null;
if (! $importsPeople && ! $importsOrganizations) {
return;
}
$login = $this->github->login($user->github_id);
$followingIds = $login === null ? ['users' => [], 'organizations' => []] : $this->github->followingIds($login, self::MaxPages);
$following = $importsPeople ? $this->users($user, $followingIds['users']) : new Collection();
$followers = $importsPeople && $login !== null ? $this->users($user, $this->github->followerIds($login, self::MaxPages)) : new Collection();
$organizationIds = collect($followingIds['organizations'])->unique()->values();
DB::transaction(function () use ($user, $notify, $following, $followers, $organizationIds, $importsPeople, $importsOrganizations): void {
$followedIds = $user->following()->syncWithoutDetaching($following->modelKeys())['attached'];
$followerIds = $user->followers()->syncWithoutDetaching($followers->modelKeys())['attached'];
if ($notify) {
foreach ($following->only($followedIds) as $followed) {
$this->createFollowNotification->handle($user, $followed);
}
foreach ($followers->only($followerIds) as $follower) {
$this->createFollowNotification->handle($follower, $user);
}
}
$followedOrganizationIds = [];
if ($importsOrganizations) {
// Waits for an organization being created from GitHub right now, which follows the pending rows it finds
// (UpdateOrCreateOrganization), so the follow is made on one side or the other.
DB::select('select pg_advisory_xact_lock(hashtextextended(?, 0))', [UpdateOrCreateOrganization::FollowsLock]);
$organizations = Profile::query()->where('type', ProfileType::Organization)->whereIn('github_id', $organizationIds)->get();
$followedOrganizationIds = $user->followedOrganizations()->syncWithoutDetaching($organizations->modelKeys())['attached'];
DB::table('pending_follows')->insertOrIgnore($organizationIds
->reject(fn (string $id): bool => $organizations->contains('github_id', $id))
->map(fn (string $id): array => ['user_id' => $user->id, 'github_id' => $id, 'created_at' => now()])
->all());
}
$user->update(array_filter([
'github_follows_imported_at' => $importsPeople ? now() : null,
'github_organization_follows_imported_at' => $importsOrganizations ? now() : null,
]));
// Even importing nobody completes it, so the onboarding stops waiting either way.
event(new UsersUpdated([$user->id], UserUpdate::Onboarding));
event(new UsersUpdated([$user->id], UserUpdate::Live));
event(new UsersUpdated($followers->only($followerIds)->modelKeys(), UserUpdate::Live));
// What following each one by one tells, for the follows actually made: counts and lists of everyone on
// either side, and who each new follower sees.
if ($followedIds !== [] || $followerIds !== [] || $followedOrganizationIds !== []) {
$people = [$user->id, ...$followedIds, ...$followerIds];
event(new UsersUpdated($people, UserUpdate::Account));
event(new UsersUpdated([...($followedIds !== [] || $followedOrganizationIds !== [] ? [$user->id] : []), ...$followerIds], UserUpdate::Relationships));
event(new ProfilesUpdated([...$people, ...$followedOrganizationIds], changesConnections: true));
}
});
}
/**
* @param list<string> $githubIds
* @return Collection<int, User>
*/
private function users(User $user, array $githubIds): Collection
{
return User::query()
->whereIn('github_id', $githubIds)
->whereKeyNot($user->id)
->whereNotIn('id', $user->idsAcrossBlocks())
->get();
}
}