Skip to content

Commit a866e85

Browse files
authored
[Chore] Refactored Discord notifications to their own listeners (alexjustesen#1271)
1 parent 25774dc commit a866e85

File tree

8 files changed

+200
-165
lines changed

8 files changed

+200
-165
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace App\Listeners\Discord;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Helpers\Number;
7+
use App\Settings\NotificationSettings;
8+
use Illuminate\Support\Facades\Log;
9+
use Illuminate\Support\Str;
10+
use Spatie\WebhookServer\WebhookCall;
11+
12+
class SendSpeedtestCompletedNotification
13+
{
14+
/**
15+
* Handle the event.
16+
*/
17+
public function handle(SpeedtestCompleted $event): void
18+
{
19+
$notificationSettings = new NotificationSettings();
20+
21+
if (! $notificationSettings->discord_enabled) {
22+
return;
23+
}
24+
25+
if (! $notificationSettings->discord_on_speedtest_run) {
26+
return;
27+
}
28+
29+
if (! count($notificationSettings->discord_webhooks)) {
30+
Log::warning('Discord urls not found, check Discord notification channel settings.');
31+
32+
return;
33+
}
34+
35+
$payload = [
36+
'content' => view('discord.speedtest-completed', [
37+
'id' => $event->result->id,
38+
'service' => Str::title($event->result->service),
39+
'serverName' => $event->result->server_name,
40+
'serverId' => $event->result->server_id,
41+
'ping' => round($event->result->ping).' ms',
42+
'download' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
43+
'upload' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
44+
])->render(),
45+
];
46+
47+
foreach ($notificationSettings->discord_webhooks as $url) {
48+
WebhookCall::create()
49+
->url($url['url'])
50+
->payload($payload)
51+
->doNotSign()
52+
->dispatch();
53+
}
54+
}
55+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace App\Listeners\Discord;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Helpers\Number;
7+
use App\Settings\NotificationSettings;
8+
use App\Settings\ThresholdSettings;
9+
use Illuminate\Support\Facades\Log;
10+
use Illuminate\Support\Str;
11+
use Spatie\WebhookServer\WebhookCall;
12+
13+
class SendSpeedtestThresholdNotification
14+
{
15+
/**
16+
* Handle the event.
17+
*/
18+
public function handle(SpeedtestCompleted $event): void
19+
{
20+
$notificationSettings = new NotificationSettings();
21+
22+
if (! $notificationSettings->discord_enabled) {
23+
return;
24+
}
25+
26+
if (! $notificationSettings->discord_on_threshold_failure) {
27+
return;
28+
}
29+
30+
if (! count($notificationSettings->discord_webhooks)) {
31+
Log::warning('Discord urls not found, check Discord notification channel settings.');
32+
33+
return;
34+
}
35+
36+
$thresholdSettings = new ThresholdSettings();
37+
38+
$failed = [];
39+
40+
if ($thresholdSettings->absolute_download > 0) {
41+
array_push($failed, $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings));
42+
}
43+
44+
if ($thresholdSettings->absolute_upload > 0) {
45+
array_push($failed, $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings));
46+
}
47+
48+
if ($thresholdSettings->absolute_ping > 0) {
49+
array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings));
50+
}
51+
52+
if (! count($failed)) {
53+
return;
54+
}
55+
56+
$payload = [
57+
'content' => view('discord.speedtest-threshold', [
58+
'id' => $event->result->id,
59+
'service' => Str::title($event->result->service),
60+
'serverName' => $event->result->server_name,
61+
'serverId' => $event->result->server_id,
62+
'metrics' => $failed,
63+
])->render(),
64+
];
65+
66+
foreach ($notificationSettings->discord_webhooks as $url) {
67+
WebhookCall::create()
68+
->url($url['url'])
69+
->payload($payload)
70+
->doNotSign()
71+
->dispatch();
72+
}
73+
}
74+
75+
/**
76+
* Build Discord notification if absolute download threshold is breached.
77+
*/
78+
protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
79+
{
80+
if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) {
81+
return [];
82+
}
83+
84+
return [
85+
'name' => 'Download',
86+
'threshold' => $thresholdSettings->absolute_download.' Mbps',
87+
'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
88+
];
89+
}
90+
91+
/**
92+
* Build Discord notification if absolute upload threshold is breached.
93+
*/
94+
protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
95+
{
96+
if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) {
97+
return [];
98+
}
99+
100+
return [
101+
'name' => 'Upload',
102+
'threshold' => $thresholdSettings->absolute_upload.' Mbps',
103+
'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
104+
];
105+
}
106+
107+
/**
108+
* Build Discord notification if absolute ping threshold is breached.
109+
*/
110+
protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
111+
{
112+
if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) {
113+
return [];
114+
}
115+
116+
return [
117+
'name' => 'Ping',
118+
'threshold' => $thresholdSettings->absolute_ping.' ms',
119+
'value' => round($event->result->ping, 2).' ms',
120+
];
121+
}
122+
}

