Skip to content
Open
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
3 changes: 2 additions & 1 deletion app/Actions/CreateStarNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ public function __construct(private ProfileRecipientsQuery $recipients)

/**
* A star notifies the commit's author, every member of it for an organization's, once while it holds:
* unstarring removes the notification.
* unstarring removes the notification. Nobody hears from people they mute.
*/
public function handle(User $stargazer, Commit $commit): void
{
/** @var Collection<int, object{user_id: string, organization_id: string|null}> $recipients */
$recipients = DB::query()->fromSub($this->recipients->builder(), 'recipients')
->where('profile_id', $commit->author_id)
->where('user_id', '<>', $stargazer->id)
->whereNotIn('user_id', $stargazer->mutedBy()->newPivotQuery()->select('muter_id'))
->get(['user_id', 'organization_id']);

Notification::query()->insertOrIgnore($recipients->map(fn (object $recipient): array => [
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/Actions/CreateStarNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use App\Actions\CreateStarNotification;
use App\Enums\NotificationType;
use App\Enums\OrganizationRole;
use App\Models\Commit;
use App\Models\Notification;
use App\Models\Organization;
use App\Models\User;

it('notifies the author of the starred commit', function (): void {
Expand All @@ -28,6 +31,31 @@
expect($commit->user->notifications()->exists())->toBeFalse();
});

it('does not notify an author who muted the stargazer', function (): void {
$user = User::factory()->create();
$commit = Commit::factory()->create();

$commit->user->muting()->attach($user);

resolve(CreateStarNotification::class)->handle($user, $commit);

expect($commit->user->notifications()->exists())->toBeFalse();
});

it('does not notify the members of an organization who muted the stargazer', function (): void {
$user = User::factory()->create();
$organization = Organization::factory()->create();
[$muter, $member] = User::factory()->count(2)->create()->all();
$organization->members()->attach([$muter->id, $member->id], ['role' => OrganizationRole::Member]);
$commit = Commit::factory()->for($member)->create(['author_id' => $organization->id]);

$muter->muting()->attach($user);

resolve(CreateStarNotification::class)->handle($user, $commit);

expect(Notification::query()->pluck('recipient_id')->all())->toBe([$member->id]);
});

it('is idempotent', function (): void {
$user = User::factory()->create();
$commit = Commit::factory()->create();
Expand Down