Skip to content

Commit

Permalink
feat: send notifications of a new reply when post is approved (#3656)
Browse files Browse the repository at this point in the history
* test(subscriptions): approved reply sends out notifications to users

Signed-off-by: Sami Mazouz <[email protected]>

* feat: send notifications when a post is approved

The code in approval was extracted into a listener because no matter what listeners are always executed before subscribers even if the extension is set to load before.

Signed-off-by: Sami Mazouz <[email protected]>

Signed-off-by: Sami Mazouz <[email protected]>
  • Loading branch information
SychO9 committed Nov 7, 2022
1 parent 996de8e commit 6e71824
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"flarum-extension": {
"title": "Subscriptions",
"category": "feature",
"optional-dependencies": [
"flarum/approval"
],
"icon": {
"name": "fas fa-star",
"backgroundColor": "#ffea7b",
Expand Down Expand Up @@ -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"
}
}
2 changes: 2 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,6 +51,7 @@
(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)
Expand Down
7 changes: 6 additions & 1 deletion src/Listener/SendNotificationWhenReplyIsPosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +26,11 @@ public function __construct(Queue $queue)
$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)
Expand Down
57 changes: 57 additions & 0 deletions tests/integration/api/discussions/ReplyNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -221,4 +222,60 @@ public function deleteLastPostsProvider(): array
[[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());
}
}

0 comments on commit 6e71824

Please sign in to comment.