diff --git a/extensions/pusher/extend.php b/extensions/pusher/extend.php index e7802a6a8..993f3d213 100644 --- a/extensions/pusher/extend.php +++ b/extensions/pusher/extend.php @@ -11,8 +11,12 @@ 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\Settings\SettingsRepositoryInterface; +use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; return [ @@ -26,8 +30,26 @@ return [ (new Extend\Routes('api')) ->post('/pusher/auth', 'pusher.auth', AuthController::class), - function (Dispatcher $events) { + function (Dispatcher $events, Container $container) { + $container->bind(Pusher::class, function ($app) { + $settings = $app->make(SettingsRepositoryInterface::class); + + $options = []; + + if ($cluster = $settings->get('flarum-pusher.app_cluster')) { + $options['cluster'] = $cluster; + } + + return new Pusher( + $settings->get('flarum-pusher.app_key'), + $settings->get('flarum-pusher.app_secret'), + $settings->get('flarum-pusher.app_id'), + $options + ); + }); + + $events->listen(Posted::class, Listener\PushNewPost::class); + $events->listen(Sending::class, Listener\PushNotification::class); $events->listen(Serializing::class, Listener\AddPusherApi::class); - $events->subscribe(Listener\PushNewPosts::class); }, ]; diff --git a/extensions/pusher/src/Listener/PushNewPost.php b/extensions/pusher/src/Listener/PushNewPost.php new file mode 100644 index 000000000..dbbb7a99b --- /dev/null +++ b/extensions/pusher/src/Listener/PushNewPost.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Pusher\Listener; + +use Flarum\Post\Event\Posted; +use Flarum\User\Guest; +use Pusher; + +class PushNewPost +{ + /** + * @var Pusher + */ + protected $pusher; + + public function __construct(Pusher $pusher) + { + $this->pusher = $pusher; + } + + public function handle(Posted $event) + { + if ($event->post->isVisibleTo(new Guest)) { + $this->pusher->trigger('public', 'newPost', [ + 'postId' => $event->post->id, + 'discussionId' => $event->post->discussion->id, + 'tagIds' => $event->post->discussion->tags()->pluck('id') + ]); + } + } +} diff --git a/extensions/pusher/src/Listener/PushNewPosts.php b/extensions/pusher/src/Listener/PushNewPosts.php deleted file mode 100644 index 4c889f636..000000000 --- a/extensions/pusher/src/Listener/PushNewPosts.php +++ /dev/null @@ -1,94 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Pusher\Listener; - -use Flarum\Notification\Event\Sending; -use Flarum\Post\Event\Posted; -use Flarum\Settings\SettingsRepositoryInterface; -use Flarum\User\Guest; -use Illuminate\Contracts\Events\Dispatcher; -use Pusher; - -class PushNewPosts -{ - /** - * @var SettingsRepositoryInterface - */ - protected $settings; - - /** - * @param SettingsRepositoryInterface $settings - */ - public function __construct(SettingsRepositoryInterface $settings) - { - $this->settings = $settings; - } - - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen(Posted::class, [$this, 'pushNewPost']); - $events->listen(Sending::class, [$this, 'pushNotification']); - } - - /** - * @param Posted $event - */ - public function pushNewPost(Posted $event) - { - if ($event->post->isVisibleTo(new Guest)) { - $pusher = $this->getPusher(); - - $pusher->trigger('public', 'newPost', [ - 'postId' => $event->post->id, - 'discussionId' => $event->post->discussion->id, - 'tagIds' => $event->post->discussion->tags()->pluck('id') - ]); - } - } - - /** - * @param Sending $event - */ - public function pushNotification(Sending $event) - { - $pusher = $this->getPusher(); - $blueprint = $event->blueprint; - - foreach ($event->users as $user) { - if ($user->shouldAlert($blueprint::getType())) { - $pusher->trigger('private-user'.$user->id, 'notification', null); - } - } - } - - /** - * @return Pusher - */ - protected function getPusher() - { - $options = []; - - if ($cluster = $this->settings->get('flarum-pusher.app_cluster')) { - $options['cluster'] = $cluster; - } - - return new Pusher( - $this->settings->get('flarum-pusher.app_key'), - $this->settings->get('flarum-pusher.app_secret'), - $this->settings->get('flarum-pusher.app_id'), - $options - ); - } -} diff --git a/extensions/pusher/src/Listener/PushNotification.php b/extensions/pusher/src/Listener/PushNotification.php new file mode 100644 index 000000000..f0f4ac627 --- /dev/null +++ b/extensions/pusher/src/Listener/PushNotification.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Pusher\Listener; + +use Flarum\Notification\Event\Sending; +use Pusher; + +class PushNotification +{ + /** + * @var Pusher + */ + protected $pusher; + + public function __construct(Pusher $pusher) + { + $this->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); + } + } + } +}