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