2021-12-07 15:55:11 +01:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 18:56:55 +02:00
|
|
|
namespace BookStack\Activity\Models;
|
2021-12-07 15:55:11 +01:00
|
|
|
|
2023-05-24 10:06:15 +02:00
|
|
|
use BookStack\Activity\ActivityType;
|
2022-01-03 20:42:48 +01:00
|
|
|
use Carbon\Carbon;
|
2021-12-08 18:35:58 +01:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2021-12-07 15:55:11 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-12-08 18:35:58 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2021-12-07 15:55:11 +01:00
|
|
|
|
2021-12-08 15:29:42 +01:00
|
|
|
/**
|
2021-12-18 12:43:05 +01:00
|
|
|
* @property int $id
|
|
|
|
* @property string $name
|
|
|
|
* @property string $endpoint
|
2021-12-08 18:35:58 +01:00
|
|
|
* @property Collection $trackedEvents
|
2021-12-18 12:43:05 +01:00
|
|
|
* @property bool $active
|
2022-01-03 20:42:48 +01:00
|
|
|
* @property int $timeout
|
|
|
|
* @property string $last_error
|
|
|
|
* @property Carbon $last_called_at
|
|
|
|
* @property Carbon $last_errored_at
|
2021-12-08 15:29:42 +01:00
|
|
|
*/
|
|
|
|
class Webhook extends Model implements Loggable
|
2021-12-07 15:55:11 +01:00
|
|
|
{
|
|
|
|
use HasFactory;
|
2021-12-08 15:29:42 +01:00
|
|
|
|
2022-09-18 02:25:20 +02:00
|
|
|
protected $fillable = ['name', 'endpoint', 'timeout'];
|
|
|
|
|
2022-01-03 20:42:48 +01:00
|
|
|
protected $casts = [
|
|
|
|
'last_called_at' => 'datetime',
|
|
|
|
'last_errored_at' => 'datetime',
|
|
|
|
];
|
|
|
|
|
2021-12-08 18:35:58 +01:00
|
|
|
/**
|
|
|
|
* Define the tracked event relation a webhook.
|
|
|
|
*/
|
|
|
|
public function trackedEvents(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(WebhookTrackedEvent::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the tracked events for a webhook from the given list of event types.
|
|
|
|
*/
|
|
|
|
public function updateTrackedEvents(array $events): void
|
|
|
|
{
|
|
|
|
$this->trackedEvents()->delete();
|
|
|
|
|
|
|
|
$eventsToStore = array_intersect($events, array_values(ActivityType::all()));
|
|
|
|
if (in_array('all', $events)) {
|
|
|
|
$eventsToStore = ['all'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$trackedEvents = [];
|
|
|
|
foreach ($eventsToStore as $event) {
|
|
|
|
$trackedEvents[] = new WebhookTrackedEvent(['event' => $event]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->trackedEvents()->saveMany($trackedEvents);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this webhook tracks the given event.
|
|
|
|
*/
|
|
|
|
public function tracksEvent(string $event): bool
|
|
|
|
{
|
|
|
|
return $this->trackedEvents->pluck('event')->contains($event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a URL for this webhook within the settings interface.
|
|
|
|
*/
|
|
|
|
public function getUrl(string $path = ''): string
|
|
|
|
{
|
|
|
|
return url('/settings/webhooks/' . $this->id . '/' . ltrim($path, '/'));
|
|
|
|
}
|
|
|
|
|
2021-12-08 15:29:42 +01:00
|
|
|
/**
|
|
|
|
* Get the string descriptor for this item.
|
|
|
|
*/
|
|
|
|
public function logDescriptor(): string
|
|
|
|
{
|
|
|
|
return "({$this->id}) {$this->name}";
|
|
|
|
}
|
2021-12-07 15:55:11 +01:00
|
|
|
}
|