From b1476e958a02eca59c1ce9b5cd3bf09aea6cd308 Mon Sep 17 00:00:00 2001 From: Benjamin Brahmer Date: Wed, 1 Mar 2023 17:41:11 +0100 Subject: If timestamp is null use timestamp "one year ago" if timestamp is not set during creation of a feed use date one year ago code fixes and linting fixes. Co-authored-by: Sean Molenaar Signed-off-by: Benjamin Brahmer --- lib/Fetcher/Fetcher.php | 7 ++++-- lib/Fetcher/IFeedFetcher.php | 1 + lib/Service/FeedServiceV2.php | 54 ++++++++++++++++++++++++++++--------------- 3 files changed, 41 insertions(+), 21 deletions(-) (limited to 'lib') diff --git a/lib/Fetcher/Fetcher.php b/lib/Fetcher/Fetcher.php index 8c755bc85..7d4690a6e 100644 --- a/lib/Fetcher/Fetcher.php +++ b/lib/Fetcher/Fetcher.php @@ -47,6 +47,7 @@ class Fetcher * @param bool $fullTextEnabled If true use a scraper to download the full article * @param string|null $user if given, basic auth is set for this feed * @param string|null $password if given, basic auth is set for this feed. Ignored if user is empty + * @param string|null $httpLastModified if given, will be used when sending a request to servers * * @throws ReadErrorException if FeedIO fails * @return array an array containing the new feed and its items, first @@ -56,7 +57,8 @@ class Fetcher string $url, bool $fullTextEnabled = false, ?string $user = null, - ?string $password = null + ?string $password = null, + ?string $httpLastModified = null ): array { foreach ($this->fetchers as $fetcher) { if (!$fetcher->canHandle($url)) { @@ -66,7 +68,8 @@ class Fetcher $url, $fullTextEnabled, $user, - $password + $password, + $httpLastModified ); } diff --git a/lib/Fetcher/IFeedFetcher.php b/lib/Fetcher/IFeedFetcher.php index 8729858b5..7f2bbe31c 100644 --- a/lib/Fetcher/IFeedFetcher.php +++ b/lib/Fetcher/IFeedFetcher.php @@ -27,6 +27,7 @@ interface IFeedFetcher * @param bool $fullTextEnabled If true use a scraper to download the full article * @param string|null $user if given, basic auth is set for this feed * @param string|null $password if given, basic auth is set for this feed. Ignored if user is empty + * @param string|null $httpLastModified if given, will be used when sending a request to servers * * @return array an array containing the new feed and its items, first * element being the Feed and second element being an array of Items diff --git a/lib/Service/FeedServiceV2.php b/lib/Service/FeedServiceV2.php index 3610c3e3a..16ca9a60a 100644 --- a/lib/Service/FeedServiceV2.php +++ b/lib/Service/FeedServiceV2.php @@ -13,6 +13,7 @@ namespace OCA\News\Service; +use DateTime; use FeedIo\Explorer; use FeedIo\Reader\ReadErrorException; use HTMLPurifier; @@ -37,6 +38,7 @@ class FeedServiceV2 extends Service { /** * Class to fetch feeds. + * * @var FeedFetcher */ protected $feedFetcher; @@ -48,11 +50,13 @@ class FeedServiceV2 extends Service protected $itemService; /** * HTML Purifier + * * @var HTMLPurifier */ protected $purifier; /** * Feed Explorer + * * @var Explorer */ protected $explorer; @@ -109,7 +113,7 @@ class FeedServiceV2 extends Service /** * Finds all feeds of a user and all items in it * - * @param string $userId the name of the user + * @param string $userId the name of the user * * @return Feed[] */ @@ -169,13 +173,14 @@ class FeedServiceV2 extends Service /** * Creates a new feed * - * @param string $userId Feed owner - * @param string $feedUrl Feed URL - * @param int|null $folderId Target folder, defaults to root - * @param bool $full_text Scrape the feed for full text - * @param string|null $title The feed title - * @param string|null $user Basic auth username, if set - * @param string|null $password Basic auth password if username is set + * @param string $userId Feed owner + * @param string $feedUrl Feed URL + * @param int|null $folderId Target folder, defaults to root + * @param bool $full_text Scrape the feed for full text + * @param string|null $title The feed title + * @param string|null $user Basic auth username, if set + * @param string|null $password Basic auth password if username is set + * @param string|null $httpLastModified timestamp send when fetching the feed * * @return Feed|Entity * @@ -190,14 +195,16 @@ class FeedServiceV2 extends Service ?string $title = null, ?string $user = null, ?string $password = null, - bool $full_discover = true + bool $full_discover = true, + ?string $httpLastModified = null ): Entity { + $httpLastModified ??= (new DateTime("-1 year"))->format(DateTime::RSS); try { /** * @var Feed $feed * @var Item[] $items */ - list($feed, $items) = $this->feedFetcher->fetch($feedUrl, $full_text, $user, $password); + list($feed, $items) = $this->feedFetcher->fetch($feedUrl, $full_text, $user, $password, $httpLastModified); } catch (ReadErrorException $ex) { $this->logger->debug($ex->getMessage()); if ($full_discover === false) { @@ -209,7 +216,13 @@ class FeedServiceV2 extends Service $feedUrl = array_shift($feeds); } try { - list($feed, $items) = $this->feedFetcher->fetch($feedUrl, $full_text, $user, $password); + list($feed, $items) = $this->feedFetcher->fetch( + $feedUrl, + $full_text, + $user, + $password, + $httpLastModified + ); } catch (ReadErrorException $ex) { throw new ServiceNotFoundException($ex->getMessage()); } @@ -234,7 +247,7 @@ class FeedServiceV2 extends Service if (!is_null($user)) { $feed->setBasicAuthUser($user) - ->setBasicAuthPassword($password); + ->setBasicAuthPassword($password); } return $this->mapper->insert($feed); @@ -295,11 +308,11 @@ class FeedServiceV2 extends Service } $feed->setHttpLastModified($fetchedFeed->getHttpLastModified()) - ->setLocation($fetchedFeed->getLocation()); + ->setLocation($fetchedFeed->getLocation()); foreach (array_reverse($items) as &$item) { $item->setFeedId($feed->getId()) - ->setBody($this->purifier->purify($item->getBody())); + ->setBody($this->purifier->purify($item->getBody())); // update modes: 0 nothing, 1 set unread if ($feed->getUpdateMode() === Feed::UPDATE_MODE_NORMAL) { @@ -315,11 +328,14 @@ class FeedServiceV2 extends Service $feed->setLastUpdateError(null); $unreadCount = 0; - array_map(function (Item $item) use (&$unreadCount): void { - if ($item->isUnread()) { - $unreadCount++; - } - }, $items); + array_map( + function (Item $item) use (&$unreadCount): void { + if ($item->isUnread()) { + $unreadCount++; + } + }, + $items + ); return $this->mapper->update($feed)->setUnreadCount($unreadCount); } -- cgit v1.2.3