Skip to content

Commit

Permalink
update a user's comment count if deleting a discussion (#2472)
Browse files Browse the repository at this point in the history
  • Loading branch information
slkrx authored Nov 29, 2020
1 parent 66482c2 commit 387b4fd
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/User/UserMetadataUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Flarum\Discussion\Event\Started;
use Flarum\Post\Event\Deleted as PostDeleted;
use Flarum\Post\Event\Posted;
use Flarum\Post\Post;
use Illuminate\Contracts\Events\Dispatcher;

class UserMetadataUpdater
Expand All @@ -35,15 +34,15 @@ public function subscribe(Dispatcher $events)
*/
public function whenPostWasPosted(Posted $event)
{
$this->updateCommentsCount($event->post);
$this->updateCommentsCount($event->post->user);
}

/**
* @param \Flarum\Post\Event\Deleted $event
*/
public function whenPostWasDeleted(PostDeleted $event)
{
$this->updateCommentsCount($event->post);
$this->updateCommentsCount($event->post->user);
}

/**
Expand All @@ -60,12 +59,14 @@ public function whenDiscussionWasStarted(Started $event)
public function whenDiscussionWasDeleted(DiscussionDeleted $event)
{
$this->updateDiscussionsCount($event->discussion);
$this->updateCommentsCount($event->discussion->user);
}

private function updateCommentsCount(Post $post)
/**
* @param \Flarum\User\User $user
*/
private function updateCommentsCount(User $user)
{
$user = $post->user;

if ($user && $user->exists) {
$user->refreshCommentCount()->save();
}
Expand Down

0 comments on commit 387b4fd

Please sign in to comment.