This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
MarkAsSpammerController.php
119 lines (102 loc) · 3.37 KB
/
MarkAsSpammerController.php
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/*
* This file is part of fof/spamblock.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FoF\Spamblock\Controllers;
use Carbon\Carbon;
use Flarum\Discussion\Command\EditDiscussion;
use Flarum\Extension\ExtensionManager;
use Flarum\Flags\Command\DeleteFlags;
use Flarum\Http\RequestUtil;
use Flarum\Post\Command\EditPost;
use Flarum\User\Command\EditUser;
use Flarum\User\User;
use FoF\Spamblock\Event\MarkedUserAsSpammer;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class MarkAsSpammerController implements RequestHandlerInterface
{
/**
* @var Dispatcher
*/
protected $bus;
/**
* @var EventsDispatcher
*/
protected $events;
/**
* @var ExtensionManager
*/
protected $extensions;
/**
* @param EventsDispatcher $events
* @param Dispatcher $bus
* @param ExtensionManager $extensions
*/
public function __construct(Dispatcher $bus, EventsDispatcher $events, ExtensionManager $extensions)
{
$this->bus = $bus;
$this->events = $events;
$this->extensions = $extensions;
}
/**
* Handle the request and return a response.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$actor = RequestUtil::getActor($request);
$userId = Arr::get($request->getQueryParams(), 'id');
$user = User::findOrFail($userId);
$actor->assertCan('spamblock', $user);
$flarumSuspend = $this->extensions->isEnabled('flarum-suspend');
$flarumFlags = $this->extensions->isEnabled('flarum-flags');
if ($flarumSuspend && !isset($user->suspended_until)) {
$this->bus->dispatch(
new EditUser($user->id, $actor, [
'attributes' => ['suspendedUntil' => Carbon::now()->addYear(20)],
])
);
}
$user->posts()->where('hidden_at', null)->chunk(50, function ($posts) use ($actor, $flarumFlags) {
foreach ($posts as $post) {
$this->bus->dispatch(
new EditPost($post->id, $actor, [
'attributes' => ['isHidden' => true],
])
);
if ($flarumFlags) {
$this->bus->dispatch(
new DeleteFlags($post->id, $actor)
);
}
}
});
$user->discussions()->where('hidden_at', null)->chunk(50, function ($discussions) use ($actor) {
foreach ($discussions as $discussion) {
$this->bus->dispatch(
new EditDiscussion($discussion->id, $actor, [
'attributes' => ['isHidden' => true],
])
);
}
});
$this->events->dispatch(
new MarkedUserAsSpammer($user, $actor)
);
return (new Response())->withStatus(204);
}
}