3 namespace BookStack\Actions;
5 use BookStack\Interfaces\Loggable;
6 use Illuminate\Database\Eloquent\Collection;
7 use Illuminate\Database\Eloquent\Factories\HasFactory;
8 use Illuminate\Database\Eloquent\Model;
9 use Illuminate\Database\Eloquent\Relations\HasMany;
13 * @property string $name
14 * @property string $endpoint
15 * @property Collection $trackedEvents
16 * @property bool $active
18 class Webhook extends Model implements Loggable
20 protected $fillable = ['name', 'endpoint'];
25 * Define the tracked event relation a webhook.
27 public function trackedEvents(): HasMany
29 return $this->hasMany(WebhookTrackedEvent::class);
33 * Update the tracked events for a webhook from the given list of event types.
35 public function updateTrackedEvents(array $events): void
37 $this->trackedEvents()->delete();
39 $eventsToStore = array_intersect($events, array_values(ActivityType::all()));
40 if (in_array('all', $events)) {
41 $eventsToStore = ['all'];
45 foreach ($eventsToStore as $event) {
46 $trackedEvents[] = new WebhookTrackedEvent(['event' => $event]);
49 $this->trackedEvents()->saveMany($trackedEvents);
53 * Check if this webhook tracks the given event.
55 public function tracksEvent(string $event): bool
57 return $this->trackedEvents->pluck('event')->contains($event);
61 * Get a URL for this webhook within the settings interface.
63 public function getUrl(string $path = ''): string
65 return url('/settings/webhooks/' . $this->id . '/' . ltrim($path, '/'));
69 * Get the string descriptor for this item.
71 public function logDescriptor(): string
73 return "({$this->id}) {$this->name}";