Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't abort opml import when a single feed exists or can't be added #2996

Merged
merged 5 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

# Releases
## [25.1.1] - 2024-12-16
Expand Down
36 changes: 27 additions & 9 deletions lib/Service/OpmlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -25,6 +29,7 @@ public function __construct(
private FeedServiceV2 $feedService,
private OPMLExporter $exporter,
private OPMLImporter $importer,
private LoggerInterface $logger,
) {
//NO-OP
}
Expand Down Expand Up @@ -57,13 +62,14 @@ public function import(string $userId, string $data): bool
{
list($folders, $feeds) = $this->importer->import($userId, $data);

$error = 0;
$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;
}

Expand All @@ -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++;
$this->logger->warning('Could not import feed ' . $feed['url']);
}
}

if ($error > 0) {
throw new ServiceValidationException("Failed to import $error feeds. Please check the server log!");
}

return true;
Expand Down
6 changes: 6 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,9 @@ protected function setUp(): void
->getMockBuilder(FeedServiceV2::class)
->disableOriginalConstructor()
->getMock();
$this->logger = $this
->getMockBuilder(LoggerInterface::class)
->getMock();

$this->time = 333333;

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