Skip to content

Commit

Permalink
perf(dashboard): implement widget item api v2
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
  • Loading branch information
st3iny committed Aug 17, 2023
1 parent 5f09e11 commit b2f4158
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 309 deletions.
66 changes: 62 additions & 4 deletions lib/Dashboard/TalkWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license GNU AGPL version 3 or any later version
*
Expand Down Expand Up @@ -33,23 +34,27 @@
use OCA\Talk\Room;
use OCA\Talk\Service\AvatarService;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\TalkSession;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\IComment;
use OCP\Dashboard\IAPIWidget;
use OCP\Dashboard\IAPIWidgetV2;
use OCP\Dashboard\IButtonWidget;
use OCP\Dashboard\IConditionalWidget;
use OCP\Dashboard\IIconWidget;
use OCP\Dashboard\IOptionWidget;
use OCP\Dashboard\IReloadableWidget;
use OCP\Dashboard\Model\WidgetButton;
use OCP\Dashboard\Model\WidgetItem;
use OCP\Dashboard\Model\WidgetItems;
use OCP\Dashboard\Model\WidgetOptions;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Util;

class TalkWidget implements IAPIWidget, IIconWidget, IButtonWidget, IOptionWidget, IConditionalWidget {
class TalkWidget implements IAPIWidget, IAPIWidgetV2, IIconWidget, IButtonWidget, IOptionWidget, IConditionalWidget, IReloadableWidget {

public function __construct(
protected IUserSession $userSession,
Expand All @@ -61,6 +66,7 @@ public function __construct(
protected ParticipantService $participantService,
protected MessageParser $messageParser,
protected ITimeFactory $timeFactory,
protected TalkSession $session,
) {
}

Expand Down Expand Up @@ -112,7 +118,7 @@ public function getWidgetButtons(string $userId): array {
$buttons[] = new WidgetButton(
WidgetButton::TYPE_MORE,
$this->url->linkToRouteAbsolute('spreed.Page.index'),
$this->l10n->t('More unread mentions')
$this->l10n->t('More talk rooms')
);
return $buttons;
}
Expand All @@ -135,8 +141,6 @@ public function getUrl(): ?string {
* @inheritDoc
*/
public function load(): void {
Util::addStyle('spreed', 'icons');
Util::addScript('spreed', 'talk-dashboard');
}

public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
Expand Down Expand Up @@ -170,6 +174,53 @@ public function getItems(string $userId, ?string $since = null, int $limit = 7):
return $result;
}

/**
* @inheritDoc
*/
public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems {
$sessionIds = $this->session->getAllActiveSessions();
$allRooms = $this->manager->getRoomsForUser($userId, $sessionIds, true);

$rooms = [];
$mentions = [];
foreach ($allRooms as $room) {
if ($room->getObjectType() !== BreakoutRoom::PARENT_OBJECT_TYPE) {
$rooms[] = $room;
}

$participant = $this->participantService->getParticipant($room, $userId);
$attendee = $participant->getAttendee();
if ($room->getCallFlag() !== Participant::FLAG_DISCONNECTED
|| $attendee->getLastMentionMessage() > $attendee->getLastReadMessage()
|| (
($room->getType() === Room::TYPE_ONE_TO_ONE || $room->getType() === Room::TYPE_ONE_TO_ONE_FORMER)
&& $room->getLastMessage()
&& $room->getLastMessage()->getId() > $attendee->getLastReadMessage()
)) {
$mentions[] = $room;
}
}

$roomsToReturn = $rooms;
if (!empty($mentions)) {
$roomsToReturn = $mentions;
}

uasort($roomsToReturn, [$this, 'sortRooms']);
$roomsToReturn = array_slice($roomsToReturn, 0, $limit);

$result = [];
foreach ($roomsToReturn as $room) {
$result[] = $this->prepareRoom($room, $userId);
}

return new WidgetItems(
$result,
empty($result) ? $this->l10n->t('Say hi to your friends and colleagues!') : '',
empty($mentions) ? $this->l10n->t('No unread mentions') : '',
);
}

protected function prepareRoom(Room $room, string $userId): WidgetItem {
$participant = $this->participantService->getParticipant($room, $userId);
$subtitle = '';
Expand Down Expand Up @@ -219,4 +270,11 @@ protected function sortRooms(Room $roomA, Room $roomB): int {

return $roomA->getLastActivity() >= $roomB->getLastActivity() ? -1 : 1;
}

/**
* @inheritDoc
*/
public function getReloadInterval(): int {
return 30;
}
}
56 changes: 0 additions & 56 deletions src/dashboard.js

This file was deleted.

Loading

0 comments on commit b2f4158

Please sign in to comment.