Get rid of event subscribers that resolve services too early

Refs flarum/core#1578.
This commit is contained in:
Franz Liedke 2018-12-15 16:58:00 +01:00
parent 6ac090c0cb
commit c0f08ff907
3 changed files with 57 additions and 50 deletions

View File

@ -9,8 +9,13 @@
* file that was distributed with this source code.
*/
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Extend;
use Flarum\Likes\Event\PostWasLiked;
use Flarum\Likes\Event\PostWasUnliked;
use Flarum\Likes\Listener;
use Flarum\Likes\Notification\PostLikedBlueprint;
use Illuminate\Contracts\Events\Dispatcher;
return [
@ -24,6 +29,11 @@ return [
function (Dispatcher $events) {
$events->subscribe(Listener\AddPostLikesRelationship::class);
$events->subscribe(Listener\SaveLikesToDatabase::class);
$events->subscribe(Listener\SendNotificationWhenPostIsLiked::class);
$events->listen(ConfigureNotificationTypes::class, function (ConfigureNotificationTypes $event) {
$event->add(PostLikedBlueprint::class, PostSerializer::class, ['alert']);
});
$events->listen(PostWasLiked::class, Listener\SendNotificationWhenPostIsLiked::class);
$events->listen(PostWasUnliked::class, Listener\SendNotificationWhenPostIsUnliked::class);
},
];

View File

@ -11,15 +11,9 @@
namespace Flarum\Likes\Listener;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Likes\Event\PostWasLiked;
use Flarum\Likes\Event\PostWasUnliked;
use Flarum\Likes\Notification\PostLikedBlueprint;
use Flarum\Notification\NotificationSyncer;
use Flarum\Post\Post;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
class SendNotificationWhenPostIsLiked
{
@ -36,51 +30,12 @@ class SendNotificationWhenPostIsLiked
$this->notifications = $notifications;
}
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
public function handle(PostWasLiked $event)
{
$events->listen(ConfigureNotificationTypes::class, [$this, 'registerNotificationType']);
$events->listen(PostWasLiked::class, [$this, 'whenPostWasLiked']);
$events->listen(PostWasUnliked::class, [$this, 'whenPostWasUnliked']);
}
/**
* @param ConfigureNotificationTypes $event
*/
public function registerNotificationType(ConfigureNotificationTypes $event)
{
$event->add(PostLikedBlueprint::class, PostSerializer::class, ['alert']);
}
/**
* @param PostWasLiked $event
*/
public function whenPostWasLiked(PostWasLiked $event)
{
$this->sync($event->post, $event->user, [$event->post->user]);
}
/**
* @param PostWasUnliked $event
*/
public function whenPostWasUnliked(PostWasUnliked $event)
{
$this->sync($event->post, $event->user, []);
}
/**
* @param Post $post
* @param User $user
* @param array $recipients
*/
public function sync(Post $post, User $user, array $recipients)
{
if ($post->user->id != $user->id) {
if ($event->post->user->id != $event->user->id) {
$this->notifications->sync(
new PostLikedBlueprint($post, $user),
$recipients
new PostLikedBlueprint($event->post, $event->user),
[$event->post->user]
);
}
}

View File

@ -0,0 +1,42 @@
<?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\Likes\Listener;
use Flarum\Likes\Event\PostWasUnliked;
use Flarum\Likes\Notification\PostLikedBlueprint;
use Flarum\Notification\NotificationSyncer;
class SendNotificationWhenPostIsUnliked
{
/**
* @var NotificationSyncer
*/
protected $notifications;
/**
* @param NotificationSyncer $notifications
*/
public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}
public function handle(PostWasUnliked $event)
{
if ($event->post->user->id != $event->user->id) {
$this->notifications->sync(
new PostLikedBlueprint($event->post, $event->user),
[]
);
}
}
}