Skip to content

Commit

Permalink
webhook record.created add some data (#4485)
Browse files Browse the repository at this point in the history
  • Loading branch information
aynsix authored Mar 15, 2024
1 parent 953f031 commit 747c8a0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
9 changes: 8 additions & 1 deletion lib/Alchemy/Phrasea/Core/Event/Record/RecordEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -29,4 +31,9 @@ public function getRecord()
{
return $this->record;
}

public function getInitiatorId()
{
return $this->initiatorId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 18 additions & 3 deletions lib/classes/record/adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 747c8a0

Please sign in to comment.