JSDataService.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UserStatus\Service;
  8. use OCP\AppFramework\Db\DoesNotExistException;
  9. use OCP\IUserSession;
  10. use OCP\UserStatus\IUserStatus;
  11. class JSDataService implements \JsonSerializable {
  12. /** @var IUserSession */
  13. private $userSession;
  14. /** @var StatusService */
  15. private $statusService;
  16. /**
  17. * JSDataService constructor.
  18. *
  19. * @param IUserSession $userSession
  20. * @param StatusService $statusService
  21. */
  22. public function __construct(IUserSession $userSession,
  23. StatusService $statusService) {
  24. $this->userSession = $userSession;
  25. $this->statusService = $statusService;
  26. }
  27. public function jsonSerialize(): array {
  28. $user = $this->userSession->getUser();
  29. if ($user === null) {
  30. return [];
  31. }
  32. try {
  33. $status = $this->statusService->findByUserId($user->getUID());
  34. } catch (DoesNotExistException $ex) {
  35. return [
  36. 'userId' => $user->getUID(),
  37. 'message' => null,
  38. 'messageId' => null,
  39. 'messageIsPredefined' => false,
  40. 'icon' => null,
  41. 'clearAt' => null,
  42. 'status' => IUserStatus::OFFLINE,
  43. 'statusIsUserDefined' => false,
  44. ];
  45. }
  46. return [
  47. 'userId' => $status->getUserId(),
  48. 'message' => $status->getCustomMessage(),
  49. 'messageId' => $status->getMessageId(),
  50. 'messageIsPredefined' => $status->getMessageId() !== null,
  51. 'icon' => $status->getCustomIcon(),
  52. 'clearAt' => $status->getClearAt(),
  53. 'status' => $status->getStatus(),
  54. 'statusIsUserDefined' => $status->getIsUserDefined(),
  55. ];
  56. }
  57. }