-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathUserIgnoreAction.class.php
More file actions
143 lines (126 loc) · 4.86 KB
/
UserIgnoreAction.class.php
File metadata and controls
143 lines (126 loc) · 4.86 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace wcf\action;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use wcf\command\user\IgnoreUser;
use wcf\command\user\UnignoreUser;
use wcf\data\user\ignore\UserIgnore;
use wcf\data\user\UserProfile;
use wcf\http\Helper;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\cache\runtime\UserRuntimeCache;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\PermissionDeniedException;
use wcf\system\form\builder\field\RadioButtonFormField;
use wcf\system\form\builder\field\validation\FormFieldValidationError;
use wcf\system\form\builder\field\validation\FormFieldValidator;
use wcf\system\form\builder\Psr15DialogForm;
use wcf\system\WCF;
/**
* Handles user ignores.
*
* @author Marcel Werk
* @copyright 2001-2023 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
*/
final class UserIgnoreAction implements RequestHandlerInterface
{
/**
* @inheritDoc
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$parameters = Helper::mapQueryParameters(
$request->getQueryParams(),
<<<'EOT'
array {
id: positive-int
}
EOT
);
$this->assertUserIsLoggedIn();
$user = UserProfileRuntimeCache::getInstance()->getObject($parameters['id']);
$ignore = UserIgnore::getIgnore($parameters['id']);
$this->assertTargetCanBeIgnored($user, $ignore);
$form = $this->getForm($user, $ignore);
if ($request->getMethod() === 'GET') {
return $form->toResponse();
} elseif ($request->getMethod() === 'POST') {
$response = $form->validateRequest($request);
if ($response !== null) {
return $response;
}
$type = \intval($form->getData()['data']['type']);
$user = UserRuntimeCache::getInstance()->getObject($parameters['id']);
if ($type === UserIgnore::TYPE_NO_IGNORE) {
(new UnignoreUser(WCF::getUser(), $user))();
} else {
(new IgnoreUser(WCF::getUser(), $user, $type))();
}
return new JsonResponse([
'result' => [
'type' => $type,
],
]);
} else {
throw new \LogicException('Unreachable');
}
}
private function assertUserIsLoggedIn(): void
{
if (!WCF::getUser()->userID) {
throw new PermissionDeniedException();
}
}
private function assertTargetCanBeIgnored(?UserProfile $target, UserIgnore $ignore): void
{
if (!$target) {
throw new IllegalLinkException();
}
if ($target->userID === WCF::getUser()->userID) {
throw new IllegalLinkException();
}
// Check if the user is not yet ignored and cannot be ignored.
if ($ignore->type == UserIgnore::TYPE_NO_IGNORE && $target->getPermission('user.profile.cannotBeIgnored')) {
throw new PermissionDeniedException();
}
}
private function getForm(UserProfile $user, UserIgnore $ignore): Psr15DialogForm
{
$form = new Psr15DialogForm(
static::class,
WCF::getLanguage()->get('wcf.user.ignore.type')
);
$form->appendChildren([
RadioButtonFormField::create('type')
->required()
->options([
UserIgnore::TYPE_NO_IGNORE => WCF::getLanguage()
->get('wcf.user.ignore.type.noIgnore'),
UserIgnore::TYPE_BLOCK_DIRECT_CONTACT => WCF::getLanguage()
->get('wcf.user.ignore.type.blockDirectContact'),
UserIgnore::TYPE_HIDE_MESSAGES => WCF::getLanguage()
->get('wcf.user.ignore.type.hideMessages'),
])
->value($ignore->type ?: 0)
->addValidator(new FormFieldValidator('type', function (RadioButtonFormField $formField) use ($user) {
if ($user->getPermission('user.profile.cannotBeIgnored')) {
if ($formField->getValue() != UserIgnore::TYPE_NO_IGNORE) {
$formField->addValidationError(
new FormFieldValidationError(
'cannotBeIgnored',
'wcf.user.ignore.error.cannotBeIgnored'
)
);
}
}
})),
]);
$form->markRequiredFields(false);
$form->build();
return $form;
}
}