diff --git a/apps/webhook_listeners/lib/Controller/WebhooksController.php b/apps/webhook_listeners/lib/Controller/WebhooksController.php index 1c4306eabb8a2..5b097c84a3c7d 100644 --- a/apps/webhook_listeners/lib/Controller/WebhooksController.php +++ b/apps/webhook_listeners/lib/Controller/WebhooksController.php @@ -48,6 +48,7 @@ public function __construct( /** * List registered webhooks * + * @param string|null $uri The callback URI to filter by * @return DataResponse * @throws OCSException Other internal error * @@ -55,9 +56,13 @@ public function __construct( */ #[ApiRoute(verb: 'GET', url: '/api/v1/webhooks')] #[AuthorizedAdminSetting(settings:Admin::class)] - public function index(): DataResponse { + public function index(?string $uri = null): DataResponse { try { - $webhookListeners = $this->mapper->getAll(); + if ($uri !== null) { + $webhookListeners = $this->mapper->getByUri($uri); + } else { + $webhookListeners = $this->mapper->getAll(); + } return new DataResponse( array_map( diff --git a/apps/webhook_listeners/lib/Db/WebhookListenerMapper.php b/apps/webhook_listeners/lib/Db/WebhookListenerMapper.php index 97e01062f2f86..a1fe4ceee8d32 100644 --- a/apps/webhook_listeners/lib/Db/WebhookListenerMapper.php +++ b/apps/webhook_listeners/lib/Db/WebhookListenerMapper.php @@ -204,4 +204,17 @@ public function getByEvent(string $event): array { return $this->findEntities($qb); } + + /** + * @throws Exception + */ + public function getByUri(string $uri): array { + $qb = $this->db->getQueryBuilder(); + + $qb->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR))); + + return $this->findEntities($qb); + } } diff --git a/apps/webhook_listeners/openapi.json b/apps/webhook_listeners/openapi.json index 8488ce8e11758..3d1e9cf7a307a 100644 --- a/apps/webhook_listeners/openapi.json +++ b/apps/webhook_listeners/openapi.json @@ -112,6 +112,15 @@ } ], "parameters": [ + { + "name": "uri", + "in": "query", + "description": "The callback URI to filter by", + "schema": { + "type": "string", + "nullable": true + } + }, { "name": "OCS-APIRequest", "in": "header", diff --git a/apps/webhook_listeners/tests/Db/WebhookListenerMapperTest.php b/apps/webhook_listeners/tests/Db/WebhookListenerMapperTest.php index b385cff1228f4..fc42a0a959775 100644 --- a/apps/webhook_listeners/tests/Db/WebhookListenerMapperTest.php +++ b/apps/webhook_listeners/tests/Db/WebhookListenerMapperTest.php @@ -82,6 +82,26 @@ public function testInsertListenerAndGetIt() { $this->assertEquals($listener1, $listener2); } + public function testInsertListenerAndGetItByUri() { + $uri = 'https://webhook.example.com/endpoint'; + $listener1 = $this->mapper->addWebhookListener( + null, + 'bob', + 'POST', + $uri, + NodeWrittenEvent::class, + null, + null, + AuthMethod::None, + null, + ); + + $listeners = $this->mapper->getByUri($uri); + + $listener1->resetUpdatedFields(); + $this->assertContains($listener1->getId(), array_map(fn ($listener) => $listener->getId(), $listeners)); + } + public function testInsertListenerAndGetItWithAuthData() { $listener1 = $this->mapper->addWebhookListener( null,