make subscription changes endpoint comply with gpodder.net api

This commit is contained in:
thrillfall 2021-07-13 21:36:34 +02:00
parent edddecd027
commit 324aecd7b8
5 changed files with 21 additions and 39 deletions

View File

@ -15,14 +15,14 @@ class SubscriptionChangeRequestParser {
}
/**
* @param string $urlsSubscribed
* @param string $urlsUnsubscribed
* @param array $urlsSubscribed
* @param array $urlsUnsubscribed
*
* @return SubscriptionChange[]
*/
public function createSubscriptionChangeList(string $urlsSubscribed, string $urlsUnsubscribed): array {
$urlsToSubscribe = $this->subscriptionChangeReader->fromString($urlsSubscribed, true);
$urlsToDelete = $this->subscriptionChangeReader->fromString($urlsUnsubscribed, false);
public function createSubscriptionChangeList(array $urlsSubscribed, array $urlsUnsubscribed): array {
$urlsToSubscribe = $this->subscriptionChangeReader->mapToSubscriptionsChanges($urlsSubscribed, true);
$urlsToDelete = $this->subscriptionChangeReader->mapToSubscriptionsChanges($urlsUnsubscribed, false);
/** @var \OCA\GPodderSync\Core\SubscriptionChange\SubscriptionChange[] $subscriptionChanges */
return array_merge($urlsToSubscribe, $urlsToDelete);

View File

@ -37,7 +37,7 @@ class SubscriptionChangeSaver {
$this->subscriptionChangeRequestParser = $subscriptionChangeRequestParser;
}
public function saveSubscriptionChanges(string $urlsSubscribed, string $urlsUnsubscribed, string $userId): void {
public function saveSubscriptionChanges(array $urlsSubscribed, array $urlsUnsubscribed, string $userId): void {
$subscriptionChanges = $this->subscriptionChangeRequestParser->createSubscriptionChangeList($urlsSubscribed, $urlsUnsubscribed);
foreach ($subscriptionChanges as $urlChangedSubscriptionStatus) {
$subscriptionChangeEntity = new SubscriptionChangeEntity();

View File

@ -6,19 +6,14 @@ namespace OCA\GPodderSync\Core\SubscriptionChange;
class SubscriptionChangesReader {
/**
* @param string $raw
* @param array $urls
* @param bool $subscribed
*
* @return array|SubscriptionChange[]
* @return SubscriptionChange[]
*/
public function fromString(string $raw, bool $subscribed):? array {
$urls = str_replace(["[", "]", " "], "", $raw);
$urlList = explode(",", $urls);
if ($urlList[0] === "") {
return [];
}
public static function mapToSubscriptionsChanges(array $urls, bool $subscribed): array {
$subscriptionChanges = [];
foreach ($urlList as $url) {
foreach ($urls as $url) {
$subscriptionChanges[] = new SubscriptionChange($url, $subscribed);
}

View File

@ -7,17 +7,11 @@ use OCA\GPodderSync\Core\SubscriptionChange\SubscriptionChangesReader;
use Test\TestCase;
class SubscriptionChangeReaderTest extends TestCase {
public function testCreateFromString(): void {
$reader = new SubscriptionChangesReader();
$subscriptionChange = $reader->fromString('[https://feeds.megaphone.fm/HSW8286374095]', true);
$this->assertCount(1, $subscriptionChange);
public function testMapUrlsToSubscriptionChanges(): void {
$subscriptionChange = SubscriptionChangesReader::mapToSubscriptionsChanges(["https://feeds.megaphone.fm/HSW8286374095", "https://feeds.megaphone.fm/another"], true);
$this->assertCount(2, $subscriptionChange);
$this->assertSame("https://feeds.megaphone.fm/HSW8286374095", $subscriptionChange[0]->getUrl());
}
public function testCreateFromEmptyString(): void {
$reader = new SubscriptionChangesReader();
$subscriptionChange = $reader->fromString('[]', true);
$this->assertCount(0, $subscriptionChange);
$this->assertSame("https://feeds.megaphone.fm/another", $subscriptionChange[1]->getUrl());
}
}

View File

@ -13,20 +13,13 @@ class SubscriptionChangeRequestParserTest extends TestCase {
new SubscriptionChangesReader(),
);
$subscriptionChanges = $subscriptionChangesParser->createSubscriptionChangeList('[https://feeds.simplecast.com/54nAGcIl]','[]');
$this->assertCount(1, $subscriptionChanges);
$this->assertSame('https://feeds.simplecast.com/54nAGcIl', $subscriptionChanges[0]->getUrl());
$subscriptionChanges = $subscriptionChangesParser->createSubscriptionChangeList(["https://feeds.simplecast.com/54nAGcIl", "https://feeds.simplecast.com/another"],["https://i.am-removed/GcIl"]);
$this->assertCount(3, $subscriptionChanges);
$this->assertSame("https://feeds.simplecast.com/54nAGcIl", $subscriptionChanges[0]->getUrl());
$this->assertSame("https://feeds.simplecast.com/another", $subscriptionChanges[1]->getUrl());
$this->assertSame("https://i.am-removed/GcIl", $subscriptionChanges[2]->getUrl());
$this->assertTrue($subscriptionChanges[0]->isSubscribed());
$this->assertFalse($subscriptionChanges[2]->isSubscribed());
}
public function testSubscriptionRequestWithMultipleChangesConvertsToSubscriptionChangeList() {
$subscriptionChangesParser = new SubscriptionChangeRequestParser(
new SubscriptionChangesReader(),
);
$subscriptionChanges = $subscriptionChangesParser->createSubscriptionChangeList(
'[https://podcastfeeds.nbcnews.com/dateline-nbc,https://feeds.megaphone.fm/ADL9840290619]',
'[]');
$this->assertCount(2, $subscriptionChanges);
}
}