mirror of
https://github.com/flarum/core.git
synced 2025-08-30 03:20:36 +02:00
feat: send notifications of a new reply when post is approved (#3656)
* test(subscriptions): approved reply sends out notifications to users Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> * 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 <sychocouldy@gmail.com> Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
|
@@ -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)
|
||||
|
@@ -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)
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user