mirror of
https://github.com/flarum/core.git
synced 2025-08-10 10:24:46 +02:00
Get rid of event subscribers that resolve services too early
Refs flarum/core#1578.
This commit is contained in:
@@ -9,8 +9,13 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use Flarum\Api\Serializer\PostSerializer;
|
||||||
|
use Flarum\Event\ConfigureNotificationTypes;
|
||||||
use Flarum\Extend;
|
use Flarum\Extend;
|
||||||
|
use Flarum\Likes\Event\PostWasLiked;
|
||||||
|
use Flarum\Likes\Event\PostWasUnliked;
|
||||||
use Flarum\Likes\Listener;
|
use Flarum\Likes\Listener;
|
||||||
|
use Flarum\Likes\Notification\PostLikedBlueprint;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
use Illuminate\Contracts\Events\Dispatcher;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@@ -24,6 +29,11 @@ return [
|
|||||||
function (Dispatcher $events) {
|
function (Dispatcher $events) {
|
||||||
$events->subscribe(Listener\AddPostLikesRelationship::class);
|
$events->subscribe(Listener\AddPostLikesRelationship::class);
|
||||||
$events->subscribe(Listener\SaveLikesToDatabase::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);
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
@@ -11,15 +11,9 @@
|
|||||||
|
|
||||||
namespace Flarum\Likes\Listener;
|
namespace Flarum\Likes\Listener;
|
||||||
|
|
||||||
use Flarum\Api\Serializer\PostSerializer;
|
|
||||||
use Flarum\Event\ConfigureNotificationTypes;
|
|
||||||
use Flarum\Likes\Event\PostWasLiked;
|
use Flarum\Likes\Event\PostWasLiked;
|
||||||
use Flarum\Likes\Event\PostWasUnliked;
|
|
||||||
use Flarum\Likes\Notification\PostLikedBlueprint;
|
use Flarum\Likes\Notification\PostLikedBlueprint;
|
||||||
use Flarum\Notification\NotificationSyncer;
|
use Flarum\Notification\NotificationSyncer;
|
||||||
use Flarum\Post\Post;
|
|
||||||
use Flarum\User\User;
|
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
|
||||||
|
|
||||||
class SendNotificationWhenPostIsLiked
|
class SendNotificationWhenPostIsLiked
|
||||||
{
|
{
|
||||||
@@ -36,51 +30,12 @@ class SendNotificationWhenPostIsLiked
|
|||||||
$this->notifications = $notifications;
|
$this->notifications = $notifications;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function handle(PostWasLiked $event)
|
||||||
* @param Dispatcher $events
|
|
||||||
*/
|
|
||||||
public function subscribe(Dispatcher $events)
|
|
||||||
{
|
{
|
||||||
$events->listen(ConfigureNotificationTypes::class, [$this, 'registerNotificationType']);
|
if ($event->post->user->id != $event->user->id) {
|
||||||
$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) {
|
|
||||||
$this->notifications->sync(
|
$this->notifications->sync(
|
||||||
new PostLikedBlueprint($post, $user),
|
new PostLikedBlueprint($event->post, $event->user),
|
||||||
$recipients
|
[$event->post->user]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
42
extensions/likes/src/Listener/SendNotificationWhenPostIsUnliked.php
Executable file
42
extensions/likes/src/Listener/SendNotificationWhenPostIsUnliked.php
Executable 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),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user