mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-30 23:52:40 +01:00
3625f12abe
Creates a new organsied formatting system for webhook data, with interfaces for extending with custom model formatting rules. Allows easy usage & extension of the default bookstack formatting behaviour when customizing webhook events via theme system, and keeps default data customizations organised. This also makes the following webhook data changes: - owned_by/created_by/updated_by user details are loaded for events with Entity details. (POTENTIALLY BREAKING CHANGE). - current_revision details are loaded for page update/create events. Added testing to cover added model formatting rules. For #3279 and #3218
83 lines
2.5 KiB
PHP
83 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Actions;
|
|
|
|
use BookStack\Auth\User;
|
|
use BookStack\Facades\Theme;
|
|
use BookStack\Interfaces\Loggable;
|
|
use BookStack\Theming\ThemeEvents;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class DispatchWebhookJob implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
protected Webhook $webhook;
|
|
protected string $event;
|
|
protected User $initiator;
|
|
protected int $initiatedTime;
|
|
|
|
/**
|
|
* @var string|Loggable
|
|
*/
|
|
protected $detail;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(Webhook $webhook, string $event, $detail)
|
|
{
|
|
$this->webhook = $webhook;
|
|
$this->event = $event;
|
|
$this->detail = $detail;
|
|
$this->initiator = user();
|
|
$this->initiatedTime = time();
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$themeResponse = Theme::dispatch(ThemeEvents::WEBHOOK_CALL_BEFORE, $this->event, $this->webhook, $this->detail, $this->initiator, $this->initiatedTime);
|
|
$webhookData = $themeResponse ?? WebhookFormatter::getDefault($this->event, $this->webhook, $this->detail, $this->initiator, $this->initiatedTime)->format();
|
|
$lastError = null;
|
|
|
|
try {
|
|
$response = Http::asJson()
|
|
->withOptions(['allow_redirects' => ['strict' => true]])
|
|
->timeout($this->webhook->timeout)
|
|
->post($this->webhook->endpoint, $webhookData);
|
|
} catch (\Exception $exception) {
|
|
$lastError = $exception->getMessage();
|
|
Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with error \"{$lastError}\"");
|
|
}
|
|
|
|
if (isset($response) && $response->failed()) {
|
|
$lastError = "Response status from endpoint was {$response->status()}";
|
|
Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with status {$response->status()}");
|
|
}
|
|
|
|
$this->webhook->last_called_at = now();
|
|
if ($lastError) {
|
|
$this->webhook->last_errored_at = now();
|
|
$this->webhook->last_error = $lastError;
|
|
}
|
|
|
|
$this->webhook->save();
|
|
}
|
|
}
|