From 4f3f0fc666a4a455589d3d874384d5f6c84fcadc Mon Sep 17 00:00:00 2001 From: "Abdulmhsen B. A. A" Date: Wed, 13 Dec 2023 14:34:05 +0300 Subject: [PATCH] updated backends.emby docs. --- src/Backends/Emby/Action/ParseWebhook.php | 83 +++++++++++++++-------- src/Backends/Emby/Action/Progress.php | 40 ++++++----- 2 files changed, 76 insertions(+), 47 deletions(-) diff --git a/src/Backends/Emby/Action/ParseWebhook.php b/src/Backends/Emby/Action/ParseWebhook.php index 24f75e14..14a7d8c4 100644 --- a/src/Backends/Emby/Action/ParseWebhook.php +++ b/src/Backends/Emby/Action/ParseWebhook.php @@ -60,7 +60,11 @@ final class ParseWebhook */ public function __invoke(Context $context, iGuid $guid, iRequest $request): Response { - return $this->tryResponse(context: $context, fn: fn() => $this->parse($context, $guid, $request)); + return $this->tryResponse( + context: $context, + fn: fn() => $this->parse($context, $guid, $request), + action: 'emby.parse.webhook' + ); } private function parse(Context $context, iGuid $guid, iRequest $request): Response @@ -68,7 +72,10 @@ private function parse(Context $context, iGuid $guid, iRequest $request): Respon if (null === ($json = $request->getParsedBody())) { return new Response(status: false, extra: [ 'http_code' => 400, - 'message' => $context->clientName . ': No payload.' + 'message' => r('[{client}: {backend}] No payload was found in request body.', [ + 'client' => $context->clientName, + 'backend' => $context->backendName, + ]) ]); } @@ -79,21 +86,33 @@ private function parse(Context $context, iGuid $guid, iRequest $request): Respon if (null === $type || false === in_array($type, self::WEBHOOK_ALLOWED_TYPES)) { return new Response(status: false, extra: [ 'http_code' => 200, - 'message' => sprintf('%s: Webhook content type [%s] is not supported.', $context->backendName, $type) + 'message' => r('[{client}: {backend}]: Webhook content type [{type}] is not supported.', [ + 'client' => $context->clientName, + 'backend' => $context->backendName, + 'type' => $type + ]) ]); } if (null === $event || false === in_array($event, self::WEBHOOK_ALLOWED_EVENTS)) { return new Response(status: false, extra: [ 'http_code' => 200, - 'message' => sprintf('%s: Webhook event type [%s] is not supported.', $context->backendName, $event) + 'message' => r('[{client}: {backend}]: Webhook event type [{event}] is not supported.', [ + 'client' => $context->clientName, + 'backend' => $context->backendName, + 'event' => $event, + ]) ]); } if (null === $id) { return new Response(status: false, extra: [ 'http_code' => 400, - 'message' => $context->backendName . ': No item id was found in body.' + 'message' => r('[{client}: {backend}]: No item id was found in request body.', [ + 'client' => $context->clientName, + 'backend' => $context->backendName, + 'event' => $event, + ]) ]); } @@ -107,15 +126,11 @@ private function parse(Context $context, iGuid $guid, iRequest $request): Respon $isPlayed = 0; $lastPlayedAt = makeDate(ag($json, 'Item.DateCreated'))->getTimestamp(); } else { - $isPlayed = (int)(bool)ag( - $json, - [ - 'Item.Played', - 'Item.PlayedToCompletion', - 'PlaybackInfo.PlayedToCompletion', - ], - false - ); + $isPlayed = (int)(bool)ag($json, [ + 'Item.Played', + 'Item.PlayedToCompletion', + 'PlaybackInfo.PlayedToCompletion', + ], false); $lastPlayedAt = (0 === $isPlayed) ? makeDate(ag($json, 'Item.DateCreated'))->getTimestamp() : time(); } @@ -125,21 +140,21 @@ private function parse(Context $context, iGuid $guid, iRequest $request): Respon 'id' => ag($obj, 'Id'), 'type' => ag($obj, 'Type'), 'title' => match (ag($obj, 'Type')) { - EmbyClient::TYPE_MOVIE => sprintf( - '%s (%s)', - ag($obj, ['Name', 'OriginalTitle'], '??'), - ag($obj, 'ProductionYear', '0000') - ), + EmbyClient::TYPE_MOVIE => r('{title} ({year})', [ + 'title' => ag($obj, ['Name', 'OriginalTitle'], '??'), + 'year' => ag($obj, 'ProductionYear', '0000') + ]), EmbyClient::TYPE_EPISODE => trim( - sprintf( - '%s - (%sx%s)', - ag($obj, 'SeriesName', '??'), - str_pad((string)ag($obj, 'ParentIndexNumber', 0), 2, '0', STR_PAD_LEFT), - str_pad((string)ag($obj, 'IndexNumber', 0), 3, '0', STR_PAD_LEFT), - ) + r('{title} - ({season}x{episode})', [ + 'title' => ag($obj, 'SeriesName', '??'), + 'season' => str_pad((string)ag($obj, 'ParentIndexNumber', 0), 2, '0', STR_PAD_LEFT), + 'episode' => str_pad((string)ag($obj, 'IndexNumber', 0), 3, '0', STR_PAD_LEFT), + ]) ), default => throw new InvalidArgumentException( - r('Unexpected Content type [{type}] was received.', [ + r('[{client}: {backend}] Unexpected Content type [{type}] was received.', [ + 'client' => $context->clientName, + 'backend' => $context->backendName, 'type' => $type ]) ), @@ -192,9 +207,10 @@ private function parse(Context $context, iGuid $guid, iRequest $request): Respon } if (false === $isPlayed && null !== ($progress = ag($json, 'PlaybackInfo.PositionTicks', null))) { + // -- Convert to milliseconds. $fields[iState::COLUMN_META_DATA][$context->backendName][iState::COLUMN_META_DATA_PROGRESS] = (string)floor( $progress / 1_00_00 - ); // -- Convert to milliseconds. + ); } $entity = $this->createEntity( @@ -208,8 +224,9 @@ private function parse(Context $context, iGuid $guid, iRequest $request): Respon return new Response( status: false, error: new Error( - message: 'Ignoring [{backend}] [{title}] webhook event. No valid/supported external ids.', + message: 'Ignoring [{client}: {backend}] [{title}] webhook event. No valid/supported external ids.', context: [ + 'client' => $context->clientName, 'backend' => $context->backendName, 'title' => $entity->getName(), 'context' => [ @@ -222,7 +239,10 @@ private function parse(Context $context, iGuid $guid, iRequest $request): Respon ), extra: [ 'http_code' => 200, - 'message' => $context->backendName . ': Import ignored. No valid/supported external ids.' + 'message' => r('[{client}: {backend}] Import ignored. No valid/supported external ids.', [ + 'client' => $context->clientName, + 'backend' => $context->backendName, + ]) ], ); } @@ -258,7 +278,10 @@ private function parse(Context $context, iGuid $guid, iRequest $request): Respon ), extra: [ 'http_code' => 200, - 'message' => $context->backendName . ': Failed to handle payload. Check logs.' + 'message' => r('[{client}: {backend}] Failed to handle webhook event payload. Check logs.', [ + 'client' => $context->clientName, + 'backend' => $context->backendName, + ]), ], ); } diff --git a/src/Backends/Emby/Action/Progress.php b/src/Backends/Emby/Action/Progress.php index 595614a0..c5c623ed 100644 --- a/src/Backends/Emby/Action/Progress.php +++ b/src/Backends/Emby/Action/Progress.php @@ -27,7 +27,7 @@ public function __construct(protected HttpClientInterface $http, protected Logge } /** - * Push Play state. + * Push play progress. * * @param Context $context * @param iGuid $guid @@ -83,17 +83,8 @@ private function action( ]; if ($context->backendName === $entity->via) { - $this->logger->info('Ignoring [{item.title}] for [{backend}]. Event originated from this backend.', [ - 'backend' => $context->backendName, - ...$logContext, - ]); - continue; - } - - if (null === ag($metadata, iState::COLUMN_ID, null)) { - $this->logger->warning( - 'Ignoring [{item.title}] for [{backend}]. No metadata was found.', - [ + $this->logger->info('Ignoring [{item.title}] for [{client}: {backend}]. Event generator.', [ + 'client' => $context->clientName, 'backend' => $context->backendName, ...$logContext, ] @@ -101,9 +92,19 @@ private function action( continue; } + if (null === ag($metadata, iState::COLUMN_ID, null)) { + $this->logger->warning('Ignoring [{item.title}] for [{client}: {backend}]. No metadata.', [ + 'client' => $context->clientName, + 'backend' => $context->backendName, + ...$logContext, + ]); + continue; + } + $senderDate = ag($entity->getExtra($entity->via), iState::COLUMN_EXTRA_DATE); if (null === $senderDate) { - $this->logger->warning('Ignoring [{item.title}] for [{backend}]. Sender did not set a date.', [ + $this->logger->warning('Ignoring [{item.title}] for [{client}: {backend}]. Sender date is not set.', [ + 'client' => $context->clientName, 'backend' => $context->backendName, ...$logContext, ]); @@ -114,8 +115,9 @@ private function action( $datetime = ag($entity->getExtra($context->backendName), iState::COLUMN_EXTRA_DATE, null); if (false === $ignoreDate && null !== $datetime && makeDate($datetime)->getTimestamp() > $senderDate) { $this->logger->warning( - 'Ignoring [{item.title}] for [{backend}]. Sender date is older than backend date.', + 'Ignoring [{item.title}] for [{client}: {backend}]. Sender date is older than recorded backend date.', [ + 'client' => $context->clientName, 'backend' => $context->backendName, ...$logContext, ] @@ -136,8 +138,9 @@ private function action( if (false === $ignoreDate && makeDate($remoteItem->updated)->getTimestamp() > $senderDate) { $this->logger->info( - 'Ignoring [{item.title}] for [{backend}]. Sender date is older than backend item date.', + 'Ignoring [{item.title}] for [{client}: {backend}]. Sender date is older than backend remote item date.', [ + 'client' => $context->clientName, 'backend' => $context->backendName, ...$logContext, ] @@ -147,8 +150,9 @@ private function action( if ($remoteItem->isWatched()) { $this->logger->info( - 'Ignoring [{item.title}] for [{backend}]. The backend reported the item as watched.', + 'Ignoring [{item.title}] for [{client}: {backend}]. The backend reported the item as watched.', [ + 'client' => $context->clientName, 'backend' => $context->backendName, ...$logContext, ] @@ -190,8 +194,10 @@ private function action( $logContext['remote']['url'] = (string)$url; - $this->logger->debug('Updating [{backend}] {item.type} [{item.title}] watch progress.', [ + $this->logger->debug('Updating [{client}: {backend}] {item.type} [{item.title}] watch progress.', [ + // -- convert time to ticks for emby to understand it. 'time' => floor($entity->getPlayProgress() * 1_00_00), + 'client' => $context->clientName, 'backend' => $context->backendName, ...$logContext, ]);