Repository navigation
feat: tell followers about someone's first commit #1189
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,83 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Actions; | ||
|
|
||
| use App\Enums\CommitStatus; | ||
| use App\Enums\NotificationType; | ||
| use App\Models\Commit; | ||
| use App\Models\Notification; | ||
| use App\Models\User; | ||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Database\Query\Builder as QueryBuilder; | ||
| use Illuminate\Support\Str; | ||
|
|
||
| final readonly class CreateFirstCommitNotifications | ||
| { | ||
| /** | ||
| * 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, 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.user_id', 'commits.user_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', [...array_values($commit->mentions ?? []), ...array_filter([$commit->quotedCommit?->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); | ||
| } | ||
| } | ||
| } | ||
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
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
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
144 changes: 144 additions & 0 deletions
144
tests/Unit/Actions/CreateFirstCommitNotificationsTest.php
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,144 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Actions\CreateFirstCommitNotifications; | ||
| use App\Actions\CreateTomFriendship; | ||
| use App\Enums\NotificationType; | ||
| use App\Models\Commit; | ||
| use App\Models\Notification; | ||
| use App\Models\User; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| it("notifies the author's followers about their first commit", function (): void { | ||
| $author = User::factory()->create(); | ||
| $followers = User::factory()->count(2)->create(); | ||
| $author->followers()->attach($followers); | ||
| $commit = Commit::factory()->for($author)->create(); | ||
|
|
||
| resolve(CreateFirstCommitNotifications::class)->handle($commit); | ||
|
|
||
| foreach ($followers as $follower) { | ||
| $notification = $follower->notifications()->sole(); | ||
|
|
||
| expect($notification->type)->toBe(NotificationType::FirstCommit) | ||
| ->and($notification->actor_id)->toBe($author->id) | ||
| ->and($notification->commit_id)->toBe($commit->id); | ||
| } | ||
| }); | ||
|
|
||
| it('only notifies about the first commit', function (): void { | ||
| $author = User::factory()->create(); | ||
| $author->followers()->attach(User::factory()->create()); | ||
| Commit::factory()->for($author)->create(['created_at' => now()->subDay()]); | ||
| $second = Commit::factory()->for($author)->create(); | ||
|
|
||
| resolve(CreateFirstCommitNotifications::class)->handle($second); | ||
|
|
||
| expect(Notification::query()->count())->toBe(0); | ||
| }); | ||
|
|
||
| it('counts a first commit that was deleted since', function (): void { | ||
| $author = User::factory()->create(); | ||
| $author->followers()->attach(User::factory()->create()); | ||
| Commit::factory()->for($author)->create(['created_at' => now()->subDay(), 'deleted_at' => now()]); | ||
| $second = Commit::factory()->for($author)->create(); | ||
|
|
||
| resolve(CreateFirstCommitNotifications::class)->handle($second); | ||
|
|
||
| expect(Notification::query()->count())->toBe(0); | ||
| }); | ||
|
|
||
| it('waits for the first commit to be approved', function (): void { | ||
| $author = User::factory()->create(); | ||
| $author->followers()->attach(User::factory()->create()); | ||
|
|
||
| resolve(CreateFirstCommitNotifications::class)->handle( | ||
| Commit::factory()->for($author)->pending()->create(), | ||
| Commit::factory()->for($author)->rejected()->create(), | ||
| ); | ||
|
|
||
| expect(Notification::query()->count())->toBe(0); | ||
| }); | ||
|
|
||
| it('notifies about the first commit approved after a rejected one', function (): void { | ||
| $author = User::factory()->create(); | ||
| $follower = User::factory()->create(); | ||
| $author->followers()->attach($follower); | ||
| Commit::factory()->for($author)->rejected()->create(['created_at' => now()->subDay()]); | ||
| $commit = Commit::factory()->for($author)->create(); | ||
|
|
||
| resolve(CreateFirstCommitNotifications::class)->handle($commit); | ||
|
|
||
| expect($follower->notifications()->sole()->commit_id)->toBe($commit->id); | ||
| }); | ||
|
|
||
| it('does not count replies as a first commit', function (): void { | ||
| $author = User::factory()->create(); | ||
| $author->followers()->attach(User::factory()->create()); | ||
| $reply = Commit::factory()->for($author)->for(Commit::factory(), 'parent')->create(); | ||
|
|
||
| resolve(CreateFirstCommitNotifications::class)->handle($reply); | ||
|
|
||
| expect(Notification::query()->count())->toBe(0); | ||
| }); | ||
|
|
||
| it('looks nothing up for replies', function (): void { | ||
| $reply = Commit::factory()->for(Commit::factory(), 'parent')->create(); | ||
|
|
||
| DB::enableQueryLog(); | ||
| resolve(CreateFirstCommitNotifications::class)->handle($reply); | ||
|
|
||
| expect(DB::getQueryLog())->toBeEmpty(); | ||
| }); | ||
|
|
||
| it('notifies only once, about the earliest of first commits approved together', function (): void { | ||
| $author = User::factory()->create(); | ||
| $follower = User::factory()->create(); | ||
| $author->followers()->attach($follower); | ||
| $first = Commit::factory()->for($author)->create(['created_at' => now()->subMinute()]); | ||
| $second = Commit::factory()->for($author)->create(); | ||
|
|
||
| $action = resolve(CreateFirstCommitNotifications::class); | ||
| $action->handle($second, $first); | ||
| $action->handle($first); | ||
|
|
||
| expect($follower->notifications()->sole()->commit_id)->toBe($first->id); | ||
| }); | ||
|
|
||
| it('leaves out bots, and followers who mute the author or are across a block', function (): void { | ||
| $author = User::factory()->create(); | ||
| resolve(CreateTomFriendship::class)->handle($author); | ||
| [$follower, $muter, $blocker, $blocked] = User::factory()->count(4)->create()->all(); | ||
| $author->followers()->attach([$follower->id, $muter->id, $blocker->id, $blocked->id]); | ||
| $muter->muting()->attach($author); | ||
| $blocker->blocking()->attach($author); | ||
| $author->blocking()->attach($blocked); | ||
|
|
||
| resolve(CreateFirstCommitNotifications::class)->handle(Commit::factory()->for($author)->create()); | ||
|
|
||
| expect(Notification::query()->pluck('recipient_id')->all())->toBe([$follower->id]); | ||
| }); | ||
|
|
||
| it('does not notify about the first commit of a bot', function (): void { | ||
| $author = User::factory()->create(['github_id' => CreateTomFriendship::GITHUB_ID]); | ||
| $author->followers()->attach(User::factory()->create()); | ||
|
|
||
| resolve(CreateFirstCommitNotifications::class)->handle(Commit::factory()->for($author)->create()); | ||
|
|
||
| expect(Notification::query()->count())->toBe(0); | ||
| }); | ||
|
|
||
| it('leaves out followers it mentions or quotes, who already hear about it', function (): void { | ||
| $author = User::factory()->create(); | ||
| [$follower, $mentioned, $quoted] = User::factory()->count(3)->create()->all(); | ||
| $author->followers()->attach([$follower->id, $mentioned->id, $quoted->id]); | ||
| $commit = Commit::factory()->for($author)->create([ | ||
| 'mentions' => [$mentioned->handle => $mentioned->id], | ||
| 'quoted_commit_id' => Commit::factory()->for($quoted)->create()->id, | ||
| ]); | ||
|
|
||
| resolve(CreateFirstCommitNotifications::class)->handle($commit); | ||
|
|
||
| expect(Notification::query()->pluck('recipient_id')->all())->toBe([$follower->id]); | ||
| }); |
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
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.