mirror of
https://github.com/flarum/core.git
synced 2025-08-06 08:27:42 +02:00
Get rid of event subscribers that resolve services too early
Refs flarum/core#1578.
This commit is contained in:
@@ -11,8 +11,12 @@
|
|||||||
|
|
||||||
use Flarum\Api\Event\Serializing;
|
use Flarum\Api\Event\Serializing;
|
||||||
use Flarum\Extend;
|
use Flarum\Extend;
|
||||||
|
use Flarum\Notification\Event\Sending;
|
||||||
|
use Flarum\Post\Event\Posted;
|
||||||
use Flarum\Pusher\Api\Controller\AuthController;
|
use Flarum\Pusher\Api\Controller\AuthController;
|
||||||
use Flarum\Pusher\Listener;
|
use Flarum\Pusher\Listener;
|
||||||
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
use Illuminate\Contracts\Events\Dispatcher;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@@ -26,8 +30,26 @@ return [
|
|||||||
(new Extend\Routes('api'))
|
(new Extend\Routes('api'))
|
||||||
->post('/pusher/auth', 'pusher.auth', AuthController::class),
|
->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->listen(Serializing::class, Listener\AddPusherApi::class);
|
||||||
$events->subscribe(Listener\PushNewPosts::class);
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
40
extensions/pusher/src/Listener/PushNewPost.php
Normal file
40
extensions/pusher/src/Listener/PushNewPost.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||||
|
*
|
||||||
|
* 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')
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,94 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of Flarum.
|
|
||||||
*
|
|
||||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
39
extensions/pusher/src/Listener/PushNotification.php
Normal file
39
extensions/pusher/src/Listener/PushNotification.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||||
|
*
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user