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 <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>
  • Loading branch information
SychO9 authored Nov 7, 2022
1 parent 4cfae1a commit b6b7e56
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
2 changes: 2 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),

Expand Down
32 changes: 0 additions & 32 deletions src/Listener/ApproveContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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();
}
}
}
40 changes: 40 additions & 0 deletions src/Listener/UpdateDiscussionAfterPostApproval.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit b6b7e56

Please sign in to comment.