coolify/app/Jobs/SendMessageToDiscordJob.php

75 lines
2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Jobs;
2024-09-30 08:06:50 +00:00
use App\Notifications\Dto\DiscordMessage;
2026-07-02 12:46:46 +00:00
use App\Rules\SafeWebhookUrl;
use Illuminate\Bus\Queueable;
2023-09-14 08:12:44 +00:00
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
2026-07-02 12:46:46 +00:00
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
2024-06-10 20:43:34 +00:00
class SendMessageToDiscordJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 5;
2024-06-10 20:43:34 +00:00
public $backoff = 10;
/**
* The maximum number of unhandled exceptions to allow before failing.
*/
public int $maxExceptions = 5;
public function __construct(
2024-09-29 22:43:35 +00:00
public DiscordMessage $message,
public string $webhookUrl
) {
$this->onQueue('high');
}
/**
* Execute the job.
*/
public function handle(): void
{
2026-07-02 12:46:46 +00:00
$validator = Validator::make(
['webhook_url' => $this->webhookUrl],
['webhook_url' => ['required', 'url', new SafeWebhookUrl]]
);
if ($validator->fails()) {
Log::warning('SendMessageToDiscordJob: blocked unsafe webhook URL', [
2026-07-02 14:35:39 +00:00
'url' => SafeWebhookUrl::redactedUrlForLog($this->webhookUrl),
2026-07-02 12:46:46 +00:00
'errors' => $validator->errors()->all(),
]);
return;
}
2026-07-02 14:35:39 +00:00
try {
$httpOptions = SafeWebhookUrl::httpClientOptions($this->webhookUrl);
} catch (\RuntimeException $e) {
Log::warning('SendMessageToDiscordJob: blocked unsafe webhook URL at send time', [
'url' => SafeWebhookUrl::redactedUrlForLog($this->webhookUrl),
'error' => $e->getMessage(),
]);
return;
}
Http::withOptions($httpOptions)->post($this->webhookUrl, $this->message->toPayload());
}
}