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

Allow all ISO 8601 timestamps #187

Merged
merged 2 commits into from
Dec 1, 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
111 changes: 75 additions & 36 deletions lib/Core/EpisodeAction/EpisodeActionSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,49 @@

class EpisodeActionSaver
{

private EpisodeActionRepository $episodeActionRepository;
private EpisodeActionWriter $episodeActionWriter;
private EpisodeActionReader $episodeActionReader;

private const DATETIME_FORMAT = 'Y-m-d\TH:i:s';

public function __construct(
EpisodeActionRepository $episodeActionRepository,
EpisodeActionWriter $episodeActionWriter,
EpisodeActionReader $episodeActionReader
)
{
EpisodeActionWriter $episodeActionWriter,
EpisodeActionReader $episodeActionReader
) {
$this->episodeActionRepository = $episodeActionRepository;
$this->episodeActionWriter = $episodeActionWriter;
$this->episodeActionReader = $episodeActionReader;
}

public function saveEpisodeActions(array $episodeActionsArray, string $userId): array
{
$episodeActions = $this->episodeActionReader->fromArray($episodeActionsArray);
public function saveEpisodeActions(
array $episodeActionsArray,
string $userId
): array {
$episodeActions = $this->episodeActionReader->fromArray(
$episodeActionsArray
);

$episodeActionEntities = [];

foreach ($episodeActions as $episodeAction) {
$episodeActionEntity = $this->hydrateEpisodeActionEntity($episodeAction, $userId);
$episodeActionEntity = $this->hydrateEpisodeActionEntity(
$episodeAction,
$userId
);

try {
$episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity);
$episodeActionEntities[] = $this->episodeActionWriter->save(
$episodeActionEntity
);
} catch (Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
if (
$exception->getReason() ===
Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION
) {
$episodeActionEntities[] = $this->updateEpisodeAction(
$episodeActionEntity,
$userId
);
}
}
}
Expand All @@ -51,42 +62,59 @@ public function saveEpisodeActions(array $episodeActionsArray, string $userId):

private function convertTimestampToUnixEpoch(string $timestamp): string
{
return DateTime::createFromFormat(self::DATETIME_FORMAT, $timestamp)
->format("U");
$dateTime = new DateTime($timestamp);
JonOfUs marked this conversation as resolved.
Show resolved Hide resolved
return $dateTime->format("U");
}

private function updateEpisodeAction(
EpisodeActionEntity $episodeActionEntity,
string $userId
): EpisodeActionEntity
{
$episodeActionToUpdate = $this->findEpisodeActionToUpdate($episodeActionEntity, $userId);
string $userId
): EpisodeActionEntity {
$episodeActionToUpdate = $this->findEpisodeActionToUpdate(
$episodeActionEntity,
$userId
);

$episodeActionEntity->setId($episodeActionToUpdate->getId());

$this->ensureGuidDoesNotGetNulledWithOldData($episodeActionToUpdate, $episodeActionEntity);
$this->ensureGuidDoesNotGetNulledWithOldData(
$episodeActionToUpdate,
$episodeActionEntity
);

try {
return $this->episodeActionWriter->update($episodeActionEntity);
} catch (Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$this->deleteConflictingEpisodeAction($episodeActionEntity, $userId);
if (
$exception->getReason() ===
Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION
) {
$this->deleteConflictingEpisodeAction(
$episodeActionEntity,
$userId
);
}
}
return $this->episodeActionWriter->update($episodeActionEntity);

}

private function ensureGuidDoesNotGetNulledWithOldData(EpisodeAction $episodeActionToUpdate, EpisodeActionEntity $episodeActionEntity): void
{
private function ensureGuidDoesNotGetNulledWithOldData(
EpisodeAction $episodeActionToUpdate,
EpisodeActionEntity $episodeActionEntity
): void {
$existingGuid = $episodeActionToUpdate->getGuid();
if ($existingGuid !== null && $episodeActionEntity->getGuid() === null) {
if (
$existingGuid !== null &&
$episodeActionEntity->getGuid() === null
) {
$episodeActionEntity->setGuid($existingGuid);
}
}

private function hydrateEpisodeActionEntity(EpisodeAction $episodeAction, string $userId): EpisodeActionEntity
{
private function hydrateEpisodeActionEntity(
EpisodeAction $episodeAction,
string $userId
): EpisodeActionEntity {
$episodeActionEntity = new EpisodeActionEntity();
$episodeActionEntity->setPodcast($episodeAction->getPodcast());
$episodeActionEntity->setEpisode($episodeAction->getEpisode());
Expand All @@ -95,14 +123,18 @@ private function hydrateEpisodeActionEntity(EpisodeAction $episodeAction, string
$episodeActionEntity->setPosition($episodeAction->getPosition());
$episodeActionEntity->setStarted($episodeAction->getStarted());
$episodeActionEntity->setTotal($episodeAction->getTotal());
$episodeActionEntity->setTimestampEpoch($this->convertTimestampToUnixEpoch($episodeAction->getTimestamp()));
$episodeActionEntity->setTimestampEpoch(
$this->convertTimestampToUnixEpoch($episodeAction->getTimestamp())
);
$episodeActionEntity->setUserId($userId);

return $episodeActionEntity;
}

private function findEpisodeActionToUpdate(EpisodeActionEntity $episodeActionEntity, string $userId): ?EpisodeAction
{
private function findEpisodeActionToUpdate(
EpisodeActionEntity $episodeActionEntity,
string $userId
): ?EpisodeAction {
$episodeAction = null;
if ($episodeActionEntity->getGuid() !== null) {
$episodeAction = $this->episodeActionRepository->findByGuid(
Expand All @@ -126,11 +158,18 @@ private function findEpisodeActionToUpdate(EpisodeActionEntity $episodeActionEnt
* @param string $userId
* @return void
*/
private function deleteConflictingEpisodeAction(EpisodeActionEntity $episodeActionEntity, string $userId): void
{
$collidingEpisodeActionId = $this->episodeActionRepository->findByEpisodeUrl($episodeActionEntity->getEpisode(), $userId)->getId();
private function deleteConflictingEpisodeAction(
EpisodeActionEntity $episodeActionEntity,
string $userId
): void {
$collidingEpisodeActionId = $this->episodeActionRepository
->findByEpisodeUrl($episodeActionEntity->getEpisode(), $userId)
->getId();
if ($collidingEpisodeActionId !== $episodeActionEntity->getId()) {
$this->episodeActionRepository->deleteEpisodeActionByEpisodeUrl($episodeActionEntity->getEpisode(), $userId);
$this->episodeActionRepository->deleteEpisodeActionByEpisodeUrl(
$episodeActionEntity->getEpisode(),
$userId
);
}
}
}
127 changes: 95 additions & 32 deletions tests/Integration/EpisodeActionRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,110 @@

class EpisodeActionRepositoryTest extends \Test\TestCase
{
private const USER_ID_0 = "testuser0";
private const USER_ID_0 = "testuser0";

private IAppContainer $container;
private IAppContainer $container;

public function setUp(): void {
parent::setUp();
$app = new App('gpoddersync');
$this->container = $app->getContainer();
}
public function setUp(): void
{
parent::setUp();
$app = new App("gpoddersync");
$this->container = $app->getContainer();
}

public function testTimestampOutputIsUTCHumandReadable() : void
{
/** @var EpisodeActionSaver $episodeActionSaver */
$episodeActionSaver = $this->container->get(EpisodeActionSaver::class);
public function testTimestampOutputIsUTCHumandReadable(): void
{
/** @var EpisodeActionSaver $episodeActionSaver */
$episodeActionSaver = $this->container->get(EpisodeActionSaver::class);

$episodeUrl = uniqid("test_https://dts.podtrac.com/");
$episodeUrl = uniqid("test_https://dts.podtrac.com/");

$timestampHumanReadable = "2021-08-22T23:58:56";
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");
$timestampHumanReadable = "2021-08-22T23:58:56";
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");

$savedEpisodeActionEntity = $episodeActionSaver->saveEpisodeActions(
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];
$savedEpisodeActionEntity = $episodeActionSaver->saveEpisodeActions(
[
[
"podcast" =>
"https://rss.art19.com/dr-death-s3-miracle-man",
"episode" => $episodeUrl,
"guid" => $guid,
"action" => "PLAY",
"timestamp" => "2021-08-22T23:58:56",
"started" => 47,
"position" => 54,
"total" => 2252,
],
],
self::USER_ID_0
)[0];

self::assertSame(1629676736, $savedEpisodeActionEntity->getTimestampEpoch());
self::assertSame(
1629676736,
$savedEpisodeActionEntity->getTimestampEpoch()
);

$timestampOutputFormatted =
(\DateTime::createFromFormat("U", (string)$savedEpisodeActionEntity->getTimestampEpoch()))
->setTimezone(new \DateTimeZone('UTC'))
->format('Y-m-d\TH:i:s');
self::assertSame(
$timestampHumanReadable,
$timestampOutputFormatted
);
$timestampOutputFormatted = \DateTime::createFromFormat(
"U",
(string) $savedEpisodeActionEntity->getTimestampEpoch()
)
->setTimezone(new \DateTimeZone("UTC"))
->format("Y-m-d\TH:i:s");
self::assertSame($timestampHumanReadable, $timestampOutputFormatted);

/** @var $episodeActionRepository EpisodeActionRepository */
$episodeActionRepository = $this->container->get(EpisodeActionRepository::class);
/** @var $episodeActionRepository EpisodeActionRepository */
$episodeActionRepository = $this->container->get(
EpisodeActionRepository::class
);

$retrievedEpisodeActionEntity = $episodeActionRepository->findByGuid($guid, self::USER_ID_0);
self::assertSame('2021-08-22T23:58:56', $retrievedEpisodeActionEntity->getTimestamp());
$retrievedEpisodeActionEntity = $episodeActionRepository->findByGuid(
$guid,
self::USER_ID_0
);
self::assertSame(
"2021-08-22T23:58:56",
$retrievedEpisodeActionEntity->getTimestamp()
);
}

}
public function testTimestampsWithTimezones(): void
{
/** @var EpisodeActionSaver $episodeActionSaver */
$episodeActionSaver = $this->container->get(EpisodeActionSaver::class);

$podcast = "https://example.com/rss";
$episodeUrl = uniqid("test_https://dts.podtrac.com/");
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");

$timestamps = [
["2024-12-01T16:29:56", 1733070596],
["2024-12-01T16:29:56Z", 1733070596],
["2024-12-01T16:29:56+01", 1733066996],
["2024-12-01T16:29:56+00:00", 1733070596],
["2024-12-01T16:29:56+02:00", 1733063396],
];

foreach ($timestamps as [$str, $epoch]) {
$savedEpisodeActionEntity = $episodeActionSaver->saveEpisodeActions(
[
[
"podcast" => $podcast,
"episode" => $episodeUrl,
"guid" => $guid,
"action" => "PLAY",
"timestamp" => $str,
"started" => 47,
"position" => 54,
"total" => 2252,
],
],
self::USER_ID_0
)[0];

self::assertSame(
$epoch,
$savedEpisodeActionEntity->getTimestampEpoch()
);
}
}
}