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
17 class Webhook extends Model implements Loggable
19 protected $fillable = ['name', 'endpoint'];
24 * Define the tracked event relation a webhook.
26 public function trackedEvents(): HasMany
28 return $this->hasMany(WebhookTrackedEvent::class);
32 * Update the tracked events for a webhook from the given list of event types.
34 public function updateTrackedEvents(array $events): void
36 $this->trackedEvents()->delete();
38 $eventsToStore = array_intersect($events, array_values(ActivityType::all()));
39 if (in_array('all', $events)) {
40 $eventsToStore = ['all'];
44 foreach ($eventsToStore as $event) {
45 $trackedEvents[] = new WebhookTrackedEvent(['event' => $event]);
48 $this->trackedEvents()->saveMany($trackedEvents);
52 * Check if this webhook tracks the given event.
54 public function tracksEvent(string $event): bool
56 return $this->trackedEvents->pluck('event')->contains($event);
60 * Get a URL for this webhook within the settings interface.
62 public function getUrl(string $path = ''): string
64 return url('/settings/webhooks/' . $this->id . '/' . ltrim($path, '/'));
68 * Get the string descriptor for this item.
70 public function logDescriptor(): string
72 return "({$this->id}) {$this->name}";