]> BookStack Code Mirror - bookstack/blob - app/Activity/DispatchWebhookJob.php
405bca49cbee925b574ff7ed7c574d702df9e62c
[bookstack] / app / Activity / DispatchWebhookJob.php
1 <?php
2
3 namespace BookStack\Activity;
4
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\Theming\ThemeEvents;
10 use BookStack\Users\Models\User;
11 use BookStack\Util\SsrUrlValidator;
12 use Illuminate\Bus\Queueable;
13 use Illuminate\Contracts\Queue\ShouldQueue;
14 use Illuminate\Foundation\Bus\Dispatchable;
15 use Illuminate\Queue\InteractsWithQueue;
16 use Illuminate\Queue\SerializesModels;
17 use Illuminate\Support\Facades\Http;
18 use Illuminate\Support\Facades\Log;
19
20 class DispatchWebhookJob implements ShouldQueue
21 {
22     use Dispatchable;
23     use InteractsWithQueue;
24     use Queueable;
25     use SerializesModels;
26
27     protected Webhook $webhook;
28     protected User $initiator;
29     protected int $initiatedTime;
30     protected array $webhookData;
31
32     /**
33      * Create a new job instance.
34      *
35      * @return void
36      */
37     public function __construct(Webhook $webhook, string $event, Loggable|string $detail)
38     {
39         $this->webhook = $webhook;
40         $this->initiator = user();
41         $this->initiatedTime = time();
42
43         $themeResponse = Theme::dispatch(ThemeEvents::WEBHOOK_CALL_BEFORE, $event, $this->webhook, $detail, $this->initiator, $this->initiatedTime);
44         $this->webhookData =  $themeResponse ?? WebhookFormatter::getDefault($event, $this->webhook, $detail, $this->initiator, $this->initiatedTime)->format();
45     }
46
47     /**
48      * Execute the job.
49      *
50      * @return void
51      */
52     public function handle()
53     {
54         $lastError = null;
55
56         try {
57             (new SsrUrlValidator())->ensureAllowed($this->webhook->endpoint);
58
59             $response = Http::asJson()
60                 ->withOptions(['allow_redirects' => ['strict' => true]])
61                 ->timeout($this->webhook->timeout)
62                 ->post($this->webhook->endpoint, $this->webhookData);
63         } catch (\Exception $exception) {
64             $lastError = $exception->getMessage();
65             Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with error \"{$lastError}\"");
66         }
67
68         if (isset($response) && $response->failed()) {
69             $lastError = "Response status from endpoint was {$response->status()}";
70             Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with status {$response->status()}");
71         }
72
73         $this->webhook->last_called_at = now();
74         if ($lastError) {
75             $this->webhook->last_errored_at = now();
76             $this->webhook->last_error = $lastError;
77         }
78
79         $this->webhook->save();
80     }
81 }