fix(approval): post approved event triggered when not approving

This commit is contained in:
Sami Mazouz 2023-11-10 10:50:53 +01:00
parent 82e08e3fa5
commit bed386f8e4
No known key found for this signature in database

View File

@ -28,18 +28,34 @@ class ApproveContent
$attributes = $event->data['attributes']; $attributes = $event->data['attributes'];
$post = $event->post; $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'])) { if (isset($attributes['isApproved'])) {
$event->actor->assertCan('approve', $post); $event->actor->assertCan('approve', $post);
$isApproved = (bool) $attributes['isApproved']; $isApproved = (bool) $attributes['isApproved'];
} elseif (! empty($attributes['isHidden']) && $event->actor->can('approve', $post)) { } elseif (isset($attributes['isHidden']) && $event->actor->can('approve', $post)) {
$isApproved = true; $isApproved = true;
$approvingSilently = $attributes['isHidden'];
} }
if (! empty($isApproved)) { if (! empty($isApproved)) {
$post->is_approved = true; $post->is_approved = true;
if (! $approvingSilently) {
$post->raise(new PostWasApproved($post, $event->actor)); $post->raise(new PostWasApproved($post, $event->actor));
} }
} }
}
} }