From a3fde2fabf2349012f47966a91c551684f3a4915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BAben=20Costa?= Date: Sat, 7 Dec 2024 00:30:24 +0000 Subject: [PATCH 1/2] [Feature] Notify Discord user when speedtest threshold is breached --- app/Filament/Pages/Settings/NotificationPage.php | 7 +++++++ .../SendSpeedtestThresholdNotification.php | 1 + app/Settings/NotificationSettings.php | 2 ++ ...024_12_06_222700_add_discord_user_mention.php | 16 ++++++++++++++++ .../views/discord/speedtest-threshold.blade.php | 4 ++++ 5 files changed, 30 insertions(+) create mode 100644 database/settings/2024_12_06_222700_add_discord_user_mention.php diff --git a/app/Filament/Pages/Settings/NotificationPage.php b/app/Filament/Pages/Settings/NotificationPage.php index bd7df5902..fc3370419 100755 --- a/app/Filament/Pages/Settings/NotificationPage.php +++ b/app/Filament/Pages/Settings/NotificationPage.php @@ -176,6 +176,13 @@ public function form(Form $form): Form ->url(), ]) ->columnSpanFull(), + Forms\Components\Fieldset::make('discord_user_mention') + ->label('User Id to mention if threshold is hit') + ->schema([ + Forms\Components\TextInput::make('discord_user_mention') + ->label('Mention ID') + ->maxLength(18), + ]), Forms\Components\Actions::make([ Forms\Components\Actions\Action::make('test discord') ->label('Test Discord webhook') diff --git a/app/Listeners/Discord/SendSpeedtestThresholdNotification.php b/app/Listeners/Discord/SendSpeedtestThresholdNotification.php index 50dd18411..3ba099671 100644 --- a/app/Listeners/Discord/SendSpeedtestThresholdNotification.php +++ b/app/Listeners/Discord/SendSpeedtestThresholdNotification.php @@ -71,6 +71,7 @@ public function handle(SpeedtestCompleted $event): void 'metrics' => $failed, 'speedtest_url' => $event->result->result_url, 'url' => url('/admin/results'), + 'mentionUser' => $notificationSettings->discord_user_mention, ])->render(), ]; diff --git a/app/Settings/NotificationSettings.php b/app/Settings/NotificationSettings.php index 0796be61a..dc7b3a201 100644 --- a/app/Settings/NotificationSettings.php +++ b/app/Settings/NotificationSettings.php @@ -46,6 +46,8 @@ class NotificationSettings extends Settings public ?array $discord_webhooks; + public ?string $discord_user_mention; + public bool $ntfy_enabled; public bool $ntfy_on_speedtest_run; diff --git a/database/settings/2024_12_06_222700_add_discord_user_mention.php b/database/settings/2024_12_06_222700_add_discord_user_mention.php new file mode 100644 index 000000000..7bb151ca2 --- /dev/null +++ b/database/settings/2024_12_06_222700_add_discord_user_mention.php @@ -0,0 +1,16 @@ +migrator->add('notification.discord_user_mention', null); + } + + public function down(): void + { + $this->migrator->delete('notification.discord_user_mention'); + } +}; diff --git a/resources/views/discord/speedtest-threshold.blade.php b/resources/views/discord/speedtest-threshold.blade.php index 95dc4bf01..bdba874a9 100644 --- a/resources/views/discord/speedtest-threshold.blade.php +++ b/resources/views/discord/speedtest-threshold.blade.php @@ -1,5 +1,9 @@ **Speedtest Threshold Breached - #{{ $id }}** +@if (!empty($mentionUser)) +{!! "<@{$mentionUser}>" !!} +@endif + A new speedtest on **{{ config('app.name') }}** was completed using **{{ $service }}** on **{{ $isp }}** but a threshold was breached. @foreach ($metrics as $item) From 14ce75a89b4e568f4b4a8547f354528924f8914b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BAben=20Costa?= Date: Sat, 7 Dec 2024 00:39:51 +0000 Subject: [PATCH 2/2] [UI] Show Discord User Mention only when 'Notify on threshold failures' is enabled --- .../Pages/Settings/NotificationPage.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/Filament/Pages/Settings/NotificationPage.php b/app/Filament/Pages/Settings/NotificationPage.php index fc3370419..bfdaaac2b 100755 --- a/app/Filament/Pages/Settings/NotificationPage.php +++ b/app/Filament/Pages/Settings/NotificationPage.php @@ -152,9 +152,7 @@ public function form(Form $form): Form ->label('Enable Discord webhook notifications') ->reactive() ->columnSpanFull(), - Forms\Components\Grid::make([ - 'default' => 1, - ]) + Forms\Components\Grid::make(['default' => 1]) ->hidden(fn (Forms\Get $get) => $get('discord_enabled') !== true) ->schema([ Forms\Components\Fieldset::make('Triggers') @@ -164,6 +162,7 @@ public function form(Form $form): Form ->columnSpanFull(), Forms\Components\Toggle::make('discord_on_threshold_failure') ->label('Notify on threshold failures') + ->reactive() ->columnSpanFull(), ]), Forms\Components\Repeater::make('discord_webhooks') @@ -177,12 +176,16 @@ public function form(Form $form): Form ]) ->columnSpanFull(), Forms\Components\Fieldset::make('discord_user_mention') - ->label('User Id to mention if threshold is hit') + ->label('Discord User Mention') + ->hidden(fn (Forms\Get $get) => $get('discord_on_threshold_failure') !== true) ->schema([ Forms\Components\TextInput::make('discord_user_mention') - ->label('Mention ID') - ->maxLength(18), - ]), + ->label('User ID') + ->placeholder('Enter the Discord User ID') + ->maxLength(18) + ->helperText('This should be the numeric User ID from Discord (e.g., 123456789012345678)'), + ]) + ->columnSpanFull(), Forms\Components\Actions::make([ Forms\Components\Actions\Action::make('test discord') ->label('Test Discord webhook') @@ -196,7 +199,6 @@ public function form(Form $form): Form 'default' => 1, 'md' => 2, ]), - Forms\Components\Section::make('Gotify') ->schema([ Forms\Components\Toggle::make('gotify_enabled')