Skip to content

Commit

Permalink
chore: cleanup extend, create serializer mutators (#84)
Browse files Browse the repository at this point in the history
* chore: cleanup extend, create serializer mutators

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Jan 29, 2024
1 parent 9acbd74 commit f460073
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 21 deletions.
25 changes: 4 additions & 21 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,17 @@

(new Extend\Event())
->listen(PostSaving::class, Listeners\SavePollsToDatabase::class)
->listen(SettingsSaved::class, function (SettingsSaved $event) {
foreach ($event->settings as $key => $value) {
if ($key === 'fof-polls.optionsColorBlend') {
resolve('fof-user-bio.formatter')->flush();

return;
}
}
}),
->listen(SettingsSaved::class, Listeners\ClearFormatterCache::class),

(new Extend\ApiSerializer(DiscussionSerializer::class))
->attribute('hasPoll', function (DiscussionSerializer $serializer, Discussion $discussion): bool {
return $discussion->polls()->exists();
})
->attribute('canStartPoll', function (DiscussionSerializer $serializer, Discussion $discussion): bool {
return $serializer->getActor()->can('polls.start', $discussion);
}),
->attributes(Api\AddDiscussionAttributes::class),

(new Extend\ApiSerializer(PostSerializer::class))
->hasMany('polls', PollSerializer::class)
->attribute('canStartPoll', function (PostSerializer $serializer, Post $post): bool {
return $serializer->getActor()->can('startPoll', $post);
}),
->attributes(Api\AddPostAttributes::class),

(new Extend\ApiSerializer(ForumSerializer::class))
->attribute('canStartPolls', function (ForumSerializer $serializer): bool {
return $serializer->getActor()->can('discussion.polls.start');
}),
->attributes(Api\AddForumAttributes::class),

(new Extend\ApiController(Controller\ListDiscussionsController::class))
->addOptionalInclude(['firstPost.polls']),
Expand Down
26 changes: 26 additions & 0 deletions src/Api/AddDiscussionAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of fof/polls.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Polls\Api;

use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Discussion\Discussion;

class AddDiscussionAttributes
{
public function __invoke(DiscussionSerializer $serializer, Discussion $discussion, array $attributes): array
{
$attributes['hasPoll'] = $discussion->polls()->exists();
$attributes['canStartPoll'] = $serializer->getActor()->can('polls.start', $discussion);

return $attributes;
}
}
24 changes: 24 additions & 0 deletions src/Api/AddForumAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of fof/polls.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Polls\Api;

use Flarum\Api\Serializer\ForumSerializer;

class AddForumAttributes
{
public function __invoke(ForumSerializer $serializer, array $model, array $attributes): array
{
$attributes['canStartPolls'] = $serializer->getActor()->can('discussion.polls.start');

return $attributes;
}
}
22 changes: 22 additions & 0 deletions src/Api/AddPostAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of fof/polls.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Polls\Api;

class AddPostAttributes
{
public function __invoke($serializer, $post, $attributes)
{
$attributes['canStartPoll'] = $serializer->getActor()->can('startPoll', $post);

return $attributes;
}
}
28 changes: 28 additions & 0 deletions src/Listeners/ClearFormatterCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of fof/polls.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Polls\Listeners;

use Flarum\Settings\Event\Saved;

class ClearFormatterCache
{
public function handle(Saved $event): void
{
foreach ($event->settings as $key => $value) {
if ($key === 'fof-polls.optionsColorBlend') {
resolve('fof-user-bio.formatter')->flush();

return;
}
}
}
}

0 comments on commit f460073

Please sign in to comment.