2015-08-27 07:40:18 +08:00
|
|
|
<?php
|
2016-11-29 13:03:53 +08:00
|
|
|
|
2015-08-26 14:48:58 +08:00
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
|
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2017-06-24 18:55:22 +08:00
|
|
|
namespace Flarum\User;
|
2015-07-04 10:54:48 +08:00
|
|
|
|
2017-06-24 19:48:04 +08:00
|
|
|
use Flarum\Discussion\Discussion;
|
2017-06-24 20:10:39 +08:00
|
|
|
use Flarum\Discussion\Event\Deleted as DiscussionDeleted;
|
2017-06-24 19:48:04 +08:00
|
|
|
use Flarum\Discussion\Event\Started;
|
2017-06-24 20:10:39 +08:00
|
|
|
use Flarum\Post\Event\Deleted as PostDeleted;
|
2017-06-24 19:43:33 +08:00
|
|
|
use Flarum\Post\Event\Hidden;
|
|
|
|
use Flarum\Post\Event\Posted;
|
|
|
|
use Flarum\Post\Event\Restored;
|
2017-06-24 20:10:39 +08:00
|
|
|
use Flarum\Post\Post;
|
2015-07-04 10:54:48 +08:00
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
|
|
|
|
|
class UserMetadataUpdater
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Dispatcher $events
|
|
|
|
*/
|
|
|
|
public function subscribe(Dispatcher $events)
|
|
|
|
{
|
2017-06-24 19:43:33 +08:00
|
|
|
$events->listen(Posted::class, [$this, 'whenPostWasPosted']);
|
2017-06-24 20:10:39 +08:00
|
|
|
$events->listen(PostDeleted::class, [$this, 'whenPostWasDeleted']);
|
2017-06-24 19:43:33 +08:00
|
|
|
$events->listen(Hidden::class, [$this, 'whenPostWasHidden']);
|
|
|
|
$events->listen(Restored::class, [$this, 'whenPostWasRestored']);
|
2017-06-24 19:48:04 +08:00
|
|
|
$events->listen(Started::class, [$this, 'whenDiscussionWasStarted']);
|
2017-06-24 20:10:39 +08:00
|
|
|
$events->listen(DiscussionDeleted::class, [$this, 'whenDiscussionWasDeleted']);
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-24 19:43:33 +08:00
|
|
|
* @param \Flarum\Post\Event\Posted $event
|
2015-07-04 10:54:48 +08:00
|
|
|
*/
|
2017-06-24 19:43:33 +08:00
|
|
|
public function whenPostWasPosted(Posted $event)
|
2015-07-04 10:54:48 +08:00
|
|
|
{
|
2015-09-04 12:21:13 +08:00
|
|
|
$this->updateCommentsCount($event->post, 1);
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-24 19:43:33 +08:00
|
|
|
* @param \Flarum\Post\Event\Deleted $event
|
2015-07-04 10:54:48 +08:00
|
|
|
*/
|
2017-06-24 20:10:39 +08:00
|
|
|
public function whenPostWasDeleted(PostDeleted $event)
|
2015-07-04 10:54:48 +08:00
|
|
|
{
|
2015-09-04 12:21:13 +08:00
|
|
|
$this->updateCommentsCount($event->post, -1);
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-24 19:43:33 +08:00
|
|
|
* @param \Flarum\Post\Event\Hidden $event
|
2015-07-04 10:54:48 +08:00
|
|
|
*/
|
2017-06-24 19:43:33 +08:00
|
|
|
public function whenPostWasHidden(Hidden $event)
|
2015-07-04 10:54:48 +08:00
|
|
|
{
|
2015-09-04 12:21:13 +08:00
|
|
|
$this->updateCommentsCount($event->post, -1);
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-24 19:43:33 +08:00
|
|
|
* @param \Flarum\Post\Event\Restored $event
|
2015-07-04 10:54:48 +08:00
|
|
|
*/
|
2017-06-24 19:43:33 +08:00
|
|
|
public function whenPostWasRestored(Restored $event)
|
2015-07-04 10:54:48 +08:00
|
|
|
{
|
2015-09-04 12:21:13 +08:00
|
|
|
$this->updateCommentsCount($event->post, 1);
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-24 19:48:04 +08:00
|
|
|
* @param \Flarum\Discussion\Event\Started $event
|
2015-07-04 10:54:48 +08:00
|
|
|
*/
|
2017-06-24 19:48:04 +08:00
|
|
|
public function whenDiscussionWasStarted(Started $event)
|
2015-07-04 10:54:48 +08:00
|
|
|
{
|
2015-09-04 12:21:13 +08:00
|
|
|
$this->updateDiscussionsCount($event->discussion, 1);
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-24 19:48:04 +08:00
|
|
|
* @param \Flarum\Discussion\Event\Deleted $event
|
2015-07-04 10:54:48 +08:00
|
|
|
*/
|
2017-06-24 20:10:39 +08:00
|
|
|
public function whenDiscussionWasDeleted(DiscussionDeleted $event)
|
2015-07-04 10:54:48 +08:00
|
|
|
{
|
2015-09-04 12:21:13 +08:00
|
|
|
$this->updateDiscussionsCount($event->discussion, -1);
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-04 12:21:13 +08:00
|
|
|
* @param Post $post
|
2015-07-04 10:54:48 +08:00
|
|
|
* @param int $amount
|
|
|
|
*/
|
2015-09-04 12:21:13 +08:00
|
|
|
protected function updateCommentsCount(Post $post, $amount)
|
2015-07-04 10:54:48 +08:00
|
|
|
{
|
2015-09-04 12:21:13 +08:00
|
|
|
$user = $post->user;
|
|
|
|
|
|
|
|
if ($user && $user->exists) {
|
|
|
|
$user->comments_count += $amount;
|
|
|
|
$user->save();
|
|
|
|
}
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-24 19:48:04 +08:00
|
|
|
* @param \Flarum\Discussion\Discussion $discussion
|
2015-07-04 10:54:48 +08:00
|
|
|
* @param int $amount
|
|
|
|
*/
|
2015-09-04 12:21:13 +08:00
|
|
|
protected function updateDiscussionsCount(Discussion $discussion, $amount)
|
2015-07-04 10:54:48 +08:00
|
|
|
{
|
2015-09-04 12:21:13 +08:00
|
|
|
$user = $discussion->startUser;
|
|
|
|
|
|
|
|
if ($user && $user->exists) {
|
|
|
|
$user->discussions_count += $amount;
|
|
|
|
$user->save();
|
|
|
|
}
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
}
|