diff --git a/extend.php b/extend.php index 828d8d8..edc5b61 100644 --- a/extend.php +++ b/extend.php @@ -10,6 +10,7 @@ use Flarum\Api\Serializer\BasicDiscussionSerializer; use Flarum\Api\Serializer\PostSerializer; use Flarum\Approval\Access; +use Flarum\Approval\Event\PostWasApproved; use Flarum\Approval\Listener; use Flarum\Discussion\Discussion; use Flarum\Extend; @@ -48,6 +49,7 @@ new Extend\Locales(__DIR__.'/locale'), (new Extend\Event()) + ->listen(PostWasApproved::class, Listener\UpdateDiscussionAfterPostApproval::class) ->subscribe(Listener\ApproveContent::class) ->subscribe(Listener\UnapproveNewContent::class), diff --git a/src/Listener/ApproveContent.php b/src/Listener/ApproveContent.php index efa0fc2..e0b996f 100755 --- a/src/Listener/ApproveContent.php +++ b/src/Listener/ApproveContent.php @@ -21,12 +21,8 @@ class ApproveContent public function subscribe(Dispatcher $events) { $events->listen(Saving::class, [$this, 'approvePost']); - $events->listen(PostWasApproved::class, [$this, 'approveDiscussion']); } - /** - * @param Saving $event - */ public function approvePost(Saving $event) { $attributes = $event->data['attributes']; @@ -46,32 +42,4 @@ public function approvePost(Saving $event) $post->raise(new PostWasApproved($post, $event->actor)); } } - - /** - * @param PostWasApproved $event - */ - public function approveDiscussion(PostWasApproved $event) - { - $post = $event->post; - $discussion = $post->discussion; - $user = $discussion->user; - - $discussion->refreshCommentCount(); - $discussion->refreshLastPost(); - - if ($post->number == 1) { - $discussion->is_approved = true; - - $discussion->afterSave(function () use ($user) { - $user->refreshDiscussionCount(); - }); - } - - $discussion->save(); - - if ($discussion->user) { - $user->refreshCommentCount(); - $user->save(); - } - } } diff --git a/src/Listener/UpdateDiscussionAfterPostApproval.php b/src/Listener/UpdateDiscussionAfterPostApproval.php new file mode 100644 index 0000000..2a62b53 --- /dev/null +++ b/src/Listener/UpdateDiscussionAfterPostApproval.php @@ -0,0 +1,40 @@ +post; + $discussion = $post->discussion; + $user = $discussion->user; + + $discussion->refreshCommentCount(); + $discussion->refreshLastPost(); + + if ($post->number == 1) { + $discussion->is_approved = true; + + $discussion->afterSave(function () use ($user) { + $user->refreshDiscussionCount(); + }); + } + + $discussion->save(); + + if ($discussion->user) { + $user->refreshCommentCount(); + $user->save(); + } + } +}