From dfadc8f5fc125e10745b55ef3e598045367692d6 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sun, 16 Dec 2018 15:00:02 +0100 Subject: [PATCH] Get rid of event subscribers that resolve services too early Refs flarum/core#1578. --- extensions/subscriptions/extend.php | 38 +++++++++-- .../AddDiscussionSubscriptionAttribute.php | 14 +--- ...eNotificationWhenPostIsHiddenOrDeleted.php | 41 +++++++++++ .../FilterDiscussionListBySubscription.php | 25 +------ .../src/Listener/FollowAfterReply.php | 24 +------ .../RestoreNotificationWhenPostIsRestored.php | 37 ++++++++++ .../Listener/SaveSubscriptionToDatabase.php | 14 +--- .../SendNotificationWhenReplyIsPosted.php | 68 +------------------ 8 files changed, 117 insertions(+), 144 deletions(-) create mode 100755 extensions/subscriptions/src/Listener/DeleteNotificationWhenPostIsHiddenOrDeleted.php create mode 100755 extensions/subscriptions/src/Listener/RestoreNotificationWhenPostIsRestored.php diff --git a/extensions/subscriptions/extend.php b/extensions/subscriptions/extend.php index 637ef5485..20aefbdb6 100644 --- a/extensions/subscriptions/extend.php +++ b/extensions/subscriptions/extend.php @@ -9,8 +9,21 @@ * file that was distributed with this source code. */ +use Flarum\Api\Event\Serializing; +use Flarum\Api\Serializer\BasicDiscussionSerializer; +use Flarum\Discussion\Event\Saving; +use Flarum\Discussion\Event\Searching; +use Flarum\Event\ConfigureDiscussionGambits; +use Flarum\Event\ConfigureNotificationTypes; +use Flarum\Event\ConfigureUserPreferences; use Flarum\Extend; +use Flarum\Post\Event\Deleted; +use Flarum\Post\Event\Hidden; +use Flarum\Post\Event\Posted; +use Flarum\Post\Event\Restored; +use Flarum\Subscriptions\Gambit\SubscriptionGambit; use Flarum\Subscriptions\Listener; +use Flarum\Subscriptions\Notification\NewPostBlueprint; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\View\Factory; @@ -21,11 +34,26 @@ return [ ->route('/following', 'following'), function (Dispatcher $events, Factory $views) { - $events->subscribe(Listener\AddDiscussionSubscriptionAttribute::class); - $events->subscribe(Listener\FilterDiscussionListBySubscription::class); - $events->subscribe(Listener\SaveSubscriptionToDatabase::class); - $events->subscribe(Listener\SendNotificationWhenReplyIsPosted::class); - $events->subscribe(Listener\FollowAfterReply::class); + $events->listen(Serializing::class, Listener\AddDiscussionSubscriptionAttribute::class); + $events->listen(Saving::class, Listener\SaveSubscriptionToDatabase::class); + + $events->listen(ConfigureDiscussionGambits::class, function (ConfigureDiscussionGambits $event) { + $event->gambits->add(SubscriptionGambit::class); + }); + $events->listen(Searching::class, Listener\FilterDiscussionListBySubscription::class); + + $events->listen(ConfigureNotificationTypes::class, function (ConfigureNotificationTypes $event) { + $event->add(NewPostBlueprint::class, BasicDiscussionSerializer::class, ['alert', 'email']); + }); + $events->listen(Posted::class, Listener\SendNotificationWhenReplyIsPosted::class); + $events->listen(Hidden::class, Listener\DeleteNotificationWhenPostIsHiddenOrDeleted::class); + $events->listen(Restored::class, Listener\RestoreNotificationWhenPostIsRestored::class); + $events->listen(Deleted::class, Listener\DeleteNotificationWhenPostIsHiddenOrDeleted::class); + + $events->listen(ConfigureUserPreferences::class, function (ConfigureUserPreferences $event) { + $event->add('followAfterReply', 'boolval', false); + }); + $events->listen(Posted::class, Listener\FollowAfterReply::class); $views->addNamespace('flarum-subscriptions', __DIR__.'/views'); } diff --git a/extensions/subscriptions/src/Listener/AddDiscussionSubscriptionAttribute.php b/extensions/subscriptions/src/Listener/AddDiscussionSubscriptionAttribute.php index 19c20697f..cced46c0b 100755 --- a/extensions/subscriptions/src/Listener/AddDiscussionSubscriptionAttribute.php +++ b/extensions/subscriptions/src/Listener/AddDiscussionSubscriptionAttribute.php @@ -13,22 +13,10 @@ namespace Flarum\Subscriptions\Listener; use Flarum\Api\Event\Serializing; use Flarum\Api\Serializer\DiscussionSerializer; -use Illuminate\Contracts\Events\Dispatcher; class AddDiscussionSubscriptionAttribute { - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen(Serializing::class, [$this, 'addAttributes']); - } - - /** - * @param Serializing $event - */ - public function addAttributes(Serializing $event) + public function handle(Serializing $event) { if ($event->isSerializer(DiscussionSerializer::class) && ($state = $event->model->state)) { diff --git a/extensions/subscriptions/src/Listener/DeleteNotificationWhenPostIsHiddenOrDeleted.php b/extensions/subscriptions/src/Listener/DeleteNotificationWhenPostIsHiddenOrDeleted.php new file mode 100755 index 000000000..b0414ba5a --- /dev/null +++ b/extensions/subscriptions/src/Listener/DeleteNotificationWhenPostIsHiddenOrDeleted.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Subscriptions\Listener; + +use Flarum\Notification\NotificationSyncer; +use Flarum\Post\Event\Deleted; +use Flarum\Post\Event\Hidden; +use Flarum\Subscriptions\Notification\NewPostBlueprint; + +class DeleteNotificationWhenPostIsHiddenOrDeleted +{ + /** + * @var NotificationSyncer + */ + protected $notifications; + + /** + * @param NotificationSyncer $notifications + */ + public function __construct(NotificationSyncer $notifications) + { + $this->notifications = $notifications; + } + + /** + * @param Hidden|Deleted $event + */ + public function handle($event) + { + $this->notifications->delete(new NewPostBlueprint($event->post)); + } +} diff --git a/extensions/subscriptions/src/Listener/FilterDiscussionListBySubscription.php b/extensions/subscriptions/src/Listener/FilterDiscussionListBySubscription.php index 172bdf1dc..812c59653 100755 --- a/extensions/subscriptions/src/Listener/FilterDiscussionListBySubscription.php +++ b/extensions/subscriptions/src/Listener/FilterDiscussionListBySubscription.php @@ -12,33 +12,10 @@ namespace Flarum\Subscriptions\Listener; use Flarum\Discussion\Event\Searching; -use Flarum\Event\ConfigureDiscussionGambits; -use Flarum\Subscriptions\Gambit\SubscriptionGambit; -use Illuminate\Contracts\Events\Dispatcher; class FilterDiscussionListBySubscription { - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen(ConfigureDiscussionGambits::class, [$this, 'addGambit']); - $events->listen(Searching::class, [$this, 'filterIgnored']); - } - - /** - * @param ConfigureDiscussionGambits $event - */ - public function addGambit(ConfigureDiscussionGambits $event) - { - $event->gambits->add(SubscriptionGambit::class); - } - - /** - * @param Searching $event - */ - public function filterIgnored(Searching $event) + public function handle(Searching $event) { if (! $event->criteria->query) { // might be better as `id IN (subquery)`? diff --git a/extensions/subscriptions/src/Listener/FollowAfterReply.php b/extensions/subscriptions/src/Listener/FollowAfterReply.php index 2c952ae88..78b6affe8 100644 --- a/extensions/subscriptions/src/Listener/FollowAfterReply.php +++ b/extensions/subscriptions/src/Listener/FollowAfterReply.php @@ -11,36 +11,14 @@ namespace Flarum\Subscriptions\Listener; -use Flarum\Event\ConfigureUserPreferences; use Flarum\Post\Event\Posted; use Flarum\User\AssertPermissionTrait; -use Illuminate\Contracts\Events\Dispatcher; class FollowAfterReply { use AssertPermissionTrait; - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen(ConfigureUserPreferences::class, [$this, 'addUserPreference']); - $events->listen(Posted::class, [$this, 'whenPosted']); - } - - /** - * @param ConfigureUserPreferences $event - */ - public function addUserPreference(ConfigureUserPreferences $event) - { - $event->add('followAfterReply', 'boolval', false); - } - - /** - * @param Posted $event - */ - public function whenPosted(Posted $event) + public function handle(Posted $event) { $actor = $event->actor; diff --git a/extensions/subscriptions/src/Listener/RestoreNotificationWhenPostIsRestored.php b/extensions/subscriptions/src/Listener/RestoreNotificationWhenPostIsRestored.php new file mode 100755 index 000000000..61c4f6db8 --- /dev/null +++ b/extensions/subscriptions/src/Listener/RestoreNotificationWhenPostIsRestored.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Subscriptions\Listener; + +use Flarum\Notification\NotificationSyncer; +use Flarum\Post\Event\Restored; +use Flarum\Subscriptions\Notification\NewPostBlueprint; + +class RestoreNotificationWhenPostIsRestored +{ + /** + * @var NotificationSyncer + */ + protected $notifications; + + /** + * @param NotificationSyncer $notifications + */ + public function __construct(NotificationSyncer $notifications) + { + $this->notifications = $notifications; + } + + public function handle(Restored $event) + { + $this->notifications->restore(new NewPostBlueprint($event->post)); + } +} diff --git a/extensions/subscriptions/src/Listener/SaveSubscriptionToDatabase.php b/extensions/subscriptions/src/Listener/SaveSubscriptionToDatabase.php index 28478cd5c..e40ffafe4 100755 --- a/extensions/subscriptions/src/Listener/SaveSubscriptionToDatabase.php +++ b/extensions/subscriptions/src/Listener/SaveSubscriptionToDatabase.php @@ -13,24 +13,12 @@ namespace Flarum\Subscriptions\Listener; use Flarum\Discussion\Event\Saving; use Flarum\User\AssertPermissionTrait; -use Illuminate\Contracts\Events\Dispatcher; class SaveSubscriptionToDatabase { use AssertPermissionTrait; - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen(Saving::class, [$this, 'whenSaving']); - } - - /** - * @param Saving $event - */ - public function whenSaving(Saving $event) + public function handle(Saving $event) { $discussion = $event->discussion; $data = $event->data; diff --git a/extensions/subscriptions/src/Listener/SendNotificationWhenReplyIsPosted.php b/extensions/subscriptions/src/Listener/SendNotificationWhenReplyIsPosted.php index 50ef67515..a2895ab5e 100755 --- a/extensions/subscriptions/src/Listener/SendNotificationWhenReplyIsPosted.php +++ b/extensions/subscriptions/src/Listener/SendNotificationWhenReplyIsPosted.php @@ -11,16 +11,9 @@ namespace Flarum\Subscriptions\Listener; -use Flarum\Api\Serializer\BasicDiscussionSerializer; -use Flarum\Event\ConfigureNotificationTypes; use Flarum\Notification\NotificationSyncer; -use Flarum\Post\Event\Deleted; -use Flarum\Post\Event\Hidden; use Flarum\Post\Event\Posted; -use Flarum\Post\Event\Restored; -use Flarum\Post\Post; use Flarum\Subscriptions\Notification\NewPostBlueprint; -use Illuminate\Contracts\Events\Dispatcher; class SendNotificationWhenReplyIsPosted { @@ -37,31 +30,7 @@ class SendNotificationWhenReplyIsPosted $this->notifications = $notifications; } - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen(ConfigureNotificationTypes::class, [$this, 'addNotificationType']); - - $events->listen(Posted::class, [$this, 'whenPosted']); - $events->listen(Hidden::class, [$this, 'whenHidden']); - $events->listen(Restored::class, [$this, 'whenRestored']); - $events->listen(Deleted::class, [$this, 'whenDeleted']); - } - - /** - * @param ConfigureNotificationTypes $event - */ - public function addNotificationType(ConfigureNotificationTypes $event) - { - $event->add(NewPostBlueprint::class, BasicDiscussionSerializer::class, ['alert', 'email']); - } - - /** - * @param Posted $event - */ - public function whenPosted(Posted $event) + public function handle(Posted $event) { $post = $event->post; $discussion = $post->discussion; @@ -73,41 +42,8 @@ class SendNotificationWhenReplyIsPosted ->get(); $this->notifications->sync( - $this->getNotification($event->post), + new NewPostBlueprint($event->post), $notify->all() ); } - - /** - * @param Hidden $event - */ - public function whenHidden(Hidden $event) - { - $this->notifications->delete($this->getNotification($event->post)); - } - - /** - * @param Restored $event - */ - public function whenRestored(Restored $event) - { - $this->notifications->restore($this->getNotification($event->post)); - } - - /** - * @param Deleted $event - */ - public function whenDeleted(Deleted $event) - { - $this->notifications->delete($this->getNotification($event->post)); - } - - /** - * @param Post $post - * @return NewPostBlueprint - */ - protected function getNotification(Post $post) - { - return new NewPostBlueprint($post); - } }