3 namespace BookStack\Activity\Models;
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
17 * @property int $timeout
18 * @property string $last_error
19 * @property Carbon $last_called_at
20 * @property Carbon $last_errored_at
22 class Webhook extends Model implements Loggable
26 protected $fillable = ['name', 'endpoint', 'timeout'];
29 'last_called_at' => 'datetime',
30 'last_errored_at' => 'datetime',
34 * Define the tracked event relation a webhook.
36 public function trackedEvents(): HasMany
38 return $this->hasMany(WebhookTrackedEvent::class);
42 * Update the tracked events for a webhook from the given list of event types.
44 public function updateTrackedEvents(array $events): void
46 $this->trackedEvents()->delete();
48 $eventsToStore = array_intersect($events, array_values(ActivityType::all()));
49 if (in_array('all', $events)) {
50 $eventsToStore = ['all'];
54 foreach ($eventsToStore as $event) {
55 $trackedEvents[] = new WebhookTrackedEvent(['event' => $event]);
58 $this->trackedEvents()->saveMany($trackedEvents);
62 * Check if this webhook tracks the given event.
64 public function tracksEvent(string $event): bool
66 return $this->trackedEvents->pluck('event')->contains($event);
70 * Get a URL for this webhook within the settings interface.
72 public function getUrl(string $path = ''): string
74 return url('/settings/webhooks/' . $this->id . '/' . ltrim($path, '/'));
78 * Get the string descriptor for this item.
80 public function logDescriptor(): string
82 return "({$this->id}) {$this->name}";