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 15, 2018
1 parent 6bfa978 commit 2f3417b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
6 changes: 5 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +32,9 @@
->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);
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
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
Expand All @@ -36,29 +34,7 @@ public function __construct(SettingsRepositoryInterface $settings)
$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');
Expand Down
25 changes: 25 additions & 0 deletions src/Listener/AddFlagsApiDates.php
Original file line number Diff line number Diff line change
@@ -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';
}
}
}

0 comments on commit 2f3417b

Please sign in to comment.