Skip to content

Commit

Permalink
updated backends.emby docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
arabcoders committed Dec 13, 2023
1 parent 1d96f3a commit 4f3f0fc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 47 deletions.
83 changes: 53 additions & 30 deletions src/Backends/Emby/Action/ParseWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,22 @@ 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
{
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,
])
]);
}

Expand All @@ -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,
])
]);
}

Expand All @@ -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();
}
Expand All @@ -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
])
),
Expand Down Expand Up @@ -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(
Expand All @@ -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' => [
Expand All @@ -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,
])
],
);
}
Expand Down Expand Up @@ -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,
]),
],
);
}
Expand Down
40 changes: 23 additions & 17 deletions src/Backends/Emby/Action/Progress.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(protected HttpClientInterface $http, protected Logge
}

/**
* Push Play state.
* Push play progress.
*
* @param Context $context
* @param iGuid $guid
Expand Down Expand Up @@ -83,27 +83,28 @@ 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,
]
);
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,
]);
Expand All @@ -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,
]
Expand All @@ -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,
]
Expand All @@ -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,
]
Expand Down Expand Up @@ -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,
]);
Expand Down

0 comments on commit 4f3f0fc

Please sign in to comment.