app/Listeners/SpeedtestCompletedListener.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

app/Listeners/Threshold/AbsoluteListener.php

Lines changed: 0 additions & 102 deletions
This file was deleted.

app/Providers/EventServiceProvider.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
use App\Listeners\Data\InfluxDb2Listener;
1010
use App\Listeners\Database\SendSpeedtestCompletedNotification as DatabaseSendSpeedtestCompletedNotification;
1111
use App\Listeners\Database\SendSpeedtestThresholdNotification as DatabaseSendSpeedtestThresholdNotification;
12+
use App\Listeners\Discord\SendSpeedtestCompletedNotification as DiscordSendSpeedtestCompletedNotification;
13+
use App\Listeners\Discord\SendSpeedtestThresholdNotification as DiscordSendSpeedtestThresholdNotification;
1214
use App\Listeners\Mail\SendSpeedtestCompletedNotification as MailSendSpeedtestCompletedNotification;
1315
use App\Listeners\Mail\SendSpeedtestThresholdNotification as MailSendSpeedtestThresholdNotification;
14-
use App\Listeners\SpeedtestCompletedListener;
1516
use App\Listeners\Telegram\SendSpeedtestCompletedNotification as TelegramSendSpeedtestCompletedNotification;
1617
use App\Listeners\Telegram\SendSpeedtestThresholdNotification as TelegramSendSpeedtestThresholdNotification;
17-
use App\Listeners\Threshold\AbsoluteListener;
1818
use App\Listeners\Webhook\SendSpeedtestCompletedNotification as WebhookSendSpeedtestCompletedNotification;
1919
use App\Listeners\Webhook\SendSpeedtestThresholdNotification as WebhookSendSpeedtestThresholdNotification;
2020
use Illuminate\Auth\Events\Registered;
@@ -40,15 +40,17 @@ class EventServiceProvider extends ServiceProvider
4040
],
4141

4242
SpeedtestCompleted::class => [
43-
SpeedtestCompletedListener::class,
44-
4543
// Data listeners
4644
InfluxDb2Listener::class,
4745

4846
// Database notification listeners
4947
DatabaseSendSpeedtestCompletedNotification::class,
5048
DatabaseSendSpeedtestThresholdNotification::class,
5149

50+
// Discord notification listeners
51+
DiscordSendSpeedtestCompletedNotification::class,
52+
DiscordSendSpeedtestThresholdNotification::class,
53+
5254
// Mail notification listeners
5355
MailSendSpeedtestCompletedNotification::class,
5456
MailSendSpeedtestThresholdNotification::class,
@@ -60,8 +62,6 @@ class EventServiceProvider extends ServiceProvider
6062
// Webhook notification listeners
6163
WebhookSendSpeedtestCompletedNotification::class,
6264
WebhookSendSpeedtestThresholdNotification::class,
63-
64-
AbsoluteListener::class,
6565
],
6666

6767
SpeedtestFailed::class => [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
**Speedtest Completed - #{{ $id }}**
2+
3+
A new speedtest was completed using **{{ $service }}**.
4+
5+
- **Server name:** {{ $serverName }}
6+
- **Server ID:** {{ $serverId }}
7+
- **Ping:** {{ $ping }}
8+
- **Download:** {{ $download }}
9+
- **Upload:** {{ $upload }}

0 commit comments

Comments
 (0)