1
0

UserStatusWidget.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UserStatus\Dashboard;
  8. use OCA\UserStatus\AppInfo\Application;
  9. use OCA\UserStatus\Db\UserStatus;
  10. use OCA\UserStatus\Service\StatusService;
  11. use OCP\AppFramework\Services\IInitialState;
  12. use OCP\Dashboard\IAPIWidget;
  13. use OCP\Dashboard\IAPIWidgetV2;
  14. use OCP\Dashboard\IIconWidget;
  15. use OCP\Dashboard\IOptionWidget;
  16. use OCP\Dashboard\Model\WidgetItem;
  17. use OCP\Dashboard\Model\WidgetItems;
  18. use OCP\Dashboard\Model\WidgetOptions;
  19. use OCP\IDateTimeFormatter;
  20. use OCP\IL10N;
  21. use OCP\IURLGenerator;
  22. use OCP\IUserManager;
  23. use OCP\IUserSession;
  24. use OCP\UserStatus\IUserStatus;
  25. /**
  26. * Class UserStatusWidget
  27. *
  28. * @package OCA\UserStatus
  29. */
  30. class UserStatusWidget implements IAPIWidget, IAPIWidgetV2, IIconWidget, IOptionWidget {
  31. /**
  32. * UserStatusWidget constructor
  33. *
  34. * @param IL10N $l10n
  35. * @param IDateTimeFormatter $dateTimeFormatter
  36. * @param IURLGenerator $urlGenerator
  37. * @param IInitialState $initialStateService
  38. * @param IUserManager $userManager
  39. * @param IUserSession $userSession
  40. * @param StatusService $service
  41. */
  42. public function __construct(
  43. private IL10N $l10n,
  44. private IDateTimeFormatter $dateTimeFormatter,
  45. private IURLGenerator $urlGenerator,
  46. private IInitialState $initialStateService,
  47. private IUserManager $userManager,
  48. private IUserSession $userSession,
  49. private StatusService $service,
  50. ) {
  51. }
  52. /**
  53. * @inheritDoc
  54. */
  55. public function getId(): string {
  56. return Application::APP_ID;
  57. }
  58. /**
  59. * @inheritDoc
  60. */
  61. public function getTitle(): string {
  62. return $this->l10n->t('Recent statuses');
  63. }
  64. /**
  65. * @inheritDoc
  66. */
  67. public function getOrder(): int {
  68. return 5;
  69. }
  70. /**
  71. * @inheritDoc
  72. */
  73. public function getIconClass(): string {
  74. return 'icon-user-status-dark';
  75. }
  76. /**
  77. * @inheritDoc
  78. */
  79. public function getIconUrl(): string {
  80. return $this->urlGenerator->getAbsoluteURL(
  81. $this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg')
  82. );
  83. }
  84. /**
  85. * @inheritDoc
  86. */
  87. public function getUrl(): ?string {
  88. return null;
  89. }
  90. /**
  91. * @inheritDoc
  92. */
  93. public function load(): void {
  94. }
  95. private function getWidgetData(string $userId, ?string $since = null, int $limit = 7): array {
  96. // Fetch status updates and filter current user
  97. $recentStatusUpdates = array_slice(
  98. array_filter(
  99. $this->service->findAllRecentStatusChanges($limit + 1, 0),
  100. static function (UserStatus $status) use ($userId, $since): bool {
  101. return $status->getUserId() !== $userId
  102. && ($since === null || $status->getStatusTimestamp() > (int)$since);
  103. }
  104. ),
  105. 0,
  106. $limit
  107. );
  108. return array_map(function (UserStatus $status): array {
  109. $user = $this->userManager->get($status->getUserId());
  110. $displayName = $status->getUserId();
  111. if ($user !== null) {
  112. $displayName = $user->getDisplayName();
  113. }
  114. return [
  115. 'userId' => $status->getUserId(),
  116. 'displayName' => $displayName,
  117. 'status' => $status->getStatus() === IUserStatus::INVISIBLE
  118. ? IUserStatus::OFFLINE
  119. : $status->getStatus(),
  120. 'icon' => $status->getCustomIcon(),
  121. 'message' => $status->getCustomMessage(),
  122. 'timestamp' => $status->getStatusMessageTimestamp(),
  123. ];
  124. }, $recentStatusUpdates);
  125. }
  126. /**
  127. * @inheritDoc
  128. */
  129. public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
  130. $widgetItemsData = $this->getWidgetData($userId, $since, $limit);
  131. return array_map(function (array $widgetData) {
  132. $formattedDate = $this->dateTimeFormatter->formatTimeSpan($widgetData['timestamp']);
  133. return new WidgetItem(
  134. $widgetData['displayName'],
  135. $widgetData['icon'] . ($widgetData['icon'] ? ' ' : '') . $widgetData['message'] . ', ' . $formattedDate,
  136. // https://nextcloud.local/index.php/u/julien
  137. $this->urlGenerator->getAbsoluteURL(
  138. $this->urlGenerator->linkToRoute('core.ProfilePage.index', ['targetUserId' => $widgetData['userId']])
  139. ),
  140. $this->urlGenerator->getAbsoluteURL(
  141. $this->urlGenerator->linkToRoute('core.avatar.getAvatar', ['userId' => $widgetData['userId'], 'size' => 44])
  142. ),
  143. (string)$widgetData['timestamp']
  144. );
  145. }, $widgetItemsData);
  146. }
  147. /**
  148. * @inheritDoc
  149. */
  150. public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems {
  151. $items = $this->getItems($userId, $since, $limit);
  152. return new WidgetItems(
  153. $items,
  154. count($items) === 0 ? $this->l10n->t('No recent status changes') : '',
  155. );
  156. }
  157. public function getWidgetOptions(): WidgetOptions {
  158. return new WidgetOptions(true);
  159. }
  160. }