Skip to content

Commit

Permalink
Merge pull request #33940 from nextcloud/dashboard-api-widgets--user_…
Browse files Browse the repository at this point in the history
…status

Implement IAPIWidget, IButtonWidget and IIconWidget in user status
  • Loading branch information
Julien Veyssier authored Sep 15, 2022
2 parents 2600d0b + 5b25f94 commit bf3a6c6
Showing 1 changed file with 76 additions and 29 deletions.
105 changes: 76 additions & 29 deletions apps/user_status/lib/Dashboard/UserStatusWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,56 @@
use OCA\UserStatus\AppInfo\Application;
use OCA\UserStatus\Db\UserStatus;
use OCA\UserStatus\Service\StatusService;
use OCP\Dashboard\IWidget;
use OCP\IInitialStateService;
use OCP\AppFramework\Services\IInitialState;
use OCP\Dashboard\IAPIWidget;
use OCP\Dashboard\IButtonWidget;
use OCP\Dashboard\IIconWidget;
use OCP\Dashboard\IOptionWidget;
use OCP\Dashboard\Model\WidgetItem;
use OCP\Dashboard\Model\WidgetOptions;
use OCP\IDateTimeFormatter;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\UserStatus\IUserStatus;
use OCP\Util;

/**
* Class UserStatusWidget
*
* @package OCA\UserStatus
*/
class UserStatusWidget implements IWidget {

/** @var IL10N */
private $l10n;

/** @var IInitialStateService */
private $initialStateService;

/** @var IUserManager */
private $userManager;

/** @var IUserSession */
private $userSession;

/** @var StatusService */
private $service;
class UserStatusWidget implements IAPIWidget, IIconWidget, IOptionWidget {
private IL10N $l10n;
private IDateTimeFormatter $dateTimeFormatter;
private IURLGenerator $urlGenerator;
private IInitialState $initialStateService;
private IUserManager $userManager;
private IUserSession $userSession;
private StatusService $service;

/**
* UserStatusWidget constructor
*
* @param IL10N $l10n
* @param IInitialStateService $initialStateService
* @param IDateTimeFormatter $dateTimeFormatter
* @param IURLGenerator $urlGenerator
* @param IInitialState $initialStateService
* @param IUserManager $userManager
* @param IUserSession $userSession
* @param StatusService $service
*/
public function __construct(IL10N $l10n,
IInitialStateService $initialStateService,
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;
Expand Down Expand Up @@ -106,6 +112,15 @@ public function getIconClass(): string {
return 'icon-user-status';
}

/**
* @inheritDoc
*/
public function getIconUrl(): string {
return $this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->imagePath(Application::APP_ID, 'app.svg')
);
}

/**
* @inheritDoc
*/
Expand All @@ -117,28 +132,33 @@ public function getUrl(): ?string {
* @inheritDoc
*/
public function load(): void {
\OCP\Util::addScript(Application::APP_ID, 'dashboard');
Util::addScript(Application::APP_ID, 'dashboard');

$currentUser = $this->userSession->getUser();
if ($currentUser === null) {
$this->initialStateService->provideInitialState(Application::APP_ID, 'dashboard_data', []);
$this->initialStateService->provideInitialState('dashboard_data', []);
return;
}
$currentUserId = $currentUser->getUID();

$widgetItemsData = $this->getWidgetData($currentUserId);
$this->initialStateService->provideInitialState('dashboard_data', $widgetItemsData);
}

private function getWidgetData(string $userId, ?string $since = null, int $limit = 7): array {
// Fetch status updates and filter current user
$recentStatusUpdates = array_slice(
array_filter(
$this->service->findAllRecentStatusChanges(8, 0),
static function (UserStatus $status) use ($currentUserId): bool {
return $status->getUserId() !== $currentUserId;
$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,
7
$limit
);

$this->initialStateService->provideInitialState(Application::APP_ID, 'dashboard_data', array_map(function (UserStatus $status): array {
return array_map(function (UserStatus $status): array {
$user = $this->userManager->get($status->getUserId());
$displayName = $status->getUserId();
if ($user !== null) {
Expand All @@ -155,6 +175,33 @@ static function (UserStatus $status) use ($currentUserId): bool {
'message' => $status->getCustomMessage(),
'timestamp' => $status->getStatusTimestamp(),
];
}, $recentStatusUpdates));
}, $recentStatusUpdates);
}

/**
* @inheritDoc
*/
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,
// https://nextcloud.local/index.php/u/julien
$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 getWidgetOptions(): WidgetOptions {
return new WidgetOptions(true);
}
}

0 comments on commit bf3a6c6

Please sign in to comment.