-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAutoMergePostReplyHandler.php
More file actions
104 lines (88 loc) · 3.04 KB
/
AutoMergePostReplyHandler.php
File metadata and controls
104 lines (88 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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;
}
}