Properly set comments_count and discussions_count (#1436)

This commit is contained in:
David Sevilla Martín 2018-07-17 13:02:37 -04:00 committed by Franz Liedke
parent cd7e0879df
commit 9c80c21240
2 changed files with 44 additions and 46 deletions

View File

@ -604,6 +604,16 @@ class User extends AbstractModel
return $this->hasMany('Flarum\Post\Post');
}
/**
* Define the relationship with the user's discussions.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function discussions()
{
return $this->hasMany('Flarum\Discussion\Discussion', 'start_user_id');
}
/**
* Define the relationship with the user's read discussions.
*
@ -747,4 +757,30 @@ class User extends AbstractModel
{
return 'notify_'.$type.'_'.$method;
}
/**
* Refresh the user's comments count.
*
* @return $this
*/
public function refreshCommentsCount()
{
$this->comments_count = $this->posts()->count();
$this->save();
return $this;
}
/**
* Refresh the user's comments count.
*
* @return $this
*/
public function refreshDiscussionsCount()
{
$this->discussions_count = $this->discussions()->count();
$this->save();
return $this;
}
}

View File

@ -15,9 +15,7 @@ use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Deleted as DiscussionDeleted;
use Flarum\Discussion\Event\Started;
use Flarum\Post\Event\Deleted as PostDeleted;
use Flarum\Post\Event\Hidden;
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Restored;
use Flarum\Post\Post;
use Illuminate\Contracts\Events\Dispatcher;
@ -30,8 +28,6 @@ class UserMetadataUpdater
{
$events->listen(Posted::class, [$this, 'whenPostWasPosted']);
$events->listen(PostDeleted::class, [$this, 'whenPostWasDeleted']);
$events->listen(Hidden::class, [$this, 'whenPostWasHidden']);
$events->listen(Restored::class, [$this, 'whenPostWasRestored']);
$events->listen(Started::class, [$this, 'whenDiscussionWasStarted']);
$events->listen(DiscussionDeleted::class, [$this, 'whenDiscussionWasDeleted']);
}
@ -41,7 +37,7 @@ class UserMetadataUpdater
*/
public function whenPostWasPosted(Posted $event)
{
$this->updateCommentsCount($event->post, 1);
$this->updateCommentsCount($event->post);
}
/**
@ -49,23 +45,7 @@ class UserMetadataUpdater
*/
public function whenPostWasDeleted(PostDeleted $event)
{
$this->updateCommentsCount($event->post, -1);
}
/**
* @param \Flarum\Post\Event\Hidden $event
*/
public function whenPostWasHidden(Hidden $event)
{
$this->updateCommentsCount($event->post, -1);
}
/**
* @param \Flarum\Post\Event\Restored $event
*/
public function whenPostWasRestored(Restored $event)
{
$this->updateCommentsCount($event->post, 1);
$this->updateCommentsCount($event->post);
}
/**
@ -73,7 +53,7 @@ class UserMetadataUpdater
*/
public function whenDiscussionWasStarted(Started $event)
{
$this->updateDiscussionsCount($event->discussion, 1);
$this->updateDiscussionsCount($event->discussion);
}
/**
@ -81,34 +61,16 @@ class UserMetadataUpdater
*/
public function whenDiscussionWasDeleted(DiscussionDeleted $event)
{
$this->updateDiscussionsCount($event->discussion, -1);
$this->updateDiscussionsCount($event->discussion);
}
/**
* @param Post $post
* @param int $amount
*/
protected function updateCommentsCount(Post $post, $amount)
private function updateCommentsCount(Post $post)
{
$user = $post->user;
if ($user && $user->exists) {
$user->comments_count += $amount;
$user->save();
}
optional($post->user)->refreshCommentsCount();
}
/**
* @param \Flarum\Discussion\Discussion $discussion
* @param int $amount
*/
protected function updateDiscussionsCount(Discussion $discussion, $amount)
private function updateDiscussionsCount(Discussion $discussion)
{
$user = $discussion->startUser;
if ($user && $user->exists) {
$user->discussions_count += $amount;
$user->save();
}
optional($discussion->startUser)->refreshDiscussionsCount();
}
}