Skip to content

Commit f466cf7

Browse files
committed
Don't send notifications to repliers
1 parent 8eecb7f commit f466cf7

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

app/Listeners/SendNewReplyNotification.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44

55
use App\Events\ReplyWasCreated;
66
use App\Notifications\NewReply;
7+
use App\User;
78

89
class SendNewReplyNotification
910
{
10-
public function handle(ReplyWasCreated $event)
11+
public function handle(ReplyWasCreated $event): void
1112
{
1213
foreach ($event->reply->replyAble()->subscriptions() as $subscription) {
13-
$subscription->user()->notify(new NewReply());
14+
if ($this->replyAuthorDoesNotMatchSubscriber($event->reply->author(), $subscription)) {
15+
$subscription->user()->notify(new NewReply());
16+
}
1417
}
1518
}
19+
20+
private function replyAuthorDoesNotMatchSubscriber(User $author, $subscription): bool
21+
{
22+
return ! $author->matches($subscription->user());
23+
}
1624
}

tests/Feature/SubscriptionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ public function thread_authors_do_not_receive_a_notification_for_a_thread_they_c
5656
Notification::assertNotSentTo($author, NewReply::class);
5757
}
5858

59+
/** @test */
60+
public function reply_authors_do_not_receive_a_notification_for_a_thread_they_are_subscribed_to()
61+
{
62+
Notification::fake();
63+
64+
$thread = factory(Thread::class)->create();
65+
$author = factory(User::class)->create();
66+
factory(Subscription::class)->create(['user_id' => $author->id(), 'subscriptionable_id' => $thread->id()]);
67+
68+
$this->dispatch(new CreateReply($this->faker->text, $this->faker->ipv4, $author, $thread));
69+
70+
Notification::assertNotSentTo($author, NewReply::class);
71+
}
72+
5973
/** @test */
6074
public function users_are_automatically_subscribed_to_a_thread_after_replying_to_it()
6175
{

0 commit comments

Comments
 (0)