UserStatusWidget.php 5.8 KB

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