From b47c791e399205f63c8c3bb2f837cf2f43b471e3 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Tue, 18 Dec 2018 00:11:36 +0100 Subject: [PATCH] Get rid of event subscribers that resolve services too early Refs flarum/core#1578. --- extensions/akismet/extend.php | 21 ++- .../akismet/src/Listener/FilterNewPosts.php | 141 ------------------ extensions/akismet/src/Listener/SubmitHam.php | 43 ++++++ .../akismet/src/Listener/SubmitSpam.php | 43 ++++++ .../akismet/src/Listener/ValidatePost.php | 66 ++++++++ 5 files changed, 171 insertions(+), 143 deletions(-) delete mode 100644 extensions/akismet/src/Listener/FilterNewPosts.php create mode 100644 extensions/akismet/src/Listener/SubmitHam.php create mode 100644 extensions/akismet/src/Listener/SubmitSpam.php create mode 100644 extensions/akismet/src/Listener/ValidatePost.php diff --git a/extensions/akismet/extend.php b/extensions/akismet/extend.php index 11083c4d7..9dea94f39 100644 --- a/extensions/akismet/extend.php +++ b/extensions/akismet/extend.php @@ -10,8 +10,14 @@ */ use Flarum\Akismet\Listener; +use Flarum\Approval\Event\PostWasApproved; use Flarum\Extend; +use Flarum\Post\Event\Hidden; +use Flarum\Post\Event\Saving; +use Flarum\Settings\SettingsRepositoryInterface; +use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; +use TijsVerkoyen\Akismet\Akismet; return [ (new Extend\Frontend('forum')) @@ -20,7 +26,18 @@ return [ (new Extend\Frontend('admin')) ->js(__DIR__.'/js/dist/admin.js'), - function (Dispatcher $events) { - $events->subscribe(Listener\FilterNewPosts::class); + function (Dispatcher $events, Container $container) { + $container->bind(Akismet::class, function ($app) { + $settings = $app->make(SettingsRepositoryInterface::class); + + return new Akismet( + $settings->get('flarum-akismet.api_key'), + $app->url() + ); + }); + + $events->listen(Saving::class, Listener\ValidatePost::class); + $events->listen(PostWasApproved::class, Listener\SubmitHam::class); + $events->listen(Hidden::class, Listener\SubmitSpam::class); }, ]; diff --git a/extensions/akismet/src/Listener/FilterNewPosts.php b/extensions/akismet/src/Listener/FilterNewPosts.php deleted file mode 100644 index 6fe657cd7..000000000 --- a/extensions/akismet/src/Listener/FilterNewPosts.php +++ /dev/null @@ -1,141 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Akismet\Listener; - -use Flarum\Approval\Event\PostWasApproved; -use Flarum\Flags\Flag; -use Flarum\Foundation\Application; -use Flarum\Post\Event\Hidden; -use Flarum\Post\Event\Saving; -use Flarum\Settings\SettingsRepositoryInterface; -use Illuminate\Contracts\Events\Dispatcher; -use TijsVerkoyen\Akismet\Akismet; - -class FilterNewPosts -{ - /** - * @var SettingsRepositoryInterface - */ - protected $settings; - - /** - * @var Application - */ - protected $app; - - /** - * @param SettingsRepositoryInterface $settings - * @param Application $app - */ - public function __construct(SettingsRepositoryInterface $settings, Application $app) - { - $this->settings = $settings; - $this->app = $app; - } - - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) - { - $events->listen(Saving::class, [$this, 'validatePost']); - $events->listen(PostWasApproved::class, [$this, 'submitHam']); - $events->listen(Hidden::class, [$this, 'submitSpam']); - } - - /** - * @param Saving $event - */ - public function validatePost(Saving $event) - { - $post = $event->post; - - if ($post->exists || $post->user->groups()->count()) { - return; - } - - $isSpam = $this->getAkismet()->isSpam( - $post->content, - $post->user->username, - $post->user->email, - null, - 'comment' - ); - - if ($isSpam) { - $post->is_approved = false; - $post->is_spam = true; - - $post->afterSave(function ($post) { - if ($post->number == 1) { - $post->discussion->is_approved = false; - $post->discussion->save(); - } - - $flag = new Flag; - - $flag->post_id = $post->id; - $flag->type = 'akismet'; - $flag->created_at = time(); - - $flag->save(); - }); - } - } - - /** - * @param PostWasApproved $event - */ - public function submitHam(PostWasApproved $event) - { - $post = $event->post; - - if ($post->is_spam) { - $this->getAkismet()->submitHam( - $post->ip_address, - null, - $post->content, - $post->user->username, - $post->user->email - ); - } - } - - /** - * @param Hidden $event - */ - public function submitSpam(Hidden $event) - { - $post = $event->post; - - if ($post->is_spam) { - $this->getAkismet()->submitSpam( - $post->ip_address, - null, - $post->content, - $post->user->username, - $post->user->email - ); - } - } - - /** - * @return Akismet - */ - protected function getAkismet() - { - return new Akismet( - $this->settings->get('flarum-akismet.api_key'), - $this->app->url() - ); - } -} diff --git a/extensions/akismet/src/Listener/SubmitHam.php b/extensions/akismet/src/Listener/SubmitHam.php new file mode 100644 index 000000000..2f7091c02 --- /dev/null +++ b/extensions/akismet/src/Listener/SubmitHam.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Akismet\Listener; + +use Flarum\Approval\Event\PostWasApproved; +use TijsVerkoyen\Akismet\Akismet; + +class SubmitHam +{ + /** + * @var Akismet + */ + protected $akismet; + + public function __construct(Akismet $akismet) + { + $this->akismet = $akismet; + } + + public function handle(PostWasApproved $event) + { + $post = $event->post; + + if ($post->is_spam) { + $this->akismet->submitHam( + $post->ip_address, + null, + $post->content, + $post->user->username, + $post->user->email + ); + } + } +} diff --git a/extensions/akismet/src/Listener/SubmitSpam.php b/extensions/akismet/src/Listener/SubmitSpam.php new file mode 100644 index 000000000..7e2df07f1 --- /dev/null +++ b/extensions/akismet/src/Listener/SubmitSpam.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Akismet\Listener; + +use Flarum\Post\Event\Hidden; +use TijsVerkoyen\Akismet\Akismet; + +class SubmitSpam +{ + /** + * @var Akismet + */ + protected $akismet; + + public function __construct(Akismet $akismet) + { + $this->akismet = $akismet; + } + + public function handle(Hidden $event) + { + $post = $event->post; + + if ($post->is_spam) { + $this->akismet->submitSpam( + $post->ip_address, + null, + $post->content, + $post->user->username, + $post->user->email + ); + } + } +} diff --git a/extensions/akismet/src/Listener/ValidatePost.php b/extensions/akismet/src/Listener/ValidatePost.php new file mode 100644 index 000000000..f989920b0 --- /dev/null +++ b/extensions/akismet/src/Listener/ValidatePost.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Akismet\Listener; + +use Flarum\Flags\Flag; +use Flarum\Post\Event\Saving; +use TijsVerkoyen\Akismet\Akismet; + +class ValidatePost +{ + /** + * @var Akismet + */ + protected $akismet; + + public function __construct(Akismet $akismet) + { + $this->akismet = $akismet; + } + + public function handle(Saving $event) + { + $post = $event->post; + + if ($post->exists || $post->user->groups()->count()) { + return; + } + + $isSpam = $this->akismet->isSpam( + $post->content, + $post->user->username, + $post->user->email, + null, + 'comment' + ); + + if ($isSpam) { + $post->is_approved = false; + $post->is_spam = true; + + $post->afterSave(function ($post) { + if ($post->number == 1) { + $post->discussion->is_approved = false; + $post->discussion->save(); + } + + $flag = new Flag; + + $flag->post_id = $post->id; + $flag->type = 'akismet'; + $flag->created_at = time(); + + $flag->save(); + }); + } + } +}