mirror of
https://github.com/flarum/core.git
synced 2025-10-21 03:36:05 +02:00
Limit notifications to one per user when dispatching events
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user