mirror of
https://github.com/flarum/framework.git
synced 2024-12-04 08:13:39 +08:00
Limit notifications to one per user when dispatching events
This commit is contained in:
parent
53357ad56f
commit
26fad11f6f
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user