UserStatusWidget.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. private IL10N $l10n;
  32. private IDateTimeFormatter $dateTimeFormatter;
  33. private IURLGenerator $urlGenerator;
  34. private IInitialState $initialStateService;
  35. private IUserManager $userManager;
  36. private IUserSession $userSession;
  37. private StatusService $service;
  38. /**
  39. * UserStatusWidget constructor
  40. *
  41. * @param IL10N $l10n
  42. * @param IDateTimeFormatter $dateTimeFormatter
  43. * @param IURLGenerator $urlGenerator
  44. * @param IInitialState $initialStateService
  45. * @param IUserManager $userManager
  46. * @param IUserSession $userSession
  47. * @param StatusService $service
  48. */
  49. public function __construct(IL10N $l10n,
  50. IDateTimeFormatter $dateTimeFormatter,
  51. IURLGenerator $urlGenerator,
  52. IInitialState $initialStateService,
  53. IUserManager $userManager,
  54. IUserSession $userSession,
  55. StatusService $service) {
  56. $this->l10n = $l10n;
  57. $this->dateTimeFormatter = $dateTimeFormatter;
  58. $this->urlGenerator = $urlGenerator;
  59. $this->initialStateService = $initialStateService;
  60. $this->userManager = $userManager;
  61. $this->userSession = $userSession;
  62. $this->service = $service;
  63. }
  64. /**
  65. * @inheritDoc
  66. */
  67. public function getId(): string {
  68. return Application::APP_ID;
  69. }
  70. /**
  71. * @inheritDoc
  72. */
  73. public function getTitle(): string {
  74. return $this->l10n->t('Recent statuses');
  75. }
  76. /**
  77. * @inheritDoc
  78. */
  79. public function getOrder(): int {
  80. return 5;
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. public function getIconClass(): string {
  86. return 'icon-user-status-dark';
  87. }
  88. /**
  89. * @inheritDoc
  90. */
  91. public function getIconUrl(): string {
  92. return $this->urlGenerator->getAbsoluteURL(
  93. $this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg')
  94. );
  95. }
  96. /**
  97. * @inheritDoc
  98. */
  99. public function getUrl(): ?string {
  100. return null;
  101. }
  102. /**
  103. * @inheritDoc
  104. */
  105. public function load(): void {
  106. }
  107. private function getWidgetData(string $userId, ?string $since = null, int $limit = 7): array {
  108. // Fetch status updates and filter current user
  109. $recentStatusUpdates = array_slice(
  110. array_filter(
  111. $this->service->findAllRecentStatusChanges($limit + 1, 0),
  112. static function (UserStatus $status) use ($userId, $since): bool {
  113. return $status->getUserId() !== $userId
  114. && ($since === null || $status->getStatusTimestamp() > (int) $since);
  115. }
  116. ),
  117. 0,
  118. $limit
  119. );
  120. return array_map(function (UserStatus $status): array {
  121. $user = $this->userManager->get($status->getUserId());
  122. $displayName = $status->getUserId();
  123. if ($user !== null) {
  124. $displayName = $user->getDisplayName();
  125. }
  126. return [
  127. 'userId' => $status->getUserId(),
  128. 'displayName' => $displayName,
  129. 'status' => $status->getStatus() === IUserStatus::INVISIBLE
  130. ? IUserStatus::OFFLINE
  131. : $status->getStatus(),
  132. 'icon' => $status->getCustomIcon(),
  133. 'message' => $status->getCustomMessage(),
  134. 'timestamp' => $status->getStatusMessageTimestamp(),
  135. ];
  136. }, $recentStatusUpdates);
  137. }
  138. /**
  139. * @inheritDoc
  140. */
  141. public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
  142. $widgetItemsData = $this->getWidgetData($userId, $since, $limit);
  143. return array_map(function (array $widgetData) {
  144. $formattedDate = $this->dateTimeFormatter->formatTimeSpan($widgetData['timestamp']);
  145. return new WidgetItem(
  146. $widgetData['displayName'],
  147. $widgetData['icon'] . ($widgetData['icon'] ? ' ' : '') . $widgetData['message'] . ', ' . $formattedDate,
  148. // https://nextcloud.local/index.php/u/julien
  149. $this->urlGenerator->getAbsoluteURL(
  150. $this->urlGenerator->linkToRoute('core.ProfilePage.index', ['targetUserId' => $widgetData['userId']])
  151. ),
  152. $this->urlGenerator->getAbsoluteURL(
  153. $this->urlGenerator->linkToRoute('core.avatar.getAvatar', ['userId' => $widgetData['userId'], 'size' => 44])
  154. ),
  155. (string) $widgetData['timestamp']
  156. );
  157. }, $widgetItemsData);
  158. }
  159. /**
  160. * @inheritDoc
  161. */
  162. public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems {
  163. $items = $this->getItems($userId, $since, $limit);
  164. return new WidgetItems(
  165. $items,
  166. count($items) === 0 ? $this->l10n->t('No recent status changes') : '',
  167. );
  168. }
  169. public function getWidgetOptions(): WidgetOptions {
  170. return new WidgetOptions(true);
  171. }
  172. }