3 namespace BookStack\Actions;
5 use BookStack\Auth\User;
6 use BookStack\Facades\Theme;
7 use BookStack\Interfaces\Loggable;
8 use BookStack\Theming\ThemeEvents;
9 use Illuminate\Bus\Queueable;
10 use Illuminate\Contracts\Queue\ShouldQueue;
11 use Illuminate\Foundation\Bus\Dispatchable;
12 use Illuminate\Queue\InteractsWithQueue;
13 use Illuminate\Queue\SerializesModels;
14 use Illuminate\Support\Facades\Http;
15 use Illuminate\Support\Facades\Log;
17 class DispatchWebhookJob implements ShouldQueue
20 use InteractsWithQueue;
24 protected Webhook $webhook;
25 protected string $event;
26 protected User $initiator;
27 protected int $initiatedTime;
30 * @var string|Loggable
35 * Create a new job instance.
39 public function __construct(Webhook $webhook, string $event, $detail)
41 $this->webhook = $webhook;
42 $this->event = $event;
43 $this->detail = $detail;
44 $this->initiator = user();
45 $this->initiatedTime = time();
53 public function handle()
55 $themeResponse = Theme::dispatch(ThemeEvents::WEBHOOK_CALL_BEFORE, $this->event, $this->webhook, $this->detail, $this->initiator, $this->initiatedTime);
56 $webhookData = $themeResponse ?? WebhookFormatter::getDefault($this->event, $this->webhook, $this->detail, $this->initiator, $this->initiatedTime)->format();
60 $response = Http::asJson()
61 ->withOptions(['allow_redirects' => ['strict' => true]])
62 ->timeout($this->webhook->timeout)
63 ->post($this->webhook->endpoint, $webhookData);
64 } catch (\Exception $exception) {
65 $lastError = $exception->getMessage();
66 Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with error \"{$lastError}\"");
69 if (isset($response) && $response->failed()) {
70 $lastError = "Response status from endpoint was {$response->status()}";
71 Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with status {$response->status()}");
74 $this->webhook->last_called_at = now();
76 $this->webhook->last_errored_at = now();
77 $this->webhook->last_error = $lastError;
80 $this->webhook->save();