From 2a76c278140dc71319d9d716327161174984f7cc Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 31 Oct 2015 14:50:07 +1030 Subject: [PATCH] Submit spam and ham feedback to Akismet --- ...2015_10_31_040129_add_is_spam_to_posts.php | 31 +++++++++++ .../akismet/src/Listener/FilterNewPosts.php | 53 +++++++++++++++---- 2 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 extensions/akismet/migrations/2015_10_31_040129_add_is_spam_to_posts.php diff --git a/extensions/akismet/migrations/2015_10_31_040129_add_is_spam_to_posts.php b/extensions/akismet/migrations/2015_10_31_040129_add_is_spam_to_posts.php new file mode 100644 index 000000000..5d8337586 --- /dev/null +++ b/extensions/akismet/migrations/2015_10_31_040129_add_is_spam_to_posts.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Akismet\Migration; + +use Flarum\Database\AbstractMigration; +use Illuminate\Database\Schema\Blueprint; + +class AddIsSpamToPosts extends AbstractMigration +{ + public function up() + { + $this->schema->table('posts', function (Blueprint $table) { + $table->boolean('is_spam')->default(0); + }); + } + + public function down() + { + $this->schema->table('posts', function (Blueprint $table) { + $table->dropColumn('is_spam'); + }); + } +} diff --git a/extensions/akismet/src/Listener/FilterNewPosts.php b/extensions/akismet/src/Listener/FilterNewPosts.php index 43659ef95..ca453cefc 100644 --- a/extensions/akismet/src/Listener/FilterNewPosts.php +++ b/extensions/akismet/src/Listener/FilterNewPosts.php @@ -11,7 +11,7 @@ namespace Flarum\Akismet\Listener; use Flarum\Approval\Event\PostWasApproved; -use Flarum\Core; +use Flarum\Event\PostWasHidden; use Flarum\Event\PostWillBeSaved; use Flarum\Flags\Flag; use Flarum\Foundation\Application; @@ -48,6 +48,7 @@ class FilterNewPosts { $events->listen(PostWillBeSaved::class, [$this, 'validatePost']); $events->listen(PostWasApproved::class, [$this, 'submitHam']); + $events->listen(PostWasHidden::class, [$this, 'submitSpam']); } /** @@ -61,9 +62,7 @@ class FilterNewPosts return; } - $akismet = new Akismet($this->settings->get('flarum-akismet.api_key'), $this->app->url()); - - $isSpam = $akismet->isSpam( + $isSpam = $this->getAkismet()->isSpam( $post->content, $post->user->username, $post->user->email, @@ -73,9 +72,7 @@ class FilterNewPosts if ($isSpam) { $post->is_approved = false; - - // TODO: - // $post->is_spam = true; + $post->is_spam = true; $post->afterSave(function ($post) { $flag = new Flag; @@ -94,7 +91,45 @@ class FilterNewPosts */ public function submitHam(PostWasApproved $event) { - // TODO - // if ($post->is_spam) + $post = $event->post; + + if ($post->is_spam) { + $this->getAkismet()->submitHam( + $post->ip_address, + null, + $post->content, + $post->user->username, + $post->user->email + ); + } + } + + /** + * @param PostWasHidden $event + */ + public function submitSpam(PostWasHidden $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() + ); } }