coolify/app/Livewire/Notifications/Email.php

256 lines
8.5 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 Illuminate\Support\Facades\RateLimiter;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
2024-06-10 20:43:34 +00:00
use Livewire\Component;
2023-05-25 16:27:52 +00:00
class Email extends Component
2023-05-25 16:27:52 +00:00
{
2023-08-31 13:00:59 +00:00
public Team $team;
2024-06-10 20:43:34 +00:00
#[Locked]
2023-07-28 08:55:26 +00:00
public string $emails;
2024-06-10 20:43:34 +00:00
#[Validate(['boolean'])]
public bool $smtpEnabled = false;
#[Validate(['boolean'])]
public bool $useInstanceEmailSettings = false;
#[Validate(['nullable', 'email'])]
public ?string $smtpFromAddress = null;
#[Validate(['nullable', 'string'])]
public ?string $smtpFromName = null;
#[Validate(['nullable', 'string'])]
public ?string $smtpRecipients = null;
#[Validate(['nullable', 'string'])]
public ?string $smtpHost = null;
#[Validate(['nullable', 'numeric'])]
public ?int $smtpPort = null;
#[Validate(['nullable', 'string'])]
public ?string $smtpEncryption = null;
#[Validate(['nullable', 'string'])]
public ?string $smtpUsername = null;
#[Validate(['nullable', 'string'])]
public ?string $smtpPassword = null;
#[Validate(['nullable', 'numeric'])]
public ?int $smtpTimeout = null;
#[Validate(['boolean'])]
2024-11-08 10:45:56 +00:00
public bool $smtpNotificationsTest = false;
#[Validate(['boolean'])]
2024-11-08 10:45:56 +00:00
public bool $smtpNotificationsDeployments = false;
#[Validate(['boolean'])]
2024-11-08 10:45:56 +00:00
public bool $smtpNotificationsStatusChanges = false;
#[Validate(['boolean'])]
2024-11-08 10:45:56 +00:00
public bool $smtpNotificationsDatabaseBackups = false;
#[Validate(['boolean'])]
2024-11-08 10:45:56 +00:00
public bool $smtpNotificationsScheduledTasks = false;
#[Validate(['boolean'])]
2024-11-08 10:45:56 +00:00
public bool $smtpNotificationsServerDiskUsage = false;
#[Validate(['boolean'])]
public bool $resendEnabled;
#[Validate(['nullable', 'string'])]
public ?string $resendApiKey = null;
public function mount()
{
try {
$this->team = auth()->user()->currentTeam();
$this->emails = auth()->user()->email;
$this->syncData();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function syncData(bool $toModel = false)
2023-08-31 13:00:59 +00:00
{
if ($toModel) {
$this->validate();
$this->team->smtp_enabled = $this->smtpEnabled;
$this->team->smtp_from_address = $this->smtpFromAddress;
$this->team->smtp_from_name = $this->smtpFromName;
$this->team->smtp_host = $this->smtpHost;
$this->team->smtp_port = $this->smtpPort;
$this->team->smtp_encryption = $this->smtpEncryption;
$this->team->smtp_username = $this->smtpUsername;
$this->team->smtp_password = $this->smtpPassword;
$this->team->smtp_timeout = $this->smtpTimeout;
$this->team->smtp_recipients = $this->smtpRecipients;
$this->team->smtp_notifications_test = $this->smtpNotificationsTest;
$this->team->smtp_notifications_deployments = $this->smtpNotificationsDeployments;
$this->team->smtp_notifications_status_changes = $this->smtpNotificationsStatusChanges;
$this->team->smtp_notifications_database_backups = $this->smtpNotificationsDatabaseBackups;
$this->team->smtp_notifications_scheduled_tasks = $this->smtpNotificationsScheduledTasks;
$this->team->smtp_notifications_server_disk_usage = $this->smtpNotificationsServerDiskUsage;
$this->team->use_instance_email_settings = $this->useInstanceEmailSettings;
$this->team->resend_enabled = $this->resendEnabled;
$this->team->resend_api_key = $this->resendApiKey;
2023-08-31 13:00:59 +00:00
$this->team->save();
2024-11-05 10:33:33 +00:00
refreshSession();
} else {
$this->smtpEnabled = $this->team->smtp_enabled;
$this->smtpFromAddress = $this->team->smtp_from_address;
$this->smtpFromName = $this->team->smtp_from_name;
$this->smtpHost = $this->team->smtp_host;
$this->smtpPort = $this->team->smtp_port;
$this->smtpEncryption = $this->team->smtp_encryption;
$this->smtpUsername = $this->team->smtp_username;
$this->smtpPassword = $this->team->smtp_password;
$this->smtpTimeout = $this->team->smtp_timeout;
$this->smtpRecipients = $this->team->smtp_recipients;
$this->smtpNotificationsTest = $this->team->smtp_notifications_test;
$this->smtpNotificationsDeployments = $this->team->smtp_notifications_deployments;
$this->smtpNotificationsStatusChanges = $this->team->smtp_notifications_status_changes;
$this->smtpNotificationsDatabaseBackups = $this->team->smtp_notifications_database_backups;
$this->smtpNotificationsScheduledTasks = $this->team->smtp_notifications_scheduled_tasks;
$this->smtpNotificationsServerDiskUsage = $this->team->smtp_notifications_server_disk_usage;
$this->useInstanceEmailSettings = $this->team->use_instance_email_settings;
$this->resendEnabled = $this->team->resend_enabled;
$this->resendApiKey = $this->team->resend_api_key;
2023-08-31 13:00:59 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
public function sendTestNotification()
{
try {
$executed = RateLimiter::attempt(
'test-email:'.$this->team->id,
$perMinute = 0,
function () {
$this->team?->notify(new Test($this->emails));
$this->dispatch('success', 'Test Email sent.');
},
$decaySeconds = 10,
);
if (! $executed) {
throw new \Exception('Too many messages sent!');
}
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-08-31 13:00:59 +00:00
}
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
public function instantSaveInstance()
2023-06-20 19:18:14 +00:00
{
2023-08-31 13:00:59 +00:00
try {
$this->smtpEnabled = false;
$this->resendEnabled = false;
$this->saveModel();
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-06-20 19:18:14 +00:00
}
}
public function instantSaveSmtpEnabled()
2023-08-31 13:00:59 +00:00
{
try {
$this->validate([
'smtpHost' => 'required',
'smtpPort' => 'required|numeric',
], [
2024-11-05 10:33:33 +00:00
'smtpHost.required' => 'SMTP Host is required.',
'smtpPort.required' => 'SMTP Port is required.',
]);
$this->resendEnabled = false;
$this->saveModel();
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
$this->smtpEnabled = false;
2024-06-10 20:43:34 +00:00
return handleError($e, $this);
2023-08-31 13:00:59 +00:00
}
}
2024-06-10 20:43:34 +00:00
public function instantSaveResend()
2023-08-31 13:00:59 +00:00
{
try {
$this->validate([
2024-11-08 12:22:21 +00:00
'resendApiKey' => 'required',
], [
2024-11-05 10:33:33 +00:00
'resendApiKey.required' => 'Resend API Key is required.',
]);
$this->smtpEnabled = false;
$this->saveModel();
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
$this->resendEnabled = false;
2024-06-10 20:43:34 +00:00
return handleError($e, $this);
2023-08-31 13:00:59 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-09-07 06:48:16 +00:00
public function saveModel()
{
$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-09-07 06:48:16 +00:00
}
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
public function submit()
{
try {
$this->resetErrorBag();
$this->saveModel();
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-08-31 13:00:59 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-06-20 17:08:43 +00:00
public function copyFromInstanceSettings()
2023-06-07 15:24:37 +00:00
{
$settings = instanceSettings();
if ($settings->smtp_enabled) {
$this->smtpEnabled = true;
$this->smtpFromAddress = $settings->smtp_from_address;
$this->smtpFromName = $settings->smtp_from_name;
$this->smtpRecipients = $settings->smtp_recipients;
$this->smtpHost = $settings->smtp_host;
$this->smtpPort = $settings->smtp_port;
$this->smtpEncryption = $settings->smtp_encryption;
$this->smtpUsername = $settings->smtp_username;
$this->smtpPassword = $settings->smtp_password;
$this->smtpTimeout = $settings->smtp_timeout;
$this->resendEnabled = false;
$this->saveModel();
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
return;
2023-06-20 19:18:14 +00:00
}
2023-08-31 13:00:59 +00:00
if ($settings->resend_enabled) {
$this->resendEnabled = true;
$this->resendApiKey = $settings->resend_api_key;
$this->smtpEnabled = false;
$this->saveModel();
2024-06-10 20:43:34 +00:00
2023-08-31 13:00:59 +00:00
return;
2023-05-25 16:27:52 +00:00
}
2023-12-07 18:06:32 +00:00
$this->dispatch('error', 'Instance SMTP/Resend settings are not enabled.');
2023-05-25 16:27:52 +00:00
}
2024-06-10 20:43:34 +00:00
public function render()
{
return view('livewire.notifications.email');
}
}