3 namespace BookStack\Actions;
5 use BookStack\Auth\User;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Interfaces\Loggable;
10 use Illuminate\Support\Carbon;
12 class WebhookFormatter
14 protected Webhook $webhook;
15 protected string $event;
16 protected User $initiator;
17 protected int $initiatedTime;
20 * @var string|Loggable
25 * @var array{condition: callable(string, Model):bool, format: callable(Model):void}[]
27 protected $modelFormatters = [];
29 public function __construct(string $event, Webhook $webhook, $detail, User $initiator, int $initiatedTime)
31 $this->webhook = $webhook;
32 $this->event = $event;
33 $this->initiator = $initiator;
34 $this->initiatedTime = $initiatedTime;
35 $this->detail = is_object($detail) ? clone $detail : $detail;
38 public function format(): array
41 'event' => $this->event,
42 'text' => $this->formatText(),
43 'triggered_at' => Carbon::createFromTimestampUTC($this->initiatedTime)->toISOString(),
44 'triggered_by' => $this->initiator->attributesToArray(),
45 'triggered_by_profile_url' => $this->initiator->getProfileUrl(),
46 'webhook_id' => $this->webhook->id,
47 'webhook_name' => $this->webhook->name,
50 if (method_exists($this->detail, 'getUrl')) {
51 $data['url'] = $this->detail->getUrl();
54 if ($this->detail instanceof Model) {
55 $data['related_item'] = $this->formatModel();
62 * @param callable(string, Model):bool $condition
63 * @param callable(Model):void $format
65 public function addModelFormatter(callable $condition, callable $format): void
67 $this->modelFormatters[] = [
68 'condition' => $condition,
73 public function addDefaultModelFormatters(): void
75 // Load entity owner, creator, updater details
76 $this->addModelFormatter(
77 fn ($event, $model) => ($model instanceof Entity),
78 fn ($model) => $model->load(['ownedBy', 'createdBy', 'updatedBy'])
81 // Load revision detail for page update and create events
82 $this->addModelFormatter(
83 fn ($event, $model) => ($model instanceof Page && ($event === ActivityType::PAGE_CREATE || $event === ActivityType::PAGE_UPDATE)),
84 fn ($model) => $model->load('currentRevision')
88 protected function formatModel(): array
90 /** @var Model $model */
91 $model = $this->detail;
92 $model->unsetRelations();
94 foreach ($this->modelFormatters as $formatter) {
95 if ($formatter['condition']($this->event, $model)) {
96 $formatter['format']($model);
100 return $model->toArray();
103 protected function formatText(): string
106 $this->initiator->name,
107 trans('activities.' . $this->event),
110 if ($this->detail instanceof Entity) {
111 $textParts[] = '"' . $this->detail->name . '"';
114 return implode(' ', $textParts);
117 public static function getDefault(string $event, Webhook $webhook, $detail, User $initiator, int $initiatedTime): self
119 $instance = new self($event, $webhook, $detail, $initiator, $initiatedTime);
120 $instance->addDefaultModelFormatters();