]> BookStack Code Mirror - bookstack/blob - app/Actions/DispatchWebhookJob.php
Aligned notification capitalisation
[bookstack] / app / Actions / DispatchWebhookJob.php
1 <?php
2
3 namespace BookStack\Actions;
4
5 use BookStack\Auth\User;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Interfaces\Loggable;
8 use BookStack\Model;
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\Carbon;
15 use Illuminate\Support\Facades\Http;
16 use Illuminate\Support\Facades\Log;
17
18 class DispatchWebhookJob implements ShouldQueue
19 {
20     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
21
22     /**
23      * @var Webhook
24      */
25     protected $webhook;
26
27     /**
28      * @var string
29      */
30     protected $event;
31
32     /**
33      * @var string|Loggable
34      */
35     protected $detail;
36
37     /**
38      * @var User
39      */
40     protected $initiator;
41
42     /**
43      * @var int
44      */
45     protected $initiatedTime;
46
47     /**
48      * Create a new job instance.
49      *
50      * @return void
51      */
52     public function __construct(Webhook $webhook, string $event, $detail)
53     {
54         $this->webhook = $webhook;
55         $this->event = $event;
56         $this->detail = $detail;
57         $this->initiator = user();
58         $this->initiatedTime = time();
59     }
60
61     /**
62      * Execute the job.
63      *
64      * @return void
65      */
66     public function handle()
67     {
68         $response = Http::asJson()
69             ->withOptions(['allow_redirects' => ['strict' => true]])
70             ->timeout(3)
71             ->post($this->webhook->endpoint, $this->buildWebhookData());
72
73         if ($response->failed()) {
74             Log::error("Webhook call to endpoint {$this->webhook->endpoint} failed with status {$response->status()}");
75         }
76     }
77
78     protected function buildWebhookData(): array
79     {
80         $textParts = [
81             $this->initiator->name,
82             trans('activities.' . $this->event),
83         ];
84
85         if ($this->detail instanceof Entity) {
86             $textParts[] = '"' . $this->detail->name . '"';
87         }
88
89         $data =  [
90             'event' => $this->event,
91             'text' => implode(' ', $textParts),
92             'triggered_at' => Carbon::createFromTimestampUTC($this->initiatedTime)->toISOString(),
93             'triggered_by' => $this->initiator->attributesToArray(),
94             'triggered_by_profile_url' => $this->initiator->getProfileUrl(),
95             'webhook_id' => $this->webhook->id,
96             'webhook_name' => $this->webhook->name,
97         ];
98
99         if (method_exists($this->detail, 'getUrl')) {
100             $data['url'] = $this->detail->getUrl();
101         }
102
103         if ($this->detail instanceof Model) {
104             $data['related_item'] = $this->detail->attributesToArray();
105         }
106
107         return $data;
108     }
109 }