From bed386f8e4f1fa29e3696784ec325c84b2020bbc Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Fri, 10 Nov 2023 10:50:53 +0100 Subject: [PATCH] fix(approval): post approved event triggered when not approving --- .../approval/src/Listener/ApproveContent.php | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/extensions/approval/src/Listener/ApproveContent.php b/extensions/approval/src/Listener/ApproveContent.php index e0b996f22f..df64826586 100755 --- a/extensions/approval/src/Listener/ApproveContent.php +++ b/extensions/approval/src/Listener/ApproveContent.php @@ -28,18 +28,34 @@ 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; - $post->raise(new PostWasApproved($post, $event->actor)); + if (! $approvingSilently) { + $post->raise(new PostWasApproved($post, $event->actor)); + } } } }