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