Skip to content

Commit

Permalink
Merge pull request #1 from blomstra/im/dismiss-dupe-flags
Browse files Browse the repository at this point in the history
Add listener, auto dismiss duplicate flags after merge
  • Loading branch information
imorland authored Jan 12, 2022
2 parents 06d41b7 + f6f0fd8 commit b1a6523
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Flarum\Api\Controller\ShowDiscussionController;
use Flarum\Extend;
use FoF\MergeDiscussions\Events\DiscussionWasMerged;

return [
(new Extend\Frontend('forum'))
Expand All @@ -23,4 +24,7 @@

(new Extend\ApiController(ShowDiscussionController::class))
->addInclude(['firstPost']),

(new Extend\Event())
->listen(DiscussionWasMerged::class, Listener\RemoveDuplicateFlagAfterMerge::class),
];
48 changes: 48 additions & 0 deletions src/Listener/RemoveDuplicateFlagAfterMerge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of blomstra/flag-duplicates.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Blomstra\FlagDuplicates\Listener;

use Flarum\Flags\Event\Deleting;
use Flarum\Flags\Flag;
use FoF\MergeDiscussions\Events\DiscussionWasMerged;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Collection;

class RemoveDuplicateFlagAfterMerge
{
/** @var Dispatcher */
protected $events;

public function __construct(Dispatcher $events)
{
$this->events = $events;
}

public function handle(DiscussionWasMerged $event)
{
$discussion = $event->discussion;

/** @var array $postIds */
$postIds = $discussion->posts->pluck('id');

/** @var Collection $flags */
$flags = Flag::where('reason', 'duplicate')->whereIn('post_id', $postIds)->where('reason_detail', $discussion->id)->get();

foreach ($flags as $flag) {
/** @var Flag $flag */
$this->events->dispatch(new Deleting($flag, $event->actor));
$flag->delete();
}

$discussion->refresh();
}
}

0 comments on commit b1a6523

Please sign in to comment.