From fc7fc41383e509a43541628f2c857ae726ce8158 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 4 Sep 2015 13:51:13 +0930 Subject: [PATCH] Prevent error when hiding/restoring a post with a deleted user --- .../Users/Listeners/UserMetadataUpdater.php | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/Core/Users/Listeners/UserMetadataUpdater.php b/src/Core/Users/Listeners/UserMetadataUpdater.php index 5d070c741..0f754fa0e 100755 --- a/src/Core/Users/Listeners/UserMetadataUpdater.php +++ b/src/Core/Users/Listeners/UserMetadataUpdater.php @@ -10,7 +10,8 @@ namespace Flarum\Core\Users\Listeners; -use Flarum\Core\Users\User; +use Flarum\Core\Posts\Post; +use Flarum\Core\Discussions\Discussion; use Flarum\Events\PostWasPosted; use Flarum\Events\PostWasDeleted; use Flarum\Events\PostWasHidden; @@ -39,7 +40,7 @@ class UserMetadataUpdater */ public function whenPostWasPosted(PostWasPosted $event) { - $this->updateCommentsCount($event->post->user, 1); + $this->updateCommentsCount($event->post, 1); } /** @@ -47,9 +48,7 @@ class UserMetadataUpdater */ public function whenPostWasDeleted(PostWasDeleted $event) { - if ($event->post->user->exists) { - $this->updateCommentsCount($event->post->user, -1); - } + $this->updateCommentsCount($event->post, -1); } /** @@ -57,7 +56,7 @@ class UserMetadataUpdater */ public function whenPostWasHidden(PostWasHidden $event) { - $this->updateCommentsCount($event->post->user, -1); + $this->updateCommentsCount($event->post, -1); } /** @@ -65,7 +64,7 @@ class UserMetadataUpdater */ public function whenPostWasRestored(PostWasRestored $event) { - $this->updateCommentsCount($event->post->user, 1); + $this->updateCommentsCount($event->post, 1); } /** @@ -73,7 +72,7 @@ class UserMetadataUpdater */ public function whenDiscussionWasStarted(DiscussionWasStarted $event) { - $this->updateDiscussionsCount($event->discussion->startUser, 1); + $this->updateDiscussionsCount($event->discussion, 1); } /** @@ -81,26 +80,34 @@ class UserMetadataUpdater */ public function whenDiscussionWasDeleted(DiscussionWasDeleted $event) { - $this->updateDiscussionsCount($event->discussion->startUser, -1); + $this->updateDiscussionsCount($event->discussion, -1); } /** - * @param User $user + * @param Post $post * @param int $amount */ - protected function updateCommentsCount(User $user, $amount) + protected function updateCommentsCount(Post $post, $amount) { - $user->comments_count += $amount; - $user->save(); + $user = $post->user; + + if ($user && $user->exists) { + $user->comments_count += $amount; + $user->save(); + } } /** - * @param User $user + * @param Discussion $discussion * @param int $amount */ - protected function updateDiscussionsCount(User $user, $amount) + protected function updateDiscussionsCount(Discussion $discussion, $amount) { - $user->discussions_count += $amount; - $user->save(); + $user = $discussion->startUser; + + if ($user && $user->exists) { + $user->discussions_count += $amount; + $user->save(); + } } }