From 26f4d37346b7c9b5833f0f187e5470774ff07298 Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Fri, 21 Mar 2025 12:16:33 +0100 Subject: [PATCH] feat(notifications): add discord ping functionality and settings --- app/Livewire/Notifications/Discord.php | 25 ++++++++++++++++- app/Models/DiscordNotificationSettings.php | 7 +++++ app/Notifications/Channels/DiscordChannel.php | 4 +++ app/Notifications/Test.php | 3 +- ...2025_03_21_104103_disable_discord_here.php | 28 +++++++++++++++++++ .../livewire/notifications/discord.blade.php | 9 ++++-- 6 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 database/migrations/2025_03_21_104103_disable_discord_here.php diff --git a/app/Livewire/Notifications/Discord.php b/app/Livewire/Notifications/Discord.php index 57007813e..9489eb128 100644 --- a/app/Livewire/Notifications/Discord.php +++ b/app/Livewire/Notifications/Discord.php @@ -56,6 +56,9 @@ class Discord extends Component #[Validate(['boolean'])] public bool $serverUnreachableDiscordNotifications = true; + #[Validate(['boolean'])] + public bool $discordPingEnabled = true; + public function mount() { try { @@ -87,6 +90,8 @@ class Discord extends Component $this->settings->server_reachable_discord_notifications = $this->serverReachableDiscordNotifications; $this->settings->server_unreachable_discord_notifications = $this->serverUnreachableDiscordNotifications; + $this->settings->discord_ping_enabled = $this->discordPingEnabled; + $this->settings->save(); refreshSession(); } else { @@ -105,12 +110,30 @@ class Discord extends Component $this->serverDiskUsageDiscordNotifications = $this->settings->server_disk_usage_discord_notifications; $this->serverReachableDiscordNotifications = $this->settings->server_reachable_discord_notifications; $this->serverUnreachableDiscordNotifications = $this->settings->server_unreachable_discord_notifications; + + $this->discordPingEnabled = $this->settings->discord_ping_enabled; + } + } + + public function instantSaveDiscordPingEnabled() + { + try { + $original = $this->discordPingEnabled; + $this->validate([ + 'discordPingEnabled' => 'required', + ]); + $this->saveModel(); + } catch (\Throwable $e) { + $this->discordPingEnabled = $original; + + return handleError($e, $this); } } public function instantSaveDiscordEnabled() { try { + $original = $this->discordEnabled; $this->validate([ 'discordWebhookUrl' => 'required', ], [ @@ -118,7 +141,7 @@ class Discord extends Component ]); $this->saveModel(); } catch (\Throwable $e) { - $this->discordEnabled = false; + $this->discordEnabled = $original; return handleError($e, $this); } diff --git a/app/Models/DiscordNotificationSettings.php b/app/Models/DiscordNotificationSettings.php index 619393ddc..1ba16ccd8 100644 --- a/app/Models/DiscordNotificationSettings.php +++ b/app/Models/DiscordNotificationSettings.php @@ -28,6 +28,7 @@ class DiscordNotificationSettings extends Model 'server_disk_usage_discord_notifications', 'server_reachable_discord_notifications', 'server_unreachable_discord_notifications', + 'discord_ping_enabled', ]; protected $casts = [ @@ -45,6 +46,7 @@ class DiscordNotificationSettings extends Model 'server_disk_usage_discord_notifications' => 'boolean', 'server_reachable_discord_notifications' => 'boolean', 'server_unreachable_discord_notifications' => 'boolean', + 'discord_ping_enabled' => 'boolean', ]; public function team() @@ -56,4 +58,9 @@ class DiscordNotificationSettings extends Model { return $this->discord_enabled; } + + public function isPingEnabled() + { + return $this->discord_ping_enabled; + } } diff --git a/app/Notifications/Channels/DiscordChannel.php b/app/Notifications/Channels/DiscordChannel.php index 362006d8e..b4ba9bf8c 100644 --- a/app/Notifications/Channels/DiscordChannel.php +++ b/app/Notifications/Channels/DiscordChannel.php @@ -20,6 +20,10 @@ class DiscordChannel return; } + if (! $discordSettings->discord_ping_enabled) { + $message->isCritical = false; + } + SendMessageToDiscordJob::dispatch($message, $discordSettings->discord_webhook_url); } } diff --git a/app/Notifications/Test.php b/app/Notifications/Test.php index ebb8735f5..2a0581bbf 100644 --- a/app/Notifications/Test.php +++ b/app/Notifications/Test.php @@ -22,7 +22,7 @@ class Test extends Notification implements ShouldQueue public $tries = 5; - public function __construct(public ?string $emails = null, public ?string $channel = null) + public function __construct(public ?string $emails = null, public ?string $channel = null, public ?bool $ping = false) { $this->onQueue('high'); } @@ -68,6 +68,7 @@ class Test extends Notification implements ShouldQueue title: ':white_check_mark: Test Success', description: 'This is a test Discord notification from Coolify. :cross_mark: :warning: :information_source:', color: DiscordMessage::successColor(), + isCritical: $this->ping, ); $message->addField(name: 'Dashboard', value: '[Link]('.base_url().')', inline: true); diff --git a/database/migrations/2025_03_21_104103_disable_discord_here.php b/database/migrations/2025_03_21_104103_disable_discord_here.php new file mode 100644 index 000000000..6aef45c04 --- /dev/null +++ b/database/migrations/2025_03_21_104103_disable_discord_here.php @@ -0,0 +1,28 @@ +boolean('discord_ping_enabled')->default(true); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('discord_notification_settings', function (Blueprint $table) { + $table->dropColumn('discord_ping_enabled'); + }); + } +}; diff --git a/resources/views/livewire/notifications/discord.blade.php b/resources/views/livewire/notifications/discord.blade.php index 6caf31176..9b48c9793 100644 --- a/resources/views/livewire/notifications/discord.blade.php +++ b/resources/views/livewire/notifications/discord.blade.php @@ -20,12 +20,15 @@ @endif -