Skip to content

Commit

Permalink
fix(approval): post approved event triggered when not approving
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Nov 10, 2023
1 parent 82e08e3 commit bed386f
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions extensions/approval/src/Listener/ApproveContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
}

0 comments on commit bed386f

Please sign in to comment.