123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- declare(strict_types=1);
- namespace OCA\UserStatus\Dashboard;
- use OCA\UserStatus\AppInfo\Application;
- use OCA\UserStatus\Db\UserStatus;
- use OCA\UserStatus\Service\StatusService;
- use OCP\AppFramework\Services\IInitialState;
- use OCP\Dashboard\IAPIWidget;
- use OCP\Dashboard\IAPIWidgetV2;
- use OCP\Dashboard\IIconWidget;
- use OCP\Dashboard\IOptionWidget;
- use OCP\Dashboard\Model\WidgetItem;
- use OCP\Dashboard\Model\WidgetItems;
- use OCP\Dashboard\Model\WidgetOptions;
- use OCP\IDateTimeFormatter;
- use OCP\IL10N;
- use OCP\IURLGenerator;
- use OCP\IUserManager;
- use OCP\IUserSession;
- use OCP\UserStatus\IUserStatus;
- class UserStatusWidget implements IAPIWidget, IAPIWidgetV2, IIconWidget, IOptionWidget {
- private IL10N $l10n;
- private IDateTimeFormatter $dateTimeFormatter;
- private IURLGenerator $urlGenerator;
- private IInitialState $initialStateService;
- private IUserManager $userManager;
- private IUserSession $userSession;
- private StatusService $service;
-
- public function __construct(IL10N $l10n,
- IDateTimeFormatter $dateTimeFormatter,
- IURLGenerator $urlGenerator,
- IInitialState $initialStateService,
- IUserManager $userManager,
- IUserSession $userSession,
- StatusService $service) {
- $this->l10n = $l10n;
- $this->dateTimeFormatter = $dateTimeFormatter;
- $this->urlGenerator = $urlGenerator;
- $this->initialStateService = $initialStateService;
- $this->userManager = $userManager;
- $this->userSession = $userSession;
- $this->service = $service;
- }
-
- public function getId(): string {
- return Application::APP_ID;
- }
-
- public function getTitle(): string {
- return $this->l10n->t('Recent statuses');
- }
-
- public function getOrder(): int {
- return 5;
- }
-
- public function getIconClass(): string {
- return 'icon-user-status-dark';
- }
-
- public function getIconUrl(): string {
- return $this->urlGenerator->getAbsoluteURL(
- $this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg')
- );
- }
-
- public function getUrl(): ?string {
- return null;
- }
-
- public function load(): void {
- }
- private function getWidgetData(string $userId, ?string $since = null, int $limit = 7): array {
-
- $recentStatusUpdates = array_slice(
- array_filter(
- $this->service->findAllRecentStatusChanges($limit + 1, 0),
- static function (UserStatus $status) use ($userId, $since): bool {
- return $status->getUserId() !== $userId
- && ($since === null || $status->getStatusTimestamp() > (int) $since);
- }
- ),
- 0,
- $limit
- );
- return array_map(function (UserStatus $status): array {
- $user = $this->userManager->get($status->getUserId());
- $displayName = $status->getUserId();
- if ($user !== null) {
- $displayName = $user->getDisplayName();
- }
- return [
- 'userId' => $status->getUserId(),
- 'displayName' => $displayName,
- 'status' => $status->getStatus() === IUserStatus::INVISIBLE
- ? IUserStatus::OFFLINE
- : $status->getStatus(),
- 'icon' => $status->getCustomIcon(),
- 'message' => $status->getCustomMessage(),
- 'timestamp' => $status->getStatusMessageTimestamp(),
- ];
- }, $recentStatusUpdates);
- }
-
- public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
- $widgetItemsData = $this->getWidgetData($userId, $since, $limit);
- return array_map(function (array $widgetData) {
- $formattedDate = $this->dateTimeFormatter->formatTimeSpan($widgetData['timestamp']);
- return new WidgetItem(
- $widgetData['displayName'],
- $widgetData['icon'] . ($widgetData['icon'] ? ' ' : '') . $widgetData['message'] . ', ' . $formattedDate,
-
- $this->urlGenerator->getAbsoluteURL(
- $this->urlGenerator->linkToRoute('core.ProfilePage.index', ['targetUserId' => $widgetData['userId']])
- ),
- $this->urlGenerator->getAbsoluteURL(
- $this->urlGenerator->linkToRoute('core.avatar.getAvatar', ['userId' => $widgetData['userId'], 'size' => 44])
- ),
- (string) $widgetData['timestamp']
- );
- }, $widgetItemsData);
- }
-
- public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems {
- $items = $this->getItems($userId, $since, $limit);
- return new WidgetItems(
- $items,
- count($items) === 0 ? $this->l10n->t('No recent status changes') : '',
- );
- }
- public function getWidgetOptions(): WidgetOptions {
- return new WidgetOptions(true);
- }
- }
|