refactor telegram notifications view

This commit is contained in:
Andras Bacsai
2024-11-04 17:48:39 +01:00
parent bf29dd4789
commit d24d1394f7
2 changed files with 124 additions and 52 deletions

View File

@@ -4,54 +4,125 @@ namespace App\Livewire\Notifications;
use App\Models\Team; use App\Models\Team;
use App\Notifications\Test; use App\Notifications\Test;
use Livewire\Attributes\Rule;
use Livewire\Component; use Livewire\Component;
class Telegram extends Component class Telegram extends Component
{ {
public Team $team; public Team $team;
protected $rules = [ #[Rule(['boolean'])]
'team.telegram_enabled' => 'nullable|boolean', public bool $telegramEnabled = false;
'team.telegram_token' => 'required|string',
'team.telegram_chat_id' => 'required|string',
'team.telegram_notifications_test' => 'nullable|boolean',
'team.telegram_notifications_deployments' => 'nullable|boolean',
'team.telegram_notifications_status_changes' => 'nullable|boolean',
'team.telegram_notifications_database_backups' => 'nullable|boolean',
'team.telegram_notifications_scheduled_tasks' => 'nullable|boolean',
'team.telegram_notifications_test_message_thread_id' => 'nullable|string',
'team.telegram_notifications_deployments_message_thread_id' => 'nullable|string',
'team.telegram_notifications_status_changes_message_thread_id' => 'nullable|string',
'team.telegram_notifications_database_backups_message_thread_id' => 'nullable|string',
'team.telegram_notifications_scheduled_tasks_thread_id' => 'nullable|string',
'team.telegram_notifications_server_disk_usage' => 'nullable|boolean',
];
protected $validationAttributes = [ #[Rule(['nullable', 'string'])]
'team.telegram_token' => 'Token', public ?string $telegramToken = null;
'team.telegram_chat_id' => 'Chat ID',
]; #[Rule(['nullable', 'string'])]
public ?string $telegramChatId = null;
#[Rule(['boolean'])]
public bool $telegramNotificationsTest = false;
#[Rule(['boolean'])]
public bool $telegramNotificationsDeployments = false;
#[Rule(['boolean'])]
public bool $telegramNotificationsStatusChanges = false;
#[Rule(['boolean'])]
public bool $telegramNotificationsDatabaseBackups = false;
#[Rule(['boolean'])]
public bool $telegramNotificationsScheduledTasks = false;
#[Rule(['nullable', 'string'])]
public ?string $telegramNotificationsTestMessageThreadId = null;
#[Rule(['nullable', 'string'])]
public ?string $telegramNotificationsDeploymentsMessageThreadId = null;
#[Rule(['nullable', 'string'])]
public ?string $telegramNotificationsStatusChangesMessageThreadId = null;
#[Rule(['nullable', 'string'])]
public ?string $telegramNotificationsDatabaseBackupsMessageThreadId = null;
#[Rule(['nullable', 'string'])]
public ?string $telegramNotificationsScheduledTasksThreadId = null;
#[Rule(['boolean'])]
public bool $telegramNotificationsServerDiskUsage = false;
public function mount() public function mount()
{ {
try {
$this->team = auth()->user()->currentTeam(); $this->team = auth()->user()->currentTeam();
$this->syncData();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function syncData(bool $toModel = false)
{
if ($toModel) {
$this->validate();
$this->team->telegram_enabled = $this->telegramEnabled;
$this->team->telegram_token = $this->telegramToken;
$this->team->telegram_chat_id = $this->telegramChatId;
$this->team->telegram_notifications_test = $this->telegramNotificationsTest;
$this->team->telegram_notifications_deployments = $this->telegramNotificationsDeployments;
$this->team->telegram_notifications_status_changes = $this->telegramNotificationsStatusChanges;
$this->team->telegram_notifications_database_backups = $this->telegramNotificationsDatabaseBackups;
$this->team->telegram_notifications_scheduled_tasks = $this->telegramNotificationsScheduledTasks;
$this->team->telegram_notifications_test_message_thread_id = $this->telegramNotificationsTestMessageThreadId;
$this->team->telegram_notifications_deployments_message_thread_id = $this->telegramNotificationsDeploymentsMessageThreadId;
$this->team->telegram_notifications_status_changes_message_thread_id = $this->telegramNotificationsStatusChangesMessageThreadId;
$this->team->telegram_notifications_database_backups_message_thread_id = $this->telegramNotificationsDatabaseBackupsMessageThreadId;
$this->team->telegram_notifications_scheduled_tasks_thread_id = $this->telegramNotificationsScheduledTasksThreadId;
$this->team->telegram_notifications_server_disk_usage = $this->telegramNotificationsServerDiskUsage;
try {
$this->saveModel();
} catch (\Throwable $e) {
return handleError($e, $this);
}
} else {
$this->telegramEnabled = $this->team->telegram_enabled;
$this->telegramToken = $this->team->telegram_token;
$this->telegramChatId = $this->team->telegram_chat_id;
$this->telegramNotificationsTest = $this->team->telegram_notifications_test;
$this->telegramNotificationsDeployments = $this->team->telegram_notifications_deployments;
$this->telegramNotificationsStatusChanges = $this->team->telegram_notifications_status_changes;
$this->telegramNotificationsDatabaseBackups = $this->team->telegram_notifications_database_backups;
$this->telegramNotificationsScheduledTasks = $this->team->telegram_notifications_scheduled_tasks;
$this->telegramNotificationsTestMessageThreadId = $this->team->telegram_notifications_test_message_thread_id;
$this->telegramNotificationsDeploymentsMessageThreadId = $this->team->telegram_notifications_deployments_message_thread_id;
$this->telegramNotificationsStatusChangesMessageThreadId = $this->team->telegram_notifications_status_changes_message_thread_id;
$this->telegramNotificationsDatabaseBackupsMessageThreadId = $this->team->telegram_notifications_database_backups_message_thread_id;
$this->telegramNotificationsScheduledTasksThreadId = $this->team->telegram_notifications_scheduled_tasks_thread_id;
$this->telegramNotificationsServerDiskUsage = $this->team->telegram_notifications_server_disk_usage;
}
} }
public function instantSave() public function instantSave()
{ {
try { try {
$this->submit(); $this->syncData(true);
} catch (\Throwable) { } catch (\Throwable $e) {
$this->team->telegram_enabled = false; return handleError($e, $this);
$this->validate();
} }
} }
public function submit() public function submit()
{ {
try {
$this->resetErrorBag(); $this->resetErrorBag();
$this->validate(); $this->syncData(true);
$this->saveModel(); $this->saveModel();
} catch (\Throwable $e) {
return handleError($e, $this);
}
} }
public function saveModel() public function saveModel()
@@ -63,8 +134,12 @@ class Telegram extends Component
public function sendTestNotification() public function sendTestNotification()
{ {
$this->team?->notify(new Test); try {
$this->team->notify(new Test);
$this->dispatch('success', 'Test notification sent.'); $this->dispatch('success', 'Test notification sent.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
} }
public function render() public function render()

View File

@@ -9,7 +9,7 @@
<x-forms.button type="submit"> <x-forms.button type="submit">
Save Save
</x-forms.button> </x-forms.button>
@if ($team->telegram_enabled) @if ($telegramEnabled)
<x-forms.button class="normal-case dark:text-white btn btn-xs no-animation btn-primary" <x-forms.button class="normal-case dark:text-white btn btn-xs no-animation btn-primary"
wire:click="sendTestNotification"> wire:click="sendTestNotification">
Send Test Notifications Send Test Notifications
@@ -17,64 +17,61 @@
@endif @endif
</div> </div>
<div class="w-32"> <div class="w-32">
<x-forms.checkbox instantSave id="team.telegram_enabled" label="Enabled" /> <x-forms.checkbox instantSave id="telegramEnabled" label="Enabled" />
</div> </div>
<div class="flex gap-2"> <div class="flex gap-2">
<x-forms.input type="password" autocomplete="new-password" <x-forms.input type="password" autocomplete="new-password"
helper="Get it from the <a class='inline-block underline dark:text-white' href='https://t.me/botfather' target='_blank'>BotFather Bot</a> on Telegram." helper="Get it from the <a class='inline-block underline dark:text-white' href='https://t.me/botfather' target='_blank'>BotFather Bot</a> on Telegram."
required id="team.telegram_token" label="Token" /> required id="telegramToken" label="Token" />
<x-forms.input helper="Recommended to add your bot to a group chat and add its Chat ID here." required <x-forms.input helper="Recommended to add your bot to a group chat and add its Chat ID here." required
id="team.telegram_chat_id" label="Chat ID" /> id="telegramChatId" label="Chat ID" />
</div> </div>
@if (data_get($team, 'telegram_enabled')) @if ($telegramEnabled)
<h2 class="mt-4">Subscribe to events</h2> <h2 class="mt-4">Subscribe to events</h2>
<div class="flex flex-col gap-4 w-96"> <div class="flex flex-col gap-4 w-96">
@if (isDev()) @if (isDev())
<div class="flex flex-col"> <div class="flex flex-col">
<h4>Test Notification</h4> <h4>Test Notification</h4>
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_test" <x-forms.checkbox instantSave="saveModel" id="telegramNotificationsTest" label="Enabled" />
label="Enabled" />
<x-forms.input <x-forms.input
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used." helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used."
id="team.telegram_notifications_test_message_thread_id" label="Custom Topic ID" /> id="telegramNotificationsTestMessageThreadId" label="Custom Topic ID" />
</div> </div>
@endif @endif
<div class="flex flex-col"> <div class="flex flex-col">
<h4>Container Status Changes</h4> <h4>Container Status Changes</h4>
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_status_changes" <x-forms.checkbox instantSave="saveModel" id="telegramNotificationsStatusChanges" label="Enabled" />
label="Enabled" />
<x-forms.input <x-forms.input
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used." helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used."
id="team.telegram_notifications_status_changes_message_thread_id" label="Custom Topic ID" /> id="telegramNotificationsStatusChangesMessageThreadId" label="Custom Topic ID" />
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
<h4>Application Deployments</h4> <h4>Application Deployments</h4>
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_deployments" <x-forms.checkbox instantSave="saveModel" id="telegramNotificationsDeployments" label="Enabled" />
label="Enabled" />
<x-forms.input <x-forms.input
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used." helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used."
id="team.telegram_notifications_deployments_message_thread_id" label="Custom Topic ID" /> id="telegramNotificationsDeploymentsMessageThreadId" label="Custom Topic ID" />
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
<h4>Database Backup Status</h4> <h4>Database Backup Status</h4>
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_database_backups" <x-forms.checkbox instantSave="saveModel" id="telegramNotificationsDatabaseBackups"
label="Enabled" /> label="Enabled" />
<x-forms.input <x-forms.input
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used." helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used."
id="team.telegram_notifications_database_backups_message_thread_id" label="Custom Topic ID" /> id="telegramNotificationsDatabaseBackupsMessageThreadId" label="Custom Topic ID" />
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
<h4>Scheduled Tasks Status</h4> <h4>Scheduled Tasks Status</h4>
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_scheduled_tasks" <x-forms.checkbox instantSave="saveModel" id="telegramNotificationsScheduledTasks"
label="Enabled" /> label="Enabled" />
<x-forms.input <x-forms.input
helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used." helper="If you are using Group chat with Topics, you can specify the topics ID. If empty, General topic will be used."
id="team.telegram_notifications_scheduled_tasks_thread_id" label="Custom Topic ID" /> id="telegramNotificationsScheduledTasksMessageThreadId" label="Custom Topic ID" />
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
<h4>Server Disk Usage</h4> <h4>Server Disk Usage</h4>
<x-forms.checkbox instantSave="saveModel" id="team.telegram_notifications_server_disk_usage" <x-forms.checkbox instantSave="saveModel" id="telegramNotificationsServerDiskUsage"
label="Enabled" /> label="Enabled" />
</div> </div>
</div> </div>