Skip to content

Commit

Permalink
fix: don't abort opml import when a single feed exists or can't be added
Browse files Browse the repository at this point in the history
Signed-off-by: Wolfgang <github@linux-dude.de>
  • Loading branch information
wofferl committed Dec 20, 2024
1 parent 5dc9b0d commit 103fcf4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 17 in CHANGELOG.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'opml'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'opml'?", "location": {"path": "CHANGELOG.md", "range": {"start": {"line": 17, "column": 15}}}, "severity": "ERROR"}

# Releases
## [25.1.1] - 2024-12-16
Expand Down
34 changes: 25 additions & 9 deletions lib/Service/OpmlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

namespace OCA\News\Service;

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
{
Expand All @@ -25,6 +27,7 @@ public function __construct(
private FeedServiceV2 $feedService,
private OPMLExporter $exporter,
private OPMLImporter $importer,
private LoggerInterface $logger,
) {
//NO-OP
}
Expand Down Expand Up @@ -57,13 +60,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']] = reset($existing);
continue;
}

Expand All @@ -75,15 +79,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 (\Exception $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;
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/Service/OPMLServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

use OCA\News\Db\Feed;

use Psr\Log\LoggerInterface;

use PHPUnit\Framework\TestCase;

class OPMLServiceTest extends TestCase
Expand Down Expand Up @@ -76,6 +78,10 @@ protected function setUp(): void
->getMockBuilder(FeedServiceV2::class)
->disableOriginalConstructor()
->getMock();
$this->logger = $this
->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();

$this->time = 333333;

Expand All @@ -84,6 +90,7 @@ protected function setUp(): void
$this->feedService,
$this->exporter,
$this->importer,
$this->logger,
);
$this->uid = 'jack';
}
Expand Down

0 comments on commit 103fcf4

Please sign in to comment.