From de759290e5bee4a3ad74a112f8ac9e5d61420239 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 20 Jun 2023 15:04:46 +0200 Subject: [PATCH] wip --- app/Data/SmtpConfiguration.php | 2 +- .../Notifications/DiscordSettings.php | 14 +++++----- .../Livewire/Notifications/EmailSettings.php | 11 ++++---- app/Http/Livewire/Settings/Email.php | 4 +-- app/Models/Team.php | 2 +- app/Models/User.php | 4 +++ .../Channels/TransactionalEmailChannel.php | 2 +- .../DeployedSuccessfullyNotification.php | 4 +-- .../DeployedWithErrorNotification.php | 4 +-- .../Notifications/TestNotification.php | 9 +++++-- bootstrap/helpers/shared.php | 2 +- database/seeders/InstanceSettingsSeeder.php | 2 +- .../notifications/discord-settings.blade.php | 26 ++++++++++--------- .../notifications/email-settings.blade.php | 24 +++++++++-------- .../views/livewire/settings/email.blade.php | 4 +-- 15 files changed, 62 insertions(+), 52 deletions(-) diff --git a/app/Data/SmtpConfiguration.php b/app/Data/SmtpConfiguration.php index 1aac317de..ce290f88c 100644 --- a/app/Data/SmtpConfiguration.php +++ b/app/Data/SmtpConfiguration.php @@ -7,7 +7,7 @@ class SmtpConfiguration extends Data { public function __construct( - public bool $smtp_active = false, + public bool $smtp_enabled = false, public string $smtp_host, public int $smtp_port, public ?string $smtp_encryption, diff --git a/app/Http/Livewire/Notifications/DiscordSettings.php b/app/Http/Livewire/Notifications/DiscordSettings.php index 4a9ae4788..1ddc23678 100644 --- a/app/Http/Livewire/Notifications/DiscordSettings.php +++ b/app/Http/Livewire/Notifications/DiscordSettings.php @@ -2,31 +2,28 @@ namespace App\Http\Livewire\Notifications; -use App\Models\Server; use App\Models\Team; use App\Notifications\Notifications\TestNotification; -use Illuminate\Support\Facades\Notification; use Livewire\Component; class DiscordSettings extends Component { public Team $model; - protected $rules = [ - 'model.extra_attributes.discord_active' => 'nullable|boolean', - 'model.extra_attributes.discord_webhook' => 'required|url', + 'model.extra_attributes.discord_enabled' => 'nullable|boolean', + 'model.extra_attributes.discord_webhook_url' => 'required|url', 'model.extra_attributes.notifications_discord_test' => 'nullable|boolean', 'model.extra_attributes.notifications_discord_deployments' => 'nullable|boolean', ]; protected $validationAttributes = [ - 'model.extra_attributes.discord_webhook' => 'Discord Webhook', + 'model.extra_attributes.discord_webhook_url' => 'Discord Webhook', ]; public function instantSave() { try { $this->submit(); } catch (\Exception $e) { - $this->model->extra_attributes->discord_active = false; + $this->model->extra_attributes->discord_enabled = false; $this->validate(); } } @@ -46,6 +43,7 @@ public function submit() } public function sendTestNotification() { - Notification::send($this->model, new TestNotification); + $this->model->notify(new TestNotification('discord')); + $this->emit('success', 'Test notification sent.'); } } diff --git a/app/Http/Livewire/Notifications/EmailSettings.php b/app/Http/Livewire/Notifications/EmailSettings.php index 2943ef14f..d928cd517 100644 --- a/app/Http/Livewire/Notifications/EmailSettings.php +++ b/app/Http/Livewire/Notifications/EmailSettings.php @@ -5,7 +5,6 @@ use App\Models\InstanceSettings; use App\Models\Team; use App\Notifications\Notifications\TestNotification; -use Illuminate\Support\Facades\Notification; use Livewire\Component; class EmailSettings extends Component @@ -13,7 +12,7 @@ class EmailSettings extends Component public Team $model; protected $rules = [ - 'model.extra_attributes.smtp_active' => 'nullable|boolean', + 'model.extra_attributes.smtp_enabled' => 'nullable|boolean', 'model.extra_attributes.smtp_from_address' => 'required|email', 'model.extra_attributes.smtp_from_name' => 'required', 'model.extra_attributes.smtp_recipients' => 'nullable', @@ -24,7 +23,7 @@ class EmailSettings extends Component 'model.extra_attributes.smtp_password' => 'nullable', 'model.extra_attributes.smtp_timeout' => 'nullable', 'model.extra_attributes.smtp_test_recipients' => 'nullable', - 'model.extra_attributes.notifications_email_test' => 'nullable|boolean', + 'model.extra_attributes.notifications_smtp_test' => 'nullable|boolean', 'model.extra_attributes.notifications_email_deployments' => 'nullable|boolean', ]; protected $validationAttributes = [ @@ -41,7 +40,7 @@ class EmailSettings extends Component public function copySMTP() { $settings = InstanceSettings::get(); - $this->model->extra_attributes->smtp_active = true; + $this->model->extra_attributes->smtp_enabled = true; $this->model->extra_attributes->smtp_from_address = $settings->extra_attributes->smtp_from_address; $this->model->extra_attributes->smtp_from_name = $settings->extra_attributes->smtp_from_name; $this->model->extra_attributes->smtp_recipients = $settings->extra_attributes->smtp_recipients; @@ -72,7 +71,7 @@ public function saveModel() } public function sendTestNotification() { - Notification::send($this->model, new TestNotification); + $this->model->notify(new TestNotification('smtp')); $this->emit('success', 'Test notification sent.'); } public function instantSave() @@ -80,7 +79,7 @@ public function instantSave() try { $this->submit(); } catch (\Exception $e) { - $this->model->extra_attributes->smtp_active = false; + $this->model->extra_attributes->smtp_enabled = false; $this->validate(); } } diff --git a/app/Http/Livewire/Settings/Email.php b/app/Http/Livewire/Settings/Email.php index fa03bb847..75f10e6e9 100644 --- a/app/Http/Livewire/Settings/Email.php +++ b/app/Http/Livewire/Settings/Email.php @@ -12,7 +12,7 @@ class Email extends Component public InstanceSettings $settings; protected $rules = [ - 'settings.extra_attributes.smtp_active' => 'nullable|boolean', + 'settings.extra_attributes.smtp_enabled' => 'nullable|boolean', 'settings.extra_attributes.smtp_host' => 'required', 'settings.extra_attributes.smtp_port' => 'required|numeric', 'settings.extra_attributes.smtp_encryption' => 'nullable', @@ -39,7 +39,7 @@ public function instantSave() try { $this->submit(); } catch (\Exception $e) { - $this->settings->extra_attributes->smtp_active = false; + $this->settings->extra_attributes->smtp_enabled = false; $this->validate(); } } diff --git a/app/Models/Team.php b/app/Models/Team.php index 5029df7eb..42342aebd 100644 --- a/app/Models/Team.php +++ b/app/Models/Team.php @@ -27,7 +27,7 @@ class Team extends Model implements SendsDiscord, SendsEmail public function routeNotificationForDiscord() { - return $this->extra_attributes->get('discord_webhook'); + return $this->extra_attributes->get('discord_webhook_url'); } public function routeNotificationForEmail(string $attribute = 'smtp_recipients') { diff --git a/app/Models/User.php b/app/Models/User.php index fa3b83859..2e5148d48 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -34,6 +34,10 @@ protected static function boot() $team = [ 'name' => $user->name . "'s Team", 'personal_team' => true, + 'extra_attributes' => [ + 'notifications_smtp_test' => true, + 'notifications_discord_test' => true, + ] ]; if ($user->id === 0) { $team['id'] = 0; diff --git a/app/Notifications/Channels/TransactionalEmailChannel.php b/app/Notifications/Channels/TransactionalEmailChannel.php index c9057d4e7..d4098f0da 100644 --- a/app/Notifications/Channels/TransactionalEmailChannel.php +++ b/app/Notifications/Channels/TransactionalEmailChannel.php @@ -13,7 +13,7 @@ class TransactionalEmailChannel public function send(User $notifiable, Notification $notification): void { $settings = InstanceSettings::get(); - if ($settings->extra_attributes?->get('smtp_active') !== true) { + if ($settings->extra_attributes?->get('smtp_enabled') !== true) { return; } $email = $notifiable->email; diff --git a/app/Notifications/Notifications/Application/DeployedSuccessfullyNotification.php b/app/Notifications/Notifications/Application/DeployedSuccessfullyNotification.php index 5ef1370e9..c8ed0293d 100644 --- a/app/Notifications/Notifications/Application/DeployedSuccessfullyNotification.php +++ b/app/Notifications/Notifications/Application/DeployedSuccessfullyNotification.php @@ -44,10 +44,10 @@ public function __construct(Application $application, string $deployment_uuid, A public function via(object $notifiable): array { $channels = []; - if ($notifiable->extra_attributes?->get('smtp_active') && $notifiable->extra_attributes?->get('notifications_email_deployments')) { + if ($notifiable->extra_attributes?->get('smtp_enabled') && $notifiable->extra_attributes?->get('notifications_email_deployments')) { $channels[] = EmailChannel::class; } - if ($notifiable->extra_attributes?->get('discord_active') && $notifiable->extra_attributes?->get('notifications_discord_deployments')) { + if ($notifiable->extra_attributes?->get('discord_enabled') && $notifiable->extra_attributes?->get('notifications_discord_deployments')) { $channels[] = DiscordChannel::class; } return $channels; diff --git a/app/Notifications/Notifications/Application/DeployedWithErrorNotification.php b/app/Notifications/Notifications/Application/DeployedWithErrorNotification.php index 01e3b4101..0499cc161 100644 --- a/app/Notifications/Notifications/Application/DeployedWithErrorNotification.php +++ b/app/Notifications/Notifications/Application/DeployedWithErrorNotification.php @@ -44,10 +44,10 @@ public function __construct(Application $application, string $deployment_uuid, A public function via(object $notifiable): array { $channels = []; - if ($notifiable->extra_attributes?->get('smtp_active') && $notifiable->extra_attributes?->get('notifications_email_deployments')) { + if ($notifiable->extra_attributes?->get('smtp_enabled') && $notifiable->extra_attributes?->get('notifications_email_deployments')) { $channels[] = EmailChannel::class; } - if ($notifiable->extra_attributes?->get('discord_active') && $notifiable->extra_attributes?->get('notifications_discord_deployments')) { + if ($notifiable->extra_attributes?->get('discord_enabled') && $notifiable->extra_attributes?->get('notifications_discord_deployments')) { $channels[] = DiscordChannel::class; } return $channels; diff --git a/app/Notifications/Notifications/TestNotification.php b/app/Notifications/Notifications/TestNotification.php index d98ed1af1..6f03c27e1 100644 --- a/app/Notifications/Notifications/TestNotification.php +++ b/app/Notifications/Notifications/TestNotification.php @@ -12,13 +12,18 @@ class TestNotification extends Notification implements ShouldQueue { use Queueable; + public string|null $type = null; + public function __construct(string|null $type = null) + { + $this->type = $type; + } public function via(object $notifiable): array { $channels = []; - if ($notifiable->extra_attributes?->get('smtp_active') && $notifiable->extra_attributes?->get('notifications_email_test')) { + if (($this->type === 'smtp' || is_null($this->type)) && $notifiable->extra_attributes?->get('smtp_enabled') && $notifiable->extra_attributes?->get('notifications_smtp_test')) { $channels[] = EmailChannel::class; } - if ($notifiable->extra_attributes?->get('discord_active') && $notifiable->extra_attributes?->get('notifications_discord_test')) { + if (($this->type === 'discord' || is_null($this->type)) && $notifiable->extra_attributes?->get('discord_enabled') && $notifiable->extra_attributes?->get('notifications_discord_test')) { $channels[] = DiscordChannel::class; } return $channels; diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index b79ed2171..cb2338106 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -69,7 +69,7 @@ function generate_application_name(string $git_repository, string $git_branch) function is_transactional_emails_active() { - return data_get(InstanceSettings::get(), 'extra_attributes.smtp_active'); + return data_get(InstanceSettings::get(), 'extra_attributes.smtp_enabled'); } function set_transanctional_email_settings() diff --git a/database/seeders/InstanceSettingsSeeder.php b/database/seeders/InstanceSettingsSeeder.php index 937e147ab..503ce62ff 100644 --- a/database/seeders/InstanceSettingsSeeder.php +++ b/database/seeders/InstanceSettingsSeeder.php @@ -18,7 +18,7 @@ public function run(): void 'id' => 0, 'is_registration_enabled' => true, 'extra_attributes' => SmtpConfiguration::from([ - 'smtp_active' => true, + 'smtp_enabled' => true, 'smtp_test_recipients' => 'test@example.com,test2@example.com', 'smtp_host' => 'coolify-mail', 'smtp_port' => 1025, diff --git a/resources/views/livewire/notifications/discord-settings.blade.php b/resources/views/livewire/notifications/discord-settings.blade.php index ac943dacb..a037912a7 100644 --- a/resources/views/livewire/notifications/discord-settings.blade.php +++ b/resources/views/livewire/notifications/discord-settings.blade.php @@ -5,7 +5,7 @@ Save - @if ($model->extra_attributes->discord_active) + @if ($model->extra_attributes->discord_enabled) Send Test Notifications @@ -13,19 +13,21 @@ @endif
- +
+ id="model.extra_attributes.discord_webhook_url" label="Webhook" /> -

Subscribe to events

-
- @if (isDev()) - - @endif - -
+ @if (data_get($model, 'extra_attributes.discord_enabled')) +

Subscribe to events

+
+ @if (isDev()) + + @endif + +
+ @endif diff --git a/resources/views/livewire/notifications/email-settings.blade.php b/resources/views/livewire/notifications/email-settings.blade.php index 015db8b00..9f3aa35ac 100644 --- a/resources/views/livewire/notifications/email-settings.blade.php +++ b/resources/views/livewire/notifications/email-settings.blade.php @@ -10,7 +10,7 @@ Copy from Instance Settings
@endif - @if ($model->extra_attributes->smtp_active) + @if ($model->extra_attributes->smtp_enabled) Send Test Notifications @@ -18,7 +18,7 @@ @endif
- +
@@ -53,13 +53,15 @@
-

Subscribe to events

-
- @if (isDev()) - - @endif - -
+ @if (data_get($model, 'extra_attributes.smtp_enabled')) +

Subscribe to events

+
+ @if (isDev()) + + @endif + +
+ @endif diff --git a/resources/views/livewire/settings/email.blade.php b/resources/views/livewire/settings/email.blade.php index ec0ba13a6..cefea888f 100644 --- a/resources/views/livewire/settings/email.blade.php +++ b/resources/views/livewire/settings/email.blade.php @@ -8,12 +8,12 @@
SMTP settings for password resets, invitations, etc.
- +
- @if ($settings->extra_attributes->smtp_active) + @if ($settings->extra_attributes->smtp_enabled) Send Test Email