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