mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 22:53:37 +08:00
Properly set comments_count and discussions_count (#1436)
This commit is contained in:
parent
cd7e0879df
commit
9c80c21240
|
@ -604,6 +604,16 @@ class User extends AbstractModel
|
||||||
return $this->hasMany('Flarum\Post\Post');
|
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.
|
* Define the relationship with the user's read discussions.
|
||||||
*
|
*
|
||||||
|
@ -747,4 +757,30 @@ class User extends AbstractModel
|
||||||
{
|
{
|
||||||
return 'notify_'.$type.'_'.$method;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,9 +15,7 @@ use Flarum\Discussion\Discussion;
|
||||||
use Flarum\Discussion\Event\Deleted as DiscussionDeleted;
|
use Flarum\Discussion\Event\Deleted as DiscussionDeleted;
|
||||||
use Flarum\Discussion\Event\Started;
|
use Flarum\Discussion\Event\Started;
|
||||||
use Flarum\Post\Event\Deleted as PostDeleted;
|
use Flarum\Post\Event\Deleted as PostDeleted;
|
||||||
use Flarum\Post\Event\Hidden;
|
|
||||||
use Flarum\Post\Event\Posted;
|
use Flarum\Post\Event\Posted;
|
||||||
use Flarum\Post\Event\Restored;
|
|
||||||
use Flarum\Post\Post;
|
use Flarum\Post\Post;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
use Illuminate\Contracts\Events\Dispatcher;
|
||||||
|
|
||||||
|
@ -30,8 +28,6 @@ class UserMetadataUpdater
|
||||||
{
|
{
|
||||||
$events->listen(Posted::class, [$this, 'whenPostWasPosted']);
|
$events->listen(Posted::class, [$this, 'whenPostWasPosted']);
|
||||||
$events->listen(PostDeleted::class, [$this, 'whenPostWasDeleted']);
|
$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(Started::class, [$this, 'whenDiscussionWasStarted']);
|
||||||
$events->listen(DiscussionDeleted::class, [$this, 'whenDiscussionWasDeleted']);
|
$events->listen(DiscussionDeleted::class, [$this, 'whenDiscussionWasDeleted']);
|
||||||
}
|
}
|
||||||
|
@ -41,7 +37,7 @@ class UserMetadataUpdater
|
||||||
*/
|
*/
|
||||||
public function whenPostWasPosted(Posted $event)
|
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)
|
public function whenPostWasDeleted(PostDeleted $event)
|
||||||
{
|
{
|
||||||
$this->updateCommentsCount($event->post, -1);
|
$this->updateCommentsCount($event->post);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,7 +53,7 @@ class UserMetadataUpdater
|
||||||
*/
|
*/
|
||||||
public function whenDiscussionWasStarted(Started $event)
|
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)
|
public function whenDiscussionWasDeleted(DiscussionDeleted $event)
|
||||||
{
|
{
|
||||||
$this->updateDiscussionsCount($event->discussion, -1);
|
$this->updateDiscussionsCount($event->discussion);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function updateCommentsCount(Post $post)
|
||||||
* @param Post $post
|
|
||||||
* @param int $amount
|
|
||||||
*/
|
|
||||||
protected function updateCommentsCount(Post $post, $amount)
|
|
||||||
{
|
{
|
||||||
$user = $post->user;
|
optional($post->user)->refreshCommentsCount();
|
||||||
|
|
||||||
if ($user && $user->exists) {
|
|
||||||
$user->comments_count += $amount;
|
|
||||||
$user->save();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function updateDiscussionsCount(Discussion $discussion)
|
||||||
* @param \Flarum\Discussion\Discussion $discussion
|
|
||||||
* @param int $amount
|
|
||||||
*/
|
|
||||||
protected function updateDiscussionsCount(Discussion $discussion, $amount)
|
|
||||||
{
|
{
|
||||||
$user = $discussion->startUser;
|
optional($discussion->startUser)->refreshDiscussionsCount();
|
||||||
|
|
||||||
if ($user && $user->exists) {
|
|
||||||
$user->discussions_count += $amount;
|
|
||||||
$user->save();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user