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.

UserLiveStatusListener.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Listener;
  24. use OCA\UserStatus\Db\UserStatus;
  25. use OCA\UserStatus\Db\UserStatusMapper;
  26. use OCP\AppFramework\Db\DoesNotExistException;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\EventDispatcher\IEventListener;
  29. use OCP\EventDispatcher\Event;
  30. use OCP\User\Events\UserLiveStatusEvent;
  31. /**
  32. * Class UserDeletedListener
  33. *
  34. * @package OCA\UserStatus\Listener
  35. */
  36. class UserLiveStatusListener implements IEventListener {
  37. /** @var UserStatusMapper */
  38. private $mapper;
  39. /** @var ITimeFactory */
  40. private $timeFactory;
  41. /** @var string[] */
  42. private $priorityOrderedStatuses = [
  43. 'online',
  44. 'away',
  45. 'dnd',
  46. 'invisible',
  47. 'offline'
  48. ];
  49. /** @var string[] */
  50. private $persistentUserStatuses = [
  51. 'away',
  52. 'dnd',
  53. 'invisible',
  54. ];
  55. /** @var int */
  56. private $offlineThreshold = 300;
  57. /**
  58. * UserLiveStatusListener constructor.
  59. *
  60. * @param UserStatusMapper $mapper
  61. * @param ITimeFactory $timeFactory
  62. */
  63. public function __construct(UserStatusMapper $mapper,
  64. ITimeFactory $timeFactory) {
  65. $this->mapper = $mapper;
  66. $this->timeFactory = $timeFactory;
  67. }
  68. /**
  69. * @inheritDoc
  70. */
  71. public function handle(Event $event): void {
  72. if (!($event instanceof UserLiveStatusEvent)) {
  73. // Unrelated
  74. return;
  75. }
  76. $user = $event->getUser();
  77. try {
  78. $userStatus = $this->mapper->findByUserId($user->getUID());
  79. } catch (DoesNotExistException $ex) {
  80. $userStatus = new UserStatus();
  81. $userStatus->setUserId($user->getUID());
  82. $userStatus->setStatus('offline');
  83. $userStatus->setStatusTimestamp(0);
  84. $userStatus->setIsUserDefined(false);
  85. }
  86. // If the status is user-defined and one of the persistent statuses, we
  87. // will not override it.
  88. if ($userStatus->getIsUserDefined() &&
  89. \in_array($userStatus->getStatus(), $this->persistentUserStatuses, true)) {
  90. return;
  91. }
  92. $needsUpdate = false;
  93. // If the current status is older than 5 minutes,
  94. // treat it as outdated and update
  95. if ($userStatus->getStatusTimestamp() < ($this->timeFactory->getTime() - $this->offlineThreshold)) {
  96. $needsUpdate = true;
  97. }
  98. // If the emitted status is more important than the current status
  99. // treat it as outdated and update
  100. if (array_search($event->getStatus(), $this->priorityOrderedStatuses) < array_search($userStatus->getStatus(), $this->priorityOrderedStatuses)) {
  101. $needsUpdate = true;
  102. }
  103. if ($needsUpdate) {
  104. $userStatus->setStatus($event->getStatus());
  105. $userStatus->setStatusTimestamp($event->getTimestamp());
  106. $userStatus->setIsUserDefined(false);
  107. if ($userStatus->getId() === null) {
  108. $this->mapper->insert($userStatus);
  109. } else {
  110. $this->mapper->update($userStatus);
  111. }
  112. }
  113. }
  114. }