coolify/app/Livewire/Notifications/Discord.php

132 lines
4.2 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
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
#[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-11-03 22:19:44 +00:00
public bool $discordNotificationsTest = false;
#[Validate(['boolean'])]
2024-11-03 22:19:44 +00:00
public bool $discordNotificationsDeployments = false;
#[Validate(['boolean'])]
2024-11-03 22:19:44 +00:00
public bool $discordNotificationsStatusChanges = false;
#[Validate(['boolean'])]
2024-11-03 22:19:44 +00:00
public bool $discordNotificationsDatabaseBackups = false;
#[Validate(['boolean'])]
2024-11-03 22:19:44 +00:00
public bool $discordNotificationsScheduledTasks = false;
#[Validate(['boolean'])]
2024-11-03 22:19:44 +00:00
public bool $discordNotificationsServerDiskUsage = false;
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();
$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();
$this->team->discord_enabled = $this->discordEnabled;
$this->team->discord_webhook_url = $this->discordWebhookUrl;
$this->team->discord_notifications_test = $this->discordNotificationsTest;
$this->team->discord_notifications_deployments = $this->discordNotificationsDeployments;
$this->team->discord_notifications_status_changes = $this->discordNotificationsStatusChanges;
$this->team->discord_notifications_database_backups = $this->discordNotificationsDatabaseBackups;
$this->team->discord_notifications_scheduled_tasks = $this->discordNotificationsScheduledTasks;
$this->team->discord_notifications_server_disk_usage = $this->discordNotificationsServerDiskUsage;
2024-11-05 10:33:33 +00:00
$this->team->save();
refreshSession();
2024-11-03 22:19:44 +00:00
} else {
$this->discordEnabled = $this->team->discord_enabled;
$this->discordWebhookUrl = $this->team->discord_webhook_url;
$this->discordNotificationsTest = $this->team->discord_notifications_test;
$this->discordNotificationsDeployments = $this->team->discord_notifications_deployments;
$this->discordNotificationsStatusChanges = $this->team->discord_notifications_status_changes;
$this->discordNotificationsDatabaseBackups = $this->team->discord_notifications_database_backups;
$this->discordNotificationsScheduledTasks = $this->team->discord_notifications_scheduled_tasks;
$this->discordNotificationsServerDiskUsage = $this->team->discord_notifications_server_disk_usage;
}
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 {
$this->validate([
'discordWebhookUrl' => 'required',
], [
'discordWebhookUrl.required' => 'Discord Webhook URL is required.',
]);
$this->saveModel();
} catch (\Throwable $e) {
$this->discordEnabled = false;
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) {
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) {
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);
$this->dispatch('success', 'Test notification sent.');
} catch (\Throwable $e) {
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');
}
}