Skip to content

Commit

Permalink
nextcloud < v22.0 throws UniqueConstraintViolationException. This can…
Browse files Browse the repository at this point in the history
… be reverted after v21 reaches end of support
  • Loading branch information
thrillfall committed Aug 21, 2021
1 parent ab155ca commit 57f0691
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
20 changes: 17 additions & 3 deletions lib/Controller/EpisodeActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use DateTime;
use DateTimeZone;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use GuzzleHttp\Psr7\Response;
use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionReader;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity;
Expand Down Expand Up @@ -67,11 +68,11 @@ public function create($data) {

try {
return $this->episodeActionWriter->save($episodeActionEntity);
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
return $this->updateEpisodeAction($episodeAction, $episodeActionEntity);
} catch (\Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$idEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisode($episodeAction->getEpisode(), $this->userId)->getId();
$episodeActionEntity->setId($idEpisodeActionEntityToUpdate);
return $this->episodeActionWriter->update($episodeActionEntity);
return $this->updateEpisodeAction($episodeAction, $episodeActionEntity);
}
}
}
Expand Down Expand Up @@ -114,4 +115,17 @@ private function convertTimestampToDbDateTimeString(string $timestamp)
->setTimezone(new DateTimeZone('UTC'))
->format("Y-m-d\TH:i:s");
}

/**
* @param \OCA\GPodderSync\Core\EpisodeAction\EpisodeAction $episodeAction
* @param EpisodeActionEntity $episodeActionEntity
*
* @return EpisodeActionEntity
*/
private function updateEpisodeAction(\OCA\GPodderSync\Core\EpisodeAction\EpisodeAction $episodeAction, EpisodeActionEntity $episodeActionEntity): EpisodeActionEntity
{
$idEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisode($episodeAction->getEpisode(), $this->userId)->getId();
$episodeActionEntity->setId($idEpisodeActionEntityToUpdate);
return $this->episodeActionWriter->update($episodeActionEntity);
}
}
29 changes: 23 additions & 6 deletions lib/Core/SubscriptionChange/SubscriptionChangeSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

namespace OCA\GPodderSync\Core\SubscriptionChange;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeEntity;
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeRepository;
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeWriter;
use OCP\DB\Exception;

class SubscriptionChangeSaver {
class SubscriptionChangeSaver
{

/**
* @var SubscriptionChangesReader
Expand All @@ -31,13 +33,15 @@ public function __construct(
SubscriptionChangeRequestParser $subscriptionChangeRequestParser,
SubscriptionChangeRepository $subscriptionChangeRepository,
SubscriptionChangeWriter $subscriptionChangeWriter
) {
)
{
$this->subscriptionChangeRepository = $subscriptionChangeRepository;
$this->subscriptionChangeWriter = $subscriptionChangeWriter;
$this->subscriptionChangeRequestParser = $subscriptionChangeRequestParser;
}

public function saveSubscriptionChanges(array $urlsSubscribed, array $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();
Expand All @@ -48,15 +52,28 @@ public function saveSubscriptionChanges(array $urlsSubscribed, array $urlsUnsubs

try {
$this->subscriptionChangeWriter->create($subscriptionChangeEntity);
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
$this->updateSubscription($subscriptionChangeEntity, $userId);
} catch (\Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$idEpisodeActionEntityToUpdate = $this->subscriptionChangeRepository->findByUrl($subscriptionChangeEntity->getUrl(), $userId)->getId();
$subscriptionChangeEntity->setId($idEpisodeActionEntityToUpdate);
$this->subscriptionChangeWriter->update($subscriptionChangeEntity);
$this->updateSubscription($subscriptionChangeEntity, $userId);
}
}
}
}

/**
* @param SubscriptionChangeEntity $subscriptionChangeEntity
* @param string $userId
*
* @return void
*/
private function updateSubscription(SubscriptionChangeEntity $subscriptionChangeEntity, string $userId): void
{
$idEpisodeActionEntityToUpdate = $this->subscriptionChangeRepository->findByUrl($subscriptionChangeEntity->getUrl(), $userId)->getId();
$subscriptionChangeEntity->setId($idEpisodeActionEntityToUpdate);
$this->subscriptionChangeWriter->update($subscriptionChangeEntity);
}


}

0 comments on commit 57f0691

Please sign in to comment.