1
0
mirror of https://github.com/flarum/core.git synced 2025-08-02 14:37:49 +02:00

Prevent error when hiding/restoring a post with a deleted user

This commit is contained in:
Toby Zerner
2015-09-04 13:51:13 +09:30
parent a5d3aa9b36
commit fc7fc41383

View File

@@ -10,7 +10,8 @@
namespace Flarum\Core\Users\Listeners; 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\PostWasPosted;
use Flarum\Events\PostWasDeleted; use Flarum\Events\PostWasDeleted;
use Flarum\Events\PostWasHidden; use Flarum\Events\PostWasHidden;
@@ -39,7 +40,7 @@ class UserMetadataUpdater
*/ */
public function whenPostWasPosted(PostWasPosted $event) 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) public function whenPostWasDeleted(PostWasDeleted $event)
{ {
if ($event->post->user->exists) { $this->updateCommentsCount($event->post, -1);
$this->updateCommentsCount($event->post->user, -1);
}
} }
/** /**
@@ -57,7 +56,7 @@ class UserMetadataUpdater
*/ */
public function whenPostWasHidden(PostWasHidden $event) 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) 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) 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) 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 * @param int $amount
*/ */
protected function updateCommentsCount(User $user, $amount) protected function updateCommentsCount(Post $post, $amount)
{ {
$user->comments_count += $amount; $user = $post->user;
$user->save();
if ($user && $user->exists) {
$user->comments_count += $amount;
$user->save();
}
} }
/** /**
* @param User $user * @param Discussion $discussion
* @param int $amount * @param int $amount
*/ */
protected function updateDiscussionsCount(User $user, $amount) protected function updateDiscussionsCount(Discussion $discussion, $amount)
{ {
$user->discussions_count += $amount; $user = $discussion->startUser;
$user->save();
if ($user && $user->exists) {
$user->discussions_count += $amount;
$user->save();
}
} }
} }