From 1c91928e597cad6d41b10ce4b3f34b130453ad27 Mon Sep 17 00:00:00 2001 From: Benjamin Brahmer Date: Tue, 31 May 2022 15:46:01 +0200 Subject: If items of feed do not provide an author fallback to feed author (#1803) Signed-off-by: Benjamin Brahmer --- CHANGELOG.md | 1 + lib/Fetcher/FeedFetcher.php | 16 +++++-- tests/command/feeds/no_guid_feed.xml | 78 ++++++++++++++++++++++++++++++++ tests/command/helpers/settings.bash | 2 +- tests/command/items.bats | 14 ++++++ tests/integration/feeds/no_guid_feed.xml | 78 -------------------------------- 6 files changed, 106 insertions(+), 83 deletions(-) create mode 100644 tests/command/feeds/no_guid_feed.xml delete mode 100644 tests/integration/feeds/no_guid_feed.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index db792e696..2b1c5f58f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is mostly based on [Keep a Changelog](https://keepachangelog.com/en/1 # Unreleased ## [18.x.x] ### Changed +- If items of feed do not provide an author fallback to feed author (#1803) ### Fixed diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index 27421f525..209722c97 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -134,6 +134,7 @@ class FeedFetcher implements IFeedFetcher $items = []; $RTL = $this->determineRtl($parsedFeed); $feedName = $parsedFeed->getTitle(); + $feedAuthor = $parsedFeed->getAuthor(); $this->logger->debug( 'Feed {url} was modified since last fetch. #{count} items', [ @@ -155,7 +156,7 @@ class FeedFetcher implements IFeedFetcher } } - $builtItem = $this->buildItem($item, $body, $currRTL); + $builtItem = $this->buildItem($item, $body, $currRTL, $feedAuthor); $this->logger->debug( 'Added item {title} for feed {feed} lastmodified: {datetime}', [ @@ -226,11 +227,16 @@ class FeedFetcher implements IFeedFetcher * @param ItemInterface $parsedItem The item to use * @param string|null $body Text of the item, if not provided use description from $parsedItem * @param bool $RTL True if the feed is RTL (Right-to-left) + * @param string|null $feedAuthor Author of the feed as fallback when the item has no Author * * @return Item */ - protected function buildItem(ItemInterface $parsedItem, ?string $body = null, bool $RTL = false): Item - { + protected function buildItem( + ItemInterface $parsedItem, + ?string $body = null, + bool $RTL = false, + $feedAuthor = null + ): Item { $item = new Item(); $item->setUnread(true); $itemLink = $parsedItem->getLink(); @@ -272,7 +278,9 @@ class FeedFetcher implements IFeedFetcher if ($itemTitle !== null) { $item->setTitle($this->decodeTwice($itemTitle)); } - $author = $parsedItem->getAuthor(); + + $author = $parsedItem->getAuthor() ?? $feedAuthor; + if ($author !== null && $author->getName() !== null) { $item->setAuthor($this->decodeTwice($author->getName())); } diff --git a/tests/command/feeds/no_guid_feed.xml b/tests/command/feeds/no_guid_feed.xml new file mode 100644 index 000000000..e27a1b07b --- /dev/null +++ b/tests/command/feeds/no_guid_feed.xml @@ -0,0 +1,78 @@ + + + + RSS Builder by B!Soft + Joshua Wright Dot Net + http://www.joshuawright.net + This is a feed of the latest comic masterpiece from joshuawright.net + en + joshua.wright@live.com.au (Josh Wright) + joshua.wright@live.com.au (Josh Wright) + 2016 JoshuaWright + + Slack Wyrm 911 - Feeling better + Wed, 13 Apr 2022 09:07:01 +0100 + https://joshuawright.net/slack-wyrm-911.html + + + + Slack Wyrm 910 - Cake trip + Mon, 11 Apr 2022 08:52:54 +0100 + https://joshuawright.net/slack-wyrm-910.html + + + + Slack Wyrm 909 - Cake time + Fri, 8 Apr 2022 09:29:32 +0100 + + + + Slack Wyrm 908 - Dragons Lair + Wed, 6 Apr 2022 09:00:13 +0100 + https://joshuawright.net/slack-wyrm-908.html + + + + Slack Wyrm 907 - Ten feet tall + Mon, 4 Apr 2022 15:12:51 +0100 + https://joshuawright.net/slack-wyrm-907.html + + + + Slack Wyrm 906 - True self + Fri, 1 Apr 2022 09:28:06 +1100 + https://joshuawright.net/slack-wyrm-906.html + + + + Slack Wyrm 905 - Drink up + Wed, 30 Mar 2022 11:07:49 +1100 + https://joshuawright.net/slack-wyrm-905.html + + + + Slack Wyrm 904 - Marvellous medicine + Mon, 28 Mar 2022 09:02:44 +1100 + https://joshuawright.net/slack-wyrm-904.html + + + + Slack Wyrm 903 - Golden still + Fri, 25 Mar 2022 09:48:06 +1100 + https://joshuawright.net/slack-wyrm-903.html + + + + Slack Wyrm 902 - Janet the genius + Wed, 23 Mar 2022 09:30:47 +1100 + https://joshuawright.net/slack-wyrm-902.html + + + + Slack Wyrm 901 - Bye bye Bucky + Mon, 21 Mar 2022 09:01:36 +1100 + https://joshuawright.net/slack-wyrm-901.html + + + + \ No newline at end of file diff --git a/tests/command/helpers/settings.bash b/tests/command/helpers/settings.bash index bf58a601a..d32924ceb 100644 --- a/tests/command/helpers/settings.bash +++ b/tests/command/helpers/settings.bash @@ -1,4 +1,4 @@ user=admin NC_FEED="https://nextcloud.com/blog/static-feed/" HEISE_FEED="https://www.heise.de/rss/heise-atom.xml" -NO_GUID_FEED="https://raw.githubusercontent.com/nextcloud/news/master/tests/integration/feeds/no_guid_feed.xml" +NO_GUID_FEED="https://raw.githubusercontent.com/nextcloud/news/master/tests/command/feeds/no_guid_feed.xml" diff --git a/tests/command/items.bats b/tests/command/items.bats index 22a4c73c7..8a7111299 100644 --- a/tests/command/items.bats +++ b/tests/command/items.bats @@ -82,3 +82,17 @@ teardown() { return $ret_status fi } + +@test "[$TESTSUITE] Test author fallback" { + ./occ news:feed:add "$user" $HEISE_FEED --title "Something-${BATS_SUITE_TEST_NUMBER}" + ID=$(./occ news:feed:list 'admin' | grep 'heise\.de' -1 | head -1 | grep -oE '[0-9]*') + + run ./occ news:item:list-feed "$user" "$ID" --limit 200 + [ "$status" -eq 0 ] + + if ! echo "$output" | grep '"author": "heise online",'; then + ret_status=$? + echo "Author fallback did not work" + return $ret_status + fi +} \ No newline at end of file diff --git a/tests/integration/feeds/no_guid_feed.xml b/tests/integration/feeds/no_guid_feed.xml deleted file mode 100644 index e27a1b07b..000000000 --- a/tests/integration/feeds/no_guid_feed.xml +++ /dev/null @@ -1,78 +0,0 @@ - - - - RSS Builder by B!Soft - Joshua Wright Dot Net - http://www.joshuawright.net - This is a feed of the latest comic masterpiece from joshuawright.net - en - joshua.wright@live.com.au (Josh Wright) - joshua.wright@live.com.au (Josh Wright) - 2016 JoshuaWright - - Slack Wyrm 911 - Feeling better - Wed, 13 Apr 2022 09:07:01 +0100 - https://joshuawright.net/slack-wyrm-911.html - - - - Slack Wyrm 910 - Cake trip - Mon, 11 Apr 2022 08:52:54 +0100 - https://joshuawright.net/slack-wyrm-910.html - - - - Slack Wyrm 909 - Cake time - Fri, 8 Apr 2022 09:29:32 +0100 - - - - Slack Wyrm 908 - Dragons Lair - Wed, 6 Apr 2022 09:00:13 +0100 - https://joshuawright.net/slack-wyrm-908.html - - - - Slack Wyrm 907 - Ten feet tall - Mon, 4 Apr 2022 15:12:51 +0100 - https://joshuawright.net/slack-wyrm-907.html - - - - Slack Wyrm 906 - True self - Fri, 1 Apr 2022 09:28:06 +1100 - https://joshuawright.net/slack-wyrm-906.html - - - - Slack Wyrm 905 - Drink up - Wed, 30 Mar 2022 11:07:49 +1100 - https://joshuawright.net/slack-wyrm-905.html - - - - Slack Wyrm 904 - Marvellous medicine - Mon, 28 Mar 2022 09:02:44 +1100 - https://joshuawright.net/slack-wyrm-904.html - - - - Slack Wyrm 903 - Golden still - Fri, 25 Mar 2022 09:48:06 +1100 - https://joshuawright.net/slack-wyrm-903.html - - - - Slack Wyrm 902 - Janet the genius - Wed, 23 Mar 2022 09:30:47 +1100 - https://joshuawright.net/slack-wyrm-902.html - - - - Slack Wyrm 901 - Bye bye Bucky - Mon, 21 Mar 2022 09:01:36 +1100 - https://joshuawright.net/slack-wyrm-901.html - - - - \ No newline at end of file -- cgit v1.2.3