From 245c6ac1f1bb236af094b9886aab93c1d6a9034a Mon Sep 17 00:00:00 2001 From: Wolfgang Date: Sat, 21 Dec 2024 00:15:54 +0100 Subject: [PATCH 1/5] fix: don't abort opml import when a single feed exists or can't be added Signed-off-by: Wolfgang --- CHANGELOG.md | 1 + lib/Service/OpmlService.php | 36 +++++++++++++++++++------- tests/Unit/Service/OPMLServiceTest.php | 6 +++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df28d9200..32891eac2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ You can also check [on GitHub](https://github.com/nextcloud/news/releases), the - adding feed via web-ui always use auto-discover - show 403 forbidden errors when fetching feeds - prevent move feed dialog from being closed unexpectedly +- don't abort `opml` import when a single feed exists or can't be added # Releases ## [25.1.1] - 2024-12-16 diff --git a/lib/Service/OpmlService.php b/lib/Service/OpmlService.php index 32bb28c62..ca3987471 100644 --- a/lib/Service/OpmlService.php +++ b/lib/Service/OpmlService.php @@ -14,9 +14,13 @@ namespace OCA\News\Service; +use OCA\News\Service\Exceptions\ServiceConflictException; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCA\News\Service\Exceptions\ServiceValidationException; use OCA\News\Utility\OPMLExporter; use OCA\News\Utility\OPMLImporter; use OCA\News\Db\Folder; +use Psr\Log\LoggerInterface; class OpmlService { @@ -25,6 +29,7 @@ public function __construct( private FeedServiceV2 $feedService, private OPMLExporter $exporter, private OPMLImporter $importer, + private LoggerInterface $logger, ) { //NO-OP } @@ -57,13 +62,14 @@ public function import(string $userId, string $data): bool { list($folders, $feeds) = $this->importer->import($userId, $data); + $error = false; $folderEntities = []; $dbFolders = $this->folderService->findAllForUser($userId); foreach ($folders as $folder) { $existing = array_filter($dbFolders, fn(Folder $dbFolder) => $dbFolder->getName() === $folder['name']); if (count($existing) > 0) { - $folderEntities[$folder['name']] = $existing[0]; + $folderEntities[$folder['name']] = array_pop($existing); continue; } @@ -75,15 +81,27 @@ public function import(string $userId, string $data): bool } foreach ($feeds as $feed) { + if ($this->feedService->existsForUser($userId, $feed['url'])) { + continue; + } $parent = $folderEntities[$feed['folder']] ?? null; - $this->feedService->create( - $userId, - $feed['url'], - $parent?->getId(), - full_text: false, - title: $feed['title'], - full_discover: false, - ); + try { + $this->feedService->create( + $userId, + $feed['url'], + $parent?->getId(), + full_text: false, + title: $feed['title'], + full_discover: false, + ); + } catch (ServiceNotFoundException | ServiceConflictException $e) { + $error = true; + $this->logger->warning('Could not import feed ' . $feed['url']); + } + } + + if ($error) { + throw new ServiceValidationException('Failed to import all feeds. Please check the server log!'); } return true; diff --git a/tests/Unit/Service/OPMLServiceTest.php b/tests/Unit/Service/OPMLServiceTest.php index 1e785d2e3..23ba84060 100644 --- a/tests/Unit/Service/OPMLServiceTest.php +++ b/tests/Unit/Service/OPMLServiceTest.php @@ -23,6 +23,8 @@ use OCA\News\Db\Feed; +use Psr\Log\LoggerInterface; + use PHPUnit\Framework\TestCase; class OPMLServiceTest extends TestCase @@ -76,6 +78,9 @@ protected function setUp(): void ->getMockBuilder(FeedServiceV2::class) ->disableOriginalConstructor() ->getMock(); + $this->logger = $this + ->getMockBuilder(LoggerInterface::class) + ->getMock(); $this->time = 333333; @@ -84,6 +89,7 @@ protected function setUp(): void $this->feedService, $this->exporter, $this->importer, + $this->logger, ); $this->uid = 'jack'; } From 3f4660eb8bbf84c263a31c6cb227632b35c07fb9 Mon Sep 17 00:00:00 2001 From: Wolfgang Date: Sat, 21 Dec 2024 13:38:09 +0100 Subject: [PATCH 2/5] Update lib/Service/OpmlService.php Co-authored-by: Sean Molenaar Signed-off-by: Wolfgang --- lib/Service/OpmlService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/OpmlService.php b/lib/Service/OpmlService.php index ca3987471..83d32dd68 100644 --- a/lib/Service/OpmlService.php +++ b/lib/Service/OpmlService.php @@ -62,7 +62,7 @@ public function import(string $userId, string $data): bool { list($folders, $feeds) = $this->importer->import($userId, $data); - $error = false; + $error = 0; $folderEntities = []; $dbFolders = $this->folderService->findAllForUser($userId); foreach ($folders as $folder) { From 5c4cf31ea32ba486a94685557cf34d12a44e8faf Mon Sep 17 00:00:00 2001 From: Wolfgang Date: Sat, 21 Dec 2024 13:38:16 +0100 Subject: [PATCH 3/5] Update lib/Service/OpmlService.php Co-authored-by: Sean Molenaar Signed-off-by: Wolfgang --- lib/Service/OpmlService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/OpmlService.php b/lib/Service/OpmlService.php index 83d32dd68..b9666680a 100644 --- a/lib/Service/OpmlService.php +++ b/lib/Service/OpmlService.php @@ -95,7 +95,7 @@ public function import(string $userId, string $data): bool full_discover: false, ); } catch (ServiceNotFoundException | ServiceConflictException $e) { - $error = true; + $error++; $this->logger->warning('Could not import feed ' . $feed['url']); } } From c51e0776b79b8bbefd75b7331c0b97d8025e396d Mon Sep 17 00:00:00 2001 From: Wolfgang Date: Sat, 21 Dec 2024 13:38:22 +0100 Subject: [PATCH 4/5] Update lib/Service/OpmlService.php Co-authored-by: Sean Molenaar Signed-off-by: Wolfgang --- lib/Service/OpmlService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/OpmlService.php b/lib/Service/OpmlService.php index b9666680a..ce9abe711 100644 --- a/lib/Service/OpmlService.php +++ b/lib/Service/OpmlService.php @@ -100,7 +100,7 @@ public function import(string $userId, string $data): bool } } - if ($error) { + if ($error > 0) { throw new ServiceValidationException('Failed to import all feeds. Please check the server log!'); } From 344346f7011469c17d520a3e262bccd583876a1a Mon Sep 17 00:00:00 2001 From: Wolfgang Date: Sat, 21 Dec 2024 13:38:28 +0100 Subject: [PATCH 5/5] Update lib/Service/OpmlService.php Co-authored-by: Sean Molenaar Signed-off-by: Wolfgang --- lib/Service/OpmlService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/OpmlService.php b/lib/Service/OpmlService.php index ce9abe711..ffa438f2e 100644 --- a/lib/Service/OpmlService.php +++ b/lib/Service/OpmlService.php @@ -101,7 +101,7 @@ public function import(string $userId, string $data): bool } if ($error > 0) { - throw new ServiceValidationException('Failed to import all feeds. Please check the server log!'); + throw new ServiceValidationException("Failed to import $error feeds. Please check the server log!"); } return true;