1
0
mirror of https://github.com/chylex/Nextcloud-News.git synced 2025-04-25 20:15:47 +02:00

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 <SMillerDev@users.noreply.github.com>
Signed-off-by: Benjamin Brahmer <info@b-brahmer.de>
This commit is contained in:
Benjamin Brahmer 2023-03-01 17:41:11 +01:00
parent eeabb4189b
commit b1476e958a
5 changed files with 53 additions and 32 deletions

View File

@ -21,7 +21,7 @@ Create a [feature request](https://github.com/nextcloud/news/discussions/new)
Report a [feed issue](https://github.com/nextcloud/news/discussions/new)
]]></description>
<version>21.1.0</version>
<version>21.2.0-beta1</version>
<licence>agpl</licence>
<author>Benjamin Brahmer</author>
<author>Sean Molenaar</author>

View File

@ -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
);
}

View File

@ -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<Feed, Item[]> an array containing the new feed and its items, first
* element being the Feed and second element being an array of Items

View File

@ -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);
}

View File

@ -326,7 +326,7 @@ class FeedFetcherTest extends TestCase
$item = $this->createItem();
$feed = $this->createFeed();
$this->mockIterator($this->feed_mock, [$this->item_mock]);
$result = $this->fetcher->fetch($this->url, false, null, null);
$result = $this->fetcher->fetch($this->url, false, null, null, null);
$this->assertEquals([$feed, [$item]], $result);
}
@ -344,7 +344,8 @@ class FeedFetcherTest extends TestCase
$this->url,
false,
'account@email.com',
'F9sEU*Rt%:KFK8HMHT&'
'F9sEU*Rt%:KFK8HMHT&',
$this->modified->format(DateTime::RSS)
);
$this->assertEquals([$feed, [$item]], $result);
@ -359,7 +360,7 @@ class FeedFetcherTest extends TestCase
$item = $this->createItem('audio/ogg');
$feed = $this->createFeed();
$this->mockIterator($this->feed_mock, [$this->item_mock]);
$result = $this->fetcher->fetch($this->url, false, null, null);
$result = $this->fetcher->fetch($this->url, false, null, null, null);
$this->assertEquals([$feed, [$item]], $result);
}
@ -373,7 +374,7 @@ class FeedFetcherTest extends TestCase
$item = $this->createItem('video/ogg');
$feed = $this->createFeed();
$this->mockIterator($this->feed_mock, [$this->item_mock]);
$result = $this->fetcher->fetch($this->url, false, null, null);
$result = $this->fetcher->fetch($this->url, false, null, null, null);
$this->assertEquals([$feed, [$item]], $result);
}
@ -388,7 +389,7 @@ class FeedFetcherTest extends TestCase
$feed = $this->createFeed('de-DE');
$item = $this->createItem();
$this->mockIterator($this->feed_mock, [$this->item_mock]);
$result = $this->fetcher->fetch($this->url, false, null, null);
$result = $this->fetcher->fetch($this->url, false, null, null, null);
$this->assertEquals([$feed, [$item]], $result);
}
@ -402,7 +403,7 @@ class FeedFetcherTest extends TestCase
$this->createFeed('he-IL');
$this->createItem();
$this->mockIterator($this->feed_mock, [$this->item_mock]);
list($_, $items) = $this->fetcher->fetch($this->url, false, null, null);
list($_, $items) = $this->fetcher->fetch($this->url, false, null, null, null);
$this->assertTrue($items[0]->getRtl());
}
@ -428,7 +429,7 @@ class FeedFetcherTest extends TestCase
$this->mockIterator($this->feed_mock, [$this->item_mock]);
list($feed, $items) = $this->fetcher->fetch($this->url, false, null, null);
list($feed, $items) = $this->fetcher->fetch($this->url, false, null, null, null);
$this->assertSame($items[0]->getPubDate(), 1522180229);
}
@ -454,7 +455,7 @@ class FeedFetcherTest extends TestCase
$this->mockIterator($this->feed_mock, [$this->item_mock]);
list($feed, $items) = $this->fetcher->fetch($this->url, false, null, null);
list($feed, $items) = $this->fetcher->fetch($this->url, false, null, null, null);
$this->assertSame($items[0]->getPubDate(), 1519761029);
}
@ -467,7 +468,7 @@ class FeedFetcherTest extends TestCase
$this->createItem();
$feed = $this->createFeed();
$this->mockIterator($this->feed_mock, [$this->item_mock]);
$result = $this->fetcher->fetch($this->url, false, null, null);
$result = $this->fetcher->fetch($this->url, false, null, null. null, null);
//Explicitly assert GUID value
$this->assertEquals(2, count($result));
$this->assertEquals(1, count($result[1]));
@ -485,7 +486,7 @@ class FeedFetcherTest extends TestCase
$this->createItem();
$feed = $this->createFeed();
$this->mockIterator($this->feed_mock, [$this->item_mock]);
$result = $this->fetcher->fetch($this->url, false, null, null);
$result = $this->fetcher->fetch($this->url, false, null, null, null);
//Explicitly assert GUID value
$this->assertEquals(2, count($result));
$this->assertEquals(1, count($result[1]));