123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- declare(strict_types=1);
- namespace OCA\UserStatus\Controller;
- use OCA\DAV\CalDAV\Status\StatusService as CalendarStatusService;
- use OCA\UserStatus\Db\UserStatus;
- use OCA\UserStatus\Exception\InvalidClearAtException;
- use OCA\UserStatus\Exception\InvalidMessageIdException;
- use OCA\UserStatus\Exception\InvalidStatusIconException;
- use OCA\UserStatus\Exception\InvalidStatusTypeException;
- use OCA\UserStatus\Exception\StatusMessageTooLongException;
- use OCA\UserStatus\ResponseDefinitions;
- use OCA\UserStatus\Service\StatusService;
- use OCP\AppFramework\Db\DoesNotExistException;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Http\Attribute\ApiRoute;
- use OCP\AppFramework\Http\Attribute\NoAdminRequired;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\AppFramework\OCS\OCSBadRequestException;
- use OCP\AppFramework\OCS\OCSNotFoundException;
- use OCP\AppFramework\OCSController;
- use OCP\IRequest;
- use Psr\Log\LoggerInterface;
- class UserStatusController extends OCSController {
- public function __construct(
- string $appName,
- IRequest $request,
- private string $userId,
- private LoggerInterface $logger,
- private StatusService $service,
- private CalendarStatusService $calendarStatusService,
- ) {
- parent::__construct($appName, $request);
- }
-
-
-
- public function getStatus(): DataResponse {
- try {
- $this->calendarStatusService->processCalendarStatus($this->userId);
- $userStatus = $this->service->findByUserId($this->userId);
- } catch (DoesNotExistException $ex) {
- throw new OCSNotFoundException('No status for the current user');
- }
- return new DataResponse($this->formatStatus($userStatus));
- }
-
-
-
- public function setStatus(string $statusType): DataResponse {
- try {
- $status = $this->service->setStatus($this->userId, $statusType, null, true);
- $this->service->removeBackupUserStatus($this->userId);
- return new DataResponse($this->formatStatus($status));
- } catch (InvalidStatusTypeException $ex) {
- $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid status type "' . $statusType . '"');
- throw new OCSBadRequestException($ex->getMessage(), $ex);
- }
- }
-
-
-
- public function setPredefinedMessage(string $messageId,
- ?int $clearAt): DataResponse {
- try {
- $status = $this->service->setPredefinedMessage($this->userId, $messageId, $clearAt);
- $this->service->removeBackupUserStatus($this->userId);
- return new DataResponse($this->formatStatus($status));
- } catch (InvalidClearAtException $ex) {
- $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid clearAt value "' . $clearAt . '"');
- throw new OCSBadRequestException($ex->getMessage(), $ex);
- } catch (InvalidMessageIdException $ex) {
- $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid message-id "' . $messageId . '"');
- throw new OCSBadRequestException($ex->getMessage(), $ex);
- }
- }
-
-
-
- public function setCustomMessage(?string $statusIcon,
- ?string $message,
- ?int $clearAt): DataResponse {
- try {
- if (($statusIcon !== null && $statusIcon !== '') || ($message !== null && $message !== '') || ($clearAt !== null && $clearAt !== 0)) {
- $status = $this->service->setCustomMessage($this->userId, $statusIcon, $message, $clearAt);
- } else {
- $this->service->clearMessage($this->userId);
- $status = $this->service->findByUserId($this->userId);
- }
- $this->service->removeBackupUserStatus($this->userId);
- return new DataResponse($this->formatStatus($status));
- } catch (InvalidClearAtException $ex) {
- $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid clearAt value "' . $clearAt . '"');
- throw new OCSBadRequestException($ex->getMessage(), $ex);
- } catch (InvalidStatusIconException $ex) {
- $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid icon value "' . $statusIcon . '"');
- throw new OCSBadRequestException($ex->getMessage(), $ex);
- } catch (StatusMessageTooLongException $ex) {
- $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to a too long status message.');
- throw new OCSBadRequestException($ex->getMessage(), $ex);
- }
- }
-
-
-
- public function clearMessage(): DataResponse {
- $this->service->clearMessage($this->userId);
- return new DataResponse([]);
- }
-
-
-
- public function revertStatus(string $messageId): DataResponse {
- $backupStatus = $this->service->revertUserStatus($this->userId, $messageId, true);
- if ($backupStatus) {
- return new DataResponse($this->formatStatus($backupStatus));
- }
- return new DataResponse([]);
- }
-
- private function formatStatus(UserStatus $status): array {
-
- $visibleStatus = $status->getStatus();
- return [
- 'userId' => $status->getUserId(),
- 'message' => $status->getCustomMessage(),
- 'messageId' => $status->getMessageId(),
- 'messageIsPredefined' => $status->getMessageId() !== null,
- 'icon' => $status->getCustomIcon(),
- 'clearAt' => $status->getClearAt(),
- 'status' => $visibleStatus,
- 'statusIsUserDefined' => $status->getIsUserDefined(),
- ];
- }
- }
|