Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move webhook endpoints to the REST API #510

Merged
merged 6 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,87 @@
}
]
}
},
"/plex/{uuid}": {
"post": {
"tags": [
"Webhooks"
],
"description": "Endpoint to scrobble your Plex watches to Movary.",
"parameters": [
{
"name": "UUID",
"in": "query",
"description": "An UUID that is generated by the user in the Plex settings.",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK"
},
"404": {
"$ref": "#/components/responses/404"
}
}
}
},
"/jellyfin/{uuid}": {
"post": {
"tags": [
"Webhooks"
],
"description": "Endpoint to scrobble your Jellyfin watches to Movary.",
"parameters": [
{
"name": "UUID",
"in": "query",
"description": "An UUID that is generated by the user in the Jellyfin settings.",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK"
},
"404": {
"$ref": "#/components/responses/404"
}
}
}
},
"/emby/{uuid}": {
"post": {
"tags": [
"Webhooks"
],
"description": "Endpoint to scrobble your Emby watches to Movary.",
"parameters": [
{
"name": "UUID",
"in": "query",
"description": "An UUID that is generated by the user in the Emby settings.",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK"
},
"404": {
"$ref": "#/components/responses/404"
}
}
}
}
},
"components": {
Expand Down
8 changes: 6 additions & 2 deletions settings/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ function addWebRoutes(RouterService $routerService, FastRoute\RouteCollector $ro
$routes->add('GET', '/docs/api', [Web\OpenApiController::class, 'renderPage']);

#####################
# Webhook listeners #
# Webhook listeners # !!! Deprecated use new api routes
#####################
$routes->add('POST', '/plex/{id:.+}', [Web\PlexController::class, 'handlePlexWebhook']);
$routes->add('POST', '/jellyfin/{id:.+}', [Web\JellyfinController::class, 'handleJellyfinWebhook']);
$routes->add('POST', '/emby/{id:.+}', [Web\EmbyController::class, 'handleEmbyWebhook']);
leepeuker marked this conversation as resolved.
Show resolved Hide resolved

#############
############
# Job Queue #
#############
$routes->add('GET', '/jobs', [Web\JobController::class, 'getJobs'], [Web\Middleware\UserIsAuthenticated::class]);
Expand Down Expand Up @@ -210,5 +210,9 @@ function addApiRoutes(RouterService $routerService, FastRoute\RouteCollector $ro

$routes->add('GET', '/movies/search', [Api\MovieSearchController::class, 'search'], [Api\Middleware\IsAuthenticated::class]);

$routes->add('POST', '/webhook/plex/{id:.+}', [Api\PlexController::class, 'handlePlexWebhook']);
$routes->add('POST', '/webhook/jellyfin/{id:.+}', [Api\JellyfinController::class, 'handleJellyfinWebhook']);
$routes->add('POST', '/webhook/emby/{id:.+}', [Api\EmbyController::class, 'handleEmbyWebhook']);

$routerService->addRoutesToRouteCollector($routeCollector, $routes);
}
38 changes: 38 additions & 0 deletions src/HttpController/Api/EmbyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Movary\HttpController\Api;

use Movary\Domain\User\UserApi;
use Movary\Service\Emby\EmbyScrobbler;
use Movary\Util\Json;
use Movary\ValueObject\Http\Request;
use Movary\ValueObject\Http\Response;
use Psr\Log\LoggerInterface;

class EmbyController
{
public function __construct(
private readonly UserApi $userApi,
private readonly EmbyScrobbler $embyScrobbler,
private readonly LoggerInterface $logger,
) {
}

public function handleEmbyWebhook(Request $request) : Response
{
$webhookId = $request->getRouteParameters()['id'];

$userId = $this->userApi->findUserIdByEmbyWebhookId($webhookId);
if ($userId === null) {
return Response::createNotFound();
}

$requestPayload = $request->getPostParameters()['data'];

$this->logger->debug('Emby: Webhook triggered with payload: ' . $requestPayload);

$this->embyScrobbler->processEmbyWebhook($userId, Json::decode($requestPayload));

return Response::createOk();
}
}
37 changes: 37 additions & 0 deletions src/HttpController/Api/JellyfinController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Movary\HttpController\Api;

use Movary\Domain\User\UserApi;
use Movary\Service\Jellyfin\JellyfinScrobbler;
use Movary\Util\Json;
use Movary\ValueObject\Http\Request;
use Movary\ValueObject\Http\Response;
use Psr\Log\LoggerInterface;

class JellyfinController
{
public function __construct(
private readonly UserApi $userApi,
private readonly JellyfinScrobbler $jellyfinScrobbler,
private readonly LoggerInterface $logger,
) {
}
public function handleJellyfinWebhook(Request $request) : Response
{
$webhookId = $request->getRouteParameters()['id'];

$userId = $this->userApi->findUserIdByJellyfinWebhookId($webhookId);
if ($userId === null) {
return Response::createNotFound();
}

$requestPayload = $request->getBody();

$this->logger->debug('Jellyfin: Webhook triggered with payload: ' . $requestPayload);

$this->jellyfinScrobbler->processJellyfinWebhook($userId, Json::decode($requestPayload));

return Response::createOk();
}
}
41 changes: 41 additions & 0 deletions src/HttpController/Api/PlexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace Movary\HttpController\Api;

use Movary\Domain\User\UserApi;
use Movary\Service\Plex\PlexScrobbler;
use Movary\Util\Json;
use Movary\ValueObject\Http\Request;
use Movary\ValueObject\Http\Response;
use Psr\Log\LoggerInterface;

class PlexController
{
public function __construct(
private readonly UserApi $userApi,
private readonly PlexScrobbler $plexScrobbler,
private readonly LoggerInterface $logger,
) {
}

public function handlePlexWebhook(Request $request) : Response
{
$webhookId = $request->getRouteParameters()['id'];

$userId = $this->userApi->findUserIdByPlexWebhookId($webhookId);
if ($userId === null) {
return Response::createNotFound();
}

$requestPayload = $request->getPostParameters()['payload'] ?? null;
if ($requestPayload === null) {
return Response::createOk();
}

$this->logger->debug('Plex: Webhook triggered with payload: ' . $requestPayload);

$this->plexScrobbler->processPlexWebhook($userId, Json::decode((string)$requestPayload));

return Response::createOk();
}
}
5 changes: 5 additions & 0 deletions src/HttpController/Web/EmbyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function deleteEmbyWebhookUrl() : Response
return Response::createOk();
}

/**
* @deprecated
* @see \Movary\HttpController\Api\EmbyController::handleEmbyWebhook()
*/
public function handleEmbyWebhook(Request $request) : Response
{
$webhookId = $request->getRouteParameters()['id'];
Expand All @@ -41,6 +45,7 @@ public function handleEmbyWebhook(Request $request) : Response
$requestPayload = $request->getPostParameters()['data'];

$this->logger->debug('Emby: Webhook triggered with payload: ' . $requestPayload);
$this->logger->warning('This emby webhook url is deprecated and will stop to work soon, regenerate the url');

$this->embyScrobbler->processEmbyWebhook($userId, Json::decode($requestPayload));

Expand Down
6 changes: 6 additions & 0 deletions src/HttpController/Web/JellyfinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public function deleteJellyfinWebhookUrl() : Response
return Response::createOk();
}


/**
* @deprecated
* @see \Movary\HttpController\Api\JellyfinController::handleJellyfinWebhook()
*/
public function handleJellyfinWebhook(Request $request) : Response
{
$webhookId = $request->getRouteParameters()['id'];
Expand All @@ -78,6 +83,7 @@ public function handleJellyfinWebhook(Request $request) : Response
$requestPayload = $request->getBody();

$this->logger->debug('Jellyfin: Webhook triggered with payload: ' . $requestPayload);
$this->logger->warning('This jellyfin webhook url is deprecated and will stop to work soon, regenerate the url');

$this->jellyfinScrobbler->processJellyfinWebhook($userId, Json::decode($requestPayload));

Expand Down
5 changes: 5 additions & 0 deletions src/HttpController/Web/PlexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function generatePlexAuthenticationUrl() : Response
return Response::createJson(Json::encode(['authenticationUrl' => $plexAuthenticationUrl]));
}

/**
* @deprecated
* @see \Movary\HttpController\Api\PlexController::handlePlexWebhook()
*/
public function handlePlexWebhook(Request $request) : Response
{
$webhookId = $request->getRouteParameters()['id'];
Expand All @@ -68,6 +72,7 @@ public function handlePlexWebhook(Request $request) : Response
}

$this->logger->debug('Plex: Webhook triggered with payload: ' . $requestPayload);
$this->logger->warning('This plex webhook url is deprecated and will stop to work soon, regenerate the url');

$this->plexScrobbler->processPlexWebhook($userId, Json::decode((string)$requestPayload));

Expand Down
2 changes: 1 addition & 1 deletion src/Service/WebhookUrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ private function buildUrl(string $webhookType, string $webhookId) : ?string
return null;
}

return rtrim($applicationUrl, '/') . '/' . $webhookType . '/' . $webhookId;
return rtrim($applicationUrl, '/') . '/api/webhook/' . $webhookType . '/' . $webhookId;
}
}
2 changes: 1 addition & 1 deletion tests/rest/web/plex-scrobble.http
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
POST http://127.0.0.1/plex/16914287-329d-4f3b-ba57-4a092f13058d
POST http://127.0.0.1/api/webhook/plex/14f14d6b-695f-41b9-a856-5ba401bcc040
Accept: */*
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
Expand Down