Passed
Push — master ( 158405...14a468 )
by John
14:13 queued 12s
created

UserStatusController::clearStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Georg Ehrke
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author Simon Spannagel <[email protected]>
11
 *
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
namespace OCA\UserStatus\Controller;
29
30
use OCA\UserStatus\Db\UserStatus;
31
use OCA\UserStatus\Exception\InvalidClearAtException;
32
use OCA\UserStatus\Exception\InvalidMessageIdException;
33
use OCA\UserStatus\Exception\InvalidStatusIconException;
34
use OCA\UserStatus\Exception\InvalidStatusTypeException;
35
use OCA\UserStatus\Exception\StatusMessageTooLongException;
36
use OCA\UserStatus\Service\StatusService;
37
use OCP\AppFramework\Db\DoesNotExistException;
38
use OCP\AppFramework\Http\DataResponse;
39
use OCP\AppFramework\OCS\OCSBadRequestException;
40
use OCP\AppFramework\OCS\OCSNotFoundException;
41
use OCP\AppFramework\OCSController;
42
use OCP\ILogger;
43
use OCP\IRequest;
44
45
class UserStatusController extends OCSController {
46
47
	/** @var string */
48
	private $userId;
49
50
	/** @var ILogger */
51
	private $logger;
52
53
	/** @var StatusService */
54
	private $service;
55
56
	/**
57
	 * StatusesController constructor.
58
	 *
59
	 * @param string $appName
60
	 * @param IRequest $request
61
	 * @param string $userId
62
	 * @param ILogger $logger;
63
	 * @param StatusService $service
64
	 */
65
	public function __construct(string $appName,
66
								IRequest $request,
67
								string $userId,
68
								ILogger $logger,
69
								StatusService $service) {
70
		parent::__construct($appName, $request);
71
		$this->userId = $userId;
72
		$this->logger = $logger;
73
		$this->service = $service;
74
	}
75
76
	/**
77
	 * @NoAdminRequired
78
	 *
79
	 * @return DataResponse
80
	 * @throws OCSNotFoundException
81
	 */
82
	public function getStatus(): DataResponse {
83
		try {
84
			$userStatus = $this->service->findByUserId($this->userId);
85
		} catch (DoesNotExistException $ex) {
86
			throw new OCSNotFoundException('No status for the current user');
87
		}
88
89
		return new DataResponse($this->formatStatus($userStatus));
90
	}
91
92
	/**
93
	 * @NoAdminRequired
94
	 *
95
	 * @param string $statusType
96
	 * @return DataResponse
97
	 * @throws OCSBadRequestException
98
	 */
99
	public function setStatus(string $statusType): DataResponse {
100
		try {
101
			$status = $this->service->setStatus($this->userId, $statusType, null, true);
102
103
			$this->service->removeBackupUserStatus($this->userId);
104
			return new DataResponse($this->formatStatus($status));
105
		} catch (InvalidStatusTypeException $ex) {
106
			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid status type "' . $statusType . '"');
107
			throw new OCSBadRequestException($ex->getMessage(), $ex);
108
		}
109
	}
110
111
	/**
112
	 * @NoAdminRequired
113
	 *
114
	 * @param string $messageId
115
	 * @param int|null $clearAt
116
	 * @return DataResponse
117
	 * @throws OCSBadRequestException
118
	 */
119
	public function setPredefinedMessage(string $messageId,
120
										 ?int $clearAt): DataResponse {
121
		try {
122
			$status = $this->service->setPredefinedMessage($this->userId, $messageId, $clearAt);
123
			$this->service->removeBackupUserStatus($this->userId);
124
			return new DataResponse($this->formatStatus($status));
125
		} catch (InvalidClearAtException $ex) {
126
			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid clearAt value "' . $clearAt . '"');
127
			throw new OCSBadRequestException($ex->getMessage(), $ex);
128
		} catch (InvalidMessageIdException $ex) {
129
			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid message-id "' . $messageId . '"');
130
			throw new OCSBadRequestException($ex->getMessage(), $ex);
131
		}
132
	}
133
134
	/**
135
	 * @NoAdminRequired
136
	 *
137
	 * @param string|null $statusIcon
138
	 * @param string|null $message
139
	 * @param int|null $clearAt
140
	 * @return DataResponse
141
	 * @throws OCSBadRequestException
142
	 */
143
	public function setCustomMessage(?string $statusIcon,
144
									 ?string $message,
145
									 ?int $clearAt): DataResponse {
146
		try {
147
			if (($message !== null && $message !== '') || ($clearAt !== null && $clearAt !== 0)) {
148
				$status = $this->service->setCustomMessage($this->userId, $statusIcon, $message, $clearAt);
149
			} else {
150
				$this->service->clearMessage($this->userId);
151
				$status = $this->service->findByUserId($this->userId);
152
			}
153
			$this->service->removeBackupUserStatus($this->userId);
154
			return new DataResponse($this->formatStatus($status));
155
		} catch (InvalidClearAtException $ex) {
156
			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid clearAt value "' . $clearAt . '"');
157
			throw new OCSBadRequestException($ex->getMessage(), $ex);
158
		} catch (InvalidStatusIconException $ex) {
159
			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid icon value "' . $statusIcon . '"');
160
			throw new OCSBadRequestException($ex->getMessage(), $ex);
161
		} catch (StatusMessageTooLongException $ex) {
162
			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to a too long status message.');
163
			throw new OCSBadRequestException($ex->getMessage(), $ex);
164
		}
165
	}
166
167
	/**
168
	 * @NoAdminRequired
169
	 *
170
	 * @return DataResponse
171
	 */
172
	public function clearMessage(): DataResponse {
173
		$this->service->clearMessage($this->userId);
174
		return new DataResponse([]);
175
	}
176
177
	/**
178
	 * @NoAdminRequired
179
	 *
180
	 * @return DataResponse
181
	 */
182
	public function revertStatus(string $messageId): DataResponse {
183
		$backupStatus = $this->service->revertUserStatus($this->userId, $messageId, true);
184
		if ($backupStatus) {
185
			return new DataResponse($this->formatStatus($backupStatus));
186
		}
187
		return new DataResponse([]);
188
	}
189
190
	/**
191
	 * @param UserStatus $status
192
	 * @return array
193
	 */
194
	private function formatStatus(UserStatus $status): array {
195
		return [
196
			'userId' => $status->getUserId(),
197
			'message' => $status->getCustomMessage(),
198
			'messageId' => $status->getMessageId(),
199
			'messageIsPredefined' => $status->getMessageId() !== null,
200
			'icon' => $status->getCustomIcon(),
201
			'clearAt' => $status->getClearAt(),
202
			'status' => $status->getStatus(),
203
			'statusIsUserDefined' => $status->getIsUserDefined(),
204
		];
205
	}
206
}
207