You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JSDataService.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UserStatus\Service;
  24. use OCP\AppFramework\Db\DoesNotExistException;
  25. use OCP\IUserSession;
  26. class JSDataService implements \JsonSerializable {
  27. /** @var IUserSession */
  28. private $userSession;
  29. /** @var StatusService */
  30. private $statusService;
  31. /**
  32. * JSDataService constructor.
  33. *
  34. * @param IUserSession $userSession
  35. * @param StatusService $statusService
  36. */
  37. public function __construct(IUserSession $userSession,
  38. StatusService $statusService) {
  39. $this->userSession = $userSession;
  40. $this->statusService = $statusService;
  41. }
  42. public function jsonSerialize() {
  43. $user = $this->userSession->getUser();
  44. if ($user === null) {
  45. return [];
  46. }
  47. try {
  48. $status = $this->statusService->findByUserId($user->getUID());
  49. } catch (DoesNotExistException $ex) {
  50. return [
  51. 'userId' => $user->getUID(),
  52. 'message' => null,
  53. 'messageId' => null,
  54. 'messageIsPredefined' => false,
  55. 'icon' => null,
  56. 'clearAt' => null,
  57. 'status' => 'offline',
  58. 'statusIsUserDefined' => false,
  59. ];
  60. }
  61. return [
  62. 'userId' => $status->getUserId(),
  63. 'message' => $status->getCustomMessage(),
  64. 'messageId' => $status->getMessageId(),
  65. 'messageIsPredefined' => $status->getMessageId() !== null,
  66. 'icon' => $status->getCustomIcon(),
  67. 'clearAt' => $status->getClearAt(),
  68. 'status' => $status->getStatus(),
  69. 'statusIsUserDefined' => $status->getIsUserDefined(),
  70. ];
  71. }
  72. }