Get rid of event subscribers that resolve services too early

Refs flarum/core#1578.
This commit is contained in:
Franz Liedke 2018-12-16 15:05:58 +01:00
parent 9302014dfa
commit de1ff99356
4 changed files with 59 additions and 53 deletions

View File

@ -9,9 +9,16 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Extend; use Flarum\Extend;
use Flarum\Suspend\Access; use Flarum\Suspend\Access;
use Flarum\Suspend\Event\Suspended;
use Flarum\Suspend\Event\Unsuspended;
use Flarum\Suspend\Listener; use Flarum\Suspend\Listener;
use Flarum\Suspend\Notification\UserSuspendedBlueprint;
use Flarum\Suspend\Notification\UserUnsuspendedBlueprint;
use Flarum\User\Event\Saving;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
return [ return [
@ -26,8 +33,15 @@ return [
function (Dispatcher $events) { function (Dispatcher $events) {
$events->subscribe(Listener\AddUserSuspendAttributes::class); $events->subscribe(Listener\AddUserSuspendAttributes::class);
$events->subscribe(Listener\RevokeAccessFromSuspendedUsers::class); $events->subscribe(Listener\RevokeAccessFromSuspendedUsers::class);
$events->subscribe(Listener\SaveSuspensionToDatabase::class);
$events->subscribe(Listener\SendNotificationWhenUserIsSuspended::class); $events->listen(Saving::class, Listener\SaveSuspensionToDatabase::class);
$events->listen(ConfigureNotificationTypes::class, function (ConfigureNotificationTypes $event) {
$event->add(UserSuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email']);
$event->add(UserUnsuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email']);
});
$events->listen(Suspended::class, Listener\SendNotificationWhenUserIsSuspended::class);
$events->listen(Unsuspended::class, Listener\SendNotificationWhenUserIsUnsuspended::class);
$events->subscribe(Access\UserPolicy::class); $events->subscribe(Access\UserPolicy::class);
} }

View File

@ -29,6 +29,7 @@ class SaveSuspensionToDatabase
* @var SuspendValidator * @var SuspendValidator
*/ */
protected $validator; protected $validator;
/** /**
* @var Dispatcher * @var Dispatcher
*/ */
@ -44,18 +45,7 @@ class SaveSuspensionToDatabase
$this->events = $events; $this->events = $events;
} }
/** public function handle(Saving $event)
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(Saving::class, [$this, 'whenSavingUser']);
}
/**
* @param Saving $event
*/
public function whenSavingUser(Saving $event)
{ {
$attributes = array_get($event->data, 'attributes', []); $attributes = array_get($event->data, 'attributes', []);

View File

@ -11,14 +11,9 @@
namespace Flarum\Suspend\Listener; namespace Flarum\Suspend\Listener;
use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Notification\NotificationSyncer; use Flarum\Notification\NotificationSyncer;
use Flarum\Suspend\Event\Suspended; use Flarum\Suspend\Event\Suspended;
use Flarum\Suspend\Event\Unsuspended;
use Flarum\Suspend\Notification\UserSuspendedBlueprint; use Flarum\Suspend\Notification\UserSuspendedBlueprint;
use Flarum\Suspend\Notification\UserUnsuspendedBlueprint;
use Illuminate\Contracts\Events\Dispatcher;
class SendNotificationWhenUserIsSuspended class SendNotificationWhenUserIsSuspended
{ {
@ -35,44 +30,11 @@ class SendNotificationWhenUserIsSuspended
$this->notifications = $notifications; $this->notifications = $notifications;
} }
/** public function handle(Suspended $event)
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureNotificationTypes::class, [$this, 'registerNotificationType']);
$events->listen(Suspended::class, [$this, 'whenSuspended']);
$events->listen(Unsuspended::class, [$this, 'whenUnsuspended']);
}
/**
* @param ConfigureNotificationTypes $event
*/
public function registerNotificationType(ConfigureNotificationTypes $event)
{
$event->add(UserSuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email']);
$event->add(UserUnsuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email']);
}
/**
* @param Suspended $event
*/
public function whenSuspended(Suspended $event)
{ {
$this->notifications->sync( $this->notifications->sync(
new UserSuspendedBlueprint($event->user, $event->actor), new UserSuspendedBlueprint($event->user, $event->actor),
[$event->user] [$event->user]
); );
} }
/**
* @param Unsuspended $event
*/
public function whenUnsuspended(Unsuspended $event)
{
$this->notifications->sync(
new UserUnsuspendedBlueprint($event->user, $event->actor),
[$event->user]
);
}
} }

View 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\Suspend\Listener;
use Flarum\Notification\NotificationSyncer;
use Flarum\Suspend\Event\Unsuspended;
use Flarum\Suspend\Notification\UserUnsuspendedBlueprint;
class SendNotificationWhenUserIsUnsuspended
{
/**
* @var NotificationSyncer
*/
protected $notifications;
/**
* @param NotificationSyncer $notifications
*/
public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}
public function handle(Unsuspended $event)
{
$this->notifications->sync(
new UserUnsuspendedBlueprint($event->user, $event->actor),
[$event->user]
);
}
}