|
4 | 4 |
|
5 | 5 | use BookStack\Auth\User; |
6 | 6 | use BookStack\Entities\Models\Entity; |
| 7 | +use BookStack\Facades\Theme; |
7 | 8 | use BookStack\Interfaces\Loggable; |
8 | 9 | use BookStack\Model; |
| 10 | +use BookStack\Theming\ThemeEvents; |
9 | 11 | use Illuminate\Bus\Queueable; |
10 | 12 | use Illuminate\Contracts\Queue\ShouldQueue; |
11 | 13 | use Illuminate\Foundation\Bus\Dispatchable; |
@@ -68,14 +70,32 @@ public function __construct(Webhook $webhook, string $event, $detail) |
68 | 70 | */ |
69 | 71 | public function handle() |
70 | 72 | { |
71 | | - $response = Http::asJson() |
72 | | - ->withOptions(['allow_redirects' => ['strict' => true]]) |
73 | | - ->timeout(3) |
74 | | - ->post($this->webhook->endpoint, $this->buildWebhookData()); |
| 73 | + $themeResponse = Theme::dispatch(ThemeEvents::WEBHOOK_CALL_BEFORE, $this->event, $this->webhook, $this->detail); |
| 74 | + $webhookData = $themeResponse ?? $this->buildWebhookData(); |
| 75 | + $lastError = null; |
| 76 | + |
| 77 | + try { |
| 78 | + $response = Http::asJson() |
| 79 | + ->withOptions(['allow_redirects' => ['strict' => true]]) |
| 80 | + ->timeout($this->webhook->timeout) |
| 81 | + ->post($this->webhook->endpoint, $webhookData); |
| 82 | + } catch (\Exception $exception) { |
| 83 | + $lastError = $exception->getMessage(); |
| 84 | + Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with error \"{$lastError}\""); |
| 85 | + } |
75 | 86 |
|
76 | | - if ($response->failed()) { |
| 87 | + if (isset($response) && $response->failed()) { |
| 88 | + $lastError = "Response status from endpoint was {$response->status()}"; |
77 | 89 | Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with status {$response->status()}"); |
78 | 90 | } |
| 91 | + |
| 92 | + $this->webhook->last_called_at = now(); |
| 93 | + if ($lastError) { |
| 94 | + $this->webhook->last_errored_at = now(); |
| 95 | + $this->webhook->last_error = $lastError; |
| 96 | + } |
| 97 | + |
| 98 | + $this->webhook->save(); |
79 | 99 | } |
80 | 100 |
|
81 | 101 | protected function buildWebhookData(): array |
|
0 commit comments