Limit notifications to one per user when dispatching events

This commit is contained in:
Toby Zerner 2015-05-19 11:24:43 +09:30
parent 53357ad56f
commit 26fad11f6f
2 changed files with 35 additions and 9 deletions

View File

@ -4,6 +4,7 @@ use Flarum\Core\Events\PostWillBeSaved;
use Flarum\Core\Repositories\DiscussionRepositoryInterface as DiscussionRepository; use Flarum\Core\Repositories\DiscussionRepositoryInterface as DiscussionRepository;
use Flarum\Core\Models\CommentPost; use Flarum\Core\Models\CommentPost;
use Flarum\Core\Support\DispatchesEvents; use Flarum\Core\Support\DispatchesEvents;
use Flarum\Core\Notifications\Notifier;
class PostReplyCommandHandler class PostReplyCommandHandler
{ {
@ -11,9 +12,12 @@ class PostReplyCommandHandler
protected $discussions; protected $discussions;
public function __construct(DiscussionRepository $discussions) protected $notifier;
public function __construct(DiscussionRepository $discussions, Notifier $notifier)
{ {
$this->discussions = $discussions; $this->discussions = $discussions;
$this->notifier = $notifier;
} }
public function handle($command) public function handle($command)
@ -41,7 +45,10 @@ class PostReplyCommandHandler
event(new PostWillBeSaved($post, $command)); event(new PostWillBeSaved($post, $command));
$post->save(); $post->save();
$this->dispatchEventsFor($post);
$this->notifier->onePerUser(function () use ($post) {
$this->dispatchEventsFor($post);
});
return $post; return $post;
} }

View File

@ -5,6 +5,7 @@ use Flarum\Core\Notifications\Senders\RetractableSender;
use Flarum\Core\Models\Notification as NotificationModel; use Flarum\Core\Models\Notification as NotificationModel;
use Flarum\Core\Models\User; use Flarum\Core\Models\User;
use Illuminate\Container\Container; use Illuminate\Container\Container;
use Closure;
class Notifier class Notifier
{ {
@ -12,6 +13,10 @@ class Notifier
protected $types = []; protected $types = [];
protected $onePerUser = false;
protected $sentTo = [];
protected $container; protected $container;
public function __construct(Container $container) public function __construct(Container $container)
@ -21,14 +26,18 @@ class Notifier
public function send(Notification $notification, array $users) public function send(Notification $notification, array $users)
{ {
foreach ($this->methods as $method => $sender) { foreach ($users as $user) {
$sender = $this->container->make($sender); if ($this->onePerUser && in_array($user->id, $this->sentTo)) {
continue;
}
if ($sender::compatibleWith($notification)) { foreach ($this->methods as $method => $sender) {
foreach ($users as $user) { $sender = $this->container->make($sender);
if ($user->shouldNotify($notification::getType(), $method)) {
$sender->send($notification, $user); 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; return $this->types;
} }
public function onePerUser(Closure $callback)
{
$this->sentTo = [];
$this->onePerUser = true;
$callback();
$this->onePerUser = false;
}
} }