diff --git a/extensions/approval/extend.php b/extensions/approval/extend.php index 828d8d802..edc5b61d3 100644 --- a/extensions/approval/extend.php +++ b/extensions/approval/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 @@ return [ 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/extensions/approval/src/Listener/ApproveContent.php b/extensions/approval/src/Listener/ApproveContent.php index efa0fc277..e0b996f22 100755 --- a/extensions/approval/src/Listener/ApproveContent.php +++ b/extensions/approval/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 @@ class ApproveContent $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/extensions/approval/src/Listener/UpdateDiscussionAfterPostApproval.php b/extensions/approval/src/Listener/UpdateDiscussionAfterPostApproval.php new file mode 100644 index 000000000..2a62b533c --- /dev/null +++ b/extensions/approval/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(); + } + } +} diff --git a/extensions/subscriptions/composer.json b/extensions/subscriptions/composer.json index 8da8732fa..f1e6bfed0 100644 --- a/extensions/subscriptions/composer.json +++ b/extensions/subscriptions/composer.json @@ -33,6 +33,9 @@ "flarum-extension": { "title": "Subscriptions", "category": "feature", + "optional-dependencies": [ + "flarum/approval" + ], "icon": { "name": "fas fa-star", "backgroundColor": "#ffea7b", @@ -86,6 +89,7 @@ "test:setup": "Sets up a database for use with integration tests. Execute this only once." }, "require-dev": { - "flarum/testing": "^1.0.0" + "flarum/testing": "^1.0.0", + "flarum/approval": "@dev" } } diff --git a/extensions/subscriptions/extend.php b/extensions/subscriptions/extend.php index 29ae4bf56..c910e7296 100644 --- a/extensions/subscriptions/extend.php +++ b/extensions/subscriptions/extend.php @@ -9,6 +9,7 @@ use Flarum\Api\Serializer\BasicDiscussionSerializer; use Flarum\Api\Serializer\DiscussionSerializer; +use Flarum\Approval\Event\PostWasApproved; use Flarum\Discussion\Discussion; use Flarum\Discussion\Event\Saving; use Flarum\Discussion\Filter\DiscussionFilterer; @@ -50,6 +51,7 @@ return [ (new Extend\Event()) ->listen(Saving::class, Listener\SaveSubscriptionToDatabase::class) ->listen(Posted::class, Listener\SendNotificationWhenReplyIsPosted::class) + ->listen(PostWasApproved::class, Listener\SendNotificationWhenReplyIsPosted::class) ->listen(Hidden::class, Listener\DeleteNotificationWhenPostIsHiddenOrDeleted::class) ->listen(Restored::class, Listener\RestoreNotificationWhenPostIsRestored::class) ->listen(Deleted::class, Listener\DeleteNotificationWhenPostIsHiddenOrDeleted::class) diff --git a/extensions/subscriptions/src/Listener/SendNotificationWhenReplyIsPosted.php b/extensions/subscriptions/src/Listener/SendNotificationWhenReplyIsPosted.php index b7d7b5a52..ec3af606b 100755 --- a/extensions/subscriptions/src/Listener/SendNotificationWhenReplyIsPosted.php +++ b/extensions/subscriptions/src/Listener/SendNotificationWhenReplyIsPosted.php @@ -9,6 +9,7 @@ namespace Flarum\Subscriptions\Listener; +use Flarum\Approval\Event\PostWasApproved; use Flarum\Post\Event\Posted; use Flarum\Subscriptions\Job\SendReplyNotification; use Illuminate\Contracts\Queue\Queue; @@ -25,7 +26,11 @@ class SendNotificationWhenReplyIsPosted $this->queue = $queue; } - public function handle(Posted $event) + /** + * @param Posted|PostWasApproved $event + * @return void + */ + public function handle($event) { $this->queue->push( new SendReplyNotification($event->post, $event->post->discussion->last_post_number) diff --git a/extensions/subscriptions/tests/integration/api/discussions/ReplyNotificationTest.php b/extensions/subscriptions/tests/integration/api/discussions/ReplyNotificationTest.php index 943c5fed6..0600865b7 100644 --- a/extensions/subscriptions/tests/integration/api/discussions/ReplyNotificationTest.php +++ b/extensions/subscriptions/tests/integration/api/discussions/ReplyNotificationTest.php @@ -10,6 +10,7 @@ namespace Flarum\Subscriptions\tests\integration\api\discussions; use Carbon\Carbon; +use Flarum\Group\Group; use Flarum\Testing\integration\RetrievesAuthorizedUsers; use Flarum\Testing\integration\TestCase; use Flarum\User\User; @@ -221,4 +222,60 @@ class ReplyNotificationTest extends TestCase [[8, 9, 10]] ]; } + + /** @test */ + public function approving_reply_sends_reply_notification() + { + // Flags was only specified because it is required for approval. + $this->extensions = ['flarum-flags', 'flarum-approval', 'flarum-subscriptions']; + + $this->app(); + + $this->database() + ->table('group_permission') + ->where('group_id', Group::MEMBER_ID) + ->where('permission', 'discussion.replyWithoutApproval') + ->delete(); + + /** @var User $mainUser */ + $mainUser = User::query()->find(2); + + $this->assertEquals(0, $mainUser->getUnreadNotificationCount()); + + $response = $this->send( + $this->request('POST', '/api/posts', [ + 'authenticatedAs' => 4, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'content' => 'reply with predetermined content for automated testing - too-obscure', + ], + 'relationships' => [ + 'discussion' => ['data' => ['id' => 1]], + ], + ], + ], + ]) + ); + + $this->assertEquals(0, $mainUser->getUnreadNotificationCount()); + + $json = json_decode($response->getBody()->getContents(), true); + + // Approve the previous post + $this->send( + $this->request('PATCH', '/api/posts/'.$json['data']['id'], [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'isApproved' => 1, + ], + ], + ], + ]) + ); + + $this->assertEquals(1, $mainUser->getUnreadNotificationCount()); + } }