mirror of
https://github.com/pelican-dev/panel.git
synced 2025-09-21 04:04:52 +02:00
Trying to simplify ProcessWebhook for testing without breaking custom discord stuff
This commit is contained in:
parent
e4221bc606
commit
2da5666ce9
@ -18,30 +18,63 @@ class ProcessWebhook implements ShouldQueue
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* @param array<mixed> $data
|
||||
* @param array<mixed>|string|null $data
|
||||
*/
|
||||
public function __construct(
|
||||
private WebhookConfiguration $webhookConfiguration,
|
||||
private string $eventName,
|
||||
private array $data
|
||||
private array|string|null $data = null
|
||||
) {}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$data = $this->data[0] ?? [];
|
||||
if (count($data) === 1) {
|
||||
$data = reset($data);
|
||||
}
|
||||
$data = is_array($data) ? $data : (json_decode($data, true) ?? []);
|
||||
$data['event'] = $this->webhookConfiguration->transformClassName($this->eventName);
|
||||
$payload = is_array($this->data) ? $this->data : (json_decode($this->data, true) ?? []);
|
||||
$payload['event'] = $this->webhookConfiguration->transformClassName($this->eventName);
|
||||
|
||||
if ($this->webhookConfiguration->type === WebhookType::Discord) {
|
||||
$payload = $this->convertToDiscord($payload);
|
||||
}
|
||||
|
||||
try {
|
||||
$customHeaders = $this->webhookConfiguration->headers ?: [];
|
||||
$headers = [];
|
||||
foreach ($customHeaders as $key => $value) {
|
||||
$headers[$key] = $this->webhookConfiguration->replaceVars($payload, $value);
|
||||
}
|
||||
|
||||
Http::withHeaders($headers)->post($this->webhookConfiguration->endpoint, $payload)->throw();
|
||||
$successful = now();
|
||||
} catch (Exception $exception) {
|
||||
report($exception->getMessage());
|
||||
$successful = null;
|
||||
}
|
||||
|
||||
$this->logWebhookCall($payload, $successful);
|
||||
}
|
||||
|
||||
private function logWebhookCall(array $payload, Carbon $success): void
|
||||
{
|
||||
$this->webhookConfiguration->webhooks()->create([
|
||||
'payload' => $payload,
|
||||
'successful_at' => $success,
|
||||
'event' => $this->eventName,
|
||||
'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();
|
||||
@ -50,27 +83,6 @@ class ProcessWebhook implements ShouldQueue
|
||||
}
|
||||
$data['embeds'] = $embeds;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$customHeaders = $this->webhookConfiguration->headers;
|
||||
$headers = [];
|
||||
foreach ($customHeaders as $key => $value) {
|
||||
$headers[$key] = $this->webhookConfiguration->replaceVars($data, $value);
|
||||
}
|
||||
|
||||
Http::withHeaders($headers)->post($this->webhookConfiguration->endpoint, $data)->throw();
|
||||
$successful = now();
|
||||
} catch (Exception $exception) {
|
||||
report($exception->getMessage());
|
||||
$successful = null;
|
||||
}
|
||||
|
||||
$this->webhookConfiguration->webhooks()->create([
|
||||
'payload' => $data,
|
||||
'successful_at' => $successful,
|
||||
'event' => $this->eventName,
|
||||
'endpoint' => $this->webhookConfiguration->endpoint,
|
||||
]);
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
@ -18,11 +18,11 @@ use Illuminate\Database\Eloquent\Model;
|
||||
*/
|
||||
class Webhook extends Model
|
||||
{
|
||||
use HasFactory, MassPrunable;
|
||||
use MassPrunable;
|
||||
|
||||
protected $fillable = ['payload', 'successful_at', 'event', 'endpoint'];
|
||||
|
||||
public function casts()
|
||||
public function casts(): array
|
||||
{
|
||||
return [
|
||||
'payload' => 'array',
|
||||
|
@ -193,7 +193,7 @@ class WebhookConfiguration extends Model
|
||||
$eventName ??= 'eloquent.created: '.Server::class;
|
||||
$eventData ??= $this->getWebhookSampleData();
|
||||
|
||||
ProcessWebhook::dispatch($this, $eventName, [$eventData]);
|
||||
ProcessWebhook::dispatch($this, $eventName, $eventData);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user