mirror of
https://github.com/flarum/core.git
synced 2025-07-25 02:31:17 +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:
@@ -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),
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Approval\Listener;
|
||||
|
||||
use Flarum\Approval\Event\PostWasApproved;
|
||||
|
||||
class UpdateDiscussionAfterPostApproval
|
||||
{
|
||||
public function handle(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();
|
||||
}
|
||||
}
|
||||
}
|
@@ -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