Skip to content

Commit

Permalink
feat(WebhooksController): Allow querying listeners by URI
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr committed Jun 26, 2024
1 parent eed6216 commit 0bdecb9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
9 changes: 7 additions & 2 deletions apps/webhook_listeners/lib/Controller/WebhooksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,21 @@ public function __construct(
/**
* 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
*
* 200: Webhook registrations returned
*/
#[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(
Expand Down
13 changes: 13 additions & 0 deletions apps/webhook_listeners/lib/Db/WebhookListenerMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 9 additions & 0 deletions apps/webhook_listeners/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions apps/webhook_listeners/tests/Db/WebhookListenerMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0bdecb9

Please sign in to comment.