2021-06-27 11:19:26 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Controller;
|
|
|
|
|
|
|
|
use DateTime;
|
2021-07-24 17:06:14 +00:00
|
|
|
use DateTimeZone;
|
2021-08-21 18:20:24 +00:00
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
2021-06-27 11:19:26 +00:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionReader;
|
|
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity;
|
|
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
|
|
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter;
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2021-07-11 20:52:30 +00:00
|
|
|
use OCP\DB\Exception;
|
2021-06-27 11:19:26 +00:00
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
class EpisodeActionController extends Controller {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EpisodeActionRepository
|
|
|
|
*/
|
|
|
|
private EpisodeActionRepository $episodeActionRepository;
|
|
|
|
/**
|
|
|
|
* @var EpisodeActionWriter
|
|
|
|
*/
|
|
|
|
private EpisodeActionWriter $episodeActionWriter;
|
|
|
|
/**
|
|
|
|
* @var EpisodeActionReader
|
|
|
|
*/
|
|
|
|
private EpisodeActionReader $episodeActionReader;
|
|
|
|
private $userId;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
string $AppName,
|
|
|
|
IRequest $request,
|
|
|
|
$UserId,
|
|
|
|
EpisodeActionRepository $episodeActionRepository,
|
|
|
|
EpisodeActionWriter $episodeActionWriter,
|
|
|
|
EpisodeActionReader $episodeActionReader
|
|
|
|
) {
|
|
|
|
parent::__construct($AppName, $request);
|
|
|
|
$this->episodeActionRepository = $episodeActionRepository;
|
|
|
|
$this->episodeActionWriter = $episodeActionWriter;
|
|
|
|
$this->episodeActionReader = $episodeActionReader;
|
|
|
|
$this->userId = $UserId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create($data) {
|
|
|
|
$episodeAction = $this->episodeActionReader->fromString($data);
|
|
|
|
$episodeActionEntity = new EpisodeActionEntity();
|
|
|
|
$episodeActionEntity->setPodcast($episodeAction->getPodcast());
|
|
|
|
$episodeActionEntity->setEpisode($episodeAction->getEpisode());
|
|
|
|
$episodeActionEntity->setAction($episodeAction->getAction());
|
|
|
|
$episodeActionEntity->setPosition($episodeAction->getPosition());
|
|
|
|
$episodeActionEntity->setStarted($episodeAction->getStarted());
|
|
|
|
$episodeActionEntity->setTotal($episodeAction->getTotal());
|
2021-07-22 21:44:45 +00:00
|
|
|
$episodeActionEntity->setTimestamp($this->convertTimestampToDbDateTimeString($episodeAction->getTimestamp()));
|
2021-06-27 11:19:26 +00:00
|
|
|
$episodeActionEntity->setUserId($this->userId);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return $this->episodeActionWriter->save($episodeActionEntity);
|
2021-08-21 18:20:24 +00:00
|
|
|
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
|
|
|
|
return $this->updateEpisodeAction($episodeAction, $episodeActionEntity);
|
2021-08-22 19:59:45 +00:00
|
|
|
} catch (Exception $exception) {
|
2021-07-11 20:52:30 +00:00
|
|
|
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
2021-08-21 18:20:24 +00:00
|
|
|
return $this->updateEpisodeAction($episodeAction, $episodeActionEntity);
|
2021-07-11 20:52:30 +00:00
|
|
|
}
|
2021-06-27 11:19:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param int $since
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
2021-07-11 20:52:30 +00:00
|
|
|
public function list(int $since): JSONResponse {
|
2021-06-27 11:19:26 +00:00
|
|
|
$sinceDatetime = $this->createDateTimeFromTimestamp($since);
|
|
|
|
return new JSONResponse([
|
|
|
|
"actions" => $this->episodeActionRepository->findAll($sinceDatetime, $this->userId),
|
|
|
|
"timestamp" => time()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $since
|
|
|
|
*
|
|
|
|
* @return DateTime
|
|
|
|
*/
|
|
|
|
private function createDateTimeFromTimestamp(?int $since): DateTime {
|
2021-08-12 13:27:58 +00:00
|
|
|
return ($since !== null)
|
2021-06-27 11:19:26 +00:00
|
|
|
? (new \DateTime)->setTimestamp($since)
|
|
|
|
: (new \DateTime('-1 week'));
|
|
|
|
}
|
2021-07-22 21:44:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $episodeAction
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function convertTimestampToDbDateTimeString(string $timestamp)
|
|
|
|
{
|
|
|
|
return \DateTime::createFromFormat('D F d H:i:s T Y', $timestamp)
|
2021-07-24 17:06:14 +00:00
|
|
|
->setTimezone(new DateTimeZone('UTC'))
|
2021-07-24 18:11:42 +00:00
|
|
|
->format("Y-m-d\TH:i:s");
|
2021-07-22 21:44:45 +00:00
|
|
|
}
|
2021-08-21 18:20:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
2021-06-27 11:19:26 +00:00
|
|
|
}
|