Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHRAS-4039 Webhook - enrich the webhook answer #4485

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading