-
-
Notifications
You must be signed in to change notification settings - Fork 837
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Implement PostWasUnapproved event for all unapproved posts, hidi…
…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
Showing
8 changed files
with
133 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
extensions/approval/src/Listener/UpdateDiscussionAfterPostUnapproval.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |