Repository navigation
Expand file tree
/
Copy pathCreateFirstCommitNotifications.php
More file actions
101 lines (88 loc) · 4.12 KB
/
Copy pathCreateFirstCommitNotifications.php
File metadata and controls
101 lines (88 loc) · 4.12 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
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Enums\CommitStatus;
use App\Enums\NotificationType;
use App\Enums\TopicType;
use App\Enums\UserUpdate;
use App\Events\UsersUpdated;
use App\Models\Commit;
use App\Models\Notification;
use App\Models\User;
use App\Queries\ProfileRecipientsQuery;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
final readonly class CreateFirstCommitNotifications
{
public function __construct(private ProfileRecipientsQuery $recipients)
{
//
}
/**
* Tells the author's followers about their first commit, so it gets an answer: a newcomer's followers mostly
* came from GitHub, and never chose to follow them here. Only once the commit is approved, and only for the
* author's first top-level commit that was ever approved. Never from or to bots, nor to the people it mentions
* or quotes, or the members of the organizations it does, who already hear about it. Nobody hears from people they mute or from across a block. Safe to call
* more than once.
*/
public function handle(Commit ...$commits): void
{
// A reply is never a first commit, so replies, most of what gets committed, cost no query.
$topLevelIds = array_values(array_map(
fn (Commit $commit): string => $commit->id,
array_filter($commits, fn (Commit $commit): bool => $commit->parent_id === null),
));
if ($topLevelIds === []) {
return;
}
$firsts = Commit::query()
->whereKey($topLevelIds)
->where('status', CommitStatus::Approved)
->whereNull('parent_id')
->whereHas('user', fn (Builder $user): Builder => $user->whereNotIn('github_id', User::BOT_GITHUB_IDS))
// Deleted commits count too: whoever committed before isn't making their first commit.
->whereNotExists(fn (QueryBuilder $earlier): QueryBuilder => $earlier
->from('commits as earlier')
->whereColumn('earlier.author_id', 'commits.author_id')
->whereNull('earlier.parent_id')
->where('earlier.status', CommitStatus::Approved)
->whereColumn('earlier.created_at', '<', 'commits.created_at'))
->with(['user', 'quotedCommit'])
->get();
$notifications = [];
foreach ($firsts as $commit) {
$author = $commit->user;
assert($author instanceof User);
$followerIds = $author->followers()
->whereNotIn('users.github_id', User::BOT_GITHUB_IDS)
->whereNotIn('users.id', $author->mutedBy()->newPivotQuery()->select('muter_id'))
->whereNotIn('users.id', $author->idsAcrossBlocks())
->whereNotIn('users.id', DB::query()->fromSub($this->recipients->builder(), 'recipients')
->whereIn('profile_id', [
...array_values($commit->mentions ?? []),
...array_filter([$commit->quotedCommit?->author_id]),
...DB::table('commit_topics')->where('commit_id', $commit->id)->where('topic_type', TopicType::Organization)->pluck('topic_id'),
])
->select('user_id'))
->pluck('users.id');
foreach ($followerIds as $followerId) {
$notifications[] = [
'id' => Str::uuid7()->toString(),
'recipient_id' => $followerId,
'actor_id' => $author->id,
'type' => NotificationType::FirstCommit->value,
'commit_id' => $commit->id,
'key' => 'first_commit:'.$author->id,
'created_at' => now(),
'updated_at' => now(),
];
}
}
foreach (array_chunk($notifications, 500) as $chunk) {
Notification::query()->insertOrIgnore($chunk);
}
event(new UsersUpdated(array_column($notifications, 'recipient_id'), UserUpdate::Notifications));
}
}