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 authored and nickvergessen committed Apr 9, 2024
1 parent fd09f89 commit 068dd03
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 307 deletions.
63 changes: 58 additions & 5 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 @@ -40,16 +41,17 @@
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, IIconWidget, IButtonWidget, IOptionWidget, IConditionalWidget, IReloadableWidget {

public function __construct(
protected IUserSession $userSession,
Expand Down Expand Up @@ -112,7 +114,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 conversations')
);
return $buttons;
}
Expand All @@ -135,8 +137,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 +170,52 @@ 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 {
$allRooms = $this->manager->getRoomsForUser($userId, [], 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 +265,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 068dd03

Please sign in to comment.