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:47:41 +01:00
parent ffc78a0218
commit 8aab6111ec
3 changed files with 32 additions and 27 deletions

View File

@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/
use Flarum\Api\Event\Serializing;
use Flarum\Event\ConfigureModelDates;
use Flarum\Extend;
use Flarum\Flags\Api\Controller\CreateFlagController;
use Flarum\Flags\Api\Controller\DeleteFlagsController;
@ -30,7 +32,9 @@ return [
->delete('/posts/{id}/flags', 'flags.delete', DeleteFlagsController::class),
function (Dispatcher $events) {
$events->subscribe(Listener\AddFlagsApi::class);
$events->listen(ConfigureModelDates::class, Listener\AddFlagsApiDates::class);
$events->listen(Serializing::class, Listener\AddFlagsApiAttributes::class);
$events->subscribe(Listener\AddPostFlagsRelationship::class);
},
];

View File

@ -15,13 +15,11 @@ use Flarum\Api\Event\Serializing;
use Flarum\Api\Serializer\CurrentUserSerializer;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Event\ConfigureModelDates;
use Flarum\Flags\Flag;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
class AddFlagsApi
class AddFlagsApiAttributes
{
/**
* @var SettingsRepositoryInterface
@ -36,29 +34,7 @@ class AddFlagsApi
$this->settings = $settings;
}
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureModelDates::class, [$this, 'configureModelDates']);
$events->listen(Serializing::class, [$this, 'prepareApiAttributes']);
}
/**
* @param ConfigureModelDates $event
*/
public function configureModelDates(ConfigureModelDates $event)
{
if ($event->isModel(User::class)) {
$event->dates[] = 'read_flags_at';
}
}
/**
* @param Serializing $event
*/
public function prepareApiAttributes(Serializing $event)
public function handle(Serializing $event)
{
if ($event->isSerializer(ForumSerializer::class)) {
$event->attributes['canViewFlags'] = $event->actor->hasPermissionLike('discussion.viewFlags');

View File

@ -0,0 +1,25 @@
<?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\Flags\Listener;
use Flarum\Event\ConfigureModelDates;
use Flarum\User\User;
class AddFlagsApiDates
{
public function handle(ConfigureModelDates $event)
{
if ($event->isModel(User::class)) {
$event->dates[] = 'read_flags_at';
}
}
}