From a565c00c6da1ab75af20abbc63e0afcf670c7a40 Mon Sep 17 00:00:00 2001 From: IanM Date: Mon, 29 Jan 2024 11:41:20 +0000 Subject: [PATCH] chore: cleanup extend, create serializer mutators --- extend.php | 25 ++++--------------------- src/Api/AddDiscussionAttributes.php | 17 +++++++++++++++++ src/Api/AddForumAttributes.php | 15 +++++++++++++++ src/Api/AddPostAttributes.php | 13 +++++++++++++ src/Listeners/ClearFormatterCache.php | 19 +++++++++++++++++++ 5 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 src/Api/AddDiscussionAttributes.php create mode 100644 src/Api/AddForumAttributes.php create mode 100644 src/Api/AddPostAttributes.php create mode 100644 src/Listeners/ClearFormatterCache.php diff --git a/extend.php b/extend.php index 6d4be766..e39fd879 100755 --- a/extend.php +++ b/extend.php @@ -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']), diff --git a/src/Api/AddDiscussionAttributes.php b/src/Api/AddDiscussionAttributes.php new file mode 100644 index 00000000..d01c5fb2 --- /dev/null +++ b/src/Api/AddDiscussionAttributes.php @@ -0,0 +1,17 @@ +polls()->exists(); + $attributes['canStartPoll'] = $serializer->getActor()->can('polls.start', $discussion); + + return $attributes; + } +} diff --git a/src/Api/AddForumAttributes.php b/src/Api/AddForumAttributes.php new file mode 100644 index 00000000..4614490b --- /dev/null +++ b/src/Api/AddForumAttributes.php @@ -0,0 +1,15 @@ +getActor()->can('discussion.polls.start'); + + return $attributes; + } +} diff --git a/src/Api/AddPostAttributes.php b/src/Api/AddPostAttributes.php new file mode 100644 index 00000000..b9f0306b --- /dev/null +++ b/src/Api/AddPostAttributes.php @@ -0,0 +1,13 @@ +getActor()->can('startPoll', $post); + + return $attributes; + } +} diff --git a/src/Listeners/ClearFormatterCache.php b/src/Listeners/ClearFormatterCache.php new file mode 100644 index 00000000..b427edb1 --- /dev/null +++ b/src/Listeners/ClearFormatterCache.php @@ -0,0 +1,19 @@ +settings as $key => $value) { + if ($key === 'fof-polls.optionsColorBlend') { + resolve('fof-user-bio.formatter')->flush(); + + return; + } + } + } +}