From a2e271b20165e3a19fcdb2c773497cb58f26166d Mon Sep 17 00:00:00 2001 From: "Abdulmhsen B. A. A" Date: Thu, 5 May 2022 03:16:08 +0300 Subject: [PATCH] Preparing state:cache to support relative GUIDs. related to #69 --- src/Libs/Servers/EmbyServer.php | 13 ++++------ src/Libs/Servers/JellyfinServer.php | 30 +++++++++++----------- src/Libs/Servers/PlexServer.php | 39 +++++++++++++---------------- 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/Libs/Servers/EmbyServer.php b/src/Libs/Servers/EmbyServer.php index 1bd8e0d6..a3c29368 100644 --- a/src/Libs/Servers/EmbyServer.php +++ b/src/Libs/Servers/EmbyServer.php @@ -7,7 +7,6 @@ namespace App\Libs\Servers; use App\Libs\Config; use App\Libs\Container; use App\Libs\Entity\StateInterface; -use App\Libs\Guid; use App\Libs\HttpException; use DateTimeInterface; use Psr\Http\Message\ServerRequestInterface; @@ -147,18 +146,12 @@ class EmbyServer extends JellyfinServer $providersId = ag($json, 'Item.ProviderIds', []); - $guids = $this->getGuids($providersId, $type); - - foreach (Guid::fromArray($guids)->getPointers() as $guid) { - $this->cacheData[$guid] = ag($json, 'Item.Id'); - } - $row = [ 'type' => $type, 'updated' => time(), 'watched' => $isWatched, 'meta' => $meta, - ...$guids + ...$this->getGuids($providersId, $type) ]; $entity = Container::get(StateInterface::class)::fromArray($row)->setIsTainted($isTainted); @@ -178,6 +171,10 @@ class EmbyServer extends JellyfinServer ); } + foreach ($entity->getPointers() as $guid) { + $this->cacheData[$guid] = ag($json, 'item.Id'); + } + if (false === $isTainted && (true === Config::get('webhook.debug') || null !== ag( $request->getQueryParams(), 'debug' diff --git a/src/Libs/Servers/JellyfinServer.php b/src/Libs/Servers/JellyfinServer.php index bc662699..ef31cc07 100644 --- a/src/Libs/Servers/JellyfinServer.php +++ b/src/Libs/Servers/JellyfinServer.php @@ -323,18 +323,12 @@ class JellyfinServer implements ServerInterface $meta['parent'] = $this->getEpisodeParent(ag($json, 'ItemId'), ag($json, 'SeriesName')); } - $guids = $this->getGuids($providersId, $type); - - foreach (Guid::fromArray($guids)->getPointers() as $guid) { - $this->cacheData[$guid] = ag($json, 'Item.ItemId'); - } - $row = [ 'type' => $type, 'updated' => time(), 'watched' => (int)(bool)ag($json, 'Played', ag($json, 'PlayedToCompletion', 0)), 'meta' => $meta, - ...$guids + ...$this->getGuids($providersId, $type) ]; $entity = Container::get(StateInterface::class)::fromArray($row)->setIsTainted($isTainted); @@ -354,6 +348,10 @@ class JellyfinServer implements ServerInterface ); } + foreach ($entity->getPointers() as $guid) { + $this->cacheData[$guid] = ag($json, 'Item.ItemId'); + } + if (false === $isTainted && (true === Config::get('webhook.debug') || null !== ag( $request->getQueryParams(), 'debug' @@ -1250,7 +1248,7 @@ class JellyfinServer implements ServerInterface ); continue; } - $this->processForCache($type, $entity); + $this->processForCache($entity, $type, $cName); } } catch (PathNotFoundException $e) { $this->logger->error( @@ -1308,7 +1306,7 @@ class JellyfinServer implements ServerInterface ] ); }, - includeParent: false, + includeParent: true, ); } @@ -1553,17 +1551,21 @@ class JellyfinServer implements ServerInterface } } - protected function processForCache(string $type, StdClass $item): void + protected function processForCache(StdClass $item, string $type, string $library): void { try { - if (!$this->hasSupportedIds((array)($item->ProviderIds ?? []))) { + if ('show' === $type) { + $this->processShow($item, $library); return; } - $guids = $this->getGuids((array)($item->ProviderIds ?? []), $type); - foreach (Guid::fromArray($guids)->getPointers() as $guid) { - $this->cacheData[$guid] = $item->Id; + $date = $item->UserData?->LastPlayedDate ?? $item->DateCreated ?? $item->PremiereDate ?? null; + + if (null === $date) { + return; } + + $this->createEntity($item, $type); } catch (Throwable $e) { $this->logger->error($e->getMessage(), [ 'file' => $e->getFile(), diff --git a/src/Libs/Servers/PlexServer.php b/src/Libs/Servers/PlexServer.php index 94819f7d..6661960e 100644 --- a/src/Libs/Servers/PlexServer.php +++ b/src/Libs/Servers/PlexServer.php @@ -366,12 +366,6 @@ class PlexServer implements ServerInterface ]; } - $guids = $this->getGuids($json['Metadata']['Guid'] ?? [], $type); - - foreach (Guid::fromArray($guids)->getPointers() as $guid) { - $this->cacheData[$guid] = ag($json, 'Metadata.guid'); - } - if (StateInterface::TYPE_EPISODE === $type) { $parentId = ag($json, 'Metadata.grandparentRatingKey', fn() => ag($json, 'Metadata.parentRatingKey')); $meta['parent'] = null !== $parentId ? $this->getEpisodeParent($parentId) : []; @@ -382,7 +376,7 @@ class PlexServer implements ServerInterface 'updated' => time(), 'watched' => (int)(bool)ag($json, 'Metadata.viewCount', 0), 'meta' => $meta, - ...$guids + ...$this->getGuids($json['Metadata']['Guid'] ?? [], $type) ]; $entity = Container::get(StateInterface::class)::fromArray($row)->setIsTainted($isTainted); @@ -402,6 +396,10 @@ class PlexServer implements ServerInterface ); } + foreach ($entity->getPointers() as $guid) { + $this->cacheData[$guid] = ag($json, 'Metadata.guid'); + } + if (false !== $isTainted && (true === Config::get('webhook.debug') || null !== ag( $request->getQueryParams(), 'debug' @@ -1274,7 +1272,8 @@ class PlexServer implements ServerInterface ); continue; } - $this->processForCache($type, $entity); + + $this->processForCache($entity, $type, $cName); } } catch (PathNotFoundException $e) { $this->logger->error( @@ -1336,7 +1335,7 @@ class PlexServer implements ServerInterface ] ); }, - includeParent: false + includeParent: true ); } @@ -1551,7 +1550,6 @@ class PlexServer implements ServerInterface ); } - $entity = $this->createEntity($item, $type); $date = (int)($item->lastViewedAt ?? $item->updatedAt ?? $item->addedAt ?? 0); @@ -1561,6 +1559,8 @@ class PlexServer implements ServerInterface return; } + $entity = $this->createEntity($item, $type); + if (!$entity->hasGuids()) { if (null === ($item->Guid ?? null)) { $item->Guid = [['id' => $item->guid]]; @@ -1600,24 +1600,21 @@ class PlexServer implements ServerInterface } } - protected function processForCache(string $type, StdClass $item): void + protected function processForCache(StdClass $item, string $type, string $library): void { try { - if (null === ($item->Guid ?? null)) { - $item->Guid = [['id' => $item->guid]]; - } else { - $item->Guid[] = ['id' => $item->guid]; - } - - if (!$this->hasSupportedIds($item->Guid)) { + if ('show' === $type) { + $this->processShow($item, $library); return; } - $guids = $this->getGuids($item->Guid ?? [], $type); + $date = (int)($item->lastViewedAt ?? $item->updatedAt ?? $item->addedAt ?? 0); - foreach (Guid::fromArray($guids)->getPointers() as $guid) { - $this->cacheData[$guid] = $item->guid; + if (0 === $date) { + return; } + + $this->createEntity($item, $type); } catch (Throwable $e) { $this->logger->error($e->getMessage(), [ 'file' => $e->getFile(),