Trying to simplify ProcessWebhook for testing without breaking custom discord stuff

This commit is contained in:
Vehikl 2025-09-18 17:21:45 -04:00
parent e4221bc606
commit 2da5666ce9
3 changed files with 42 additions and 30 deletions

View File

@ -18,59 +18,71 @@ class ProcessWebhook implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/** /**
* @param array<mixed> $data * @param array<mixed>|string|null $data
*/ */
public function __construct( public function __construct(
private WebhookConfiguration $webhookConfiguration, private WebhookConfiguration $webhookConfiguration,
private string $eventName, private string $eventName,
private array $data private array|string|null $data = null
) {} ) {}
public function handle(): void public function handle(): void
{ {
$data = $this->data[0] ?? []; $payload = is_array($this->data) ? $this->data : (json_decode($this->data, true) ?? []);
if (count($data) === 1) { $payload['event'] = $this->webhookConfiguration->transformClassName($this->eventName);
$data = reset($data);
}
$data = is_array($data) ? $data : (json_decode($data, true) ?? []);
$data['event'] = $this->webhookConfiguration->transformClassName($this->eventName);
if ($this->webhookConfiguration->type === WebhookType::Discord) { if ($this->webhookConfiguration->type === WebhookType::Discord) {
$payload = json_encode($this->webhookConfiguration->payload); $payload = $this->convertToDiscord($payload);
$tmp = $this->webhookConfiguration->replaceVars($data, $payload);
$data = json_decode($tmp, true);
$embeds = data_get($data, 'embeds');
if ($embeds) {
foreach ($embeds as &$embed) {
if (data_get($embed, 'has_timestamp')) {
$embed['timestamp'] = Carbon::now();
unset($embed['has_timestamp']);
}
}
$data['embeds'] = $embeds;
}
} }
try { try {
$customHeaders = $this->webhookConfiguration->headers; $customHeaders = $this->webhookConfiguration->headers ?: [];
$headers = []; $headers = [];
foreach ($customHeaders as $key => $value) { foreach ($customHeaders as $key => $value) {
$headers[$key] = $this->webhookConfiguration->replaceVars($data, $value); $headers[$key] = $this->webhookConfiguration->replaceVars($payload, $value);
} }
Http::withHeaders($headers)->post($this->webhookConfiguration->endpoint, $data)->throw(); Http::withHeaders($headers)->post($this->webhookConfiguration->endpoint, $payload)->throw();
$successful = now(); $successful = now();
} catch (Exception $exception) { } catch (Exception $exception) {
report($exception->getMessage()); report($exception->getMessage());
$successful = null; $successful = null;
} }
$this->logWebhookCall($payload, $successful);
}
private function logWebhookCall(array $payload, Carbon $success): void
{
$this->webhookConfiguration->webhooks()->create([ $this->webhookConfiguration->webhooks()->create([
'payload' => $data, 'payload' => $payload,
'successful_at' => $successful, 'successful_at' => $success,
'event' => $this->eventName, 'event' => $this->eventName,
'endpoint' => $this->webhookConfiguration->endpoint, 'endpoint' => $this->webhookConfiguration->endpoint,
]); ]);
} }
/**
* @param mixed $data
* @return array
*/
public function convertToDiscord(mixed $data): array
{
$payload = json_encode($this->webhookConfiguration->payload);
$tmp = $this->webhookConfiguration->replaceVars($data, $payload);
$data = json_decode($tmp, true);
$embeds = data_get($data, 'embeds');
if ($embeds) {
// copied from previous, is the & needed?
foreach ($embeds as &$embed) {
if (data_get($embed, 'has_timestamp')) {
$embed['timestamp'] = Carbon::now();
unset($embed['has_timestamp']);
}
}
$data['embeds'] = $embeds;
}
return $data;
}
} }

View File

@ -18,11 +18,11 @@ use Illuminate\Database\Eloquent\Model;
*/ */
class Webhook extends Model class Webhook extends Model
{ {
use HasFactory, MassPrunable; use MassPrunable;
protected $fillable = ['payload', 'successful_at', 'event', 'endpoint']; protected $fillable = ['payload', 'successful_at', 'event', 'endpoint'];
public function casts() public function casts(): array
{ {
return [ return [
'payload' => 'array', 'payload' => 'array',

View File

@ -193,7 +193,7 @@ class WebhookConfiguration extends Model
$eventName ??= 'eloquent.created: '.Server::class; $eventName ??= 'eloquent.created: '.Server::class;
$eventData ??= $this->getWebhookSampleData(); $eventData ??= $this->getWebhookSampleData();
ProcessWebhook::dispatch($this, $eventName, [$eventData]); ProcessWebhook::dispatch($this, $eventName, $eventData);
} }
/** /**