UserStatusWidget.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Dashboard;
  24. use OCA\UserStatus\AppInfo\Application;
  25. use OCA\UserStatus\Db\UserStatus;
  26. use OCA\UserStatus\Service\StatusService;
  27. use OCP\Dashboard\IWidget;
  28. use OCP\IInitialStateService;
  29. use OCP\IL10N;
  30. use OCP\IUserManager;
  31. use OCP\IUserSession;
  32. use OCP\UserStatus\IUserStatus;
  33. /**
  34. * Class UserStatusWidget
  35. *
  36. * @package OCA\UserStatus
  37. */
  38. class UserStatusWidget implements IWidget {
  39. /** @var IL10N */
  40. private $l10n;
  41. /** @var IInitialStateService */
  42. private $initialStateService;
  43. /** @var IUserManager */
  44. private $userManager;
  45. /** @var IUserSession */
  46. private $userSession;
  47. /** @var StatusService */
  48. private $service;
  49. /**
  50. * UserStatusWidget constructor
  51. *
  52. * @param IL10N $l10n
  53. * @param IInitialStateService $initialStateService
  54. * @param IUserManager $userManager
  55. * @param IUserSession $userSession
  56. * @param StatusService $service
  57. */
  58. public function __construct(IL10N $l10n,
  59. IInitialStateService $initialStateService,
  60. IUserManager $userManager,
  61. IUserSession $userSession,
  62. StatusService $service) {
  63. $this->l10n = $l10n;
  64. $this->initialStateService = $initialStateService;
  65. $this->userManager = $userManager;
  66. $this->userSession = $userSession;
  67. $this->service = $service;
  68. }
  69. /**
  70. * @inheritDoc
  71. */
  72. public function getId(): string {
  73. return Application::APP_ID;
  74. }
  75. /**
  76. * @inheritDoc
  77. */
  78. public function getTitle(): string {
  79. return $this->l10n->t('Recent statuses');
  80. }
  81. /**
  82. * @inheritDoc
  83. */
  84. public function getOrder(): int {
  85. return 5;
  86. }
  87. /**
  88. * @inheritDoc
  89. */
  90. public function getIconClass(): string {
  91. return 'icon-user-status';
  92. }
  93. /**
  94. * @inheritDoc
  95. */
  96. public function getUrl(): ?string {
  97. return null;
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. public function load(): void {
  103. \OCP\Util::addScript(Application::APP_ID, 'dashboard');
  104. $currentUser = $this->userSession->getUser();
  105. if ($currentUser === null) {
  106. $this->initialStateService->provideInitialState(Application::APP_ID, 'dashboard_data', []);
  107. return;
  108. }
  109. $currentUserId = $currentUser->getUID();
  110. // Fetch status updates and filter current user
  111. $recentStatusUpdates = array_slice(
  112. array_filter(
  113. $this->service->findAllRecentStatusChanges(8, 0),
  114. static function (UserStatus $status) use ($currentUserId): bool {
  115. return $status->getUserId() !== $currentUserId;
  116. }
  117. ),
  118. 0,
  119. 7
  120. );
  121. $this->initialStateService->provideInitialState(Application::APP_ID, 'dashboard_data', array_map(function (UserStatus $status): array {
  122. $user = $this->userManager->get($status->getUserId());
  123. $displayName = $status->getUserId();
  124. if ($user !== null) {
  125. $displayName = $user->getDisplayName();
  126. }
  127. return [
  128. 'userId' => $status->getUserId(),
  129. 'displayName' => $displayName,
  130. 'status' => $status->getStatus() === IUserStatus::INVISIBLE
  131. ? IUserStatus::OFFLINE
  132. : $status->getStatus(),
  133. 'icon' => $status->getCustomIcon(),
  134. 'message' => $status->getCustomMessage(),
  135. 'timestamp' => $status->getStatusTimestamp(),
  136. ];
  137. }, $recentStatusUpdates));
  138. }
  139. }