Added Pre-processor for webhook requests to extract needed attributes.
This commit is contained in:
@@ -53,6 +53,35 @@ class EmbyServer extends JellyfinServer
|
||||
);
|
||||
}
|
||||
|
||||
public static function processRequest(ServerRequestInterface $request): ServerRequestInterface
|
||||
{
|
||||
$userAgent = ag($request->getServerParams(), 'HTTP_USER_AGENT', '');
|
||||
|
||||
if (!str_starts_with($userAgent, 'Emby Server/')) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
$body = clone $request->getBody();
|
||||
|
||||
if (null === ($json = json_decode((string)$body, true))) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
$attributes = [
|
||||
'SERVER_ID' => ag($json, 'Server.Id', ''),
|
||||
'SERVER_NAME' => ag($json, 'Server.Name', ''),
|
||||
'SERVER_VERSION' => afterLast($userAgent, '/'),
|
||||
'USER_ID' => ag($json, 'User.Id', ''),
|
||||
'USER_NAME' => ag($json, 'User.Name', ''),
|
||||
];
|
||||
|
||||
foreach ($attributes as $key => $val) {
|
||||
$request = $request->withAttribute($key, $val);
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function parseWebhook(ServerRequestInterface $request): StateInterface
|
||||
{
|
||||
$payload = ag($request->getParsedBody(), 'data', null);
|
||||
|
||||
@@ -112,6 +112,35 @@ class JellyfinServer implements ServerInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function processRequest(ServerRequestInterface $request): ServerRequestInterface
|
||||
{
|
||||
$userAgent = ag($request->getServerParams(), 'HTTP_USER_AGENT', '');
|
||||
|
||||
if (!str_starts_with($userAgent, 'Jellyfin-Server/')) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
$body = clone $request->getBody();
|
||||
|
||||
if (null === ($json = json_decode((string)$body, true))) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
$attributes = [
|
||||
'SERVER_ID' => ag($json, 'ServerId', ''),
|
||||
'SERVER_NAME' => ag($json, 'ServerName', ''),
|
||||
'SERVER_VERSION' => afterLast($userAgent, '/'),
|
||||
'USER_ID' => ag($json, 'UserId', ''),
|
||||
'USER_NAME' => ag($json, 'NotificationUsername', ''),
|
||||
];
|
||||
|
||||
foreach ($attributes as $key => $val) {
|
||||
$request = $request->withAttribute($key, $val);
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function parseWebhook(ServerRequestInterface $request): StateInterface
|
||||
{
|
||||
if (null === ($json = json_decode($request->getBody()->getContents(), true))) {
|
||||
|
||||
@@ -110,6 +110,35 @@ class PlexServer implements ServerInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function processRequest(ServerRequestInterface $request): ServerRequestInterface
|
||||
{
|
||||
$userAgent = ag($request->getServerParams(), 'HTTP_USER_AGENT', '');
|
||||
|
||||
if (!str_starts_with($userAgent, 'PlexMediaServer/')) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
$payload = ag($request->getParsedBody() ?? [], 'payload', null);
|
||||
|
||||
if (null === $payload) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
$attributes = [
|
||||
'SERVER_ID' => ag($payload, 'Server.uuid', ''),
|
||||
'SERVER_NAME' => ag($payload, 'Server.title', ''),
|
||||
'SERVER_VERSION' => afterLast($userAgent, '/'),
|
||||
'USER_ID' => ag($payload, 'Account.id', ''),
|
||||
'USER_NAME' => ag($payload, 'Account.title', ''),
|
||||
];
|
||||
|
||||
foreach ($attributes as $key => $val) {
|
||||
$request = $request->withAttribute($key, $val);
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function parseWebhook(ServerRequestInterface $request): StateInterface
|
||||
{
|
||||
$payload = ag($request->getParsedBody() ?? [], 'payload', null);
|
||||
|
||||
@@ -19,7 +19,7 @@ interface ServerInterface
|
||||
public const OPT_EXPORT_IGNORE_DATE = 'exportIgnoreDate';
|
||||
|
||||
/**
|
||||
* Initiate Server. It should return **NEW OBJECT**
|
||||
* Initiate server. It should return **NEW OBJECT**
|
||||
*
|
||||
* @param string $name Server name
|
||||
* @param UriInterface $url Server url
|
||||
@@ -40,7 +40,7 @@ interface ServerInterface
|
||||
): self;
|
||||
|
||||
/**
|
||||
* Inject Logger.
|
||||
* Inject logger.
|
||||
*
|
||||
* @param LoggerInterface $logger
|
||||
*
|
||||
@@ -49,7 +49,15 @@ interface ServerInterface
|
||||
public function setLogger(LoggerInterface $logger): ServerInterface;
|
||||
|
||||
/**
|
||||
* Parse Server Specific Webhook event. for play/unplayed event.
|
||||
* Process The request For attributes extraction.
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ServerRequestInterface
|
||||
*/
|
||||
public static function processRequest(ServerRequestInterface $request): ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* Parse server specific webhook event. for play/unplayed event.
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @return StateInterface
|
||||
@@ -57,7 +65,7 @@ interface ServerInterface
|
||||
public function parseWebhook(ServerRequestInterface $request): StateInterface;
|
||||
|
||||
/**
|
||||
* Import Watch state.
|
||||
* Import watch state.
|
||||
*
|
||||
* @param ImportInterface $mapper
|
||||
* @param DateTimeInterface|null $after
|
||||
@@ -67,7 +75,7 @@ interface ServerInterface
|
||||
public function pull(ImportInterface $mapper, DateTimeInterface|null $after = null): array;
|
||||
|
||||
/**
|
||||
* Export Watch State to Server.
|
||||
* Export watch state to server.
|
||||
*
|
||||
* @param ExportInterface $mapper
|
||||
* @param DateTimeInterface|null $after
|
||||
@@ -77,7 +85,7 @@ interface ServerInterface
|
||||
public function export(ExportInterface $mapper, DateTimeInterface|null $after = null): array;
|
||||
|
||||
/**
|
||||
* Export Queued States.
|
||||
* Push webhook queued states.
|
||||
*
|
||||
* @param array<StateInterface> $entities
|
||||
* @param DateTimeInterface|null $after
|
||||
|
||||
@@ -278,9 +278,23 @@ if (!function_exists('httpClientChunks')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('preServeHttpRequest')) {
|
||||
function preServeHttpRequest(ServerRequestInterface $request): ServerRequestInterface
|
||||
{
|
||||
foreach (Config::get('supported', []) as $server) {
|
||||
assert($server instanceof ServerInterface);
|
||||
$request = $server::processRequest($request);
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('serveHttpRequest')) {
|
||||
function serveHttpRequest(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$request = preServeHttpRequest($request);
|
||||
|
||||
$logger = Container::get(LoggerInterface::class);
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user