diff --git a/framework/core/src/Core/Handlers/Commands/PostReplyCommandHandler.php b/framework/core/src/Core/Handlers/Commands/PostReplyCommandHandler.php index 0d496483a..68b39b80d 100644 --- a/framework/core/src/Core/Handlers/Commands/PostReplyCommandHandler.php +++ b/framework/core/src/Core/Handlers/Commands/PostReplyCommandHandler.php @@ -4,6 +4,7 @@ use Flarum\Core\Events\PostWillBeSaved; use Flarum\Core\Repositories\DiscussionRepositoryInterface as DiscussionRepository; use Flarum\Core\Models\CommentPost; use Flarum\Core\Support\DispatchesEvents; +use Flarum\Core\Notifications\Notifier; class PostReplyCommandHandler { @@ -11,9 +12,12 @@ class PostReplyCommandHandler protected $discussions; - public function __construct(DiscussionRepository $discussions) + protected $notifier; + + public function __construct(DiscussionRepository $discussions, Notifier $notifier) { $this->discussions = $discussions; + $this->notifier = $notifier; } public function handle($command) @@ -41,7 +45,10 @@ class PostReplyCommandHandler event(new PostWillBeSaved($post, $command)); $post->save(); - $this->dispatchEventsFor($post); + + $this->notifier->onePerUser(function () use ($post) { + $this->dispatchEventsFor($post); + }); return $post; } diff --git a/framework/core/src/Core/Notifications/Notifier.php b/framework/core/src/Core/Notifications/Notifier.php index 401040896..7e692f095 100644 --- a/framework/core/src/Core/Notifications/Notifier.php +++ b/framework/core/src/Core/Notifications/Notifier.php @@ -5,6 +5,7 @@ use Flarum\Core\Notifications\Senders\RetractableSender; use Flarum\Core\Models\Notification as NotificationModel; use Flarum\Core\Models\User; use Illuminate\Container\Container; +use Closure; class Notifier { @@ -12,6 +13,10 @@ class Notifier protected $types = []; + protected $onePerUser = false; + + protected $sentTo = []; + protected $container; public function __construct(Container $container) @@ -21,14 +26,18 @@ class Notifier public function send(Notification $notification, array $users) { - foreach ($this->methods as $method => $sender) { - $sender = $this->container->make($sender); + foreach ($users as $user) { + if ($this->onePerUser && in_array($user->id, $this->sentTo)) { + continue; + } - if ($sender::compatibleWith($notification)) { - foreach ($users as $user) { - if ($user->shouldNotify($notification::getType(), $method)) { - $sender->send($notification, $user); - } + foreach ($this->methods as $method => $sender) { + $sender = $this->container->make($sender); + + if ($sender::compatibleWith($notification) && + $user->shouldNotify($notification::getType(), $method)) { + $sender->send($notification, $user); + $this->sentTo[] = $user->id; } } } @@ -64,4 +73,14 @@ class Notifier { return $this->types; } + + public function onePerUser(Closure $callback) + { + $this->sentTo = []; + $this->onePerUser = true; + + $callback(); + + $this->onePerUser = false; + } }