3 namespace BookStack\Activity;
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\Activity\Models\Webhook;
7 use BookStack\Activity\Tools\WebhookFormatter;
8 use BookStack\Facades\Theme;
9 use BookStack\Http\HttpRequestService;
10 use BookStack\Theming\ThemeEvents;
11 use BookStack\Users\Models\User;
12 use BookStack\Util\SsrUrlValidator;
13 use Illuminate\Bus\Queueable;
14 use Illuminate\Contracts\Queue\ShouldQueue;
15 use Illuminate\Foundation\Bus\Dispatchable;
16 use Illuminate\Queue\InteractsWithQueue;
17 use Illuminate\Queue\SerializesModels;
18 use Illuminate\Support\Facades\Log;
19 use Psr\Http\Client\ClientExceptionInterface;
21 class DispatchWebhookJob implements ShouldQueue
24 use InteractsWithQueue;
28 protected Webhook $webhook;
29 protected User $initiator;
30 protected int $initiatedTime;
31 protected array $webhookData;
34 * Create a new job instance.
38 public function __construct(Webhook $webhook, string $event, Loggable|string $detail)
40 $this->webhook = $webhook;
41 $this->initiator = user();
42 $this->initiatedTime = time();
44 $themeResponse = Theme::dispatch(ThemeEvents::WEBHOOK_CALL_BEFORE, $event, $this->webhook, $detail, $this->initiator, $this->initiatedTime);
45 $this->webhookData = $themeResponse ?? WebhookFormatter::getDefault($event, $this->webhook, $detail, $this->initiator, $this->initiatedTime)->format();
53 public function handle(HttpRequestService $http)
58 (new SsrUrlValidator())->ensureAllowed($this->webhook->endpoint);
60 $client = $http->buildClient($this->webhook->timeout, [
61 'connect_timeout' => 10,
62 'allow_redirects' => ['strict' => true],
65 $response = $client->sendRequest($http->jsonRequest('POST', $this->webhook->endpoint, $this->webhookData));
66 $statusCode = $response->getStatusCode();
68 if ($statusCode >= 400) {
69 $lastError = "Response status from endpoint was {$statusCode}";
70 Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with status {$statusCode}");
72 } catch (ClientExceptionInterface $error) {
73 $lastError = $error->getMessage();
74 Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with error \"{$lastError}\"");
77 $this->webhook->last_called_at = now();
79 $this->webhook->last_errored_at = now();
80 $this->webhook->last_error = $lastError;
83 $this->webhook->save();