1
0
mirror of https://github.com/flarum/core.git synced 2025-10-19 18:56:44 +02:00

Merge branch 'master' into 1236-database-changes

# Conflicts:
#	src/Forum/Controller/IndexController.php
#	src/User/UserMetadataUpdater.php
This commit is contained in:
Toby Zerner
2018-07-21 21:37:49 +09:30
101 changed files with 3073 additions and 2272 deletions

View File

@@ -584,6 +584,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');
}
/**
* Define the relationship with the user's read discussions.
*
@@ -727,4 +737,28 @@ class User extends AbstractModel
{
return 'notify_'.$type.'_'.$method;
}
/**
* Refresh the user's comments count.
*
* @return $this
*/
public function refreshCommentsCount()
{
$this->comment_count = $this->posts()->count();
return $this;
}
/**
* Refresh the user's comments count.
*
* @return $this
*/
public function refreshDiscussionsCount()
{
$this->discussion_count = $this->discussions()->count();
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,24 @@ 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->comment_count += $amount;
$user->save();
$user->refreshCommentsCount()->save();
}
}
/**
* @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->discussion_count += $amount;
$user->save();
$user->refreshDiscussionsCount()->save();
}
}
}