coolify/app/Livewire/Notifications/Discord.php

197 lines
7.4 KiB
PHP
Raw Normal View History

2023-05-25 16:27:52 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Notifications;
2023-05-25 16:27:52 +00:00
2024-12-09 15:38:25 +00:00
use App\Models\DiscordNotificationSettings;
2023-05-25 16:27:52 +00:00
use App\Models\Team;
2023-07-28 08:55:26 +00:00
use App\Notifications\Test;
use Livewire\Attributes\Validate;
2023-05-25 16:27:52 +00:00
use Livewire\Component;
class Discord extends Component
2023-05-25 16:27:52 +00:00
{
2023-09-06 12:31:38 +00:00
public Team $team;
2024-06-10 20:43:34 +00:00
2024-12-09 15:38:25 +00:00
public DiscordNotificationSettings $settings;
#[Validate(['boolean'])]
2024-11-03 22:19:44 +00:00
public bool $discordEnabled = false;
#[Validate(['url', 'nullable'])]
2024-11-03 22:19:44 +00:00
public ?string $discordWebhookUrl = null;
#[Validate(['boolean'])]
2024-12-09 15:38:25 +00:00
public bool $deploymentSuccessDiscordNotifications = false;
#[Validate(['boolean'])]
public bool $deploymentFailureDiscordNotifications = true;
#[Validate(['boolean'])]
public bool $statusChangeDiscordNotifications = false;
#[Validate(['boolean'])]
public bool $backupSuccessDiscordNotifications = false;
2024-11-03 22:19:44 +00:00
#[Validate(['boolean'])]
2024-12-09 15:38:25 +00:00
public bool $backupFailureDiscordNotifications = true;
2024-11-03 22:19:44 +00:00
#[Validate(['boolean'])]
2024-12-09 15:38:25 +00:00
public bool $scheduledTaskSuccessDiscordNotifications = false;
2024-11-03 22:19:44 +00:00
#[Validate(['boolean'])]
2024-12-09 15:38:25 +00:00
public bool $scheduledTaskFailureDiscordNotifications = true;
2024-11-03 22:19:44 +00:00
#[Validate(['boolean'])]
public bool $dockerCleanupSuccessDiscordNotifications = false;
#[Validate(['boolean'])]
public bool $dockerCleanupFailureDiscordNotifications = true;
2024-11-03 22:19:44 +00:00
#[Validate(['boolean'])]
2024-12-09 15:38:25 +00:00
public bool $serverDiskUsageDiscordNotifications = true;
#[Validate(['boolean'])]
public bool $serverReachableDiscordNotifications = false;
#[Validate(['boolean'])]
public bool $serverUnreachableDiscordNotifications = true;
#[Validate(['boolean'])]
public bool $serverPatchDiscordNotifications = false;
#[Validate(['boolean'])]
public bool $discordPingEnabled = true;
2023-09-06 12:31:38 +00:00
public function mount()
{
2024-11-03 22:19:44 +00:00
try {
$this->team = auth()->user()->currentTeam();
2024-12-09 15:38:25 +00:00
$this->settings = $this->team->discordNotificationSettings;
2024-11-03 22:19:44 +00:00
$this->syncData();
} catch (\Throwable $e) {
2024-11-05 10:33:33 +00:00
return handleError($e, $this);
2024-11-03 22:19:44 +00:00
}
}
public function syncData(bool $toModel = false)
{
if ($toModel) {
$this->validate();
2024-12-09 15:38:25 +00:00
$this->settings->discord_enabled = $this->discordEnabled;
$this->settings->discord_webhook_url = $this->discordWebhookUrl;
$this->settings->deployment_success_discord_notifications = $this->deploymentSuccessDiscordNotifications;
$this->settings->deployment_failure_discord_notifications = $this->deploymentFailureDiscordNotifications;
$this->settings->status_change_discord_notifications = $this->statusChangeDiscordNotifications;
$this->settings->backup_success_discord_notifications = $this->backupSuccessDiscordNotifications;
$this->settings->backup_failure_discord_notifications = $this->backupFailureDiscordNotifications;
$this->settings->scheduled_task_success_discord_notifications = $this->scheduledTaskSuccessDiscordNotifications;
$this->settings->scheduled_task_failure_discord_notifications = $this->scheduledTaskFailureDiscordNotifications;
$this->settings->docker_cleanup_success_discord_notifications = $this->dockerCleanupSuccessDiscordNotifications;
$this->settings->docker_cleanup_failure_discord_notifications = $this->dockerCleanupFailureDiscordNotifications;
2024-12-09 15:38:25 +00:00
$this->settings->server_disk_usage_discord_notifications = $this->serverDiskUsageDiscordNotifications;
$this->settings->server_reachable_discord_notifications = $this->serverReachableDiscordNotifications;
$this->settings->server_unreachable_discord_notifications = $this->serverUnreachableDiscordNotifications;
$this->settings->server_patch_discord_notifications = $this->serverPatchDiscordNotifications;
2024-12-09 15:38:25 +00:00
$this->settings->discord_ping_enabled = $this->discordPingEnabled;
2024-12-09 15:38:25 +00:00
$this->settings->save();
2024-11-05 10:33:33 +00:00
refreshSession();
2024-11-03 22:19:44 +00:00
} else {
2024-12-09 15:38:25 +00:00
$this->discordEnabled = $this->settings->discord_enabled;
$this->discordWebhookUrl = $this->settings->discord_webhook_url;
$this->deploymentSuccessDiscordNotifications = $this->settings->deployment_success_discord_notifications;
$this->deploymentFailureDiscordNotifications = $this->settings->deployment_failure_discord_notifications;
$this->statusChangeDiscordNotifications = $this->settings->status_change_discord_notifications;
$this->backupSuccessDiscordNotifications = $this->settings->backup_success_discord_notifications;
$this->backupFailureDiscordNotifications = $this->settings->backup_failure_discord_notifications;
$this->scheduledTaskSuccessDiscordNotifications = $this->settings->scheduled_task_success_discord_notifications;
$this->scheduledTaskFailureDiscordNotifications = $this->settings->scheduled_task_failure_discord_notifications;
$this->dockerCleanupSuccessDiscordNotifications = $this->settings->docker_cleanup_success_discord_notifications;
$this->dockerCleanupFailureDiscordNotifications = $this->settings->docker_cleanup_failure_discord_notifications;
2024-12-09 15:38:25 +00:00
$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->serverPatchDiscordNotifications = $this->settings->server_patch_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);
2024-11-03 22:19:44 +00:00
}
2023-09-06 12:31:38 +00:00
}
2024-06-10 20:43:34 +00:00
2024-11-05 10:33:33 +00:00
public function instantSaveDiscordEnabled()
{
try {
$original = $this->discordEnabled;
2024-11-05 10:33:33 +00:00
$this->validate([
'discordWebhookUrl' => 'required',
], [
'discordWebhookUrl.required' => 'Discord Webhook URL is required.',
]);
$this->saveModel();
} catch (\Throwable $e) {
$this->discordEnabled = $original;
2024-11-05 10:33:33 +00:00
return handleError($e, $this);
}
}
2023-06-01 10:15:33 +00:00
public function instantSave()
{
try {
2024-11-03 22:19:44 +00:00
$this->syncData(true);
} catch (\Throwable $e) {
2024-11-03 22:19:44 +00:00
return handleError($e, $this);
2023-06-01 10:15:33 +00:00
}
}
public function submit()
{
2024-11-03 22:19:44 +00:00
try {
$this->resetErrorBag();
$this->syncData(true);
$this->saveModel();
} catch (\Throwable $e) {
2024-11-03 22:19:44 +00:00
return handleError($e, $this);
}
}
2023-06-19 13:24:04 +00:00
public function saveModel()
2023-05-25 16:27:52 +00:00
{
2024-11-05 10:33:33 +00:00
$this->syncData(true);
2023-09-15 15:30:26 +00:00
refreshSession();
2023-12-07 18:06:32 +00:00
$this->dispatch('success', 'Settings saved.');
2023-05-25 16:27:52 +00:00
}
2023-06-01 10:15:33 +00:00
public function sendTestNotification()
2023-05-25 16:27:52 +00:00
{
2024-11-03 22:19:44 +00:00
try {
$this->team->notify(new Test(channel: 'discord'));
2024-11-03 22:19:44 +00:00
$this->dispatch('success', 'Test notification sent.');
} catch (\Throwable $e) {
2024-11-03 22:19:44 +00:00
return handleError($e, $this);
}
2023-05-25 16:27:52 +00:00
}
2024-06-10 20:43:34 +00:00
public function render()
{
return view('livewire.notifications.discord');
}
}