mirror of
https://github.com/flarum/core.git
synced 2025-07-17 23:01:17 +02:00
Get rid of event subscribers that resolve services too early
Refs flarum/core#1578.
This commit is contained in:
@@ -10,8 +10,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
use Flarum\Akismet\Listener;
|
use Flarum\Akismet\Listener;
|
||||||
|
use Flarum\Approval\Event\PostWasApproved;
|
||||||
use Flarum\Extend;
|
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 Illuminate\Contracts\Events\Dispatcher;
|
||||||
|
use TijsVerkoyen\Akismet\Akismet;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
(new Extend\Frontend('forum'))
|
(new Extend\Frontend('forum'))
|
||||||
@@ -20,7 +26,18 @@ return [
|
|||||||
(new Extend\Frontend('admin'))
|
(new Extend\Frontend('admin'))
|
||||||
->js(__DIR__.'/js/dist/admin.js'),
|
->js(__DIR__.'/js/dist/admin.js'),
|
||||||
|
|
||||||
function (Dispatcher $events) {
|
function (Dispatcher $events, Container $container) {
|
||||||
$events->subscribe(Listener\FilterNewPosts::class);
|
$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);
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
@@ -1,141 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
43
extensions/akismet/src/Listener/SubmitHam.php
Normal file
43
extensions/akismet/src/Listener/SubmitHam.php
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
43
extensions/akismet/src/Listener/SubmitSpam.php
Normal file
43
extensions/akismet/src/Listener/SubmitSpam.php
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
66
extensions/akismet/src/Listener/ValidatePost.php
Normal file
66
extensions/akismet/src/Listener/ValidatePost.php
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user