From 9e1b4f52ef38bbc9a704479f1667a4fea2c4dcd8 Mon Sep 17 00:00:00 2001 From: "Abdulmhsen B. A. A" Date: Wed, 1 Jun 2022 15:00:51 +0300 Subject: [PATCH] Special case watched/updated column to better support mark as unwatched. --- src/Libs/Initializer.php | 6 +++--- src/Libs/Servers/JellyfinServer.php | 24 ++++++++++++++++-------- src/Libs/Servers/PlexServer.php | 28 ++++++++++++++++------------ 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/Libs/Initializer.php b/src/Libs/Initializer.php index 0d356654..7259fec2 100644 --- a/src/Libs/Initializer.php +++ b/src/Libs/Initializer.php @@ -367,7 +367,7 @@ final class Initializer return jsonResponse( status: 200, body: $local->getAll(), - headers: $responseHeaders + ['X-Status' => sprintf('Updated %s.', $entity->type)] + headers: $responseHeaders + ['X-Status' => sprintf('[D] Updated %s.', $entity->type)] ); } @@ -383,7 +383,7 @@ final class Initializer $message = 'Updated %1$s.'; if ($cloned->isWatched() !== $local->isWatched()) { - $message = 'Queued %1$s For push event. [Played: %1$s]'; + $message = '%1$s Marked as %2$s'; queuePush($local); } @@ -394,7 +394,7 @@ final class Initializer 'X-Status' => sprintf( $message, $entity->type, - $entity->isWatched() ? 'Yes' : 'No', + $entity->isWatched() ? 'Played' : 'Unplayed', ), ] ); diff --git a/src/Libs/Servers/JellyfinServer.php b/src/Libs/Servers/JellyfinServer.php index ea9773f5..96fd85fb 100644 --- a/src/Libs/Servers/JellyfinServer.php +++ b/src/Libs/Servers/JellyfinServer.php @@ -319,6 +319,12 @@ class JellyfinServer implements ServerInterface $lastPlayedAt = true === $isPlayed ? ag($json, 'LastPlayedDate') : null; $fields = [ + iFace::COLUMN_WATCHED => (int)$isPlayed, + iFace::COLUMN_META_DATA => [ + $this->name => [ + iFace::COLUMN_WATCHED => true === $isPlayed ? '1' : '0', + ] + ], iFace::COLUMN_EXTRA => [ $this->name => [ iFace::COLUMN_EXTRA_EVENT => $event, @@ -329,17 +335,14 @@ class JellyfinServer implements ServerInterface if (true === $isPlayed && null !== $lastPlayedAt) { $lastPlayedAt = makeDate($lastPlayedAt)->getTimestamp(); - - $fields += [ + $fields = array_replace_recursive($fields, [ iFace::COLUMN_UPDATED => $lastPlayedAt, - iFace::COLUMN_WATCHED => 1, iFace::COLUMN_META_DATA => [ $this->name => [ - iFace::COLUMN_WATCHED => '1', iFace::COLUMN_META_DATA_PLAYED_AT => (string)$lastPlayedAt, ] ], - ]; + ]); } $providersId = []; @@ -1984,9 +1987,14 @@ class JellyfinServer implements ServerInterface protected function createEntity(array $item, string $type, array $opts = []): StateEntity { - $isPlayed = (bool)ag($item, 'UserData.Played', false); - - $date = ag($item, true === $isPlayed ? 'UserData.LastPlayedDate' : 'DateCreated'); + // -- Handle watched/updated column in a special way to support mark as unplayed. + if (null !== ($opts['override'][iFace::COLUMN_WATCHED] ?? null)) { + $isPlayed = (bool)$opts['override'][iFace::COLUMN_WATCHED]; + $date = $opts['override'][iFace::COLUMN_WATCHED] ?? ag($item, 'DateCreated'); + } else { + $isPlayed = (bool)ag($item, 'UserData.Played', false); + $date = ag($item, true === $isPlayed ? ['UserData.LastPlayedDate', 'DateCreated'] : 'DateCreated'); + } if (null === $date) { throw new RuntimeException('No date was set on object.'); diff --git a/src/Libs/Servers/PlexServer.php b/src/Libs/Servers/PlexServer.php index 2050e1e4..7f362040 100644 --- a/src/Libs/Servers/PlexServer.php +++ b/src/Libs/Servers/PlexServer.php @@ -30,7 +30,6 @@ use Psr\Log\LoggerInterface; use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\InvalidArgumentException; use RuntimeException; -use stdClass; use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; @@ -366,6 +365,12 @@ class PlexServer implements ServerInterface $lastPlayedAt = true === $isPlayed ? ag($item, 'lastViewedAt') : null; $fields = [ + iFace::COLUMN_WATCHED => (int)$isPlayed, + iFace::COLUMN_META_DATA => [ + $this->name => [ + iFace::COLUMN_WATCHED => true === $isPlayed ? '1' : '0', + ] + ], iFace::COLUMN_EXTRA => [ $this->name => [ iFace::COLUMN_EXTRA_EVENT => $event, @@ -375,16 +380,14 @@ class PlexServer implements ServerInterface ]; if (true === $isPlayed && null !== $lastPlayedAt) { - $fields += [ + $fields = array_replace_recursive($fields, [ iFace::COLUMN_UPDATED => (int)$lastPlayedAt, - iFace::COLUMN_WATCHED => 1, iFace::COLUMN_META_DATA => [ $this->name => [ - iFace::COLUMN_WATCHED => '1', iFace::COLUMN_META_DATA_PLAYED_AT => (string)$lastPlayedAt, ] ], - ]; + ]); } if (null !== ($guids = $this->getGuids(ag($item, 'Guid', []))) && false === empty($guids)) { @@ -2129,16 +2132,17 @@ class PlexServer implements ServerInterface } } - protected function createEntity(StdClass|array $item, string $type, array $opts = []): StateEntity + protected function createEntity(array $item, string $type, array $opts = []): StateEntity { - if (false === is_array($item)) { - $item = (array)$item; + // -- Handle watched/updated column in a special way to support mark as unplayed. + if (null !== ($opts['override'][iFace::COLUMN_WATCHED] ?? null)) { + $isPlayed = (bool)$opts['override'][iFace::COLUMN_WATCHED]; + $date = $opts['override'][iFace::COLUMN_WATCHED] ?? ag($item, 'addedAt'); + } else { + $isPlayed = (bool)ag($item, 'viewCount', false); + $date = ag($item, true === $isPlayed ? 'lastViewedAt' : 'addedAt'); } - // -- Plex does not send played flag, so we have to rely on viewCount. - $isPlayed = (bool)ag($item, 'viewCount', false); - $date = ag($item, true === $isPlayed ? 'lastViewedAt' : 'addedAt'); - if (null === $date) { throw new RuntimeException('No date was set on object.'); }