From 5bb266ec3bbe7b055e4a1d81771f037f6d158e0c Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 22 Sep 2015 18:15:56 +0930 Subject: [PATCH] Update for new Flags/Approval extensions --- extensions/akismet/js/forum/src/main.js | 38 ++++++++++--------- .../akismet/src/Listeners/ValidatePost.php | 36 +++++++++--------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/extensions/akismet/js/forum/src/main.js b/extensions/akismet/js/forum/src/main.js index dbe61771c..8d559d81a 100644 --- a/extensions/akismet/js/forum/src/main.js +++ b/extensions/akismet/js/forum/src/main.js @@ -1,25 +1,27 @@ -import { extend } from 'flarum/extend'; +import { extend, override } from 'flarum/extend'; import app from 'flarum/app'; -import Button from 'flarum/components/Button'; -import CommentPost from 'flarum/components/CommentPost'; import PostControls from 'flarum/utils/PostControls'; +import CommentPost from 'flarum/components/CommentPost'; app.initializers.add('akismet', () => { - extend(CommentPost.prototype, 'reportActionItems', function(items) { - if (this.props.post.reports()[0].reporter() === 'Akismet') { - items.add('notSpam', - - ); + extend(PostControls, 'destructiveControls', function(items, post) { + if (items.approve) { + const flags = post.flags(); + + if (flags && flags.some(flag => flag.type() === 'akismet')) { + Object.assign(items.approve.content.props, { + children: 'Not Spam' + }); + } } }); -}, -10); // set initializer priority to run after reports + + override(CommentPost.prototype, 'flagReason', function(original, flag) { + if (flag.type() === 'akismet') { + return 'Akismet flagged as Spam'; + } + + return original(flag); + }); +}, -20); // run after the approval extension diff --git a/extensions/akismet/src/Listeners/ValidatePost.php b/extensions/akismet/src/Listeners/ValidatePost.php index 2e31def03..1b70d6a31 100644 --- a/extensions/akismet/src/Listeners/ValidatePost.php +++ b/extensions/akismet/src/Listeners/ValidatePost.php @@ -16,14 +16,13 @@ use TijsVerkoyen\Akismet\Akismet; use Flarum\Core; use Flarum\Core\Posts\CommentPost; use Flarum\Core\Settings\SettingsRepository; -use Flarum\Reports\Report; +use Flarum\Flags\Flag; +use Flarum\Approval\Events\PostWasApproved; class ValidatePost { protected $settings; - private $savingPost; - public function __construct(SettingsRepository $settings) { $this->settings = $settings; @@ -32,6 +31,7 @@ class ValidatePost public function subscribe(Dispatcher $events) { $events->listen(PostWillBeSaved::class, [$this, 'validatePost']); + $events->listen(PostWasApproved::class, [$this, 'submitHam']); } public function validatePost(PostWillBeSaved $event) @@ -53,26 +53,26 @@ class ValidatePost ); if ($isSpam) { - $post->hide(); + $post->is_approved = false; - $this->savingPost = $post; + // TODO: + // $post->is_spam = true; - CommentPost::saved(function (CommentPost $post) { - if ($post !== $this->savingPost) { - return; - } + $post->afterSave(function ($post) { + $flag = new Flag; - $report = new Report; + $flag->post_id = $post->id; + $flag->type = 'akismet'; + $flag->time = time(); - $report->post_id = $post->id; - $report->reporter = 'Akismet'; - $report->reason = 'spam'; - $report->time = time(); - - $report->save(); - - $this->savingPost = null; + $flag->save(); }); } } + + public function submitHam(PostWasApproved $event) + { + // TODO + // if ($post->is_spam) + } }