]> BookStack Code Mirror - bookstack/blob - app/Actions/Webhook.php
Added code editor changes mobile design handling
[bookstack] / app / Actions / Webhook.php
1 <?php
2
3 namespace BookStack\Actions;
4
5 use BookStack\Interfaces\Loggable;
6 use Carbon\Carbon;
7 use Illuminate\Database\Eloquent\Collection;
8 use Illuminate\Database\Eloquent\Factories\HasFactory;
9 use Illuminate\Database\Eloquent\Model;
10 use Illuminate\Database\Eloquent\Relations\HasMany;
11
12 /**
13  * @property int        $id
14  * @property string     $name
15  * @property string     $endpoint
16  * @property Collection $trackedEvents
17  * @property bool       $active
18  * @property int        $timeout
19  * @property string     $last_error
20  * @property Carbon     $last_called_at
21  * @property Carbon     $last_errored_at
22  */
23 class Webhook extends Model implements Loggable
24 {
25     protected $fillable = ['name', 'endpoint', 'timeout'];
26
27     use HasFactory;
28
29     protected $casts = [
30         'last_called_at'  => 'datetime',
31         'last_errored_at' => 'datetime',
32     ];
33
34     /**
35      * Define the tracked event relation a webhook.
36      */
37     public function trackedEvents(): HasMany
38     {
39         return $this->hasMany(WebhookTrackedEvent::class);
40     }
41
42     /**
43      * Update the tracked events for a webhook from the given list of event types.
44      */
45     public function updateTrackedEvents(array $events): void
46     {
47         $this->trackedEvents()->delete();
48
49         $eventsToStore = array_intersect($events, array_values(ActivityType::all()));
50         if (in_array('all', $events)) {
51             $eventsToStore = ['all'];
52         }
53
54         $trackedEvents = [];
55         foreach ($eventsToStore as $event) {
56             $trackedEvents[] = new WebhookTrackedEvent(['event' => $event]);
57         }
58
59         $this->trackedEvents()->saveMany($trackedEvents);
60     }
61
62     /**
63      * Check if this webhook tracks the given event.
64      */
65     public function tracksEvent(string $event): bool
66     {
67         return $this->trackedEvents->pluck('event')->contains($event);
68     }
69
70     /**
71      * Get a URL for this webhook within the settings interface.
72      */
73     public function getUrl(string $path = ''): string
74     {
75         return url('/settings/webhooks/' . $this->id . '/' . ltrim($path, '/'));
76     }
77
78     /**
79      * Get the string descriptor for this item.
80      */
81     public function logDescriptor(): string
82     {
83         return "({$this->id}) {$this->name}";
84     }
85 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.