Skip to content

Commit

Permalink
fix: Implement PostWasUnapproved event for all unapproved posts, hidi…
Browse files Browse the repository at this point in the history
…ng discussions for unapproved initial posts

Introduces the PostWasUnapproved event, fired upon every post unapproval. Specifically, when the unapproved post is the initial post of a discussion, this event also hides the entire discussion. This change ensures that unapproved initial posts do not leave their discussions visible on the forum.
  • Loading branch information
rafaucau committed Nov 6, 2023
1 parent 6596569 commit 3bbd1f7
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 30 deletions.
4 changes: 2 additions & 2 deletions extensions/akismet/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Flarum\Akismet\Listener;
use Flarum\Akismet\Provider\AkismetProvider;
use Flarum\Approval\Event\PostWasApproved;
use Flarum\Approval\Event\PostWasUnapproved;
use Flarum\Extend;
use Flarum\Post\Event\Hidden;
use Flarum\Post\Event\Saving;
use Flarum\Post\Post;

Expand All @@ -25,7 +25,7 @@
new Extend\Locales(__DIR__.'/locale'),

(new Extend\Event())
->listen(Hidden::class, Listener\SubmitSpam::class)
->listen(PostWasUnapproved::class, Listener\SubmitSpam::class)
->listen(PostWasApproved::class, Listener\SubmitHam::class)
->listen(Saving::class, Listener\ValidatePost::class),

Expand Down
4 changes: 2 additions & 2 deletions extensions/akismet/src/Listener/SubmitSpam.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Flarum\Akismet\Listener;

use Flarum\Akismet\Akismet;
use Flarum\Post\Event\Hidden;
use Flarum\Approval\Event\PostWasUnapproved;

class SubmitSpam
{
Expand All @@ -24,7 +24,7 @@ public function __construct(Akismet $akismet)
$this->akismet = $akismet;
}

public function handle(Hidden $event)
public function handle(PostWasUnapproved $event)
{
if (! $this->akismet->isConfigured()) {
return;
Expand Down
2 changes: 2 additions & 0 deletions extensions/approval/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Approval\Access;
use Flarum\Approval\Event\PostWasApproved;
use Flarum\Approval\Event\PostWasUnapproved;
use Flarum\Approval\Listener;
use Flarum\Discussion\Discussion;
use Flarum\Extend;
Expand Down Expand Up @@ -52,6 +53,7 @@

(new Extend\Event())
->listen(PostWasApproved::class, Listener\UpdateDiscussionAfterPostApproval::class)
->listen(PostWasUnapproved::class, Listener\UpdateDiscussionAfterPostUnapproval::class)
->subscribe(Listener\ApproveContent::class)
->subscribe(Listener\UnapproveNewContent::class),

Expand Down
38 changes: 38 additions & 0 deletions extensions/approval/src/Event/PostWasUnapproved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\Event;

use Flarum\Post\Post;
use Flarum\User\User;

class PostWasUnapproved
{
/**
* The post that was unapproved.
*
* @var Post
*/
public $post;

/**
* @var User
*/
public $actor;

/**
* @param Post $post
* @param User $actor
*/
public function __construct(Post $post, User $actor)
{
$this->post = $post;
$this->actor = $actor;
}
}
4 changes: 4 additions & 0 deletions extensions/approval/src/Listener/ApproveContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Flarum\Approval\Listener;

use Flarum\Approval\Event\PostWasApproved;
use Flarum\Approval\Event\PostWasUnapproved;
use Flarum\Post\Event\Saving;
use Illuminate\Contracts\Events\Dispatcher;

Expand Down Expand Up @@ -37,10 +38,13 @@ public function approvePost(Saving $event)
}

if (! empty($isApproved)) {
// Set the post's approval status to true to clear any pending approval status, even if the post is hidden.
$post->is_approved = true;

if(! $post->hidden_at) {
$post->raise(new PostWasApproved($post, $event->actor));
} else {
$post->raise(new PostWasUnapproved($post, $event->actor));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,22 @@
namespace Flarum\Approval\Listener;

use Flarum\Approval\Event\PostWasApproved;
use Flarum\Approval\RefreshesDiscussionTrait;

class UpdateDiscussionAfterPostApproval
{
use RefreshesDiscussionTrait;

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();
}

if ($post->user) {
$post->user->refreshCommentCount();
$post->user->save();
}
$this->refreshAndSaveDiscussion($event->post, function ($post, $discussion, $user) {
if ($post->number === 1) {
$discussion->is_approved = true;

$discussion->afterSave(function () use ($user) {
$user->refreshDiscussionCount();
});
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\PostWasUnapproved;
use Flarum\Approval\RefreshesDiscussionTrait;

class UpdateDiscussionAfterPostUnapproval
{
use RefreshesDiscussionTrait;

public function handle(PostWasUnapproved $event)
{
$this->refreshAndSaveDiscussion($event->post,function ($post, $discussion, $user) {
if ($post->number === 1) {
$discussion->is_approved = true;
$discussion->hide();

$discussion->afterSave(function () use ($user) {
$user->refreshDiscussionCount();
});
}
});
}
}
41 changes: 41 additions & 0 deletions extensions/approval/src/RefreshesDiscussionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Flarum\Approval;

use Flarum\Discussion\Discussion;
use Flarum\Post\Post;
use Flarum\User\User;

trait RefreshesDiscussionTrait
{
/**
* Refreshes and saves the discussion data.
*
* @param Post $post
* @param callable(Post $post, Discussion $discussion, User $user): void|null $beforeDiscussionSaveCallback
*/
protected function refreshAndSaveDiscussion(Post $post, callable $beforeDiscussionSaveCallback = null)
{
$discussion = $post->discussion;
$user = $discussion->user;

$discussion->refreshCommentCount();
$discussion->refreshLastPost();

if ($beforeDiscussionSaveCallback) {
$beforeDiscussionSaveCallback($post, $discussion, $user);
}

$discussion->save();

if ($discussion->user) {
$user->refreshCommentCount();
$user->save();
}

if ($post->user) {
$post->user->refreshCommentCount();
$post->user->save();
}
}
}

0 comments on commit 3bbd1f7

Please sign in to comment.