Skip to content

Commit

Permalink
only update the feed if nextUpdateTime has been reached
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Brahmer <info@b-brahmer.de>
  • Loading branch information
Grotax committed Dec 30, 2024
1 parent d6a3afe commit d1f6910
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 39 deletions.
4 changes: 3 additions & 1 deletion lib/Db/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,12 @@ public function setUserId(string $userId): Feed
/**
* @param int $nextUpdateTime
*/
public function setNextUpdateTime(?int $nextUpdateTime): void
public function setNextUpdateTime(?int $nextUpdateTime): Feed
{
$this->nextUpdateTime = $nextUpdateTime;
$this->markFieldUpdated('nextUpdateTime');

return $this;
}

public function toAPI(): array
Expand Down
46 changes: 44 additions & 2 deletions lib/Service/FeedServiceV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

use OCA\News\Db\FeedMapperV2;
use OCA\News\Fetcher\FeedFetcher;
use OCA\News\AppInfo\Application;
use OCA\News\Service\Exceptions\ServiceConflictException;
use OCA\News\Service\Exceptions\ServiceNotFoundException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IAppConfig;

use OCA\News\Db\Feed;
use OCA\News\Db\Item;
Expand Down Expand Up @@ -61,6 +63,10 @@ class FeedServiceV2 extends Service
* @var Explorer
*/
protected $explorer;
/**
* @var IAppConfig
*/
protected $config;

/**
* FeedService constructor.
Expand All @@ -71,21 +77,24 @@ class FeedServiceV2 extends Service
* @param Explorer $explorer Feed Explorer
* @param HTMLPurifier $purifier HTML Purifier
* @param LoggerInterface $logger Logger
* @param IAppConfig $config App config
*/
public function __construct(
FeedMapperV2 $mapper,
FeedFetcher $feedFetcher,
ItemServiceV2 $itemService,
Explorer $explorer,
HTMLPurifier $purifier,
LoggerInterface $logger
LoggerInterface $logger,
IAppConfig $config
) {
parent::__construct($mapper, $logger);

$this->feedFetcher = $feedFetcher;
$this->itemService = $itemService;
$this->explorer = $explorer;
$this->purifier = $purifier;
$this->config = $config;
}

/**
Expand Down Expand Up @@ -244,12 +253,13 @@ public function create(
$feed->setFolderId($folderId)
->setUserId($userId)
->setHttpLastModified(null)
->setNextUpdateTime(null)
->setArticlesPerUpdate(count($items));

if ($title !== null) {
$feed->setTitle($title);
}

if ($feed->getTitle() === null) {
$feed->setTitle(parse_url($feedUrl)['host']);
}
Expand All @@ -276,6 +286,28 @@ public function fetch(Entity $feed): Entity
return $feed;
}

// Check if the nextUpdateTime check should be used
$useNextUpdateTime = $this->config->getValueBool(
Application::NAME,
'useNextUpdateTime',
Application::DEFAULT_SETTINGS['useNextUpdateTime']
);

if ($useNextUpdateTime) {
$nextUpdateTime = $feed->getNextUpdateTime();
$currentTime = time();
$tolerance = 10 * 60; // 10 minutes tolerance

if ($nextUpdateTime !== null && ($currentTime + $tolerance) < $nextUpdateTime) {
$this->logger->info('Feed update skipped. Next update time not reached.', [
'feedUrl' => $feed->getUrl(),
'nextUpdateTime' => $nextUpdateTime,
'currentTime' => $currentTime,
]);
return $feed;
}
}

// for backwards compatibility it can be that the location is not set
// yet, if so use the url
$location = $feed->getLocation() ?? $feed->getUrl();
Expand Down Expand Up @@ -326,6 +358,16 @@ public function fetch(Entity $feed): Entity
$feed->setHttpLastModified($fetchedFeed->getHttpLastModified())
->setLocation($fetchedFeed->getLocation());

// check if useNextUpdateTime is set by the admin
// if so update value with the timestamp
// otherwise set it to null to indicate to clients
// that they can not use that field for any info.
if ($useNextUpdateTime) {
$feed->setNextUpdateTime($fetchedFeed->getNextUpdateTime());
} else {
$feed->setNextUpdateTime(null);
}

foreach (array_reverse($items) as &$item) {
$item->setFeedId($feed->getId())
->setBody($this->purifier->purify($item->getBody()));
Expand Down
15 changes: 13 additions & 2 deletions tests/Unit/Service/FeedServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use OCA\News\Service\ItemServiceV2;
use OCA\News\Utility\Time;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IAppConfig;

use OCA\News\Db\Feed;
use OCA\News\Db\Item;
Expand Down Expand Up @@ -77,6 +78,11 @@ class FeedServiceTest extends TestCase
*/
private $explorer;

/**
* @var \PHPUnit\Framework\MockObject\MockObject|IAppConfig
*/
private $config;

private $response;

protected function setUp(): void
Expand Down Expand Up @@ -112,14 +118,19 @@ protected function setUp(): void
->getMockBuilder(\HTMLPurifier::class)
->disableOriginalConstructor()
->getMock();

$this->config = $this
->getMockBuilder(IAppConfig::class)
->disableOriginalConstructor()
->getMock();

$this->class = new FeedServiceV2(
$this->mapper,
$this->fetcher,
$this->itemService,
$this->explorer,
$this->purifier,
$this->logger
$this->logger,
$this->config
);
$this->uid = 'jack';
}
Expand Down
Loading

0 comments on commit d1f6910

Please sign in to comment.