diff --git a/extensions/pusher/extend.php b/extensions/pusher/extend.php index 497bc48c1..7d5f67d96 100644 --- a/extensions/pusher/extend.php +++ b/extensions/pusher/extend.php @@ -9,10 +9,10 @@ use Flarum\Api\Event\Serializing; use Flarum\Extend; -use Flarum\Notification\Event\Sending; use Flarum\Post\Event\Posted; use Flarum\Pusher\Api\Controller\AuthController; use Flarum\Pusher\Listener; +use Flarum\Pusher\PusherNotificationDriver; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; @@ -30,6 +30,9 @@ return [ new Extend\Locales(__DIR__.'/locale'), + (new Extend\Notification()) + ->driver('pusher', PusherNotificationDriver::class), + function (Dispatcher $events, Container $container) { $container->bind(Pusher::class, function ($app) { $settings = $app->make(SettingsRepositoryInterface::class); @@ -49,7 +52,6 @@ return [ }); $events->listen(Posted::class, Listener\PushNewPost::class); - $events->listen(Sending::class, Listener\PushNotification::class); $events->listen(Serializing::class, Listener\AddPusherApi::class); }, ]; diff --git a/extensions/pusher/src/Listener/PushNotification.php b/extensions/pusher/src/Listener/PushNotification.php deleted file mode 100644 index 51d6c5e88..000000000 --- a/extensions/pusher/src/Listener/PushNotification.php +++ /dev/null @@ -1,37 +0,0 @@ -pusher = $pusher; - } - - public function handle(Sending $event) - { - $blueprint = $event->blueprint; - - foreach ($event->users as $user) { - if ($user->shouldAlert($blueprint::getType())) { - $this->pusher->trigger('private-user'.$user->id, 'notification', null); - } - } - } -} diff --git a/extensions/pusher/src/PusherNotificationDriver.php b/extensions/pusher/src/PusherNotificationDriver.php new file mode 100644 index 000000000..6fb15ee4c --- /dev/null +++ b/extensions/pusher/src/PusherNotificationDriver.php @@ -0,0 +1,45 @@ +queue = $queue; + } + + /** + * {@inheritDoc} + */ + public function send(BlueprintInterface $blueprint, array $users): void + { + if (count($users)) { + $this->queue->push(new SendPusherNotificationsJob($blueprint, $users)); + } + } + + /** + * {@inheritDoc} + */ + public function registerType(string $blueprintClass, array $driversEnabledByDefault): void + { + // ... + } +} diff --git a/extensions/pusher/src/SendPusherNotificationsJob.php b/extensions/pusher/src/SendPusherNotificationsJob.php new file mode 100644 index 000000000..def83ca28 --- /dev/null +++ b/extensions/pusher/src/SendPusherNotificationsJob.php @@ -0,0 +1,43 @@ +blueprint = $blueprint; + $this->recipients = $recipients; + } + + public function handle(Pusher $pusher) + { + foreach ($this->recipients as $user) { + if ($user->shouldAlert($this->blueprint::getType())) { + $pusher->trigger('private-user'.$user->id, 'notification', null); + } + } + } +}