3 namespace BookStack\Actions;
5 use BookStack\Interfaces\Loggable;
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;
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
23 class Webhook extends Model implements Loggable
25 protected $fillable = ['name', 'endpoint', 'timeout'];
30 'last_called_at' => 'datetime',
31 'last_errored_at' => 'datetime',
35 * Define the tracked event relation a webhook.
37 public function trackedEvents(): HasMany
39 return $this->hasMany(WebhookTrackedEvent::class);
43 * Update the tracked events for a webhook from the given list of event types.
45 public function updateTrackedEvents(array $events): void
47 $this->trackedEvents()->delete();
49 $eventsToStore = array_intersect($events, array_values(ActivityType::all()));
50 if (in_array('all', $events)) {
51 $eventsToStore = ['all'];
55 foreach ($eventsToStore as $event) {
56 $trackedEvents[] = new WebhookTrackedEvent(['event' => $event]);
59 $this->trackedEvents()->saveMany($trackedEvents);
63 * Check if this webhook tracks the given event.
65 public function tracksEvent(string $event): bool
67 return $this->trackedEvents->pluck('event')->contains($event);
71 * Get a URL for this webhook within the settings interface.
73 public function getUrl(string $path = ''): string
75 return url('/settings/webhooks/' . $this->id . '/' . ltrim($path, '/'));
79 * Get the string descriptor for this item.
81 public function logDescriptor(): string
83 return "({$this->id}) {$this->name}";