JSDataService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. /**
  13. * JSDataService constructor.
  14. *
  15. * @param IUserSession $userSession
  16. * @param StatusService $statusService
  17. */
  18. public function __construct(
  19. private IUserSession $userSession,
  20. private StatusService $statusService,
  21. ) {
  22. }
  23. public function jsonSerialize(): array {
  24. $user = $this->userSession->getUser();
  25. if ($user === null) {
  26. return [];
  27. }
  28. try {
  29. $status = $this->statusService->findByUserId($user->getUID());
  30. } catch (DoesNotExistException $ex) {
  31. return [
  32. 'userId' => $user->getUID(),
  33. 'message' => null,
  34. 'messageId' => null,
  35. 'messageIsPredefined' => false,
  36. 'icon' => null,
  37. 'clearAt' => null,
  38. 'status' => IUserStatus::OFFLINE,
  39. 'statusIsUserDefined' => false,
  40. ];
  41. }
  42. return [
  43. 'userId' => $status->getUserId(),
  44. 'message' => $status->getCustomMessage(),
  45. 'messageId' => $status->getMessageId(),
  46. 'messageIsPredefined' => $status->getMessageId() !== null,
  47. 'icon' => $status->getCustomIcon(),
  48. 'clearAt' => $status->getClearAt(),
  49. 'status' => $status->getStatus(),
  50. 'statusIsUserDefined' => $status->getIsUserDefined(),
  51. ];
  52. }
  53. }