Skip to content

Commit

Permalink
chore: cleanup extend, create serializer mutators
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jan 29, 2024
1 parent 9acbd74 commit a565c00
Show file tree
Hide file tree
Showing 5 changed files with 68 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
17 changes: 17 additions & 0 deletions src/Api/AddDiscussionAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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;
}
}
15 changes: 15 additions & 0 deletions src/Api/AddForumAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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;
}
}
13 changes: 13 additions & 0 deletions src/Api/AddPostAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace FoF\Polls\Api;

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

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

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 a565c00

Please sign in to comment.