aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorCôme Chilliet <come.chilliet@nextcloud.com>2024-05-23 15:41:55 +0200
committerCôme Chilliet <91878298+come-nc@users.noreply.github.com>2024-06-11 14:10:29 +0200
commitd835d23e3a73bb09f46096a93269bb34b51fbb90 (patch)
tree328af5f48e967392c4c7b07cfa2b6c745028b7c4 /lib
parent8d18607f8029606381489dccd6c66efdffb842f0 (diff)
downloadnextcloud-server-d835d23e3a73bb09f46096a93269bb34b51fbb90.tar.gz
nextcloud-server-d835d23e3a73bb09f46096a93269bb34b51fbb90.zip
feat: Serialize event data and author userid to webhook
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/EventDispatcher/WebhookCaller.php51
1 files changed, 45 insertions, 6 deletions
diff --git a/lib/private/EventDispatcher/WebhookCaller.php b/lib/private/EventDispatcher/WebhookCaller.php
index 2f0d1a2ab06..0265ef317b4 100644
--- a/lib/private/EventDispatcher/WebhookCaller.php
+++ b/lib/private/EventDispatcher/WebhookCaller.php
@@ -28,10 +28,14 @@ namespace OC\EventDispatcher;
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,
) {
}
@@ -42,12 +46,47 @@ class WebhookCaller {
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;
+ }
}
}