Skip to content

Commit

Permalink
Get rid of event subscribers that resolve services too early
Browse files Browse the repository at this point in the history
Refs flarum/core#1578.
  • Loading branch information
franzliedke committed Dec 16, 2018
1 parent 16a441c commit 7ac73b8
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 144 deletions.
38 changes: 33 additions & 5 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,11 +34,26 @@
->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');
}
Expand Down
14 changes: 1 addition & 13 deletions src/Listener/AddDiscussionSubscriptionAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,10 @@

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)) {
Expand Down
41 changes: 41 additions & 0 deletions src/Listener/DeleteNotificationWhenPostIsHiddenOrDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\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));
}
}
25 changes: 1 addition & 24 deletions src/Listener/FilterDiscussionListBySubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)`?
Expand Down
24 changes: 1 addition & 23 deletions src/Listener/FollowAfterReply.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
37 changes: 37 additions & 0 deletions src/Listener/RestoreNotificationWhenPostIsRestored.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\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));
}
}
14 changes: 1 addition & 13 deletions src/Listener/SaveSubscriptionToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,12 @@

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;
Expand Down
68 changes: 2 additions & 66 deletions src/Listener/SendNotificationWhenReplyIsPosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -37,31 +30,7 @@ public function __construct(NotificationSyncer $notifications)
$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;
Expand All @@ -73,41 +42,8 @@ public function whenPosted(Posted $event)
->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);
}
}

0 comments on commit 7ac73b8

Please sign in to comment.