Skip to content

Commit 3651a51

Browse files
committed
fix: don't abort opml import when a single feed exists or can't be added
Signed-off-by: Wolfgang <[email protected]>
1 parent 670d48b commit 3651a51

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ You can also check [on GitHub](https://github.com/nextcloud/news/releases), the
1212
### Fixed
1313
- adding feed via web-ui always use auto-discover
1414
- show 403 forbidden errors when fetching feeds
15+
- don't abort opml import when a single feed exists or can't be added
1516

1617
# Releases
1718
## [25.1.1] - 2024-12-16

lib/Service/OpmlService.php

+25-9
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
namespace OCA\News\Service;
1616

17+
use OCA\News\Service\Exceptions\ServiceValidationException;
1718
use OCA\News\Utility\OPMLExporter;
1819
use OCA\News\Utility\OPMLImporter;
1920
use OCA\News\Db\Folder;
21+
use Psr\Log\LoggerInterface;
2022

2123
class OpmlService
2224
{
@@ -25,6 +27,7 @@ public function __construct(
2527
private FeedServiceV2 $feedService,
2628
private OPMLExporter $exporter,
2729
private OPMLImporter $importer,
30+
private LoggerInterface $logger,
2831
) {
2932
//NO-OP
3033
}
@@ -57,13 +60,14 @@ public function import(string $userId, string $data): bool
5760
{
5861
list($folders, $feeds) = $this->importer->import($userId, $data);
5962

63+
$error = false;
6064
$folderEntities = [];
6165
$dbFolders = $this->folderService->findAllForUser($userId);
6266
foreach ($folders as $folder) {
6367
$existing = array_filter($dbFolders, fn(Folder $dbFolder) => $dbFolder->getName() === $folder['name']);
6468

6569
if (count($existing) > 0) {
66-
$folderEntities[$folder['name']] = $existing[0];
70+
$folderEntities[$folder['name']] = reset($existing);
6771
continue;
6872
}
6973

@@ -75,15 +79,27 @@ public function import(string $userId, string $data): bool
7579
}
7680

7781
foreach ($feeds as $feed) {
82+
if ($this->feedService->existsForUser($userId, $feed['url'])) {
83+
continue;
84+
}
7885
$parent = $folderEntities[$feed['folder']] ?? null;
79-
$this->feedService->create(
80-
$userId,
81-
$feed['url'],
82-
$parent?->getId(),
83-
full_text: false,
84-
title: $feed['title'],
85-
full_discover: false,
86-
);
86+
try {
87+
$this->feedService->create(
88+
$userId,
89+
$feed['url'],
90+
$parent?->getId(),
91+
full_text: false,
92+
title: $feed['title'],
93+
full_discover: false,
94+
);
95+
} catch (\Exception $e) {
96+
$error = true;
97+
$this->logger->warning('Could not import feed ' . $feed['url']);
98+
}
99+
}
100+
101+
if ($error) {
102+
throw new ServiceValidationException('Failed to import all feeds. Please check the server log!');
87103
}
88104

89105
return true;

tests/Unit/Service/OPMLServiceTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
use OCA\News\Db\Feed;
2525

26+
use Psr\Log\LoggerInterface;
27+
2628
use PHPUnit\Framework\TestCase;
2729

2830
class OPMLServiceTest extends TestCase
@@ -76,6 +78,10 @@ protected function setUp(): void
7678
->getMockBuilder(FeedServiceV2::class)
7779
->disableOriginalConstructor()
7880
->getMock();
81+
$this->logger = $this
82+
->getMockBuilder(LoggerInterface::class)
83+
->disableOriginalConstructor()
84+
->getMock();
7985

8086
$this->time = 333333;
8187

@@ -84,6 +90,7 @@ protected function setUp(): void
8490
$this->feedService,
8591
$this->exporter,
8692
$this->importer,
93+
$this->logger,
8794
);
8895
$this->uid = 'jack';
8996
}

0 commit comments

Comments
 (0)