Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
namespace FoF\Filter;

use Flarum\Extend;
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Saving as PostSaving;
use Flarum\Post\Post;
use Flarum\Settings\Event\Saving as SettingSaving;
use FoF\Filter\Listener\AddCensorChecks;
use FoF\Filter\Listener\AutoMerge;
use FoF\Filter\Listener\CheckPost;
use FoF\Filter\Provider\AutoMergeServiceProvider;

return [
(new Extend\Frontend('admin'))
Expand All @@ -39,8 +38,10 @@

(new Extend\Event())
->listen(SettingSaving::class, AddCensorChecks::class)
->listen(PostSaving::class, CheckPost::class)
->listen(Posted::class, AutoMerge::class),
->listen(PostSaving::class, CheckPost::class),

(new Extend\ServiceProvider())
->register(AutoMergeServiceProvider::class),

(new Extend\Settings())
->default('fof-filter.autoDeletePosts', false)
Expand Down
104 changes: 104 additions & 0 deletions src/Handler/AutoMergePostReplyHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/*
* This file is part of fof/filter.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Filter\Handler;

use Flarum\Discussion\DiscussionRepository;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Post\Command\PostReply;
use Flarum\Post\Command\PostReplyHandler;
use Flarum\Post\CommentPost;
use Flarum\Post\Event\Saving;
use Flarum\Post\PostRepository;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;

class AutoMergePostReplyHandler
{
use DispatchEventsTrait;

protected PostReplyHandler $original;
protected SettingsRepositoryInterface $settings;
protected PostRepository $posts;
protected DiscussionRepository $discussions;

public function __construct(
PostReplyHandler $original,
SettingsRepositoryInterface $settings,
PostRepository $posts,
DiscussionRepository $discussions,
Dispatcher $events
) {
$this->original = $original;
$this->settings = $settings;
$this->posts = $posts;
$this->discussions = $discussions;
$this->events = $events;
}

/**
* @throws PermissionDeniedException
*/
public function handle(PostReply $command): CommentPost
{
if (
!$this->settings->get('fof-filter.autoMergePosts')
|| $command->isFirstPost
// If the new post contains a poll, skip merging to avoid potential issues.
|| Arr::has($command->data, 'attributes.poll')
) {
return $this->original->handle($command);
}

$actor = $command->actor;
$discussion = $this->discussions->findOrFail(
$command->discussionId,
$actor
);

$lastPost = $this->posts->query()
->where('discussion_id', '=', $discussion->id)
->whereNull('hidden_at')
->orderBy('number', 'desc')
->first();

$cooldown = (int) $this->settings->get('fof-filter.cooldown');
if (
!$lastPost instanceof CommentPost
|| $lastPost->user_id !== $actor->id
|| $lastPost->auto_mod
|| ($cooldown > 0 && $lastPost->created_at->lessThanOrEqualTo(Carbon::now()->subMinutes($cooldown)))
) {
return $this->original->handle($command);
}

$actor->assertCan('reply', $discussion);

$newContent = Arr::get($command->data, 'attributes.content', '');
$mergedContent = $lastPost->content."\n\n".$newContent;

$lastPost->revise($mergedContent, $actor);

$this->events->dispatch(
new Saving($lastPost, $actor, [
'attributes' => ['content' => $mergedContent],
])
);

$lastPost->save();
$this->dispatchEventsFor($lastPost, $actor);

return $lastPost;
}
}
68 changes: 0 additions & 68 deletions src/Listener/AutoMerge.php

This file was deleted.

26 changes: 26 additions & 0 deletions src/Provider/AutoMergeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of fof/filter.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Filter\Provider;

use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Post\Command\PostReplyHandler;
use FoF\Filter\Handler\AutoMergePostReplyHandler;

class AutoMergeServiceProvider extends AbstractServiceProvider
{
public function register()
{
$this->container->extend(PostReplyHandler::class, function (PostReplyHandler $handler, $container) {
return $container->make(AutoMergePostReplyHandler::class, ['original' => $handler]);
});
}
}
Loading