UserStatusController.php 6.6 KB

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