UserStatusWidget.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * @author Richard Steinmetz <richard@steinmetz.cloud>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\UserStatus\Dashboard;
  26. use OCA\UserStatus\AppInfo\Application;
  27. use OCA\UserStatus\Db\UserStatus;
  28. use OCA\UserStatus\Service\StatusService;
  29. use OCP\AppFramework\Services\IInitialState;
  30. use OCP\Dashboard\IAPIWidget;
  31. use OCP\Dashboard\IAPIWidgetV2;
  32. use OCP\Dashboard\IIconWidget;
  33. use OCP\Dashboard\IOptionWidget;
  34. use OCP\Dashboard\Model\WidgetItem;
  35. use OCP\Dashboard\Model\WidgetItems;
  36. use OCP\Dashboard\Model\WidgetOptions;
  37. use OCP\IDateTimeFormatter;
  38. use OCP\IL10N;
  39. use OCP\IURLGenerator;
  40. use OCP\IUserManager;
  41. use OCP\IUserSession;
  42. use OCP\UserStatus\IUserStatus;
  43. /**
  44. * Class UserStatusWidget
  45. *
  46. * @package OCA\UserStatus
  47. */
  48. class UserStatusWidget implements IAPIWidget, IAPIWidgetV2, IIconWidget, IOptionWidget {
  49. private IL10N $l10n;
  50. private IDateTimeFormatter $dateTimeFormatter;
  51. private IURLGenerator $urlGenerator;
  52. private IInitialState $initialStateService;
  53. private IUserManager $userManager;
  54. private IUserSession $userSession;
  55. private StatusService $service;
  56. /**
  57. * UserStatusWidget constructor
  58. *
  59. * @param IL10N $l10n
  60. * @param IDateTimeFormatter $dateTimeFormatter
  61. * @param IURLGenerator $urlGenerator
  62. * @param IInitialState $initialStateService
  63. * @param IUserManager $userManager
  64. * @param IUserSession $userSession
  65. * @param StatusService $service
  66. */
  67. public function __construct(IL10N $l10n,
  68. IDateTimeFormatter $dateTimeFormatter,
  69. IURLGenerator $urlGenerator,
  70. IInitialState $initialStateService,
  71. IUserManager $userManager,
  72. IUserSession $userSession,
  73. StatusService $service) {
  74. $this->l10n = $l10n;
  75. $this->dateTimeFormatter = $dateTimeFormatter;
  76. $this->urlGenerator = $urlGenerator;
  77. $this->initialStateService = $initialStateService;
  78. $this->userManager = $userManager;
  79. $this->userSession = $userSession;
  80. $this->service = $service;
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. public function getId(): string {
  86. return Application::APP_ID;
  87. }
  88. /**
  89. * @inheritDoc
  90. */
  91. public function getTitle(): string {
  92. return $this->l10n->t('Recent statuses');
  93. }
  94. /**
  95. * @inheritDoc
  96. */
  97. public function getOrder(): int {
  98. return 5;
  99. }
  100. /**
  101. * @inheritDoc
  102. */
  103. public function getIconClass(): string {
  104. return 'icon-user-status-dark';
  105. }
  106. /**
  107. * @inheritDoc
  108. */
  109. public function getIconUrl(): string {
  110. return $this->urlGenerator->getAbsoluteURL(
  111. $this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg')
  112. );
  113. }
  114. /**
  115. * @inheritDoc
  116. */
  117. public function getUrl(): ?string {
  118. return null;
  119. }
  120. /**
  121. * @inheritDoc
  122. */
  123. public function load(): void {
  124. }
  125. private function getWidgetData(string $userId, ?string $since = null, int $limit = 7): array {
  126. // Fetch status updates and filter current user
  127. $recentStatusUpdates = array_slice(
  128. array_filter(
  129. $this->service->findAllRecentStatusChanges($limit + 1, 0),
  130. static function (UserStatus $status) use ($userId, $since): bool {
  131. return $status->getUserId() !== $userId
  132. && ($since === null || $status->getStatusTimestamp() > (int) $since);
  133. }
  134. ),
  135. 0,
  136. $limit
  137. );
  138. return array_map(function (UserStatus $status): array {
  139. $user = $this->userManager->get($status->getUserId());
  140. $displayName = $status->getUserId();
  141. if ($user !== null) {
  142. $displayName = $user->getDisplayName();
  143. }
  144. return [
  145. 'userId' => $status->getUserId(),
  146. 'displayName' => $displayName,
  147. 'status' => $status->getStatus() === IUserStatus::INVISIBLE
  148. ? IUserStatus::OFFLINE
  149. : $status->getStatus(),
  150. 'icon' => $status->getCustomIcon(),
  151. 'message' => $status->getCustomMessage(),
  152. 'timestamp' => $status->getStatusMessageTimestamp(),
  153. ];
  154. }, $recentStatusUpdates);
  155. }
  156. /**
  157. * @inheritDoc
  158. */
  159. public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
  160. $widgetItemsData = $this->getWidgetData($userId, $since, $limit);
  161. return array_map(function (array $widgetData) {
  162. $formattedDate = $this->dateTimeFormatter->formatTimeSpan($widgetData['timestamp']);
  163. return new WidgetItem(
  164. $widgetData['displayName'],
  165. $widgetData['icon'] . ($widgetData['icon'] ? ' ' : '') . $widgetData['message'] . ', ' . $formattedDate,
  166. // https://nextcloud.local/index.php/u/julien
  167. $this->urlGenerator->getAbsoluteURL(
  168. $this->urlGenerator->linkToRoute('core.ProfilePage.index', ['targetUserId' => $widgetData['userId']])
  169. ),
  170. $this->urlGenerator->getAbsoluteURL(
  171. $this->urlGenerator->linkToRoute('core.avatar.getAvatar', ['userId' => $widgetData['userId'], 'size' => 44])
  172. ),
  173. (string) $widgetData['timestamp']
  174. );
  175. }, $widgetItemsData);
  176. }
  177. /**
  178. * @inheritDoc
  179. */
  180. public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems {
  181. $items = $this->getItems($userId, $since, $limit);
  182. return new WidgetItems(
  183. $items,
  184. count($items) === 0 ? $this->l10n->t('No recent status changes') : '',
  185. );
  186. }
  187. public function getWidgetOptions(): WidgetOptions {
  188. return new WidgetOptions(true);
  189. }
  190. }