From 330996e2d37cb7aa30049135af3116b56103fb74 Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 14 Mar 2024 13:57:18 +0300 Subject: [PATCH] webhook record.created add some data --- .../Phrasea/Core/Event/Record/RecordEvent.php | 9 ++++- .../WebhookRecordEventSubscriber.php | 38 ++++++++++++++++++- lib/classes/record/adapter.php | 21 ++++++++-- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/lib/Alchemy/Phrasea/Core/Event/Record/RecordEvent.php b/lib/Alchemy/Phrasea/Core/Event/Record/RecordEvent.php index 81b25fd336..2c08f7d785 100644 --- a/lib/Alchemy/Phrasea/Core/Event/Record/RecordEvent.php +++ b/lib/Alchemy/Phrasea/Core/Event/Record/RecordEvent.php @@ -16,10 +16,12 @@ abstract class RecordEvent extends Event { private $record; + private $initiatorId; - public function __construct(RecordInterface $record) + public function __construct(RecordInterface $record, $initiatorId = null) { $this->record = $record; + $this->initiatorId = $initiatorId; } /** @@ -29,4 +31,9 @@ public function getRecord() { return $this->record; } + + public function getInitiatorId() + { + return $this->initiatorId; + } } diff --git a/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookRecordEventSubscriber.php b/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookRecordEventSubscriber.php index e2a695e0ab..c6ac875371 100644 --- a/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookRecordEventSubscriber.php +++ b/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookRecordEventSubscriber.php @@ -38,7 +38,43 @@ public function __construct(Application $application) public function onRecordCreated(RecordEvent $event) { - $this->createWebhookEvent($event, WebhookEvent::RECORD_CREATED); + $record = $this->convertToRecordAdapter($event->getRecord()); + + if ($record !== null) { + $permalinkUrl = ''; + + try { + $permalink = $record->get_subdef('document')->get_permalink(); + if ($permalink != null) { + $permalinkUrl = $permalink->get_url()->__toString(); + } + } catch (\Exception $e) { + // there is no subdef 'document' + } catch (\Throwable $e) { + } + + $eventData = [ + 'databox_id' => $event->getRecord()->getDataboxId(), + 'record_id' => $event->getRecord()->getRecordId(), + 'collection_name' => $record->getCollection()->get_name(), + 'base_id' => $record->getBaseId(), + 'record_type' => $event->getRecord()->isStory() ? "story" : "record", + 'media_type' => $record->getType(), + 'type' => $record->getMimeType(), + 'original_name' => $record->getOriginalName(), + 'initiator_user_id' => $event->getInitiatorId(), + 'permalink' => $permalinkUrl + ]; + + $this->app['manipulator.webhook-event']->create( + WebhookEvent::RECORD_CREATED, + WebhookEvent::RECORD_TYPE, + $eventData, + [$event->getRecord()->getBaseId()] + ); + } else { + $this->app['logger']->error("Record not found when wanting to create webhook data!"); + } } public function onRecordEdit(RecordEdit $event) diff --git a/lib/classes/record/adapter.php b/lib/classes/record/adapter.php index 0af62920d6..4c659f8192 100644 --- a/lib/classes/record/adapter.php +++ b/lib/classes/record/adapter.php @@ -1789,7 +1789,12 @@ public static function createStory(Application $app, collection $collection) unset($e); } - $story->dispatch(RecordEvents::CREATED, new CreatedEvent($story)); + $initiatorId = null; + if ($app->getAuthenticatedUser() != null) { + $initiatorId = $app->getAuthenticatedUser()->getId(); + } + + $story->dispatch(RecordEvents::CREATED, new CreatedEvent($story, $initiatorId)); return $story; } @@ -1902,7 +1907,12 @@ public static function createFromFile(File $file, Application $app) $record->insertTechnicalDatas($app['mediavorus']); - $record->dispatch(RecordEvents::CREATED, new CreatedEvent($record)); + $initiatorId = null; + if ($app->getAuthenticatedUser() != null) { + $initiatorId = $app->getAuthenticatedUser()->getId(); + } + + $record->dispatch(RecordEvents::CREATED, new CreatedEvent($record, $initiatorId)); } return $record; @@ -1920,8 +1930,13 @@ public static function createFromFile(File $file, Application $app) public static function create(collection $collection, Application $app) { $record = self::_create($collection, $app); + $initiatorId = null; if($record) { - $record->dispatch(RecordEvents::CREATED, new CreatedEvent($record)); + if ($app->getAuthenticatedUser() != null) { + $initiatorId = $app->getAuthenticatedUser()->getId(); + } + + $record->dispatch(RecordEvents::CREATED, new CreatedEvent($record, $initiatorId)); } return $record;