UserStatusWidget.php 5.8 KB

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