1
0

JSDataService.php 2.3 KB

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