mirror of
https://github.com/flarum/framework.git
synced 2025-02-02 02:55:31 +08:00
Submit spam and ham feedback to Akismet
This commit is contained in:
parent
f7f25c8f41
commit
2a76c27814
|
@ -0,0 +1,31 @@
|
||||||
|
<?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\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');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,7 @@
|
||||||
namespace Flarum\Akismet\Listener;
|
namespace Flarum\Akismet\Listener;
|
||||||
|
|
||||||
use Flarum\Approval\Event\PostWasApproved;
|
use Flarum\Approval\Event\PostWasApproved;
|
||||||
use Flarum\Core;
|
use Flarum\Event\PostWasHidden;
|
||||||
use Flarum\Event\PostWillBeSaved;
|
use Flarum\Event\PostWillBeSaved;
|
||||||
use Flarum\Flags\Flag;
|
use Flarum\Flags\Flag;
|
||||||
use Flarum\Foundation\Application;
|
use Flarum\Foundation\Application;
|
||||||
|
@ -48,6 +48,7 @@ class FilterNewPosts
|
||||||
{
|
{
|
||||||
$events->listen(PostWillBeSaved::class, [$this, 'validatePost']);
|
$events->listen(PostWillBeSaved::class, [$this, 'validatePost']);
|
||||||
$events->listen(PostWasApproved::class, [$this, 'submitHam']);
|
$events->listen(PostWasApproved::class, [$this, 'submitHam']);
|
||||||
|
$events->listen(PostWasHidden::class, [$this, 'submitSpam']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,9 +62,7 @@ class FilterNewPosts
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$akismet = new Akismet($this->settings->get('flarum-akismet.api_key'), $this->app->url());
|
$isSpam = $this->getAkismet()->isSpam(
|
||||||
|
|
||||||
$isSpam = $akismet->isSpam(
|
|
||||||
$post->content,
|
$post->content,
|
||||||
$post->user->username,
|
$post->user->username,
|
||||||
$post->user->email,
|
$post->user->email,
|
||||||
|
@ -73,9 +72,7 @@ class FilterNewPosts
|
||||||
|
|
||||||
if ($isSpam) {
|
if ($isSpam) {
|
||||||
$post->is_approved = false;
|
$post->is_approved = false;
|
||||||
|
$post->is_spam = true;
|
||||||
// TODO:
|
|
||||||
// $post->is_spam = true;
|
|
||||||
|
|
||||||
$post->afterSave(function ($post) {
|
$post->afterSave(function ($post) {
|
||||||
$flag = new Flag;
|
$flag = new Flag;
|
||||||
|
@ -94,7 +91,45 @@ class FilterNewPosts
|
||||||
*/
|
*/
|
||||||
public function submitHam(PostWasApproved $event)
|
public function submitHam(PostWasApproved $event)
|
||||||
{
|
{
|
||||||
// TODO
|
$post = $event->post;
|
||||||
// if ($post->is_spam)
|
|
||||||
|
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()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user