This commit is contained in:
Andras Bacsai 2023-06-20 15:04:46 +02:00
parent 8910d5a65d
commit de759290e5
15 changed files with 62 additions and 52 deletions

View File

@ -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,

View File

@ -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.');
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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')
{

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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()

View File

@ -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,

View File

@ -5,7 +5,7 @@
<x-forms.button type="submit">
Save
</x-forms.button>
@if ($model->extra_attributes->discord_active)
@if ($model->extra_attributes->discord_enabled)
<x-forms.button class="text-white normal-case btn btn-xs no-animation btn-primary"
wire:click="sendTestNotification">
Send Test Notifications
@ -13,19 +13,21 @@
@endif
</div>
<div class="w-48">
<x-forms.checkbox instantSave id="model.extra_attributes.discord_active" label="Notification Enabled" />
<x-forms.checkbox instantSave id="model.extra_attributes.discord_enabled" label="Notification Enabled" />
</div>
<x-forms.input type="string"
helper="Generate a webhook in Discord.<br>Example: https://discord.com/api/webhooks/...." required
id="model.extra_attributes.discord_webhook" label="Webhook" />
id="model.extra_attributes.discord_webhook_url" label="Webhook" />
</form>
<h4 class="mt-4">Subscribe to events</h4>
<div class="w-64 ">
@if (isDev())
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_discord_test"
label="Test Notifications" />
@endif
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_discord_deployments"
label="New Deployments" />
</div>
@if (data_get($model, 'extra_attributes.discord_enabled'))
<h4 class="mt-4">Subscribe to events</h4>
<div class="w-64 ">
@if (isDev())
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_discord_test"
label="Test Notifications" />
@endif
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_discord_deployments"
label="New Deployments" />
</div>
@endif
</div>

View File

@ -10,7 +10,7 @@
Copy from Instance Settings
</x-forms.button>
@endif
@if ($model->extra_attributes->smtp_active)
@if ($model->extra_attributes->smtp_enabled)
<x-forms.button class="text-white normal-case btn btn-xs no-animation btn-primary"
wire:click="sendTestNotification">
Send Test Notifications
@ -18,7 +18,7 @@
@endif
</div>
<div class="w-48">
<x-forms.checkbox instantSave id="model.extra_attributes.smtp_active" label="Notification Enabled" />
<x-forms.checkbox instantSave id="model.extra_attributes.smtp_enabled" label="Notification Enabled" />
</div>
<div class="flex flex-col gap-2 xl:flex-row">
@ -53,13 +53,15 @@
</div>
</div>
</form>
<h4 class="mt-4">Subscribe to events</h4>
<div class="w-64 ">
@if (isDev())
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_email_test"
label="Test Notifications" />
@endif
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_email_deployments"
label="New Deployments" />
</div>
@if (data_get($model, 'extra_attributes.smtp_enabled'))
<h4 class="mt-4">Subscribe to events</h4>
<div class="w-64 ">
@if (isDev())
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_smtp_test"
label="Test Notifications" />
@endif
<x-forms.checkbox instantSave="saveModel" id="model.extra_attributes.notifications_email_deployments"
label="New Deployments" />
</div>
@endif
</div>

View File

@ -8,12 +8,12 @@
</div>
<div class="pt-2 pb-4 ">SMTP settings for password resets, invitations, etc.</div>
<div class="flex flex-col">
<x-forms.checkbox instantSave id="settings.extra_attributes.smtp_active" label="Enabled" />
<x-forms.checkbox instantSave id="settings.extra_attributes.smtp_enabled" label="Enabled" />
</div>
<div class="flex items-end gap-2">
<x-forms.input id="settings.extra_attributes.smtp_test_recipients" label="Test Recipients"
helper="Email list to send a test email to, separated by comma." />
@if ($settings->extra_attributes->smtp_active)
@if ($settings->extra_attributes->smtp_enabled)
<x-forms.button wire:click='test_email'>
Send Test Email
</x-forms.button>