Skip to content

Commit

Permalink
Widget: fix RSS/Mastodon/Notification duration calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dasgarner committed Aug 5, 2023
1 parent e4e46c9 commit 7e11fdd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
3 changes: 3 additions & 0 deletions lib/Entity/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ public function calculateDuration(Widget $widget): ?int
} else if ($this->widgetProvider === null) {
return null;
}

$this->getLog()->debug('calculateDuration: using widget provider');

$durationProvider = $this->moduleFactory->createDurationProvider($this, $widget);
$this->widgetProvider->fetchDuration($durationProvider);

Expand Down
8 changes: 2 additions & 6 deletions lib/Widget/MastodonProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use GuzzleHttp\Exception\RequestException;
use Xibo\Widget\DataType\SocialMedia;
use Xibo\Widget\Provider\DataProviderInterface;
use Xibo\Widget\Provider\DurationProviderInterface;
use Xibo\Widget\Provider\DurationProviderNumItemsTrait;
use Xibo\Widget\Provider\WidgetProviderInterface;
use Xibo\Widget\Provider\WidgetProviderTrait;

Expand All @@ -36,6 +36,7 @@
class MastodonProvider implements WidgetProviderInterface
{
use WidgetProviderTrait;
use DurationProviderNumItemsTrait;

public function fetchData(DataProviderInterface $dataProvider): WidgetProviderInterface
{
Expand Down Expand Up @@ -146,11 +147,6 @@ public function fetchData(DataProviderInterface $dataProvider): WidgetProviderIn
return $this;
}

public function fetchDuration(DurationProviderInterface $durationProvider): WidgetProviderInterface
{
return $this;
}

public function getDataCacheKey(DataProviderInterface $dataProvider): ?string
{
// No special cache key requirements.
Expand Down
10 changes: 3 additions & 7 deletions lib/Widget/NotificationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) 2023 Xibo Signage Ltd
*
* Xibo - Digital Signage - http://www.xibo.org.uk
* Xibo - Digital Signage - https://xibosignage.com
*
* This file is part of Xibo.
*
Expand All @@ -25,13 +25,14 @@
use Carbon\Carbon;
use Xibo\Event\NotificationDataRequestEvent;
use Xibo\Widget\Provider\DataProviderInterface;
use Xibo\Widget\Provider\DurationProviderInterface;
use Xibo\Widget\Provider\DurationProviderNumItemsTrait;
use Xibo\Widget\Provider\WidgetProviderInterface;
use Xibo\Widget\Provider\WidgetProviderTrait;

class NotificationProvider implements WidgetProviderInterface
{
use WidgetProviderTrait;
use DurationProviderNumItemsTrait;

public function fetchData(DataProviderInterface $dataProvider): WidgetProviderInterface
{
Expand All @@ -43,11 +44,6 @@ public function fetchData(DataProviderInterface $dataProvider): WidgetProviderIn
return $this;
}

public function fetchDuration(DurationProviderInterface $durationProvider): WidgetProviderInterface
{
return $this;
}

public function getDataCacheKey(DataProviderInterface $dataProvider): ?string
{
return $dataProvider->getWidgetId() . '_' . $dataProvider->getDisplayId();
Expand Down
50 changes: 50 additions & 0 deletions lib/Widget/Provider/DurationProviderNumItemsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* Copyright (C) 2023 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Xibo\Widget\Provider;

/**
* A trait providing the duration for widgets using numItems, durationIsPerItem and itemsPerPage
*/
trait DurationProviderNumItemsTrait
{
public function fetchDuration(DurationProviderInterface $durationProvider): WidgetProviderInterface
{
$this->getLog()->debug('fetchDuration: DurationProviderNumItemsTrait');

// Take some default action to cover the majourity of region specific widgets
// Duration can depend on the number of items per page for some widgets
// this is a legacy way of working, and our preference is to use elements
$numItems = $durationProvider->getWidget()->getOptionValue('numItems', 15);

if ($durationProvider->getWidget()->getOptionValue('durationIsPerItem', 0) == 1 && $numItems > 1) {
// If we have paging involved then work out the page count.
$itemsPerPage = $durationProvider->getWidget()->getOptionValue('itemsPerPage', 0);
if ($itemsPerPage > 0) {
$numItems = ceil($numItems / $itemsPerPage);
}

$durationProvider->setDuration($durationProvider->getWidget()->calculatedDuration * $numItems);
}
return $this;
}
}
10 changes: 3 additions & 7 deletions lib/Widget/RssProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@
use Xibo\Support\Exception\InvalidArgumentException;
use Xibo\Widget\DataType\Article;
use Xibo\Widget\Provider\DataProviderInterface;
use Xibo\Widget\Provider\DurationProviderInterface;
use Xibo\Widget\Provider\DurationProviderNumItemsTrait;
use Xibo\Widget\Provider\WidgetProviderInterface;
use Xibo\Widget\Provider\WidgetProviderTrait;

/**
* Downloads a RSS feed and returns Article data types
* Downloads an RSS feed and returns Article data types
*/
class RssProvider implements WidgetProviderInterface
{
use WidgetProviderTrait;
use DurationProviderNumItemsTrait;

public function fetchData(DataProviderInterface $dataProvider): WidgetProviderInterface
{
Expand Down Expand Up @@ -225,11 +226,6 @@ public function fetchData(DataProviderInterface $dataProvider): WidgetProviderIn
return $this;
}

public function fetchDuration(DurationProviderInterface $durationProvider): WidgetProviderInterface
{
return $this;
}

public function getDataCacheKey(DataProviderInterface $dataProvider): ?string
{
// No special cache key requirements.
Expand Down

0 comments on commit 7e11fdd

Please sign in to comment.