@@ -44,163 +44,163 @@ |
||
| 44 | 44 | |
| 45 | 45 | class UserStatusController extends OCSController { |
| 46 | 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 | - } |
|
| 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 | 206 | } |