Special case watched/updated column to better support mark as unwatched.

This commit is contained in:
Abdulmhsen B. A. A
2022-06-01 15:00:51 +03:00
parent d04d398ac0
commit 9e1b4f52ef
3 changed files with 35 additions and 23 deletions

View File

@@ -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',
),
]
);

View File

@@ -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.');

View File

@@ -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.');
}