From 9cd61c7b00201ad7ff2aeb1326764bbb1838d58c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ca=C5=82ka?= Date: Fri, 10 Nov 2023 13:37:53 +0100 Subject: [PATCH] fix(approval): post approved event triggered when not approving Co-authored-by: SychO9 --- .../approval/src/Listener/ApproveContent.php | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/extensions/approval/src/Listener/ApproveContent.php b/extensions/approval/src/Listener/ApproveContent.php index 72e3760c8d..28ffd444a6 100755 --- a/extensions/approval/src/Listener/ApproveContent.php +++ b/extensions/approval/src/Listener/ApproveContent.php @@ -11,35 +11,53 @@ use Flarum\Approval\Event\PostWasApproved; use Flarum\Post\Event\Saving; +use Flarum\User\Exception\PermissionDeniedException; use Illuminate\Contracts\Events\Dispatcher; class ApproveContent { /** - * @param Dispatcher $events + * @param Dispatcher $events */ public function subscribe(Dispatcher $events) { $events->listen(Saving::class, [$this, 'approvePost']); } + /** + * @throws PermissionDeniedException + */ public function approvePost(Saving $event) { $attributes = $event->data['attributes']; $post = $event->post; + // Nothing to do if it is already approved. + if ($post->is_approved) { + return; + } + + /* + * We approve a post in one of two cases: + * - The post was unapproved and the allowed action is approving it. We trigger an event. + * - The post was unapproved and the allowed actor is hiding or un-hiding it. + * We approve it silently if the action is unhiding. + */ + $approvingSilently = false; + if (isset($attributes['isApproved'])) { $event->actor->assertCan('approve', $post); $isApproved = (bool) $attributes['isApproved']; - } elseif (! empty($attributes['isHidden']) && $event->actor->can('approve', $post)) { + } elseif (isset($attributes['isHidden']) && $event->actor->can('approve', $post)) { $isApproved = true; + $approvingSilently = $attributes['isHidden']; } if (! empty($isApproved)) { $post->is_approved = true; - if(! $post->hidden_at) { + if (! $approvingSilently) { $post->raise(new PostWasApproved($post, $event->actor)); } }