Skip to content

Commit

Permalink
feat: Serialize event data and author userid to webhook
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed May 23, 2024
1 parent 3b790df commit 45d3bd6
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions lib/private/EventDispatcher/WebhookCaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@

use OCP\EventDispatcher\Event;
use OCP\Http\Client\IClientService;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

class WebhookCaller {
public function __construct(
private IClientService $clientService,
private IUserSession $userSession,
private LoggerInterface $logger,
) {
}

Expand All @@ -42,12 +46,47 @@ public function callWebhook(
array $options,
): void {
$client = $this->clientService->newClient();
$client->request($method, $uri, $options + ['query' => ['event' => $event::class]]);
if (!isset($options['body'])) {
$options['body'] = json_encode([
'event' => $this->serializeEvent($event),
'userid' => $this->userSession->getUser()?->getUID() ?? null,
]);
}
try {
$response = $client->request($method, $uri, $options + ['query' => ['event' => $event::class]]);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 300) {
$this->logger->warning('Webhook returned unexpected status code '.$statusCode, ['body' => $response->getBody()]);
} else {
$this->logger->debug('Webhook returned status code '.$statusCode, ['body' => $response->getBody()]);
}
} catch (\Exception $e) {
$this->logger->error('Webhook call failed: '.$e->getMessage(), ['exception' => $e]);
}
}

/**
* TODO:
* Serialization of the event
* Timeout or async
*/
private function serializeEvent(Event $event): array|\JsonSerializable {
if ($event instanceof \JsonSerializable) {
return $event;
} else {
/* Event is not serializable, we fallback to reflection to still send something */
$data = [];
$ref = new \ReflectionClass($event);
foreach ($ref->getMethods() as $method) {
if (str_starts_with($method->getName(), 'get')) {
$key = strtolower(substr($method->getName(), 3));
$value = $method->invoke($event);
if ($value instanceof \OCP\Files\FileInfo) {
$value = [
'id' => $value->getId(),
'path' => $value->getPath(),
];
}
$data[$key] = $value;
}
}
$this->logger->debug('Webhook had to use fallback to serialize event '.$event::class);
return $data;
}
}
}

0 comments on commit 45d3bd6

Please sign in to comment.