Signed-off-by: Marcel Klehr <mklehr@gmx.net>pull/46107/head
@@ -48,6 +48,7 @@ class WebhooksController extends OCSController { | |||
/** | |||
* List registered webhooks | |||
* | |||
* @param string|null $uri The callback URI to filter by | |||
* @return DataResponse<Http::STATUS_OK, WebhookListenersWebhookInfo[], array{}> | |||
* @throws OCSException Other internal error | |||
* | |||
@@ -55,9 +56,13 @@ class WebhooksController extends OCSController { | |||
*/ | |||
#[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( |
@@ -204,4 +204,17 @@ class WebhookListenerMapper extends QBMapper { | |||
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); | |||
} | |||
} |
@@ -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", |
@@ -82,6 +82,26 @@ class WebhookListenerMapperTest extends TestCase { | |||
$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, |