UserLiveStatusListener.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Listener;
  25. use OCA\DAV\CalDAV\Status\StatusService as CalendarStatusService;
  26. use OCA\UserStatus\Connector\UserStatus as ConnectorUserStatus;
  27. use OCA\UserStatus\Db\UserStatus;
  28. use OCA\UserStatus\Db\UserStatusMapper;
  29. use OCA\UserStatus\Service\StatusService;
  30. use OCP\AppFramework\Db\DoesNotExistException;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\DB\Exception;
  33. use OCP\EventDispatcher\Event;
  34. use OCP\EventDispatcher\IEventListener;
  35. use OCP\User\Events\UserLiveStatusEvent;
  36. use OCP\UserStatus\IUserStatus;
  37. use Psr\Log\LoggerInterface;
  38. /**
  39. * Class UserDeletedListener
  40. *
  41. * @package OCA\UserStatus\Listener
  42. * @template-implements IEventListener<UserLiveStatusEvent>
  43. */
  44. class UserLiveStatusListener implements IEventListener {
  45. private UserStatusMapper $mapper;
  46. private StatusService $statusService;
  47. private ITimeFactory $timeFactory;
  48. public function __construct(UserStatusMapper $mapper,
  49. StatusService $statusService,
  50. ITimeFactory $timeFactory,
  51. private CalendarStatusService $calendarStatusService,
  52. private LoggerInterface $logger) {
  53. $this->mapper = $mapper;
  54. $this->statusService = $statusService;
  55. $this->timeFactory = $timeFactory;
  56. }
  57. /**
  58. * @inheritDoc
  59. */
  60. public function handle(Event $event): void {
  61. if (!($event instanceof UserLiveStatusEvent)) {
  62. // Unrelated
  63. return;
  64. }
  65. $user = $event->getUser();
  66. try {
  67. $this->calendarStatusService->processCalendarStatus($user->getUID());
  68. $userStatus = $this->statusService->findByUserId($user->getUID());
  69. } catch (DoesNotExistException $ex) {
  70. $userStatus = new UserStatus();
  71. $userStatus->setUserId($user->getUID());
  72. $userStatus->setStatus(IUserStatus::OFFLINE);
  73. $userStatus->setStatusTimestamp(0);
  74. $userStatus->setIsUserDefined(false);
  75. }
  76. // If the status is user-defined and one of the persistent status, we
  77. // will not override it.
  78. if ($userStatus->getIsUserDefined() &&
  79. \in_array($userStatus->getStatus(), StatusService::PERSISTENT_STATUSES, true)) {
  80. return;
  81. }
  82. // Don't overwrite the "away" calendar status if it's set
  83. if($userStatus->getMessageId() === IUserStatus::MESSAGE_CALENDAR_BUSY) {
  84. $event->setUserStatus(new ConnectorUserStatus($userStatus));
  85. return;
  86. }
  87. $needsUpdate = false;
  88. // If the current status is older than 5 minutes,
  89. // treat it as outdated and update
  90. if ($userStatus->getStatusTimestamp() < ($this->timeFactory->getTime() - StatusService::INVALIDATE_STATUS_THRESHOLD)) {
  91. $needsUpdate = true;
  92. }
  93. // If the emitted status is more important than the current status
  94. // treat it as outdated and update
  95. if (array_search($event->getStatus(), StatusService::PRIORITY_ORDERED_STATUSES) < array_search($userStatus->getStatus(), StatusService::PRIORITY_ORDERED_STATUSES)) {
  96. $needsUpdate = true;
  97. }
  98. if ($needsUpdate) {
  99. $userStatus->setStatus($event->getStatus());
  100. $userStatus->setStatusTimestamp($event->getTimestamp());
  101. $userStatus->setIsUserDefined(false);
  102. if ($userStatus->getId() === null) {
  103. try {
  104. $this->mapper->insert($userStatus);
  105. } catch (Exception $e) {
  106. if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
  107. // A different process might have written another status
  108. // update to the DB while we're processing our stuff.
  109. // We can safely ignore it as we're only changing between AWAY and ONLINE
  110. // and not doing anything with the message or icon.
  111. $this->logger->debug('Unique constraint violation for live user status', ['exception' => $e]);
  112. return;
  113. }
  114. throw $e;
  115. }
  116. } else {
  117. $this->mapper->update($userStatus);
  118. }
  119. }
  120. $event->setUserStatus(new ConnectorUserStatus($userStatus));
  121. }
  122. }