Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor server limit check job + email notifications view

Andras Bacsai5ce6c3ded180
diff --git a/app/Jobs/ServerLimitCheckJob.php b/app/Jobs/ServerLimitCheckJob.php
index 084b6bf81e..aa82c6dade 100644
--- a/app/Jobs/ServerLimitCheckJob.php
+++ b/app/Jobs/ServerLimitCheckJob.php
@@ -30,8 +30,7 @@ class ServerLimitCheckJob implements ShouldBeEncrypted, ShouldQueue
try {
$servers = $this->team->servers;
$servers_count = $servers->count();
- $limit = data_get($this->team->limits, 'serverLimit', 2);
- $number_of_servers_to_disable = $servers_count - $limit;
+ $number_of_servers_to_disable = $servers_count - $this->team->limits;
if ($number_of_servers_to_disable > 0) {
$servers = $servers->sortbyDesc('created_at');
$servers_to_disable = $servers->take($number_of_servers_to_disable);
diff --git a/app/Livewire/Notifications/Email.php b/app/Livewire/Notifications/Email.php
index 6d67c89509..ae053b8be4 100644
--- a/app/Livewire/Notifications/Email.php
+++ b/app/Livewire/Notifications/Email.php
@@ -5,6 +5,7 @@ namespace App\Livewire\Notifications;
use App\Models\Team;
use App\Notifications\Test;
use Illuminate\Support\Facades\RateLimiter;
+use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
use Livewire\Component;
@@ -12,69 +13,121 @@ class Email extends Component
{
public Team $team;
+ #[Locked]
public string $emails;
- public bool $sharedEmailEnabled = false;
-
#[Validate(['boolean'])]
public bool $smtpEnabled = false;
- protected $rules = [
- 'team.smtp_enabled' => 'nullable|boolean',
- 'team.smtp_from_address' => 'required|email',
- 'team.smtp_from_name' => 'required',
- 'team.smtp_recipients' => 'nullable',
- 'team.smtp_host' => 'required',
- 'team.smtp_port' => 'required',
- 'team.smtp_encryption' => 'nullable',
- 'team.smtp_username' => 'nullable',
- 'team.smtp_password' => 'nullable',
- 'team.smtp_timeout' => 'nullable',
- 'team.smtp_notifications_test' => 'nullable|boolean',
- 'team.smtp_notifications_deployments' => 'nullable|boolean',
- 'team.smtp_notifications_status_changes' => 'nullable|boolean',
- 'team.smtp_notifications_database_backups' => 'nullable|boolean',
- 'team.smtp_notifications_scheduled_tasks' => 'nullable|boolean',
- 'team.smtp_notifications_server_disk_usage' => 'nullable|boolean',
- 'team.use_instance_email_settings' => 'boolean',
- 'team.resend_enabled' => 'nullable|boolean',
- 'team.resend_api_key' => 'nullable',
- ];
-
- protected $validationAttributes = [
- 'team.smtp_from_address' => 'From Address',
- 'team.smtp_from_name' => 'From Name',
- 'team.smtp_recipients' => 'Recipients',
- 'team.smtp_host' => 'Host',
- 'team.smtp_port' => 'Port',
- 'team.smtp_encryption' => 'Encryption',
- 'team.smtp_username' => 'Username',
- 'team.smtp_password' => 'Password',
- 'team.smtp_timeout' => 'Timeout',
- 'team.resend_enabled' => 'Resend Enabled',
- 'team.resend_api_key' => 'Resend API Key',
- ];
+ #[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'])]
+ public bool $smtpNotificationsTest;
+
+ #[Validate(['boolean'])]
+ public bool $smtpNotificationsDeployments;
+
+ #[Validate(['boolean'])]
+ public bool $smtpNotificationsStatusChanges;
+
+ #[Validate(['boolean'])]
+ public bool $smtpNotificationsDatabaseBackups;
+
+ #[Validate(['boolean'])]
+ public bool $smtpNotificationsScheduledTasks;
+
+ #[Validate(['boolean'])]
+ public bool $smtpNotificationsServerDiskUsage;
+
+ #[Validate(['boolean'])]
+ public bool $resendEnabled;
+
+ #[Validate(['nullable', 'string'])]
+ public ?string $resendApiKey = null;
public function mount()
{
- $this->team = auth()->user()->currentTeam();
- ['sharedEmailEnabled' => $this->sharedEmailEnabled] = $this->team->limits;
- $this->emails = auth()->user()->email;
+ try {
+ $this->team = auth()->user()->currentTeam();
+ $this->emails = auth()->user()->email;
+ $this->syncData();
+ } catch (\Throwable $e) {
+ return handleError($e, $this);
+ }
}
- public function submitFromFields()
+ public function syncData(bool $toModel = false)
{
- try {
- $this->resetErrorBag();
- $this->validate([
- 'team.smtp_from_address' => 'required|email',
- 'team.smtp_from_name' => 'required',
- ]);
+ 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;
$this->team->save();
- refreshSession();
- $this->dispatch('success', 'Settings saved.');
- } catch (\Throwable $e) {
- return handleError($e, $this);
+ } 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;
}
}
@@ -102,38 +155,45 @@ class Email extends Component
public function instantSaveInstance()
{
try {
- if (! $this->sharedEmailEnabled) {
- throw new \Exception('Not allowed to change settings. Please upgrade your subscription.');
- }
- $this->team->smtp_enabled = false;
- $this->team->resend_enabled = false;
- $this->team->save();
- refreshSession();
- $this->dispatch('success', 'Settings saved.');
+ $this->smtpEnabled = false;
+ $this->resendEnabled = false;
+ $this->saveModel();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
- public function instantSaveResend()
+ public function instantSaveSmtpEnabled()
{
try {
- $this->team->smtp_enabled = false;
- $this->submitResend();
+ $this->validate([
+ 'smtpHost' => 'required',
+ 'smtpPort' => 'required|numeric',
+ ], [
+ 'smtpHost.required' => 'SMTP Host is required',
+ 'smtpPort.required' => 'SMTP Port is required',
+ ]);
+ $this->resendEnabled = false;
+ $this->saveModel();
} catch (\Throwable $e) {
- $this->team->smtp_enabled = false;
+ $this->smtpEnabled = false;
return handleError($e, $this);
}
}
- public function instantSave()
+ public function instantSaveResend()
{
try {
- $this->team->resend_enabled = false;
- $this->submit();
+ $this->validate([
+ 'resendApiKey' => 'required',
+ ], [
+ 'resendApiKey.required' => 'Resend API Key is required',
+ ]);
+ $this->smtpEnabled = false;
+ $this->saveModel();
} catch (\Throwable $e) {
- $this->team->smtp_enabled = false;
+ $this->resendEnabled = false;
return handleError($e, $this);
}
@@ -141,7 +201,7 @@ class Email extends Component
public function saveModel()
{
- $this->team->save();
+ $this->syncData(true);
refreshSession();
$this->dispatch('success', 'Settings saved.');
}
@@ -150,43 +210,8 @@ class Email extends Component
{
try {
$this->resetErrorBag();
- if (! $this->team->use_instance_email_settings) {
- $this->validate([
- 'team.smtp_from_address' => 'required|email',
- 'team.smtp_from_name' => 'required',
- 'team.smtp_host' => 'required',
- 'team.smtp_port' => 'required|numeric',
- 'team.smtp_encryption' => 'nullable',
- 'team.smtp_username' => 'nullable',
- 'team.smtp_password' => 'nullable',
- 'team.smtp_timeout' => 'nullable',
- ]);
- }
- $this->team->save();
- refreshSession();
- $this->dispatch('success', 'Settings saved.');
+ $this->saveModel();
} catch (\Throwable $e) {
- $this->team->smtp_enabled = false;
-
- return handleError($e, $this);
- }
- }
-
- public function submitResend()
- {
- try {
- $this->resetErrorBag();
- $this->validate([
- 'team.smtp_from_address' => 'required|email',
- 'team.smtp_from_name' => 'required',
- 'team.resend_api_key' => 'required',
- ]);
- $this->team->save();
- refreshSession();
- $this->dispatch('success', 'Settings saved.');
- } catch (\Throwable $e) {
- $this->team->resend_enabled = false;
-
return handleError($e, $this);
}
}
@@ -194,35 +219,28 @@ class Email extends Component
public function copyFromInstanceSettings()
{
$settings = instanceSettings();
+
if ($settings->smtp_enabled) {
- $team = currentTeam();
- $team->update([
- 'smtp_enabled' => $settings->smtp_enabled,
- 'smtp_from_address' => $settings->smtp_from_address,
- 'smtp_from_name' => $settings->smtp_from_name,
- 'smtp_recipients' => $settings->smtp_recipients,
- 'smtp_host' => $settings->smtp_host,
- 'smtp_port' => $settings->smtp_port,
- 'smtp_encryption' => $settings->smtp_encryption,
- 'smtp_username' => $settings->smtp_username,
- 'smtp_password' => $settings->smtp_password,
- 'smtp_timeout' => $settings->smtp_timeout,
- ]);
- refreshSession();
- $this->team = $team;
- $this->dispatch('success', 'Settings saved.');
+ $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();
return;
}
if ($settings->resend_enabled) {
- $team = currentTeam();
- $team->update([
- 'resend_enabled' => $settings->resend_enabled,
- 'resend_api_key' => $settings->resend_api_key,
- ]);
- refreshSession();
- $this->team = $team;
- $this->dispatch('success', 'Settings saved.');
+ $this->resendEnabled = true;
+ $this->resendApiKey = $settings->resend_api_key;
+ $this->smtpEnabled = false;
+ $this->saveModel();
return;
}
diff --git a/app/Models/Team.php b/app/Models/Team.php
index 5b4a80cb1f..50639422da 100644
--- a/app/Models/Team.php
+++ b/app/Models/Team.php
@@ -187,9 +187,8 @@ class Team extends Model implements SendsDiscord, SendsEmail
} else {
$serverLimit = config('constants.limits.server')[strtolower($subscription)];
}
- $sharedEmailEnabled = config('constants.limits.email')[strtolower($subscription)];
- return ['serverLimit' => $serverLimit, 'sharedEmailEnabled' => $sharedEmailEnabled];
+ return $serverLimit ?? 2;
}
);
diff --git a/resources/views/livewire/notifications/email.blade.php b/resources/views/livewire/notifications/email.blade.php
index a87a31c24f..a2e5326c69 100644
--- a/resources/views/livewire/notifications/email.blade.php
+++ b/resources/views/livewire/notifications/email.blade.php
@@ -9,7 +9,7 @@
<x-forms.button type="submit">
Save
</x-forms.button>
- @if (isInstanceAdmin() && !$team->use_instance_email_settings)
+ @if (isInstanceAdmin() && !$useInstanceEmailSettings)
<x-forms.button wire:click='copyFromInstanceSettings'>
Copy from Instance Settings
</x-forms.button>
@@ -25,99 +25,90 @@
</x-modal-input>
@endif
</div>
- </form>
- @if (isCloud())
- @if ($this->sharedEmailEnabled)
- <div class="w-64 py-4">
- <x-forms.checkbox instantSave="instantSaveInstance" id="team.use_instance_email_settings"
- label="Use Hosted Email Service" />
+ @if (!isCloud())
+ <div class="w-96">
+ <x-forms.checkbox instantSave="instantSaveInstance" id="useInstanceEmailSettings"
+ label="Use system wide (transactional) email settings" />
</div>
- @else
- <div class="w-96 pb-4">
- <x-forms.checkbox disabled id="team.use_instance_email_settings"
- label="Use Hosted Email Service (Pro+ subscription required)" />
+ @endif
+ @if (!$useInstanceEmailSettings)
+ <div class="flex gap-4">
+ <x-forms.input required id="smtpFromName" helper="Name used in emails." label="From Name" />
+ <x-forms.input required id="smtpFromAddress" helper="Email address used in emails."
+ label="From Address" />
</div>
@endif
- @else
- <div class="w-96">
- <x-forms.checkbox instantSave="instantSaveInstance" id="team.use_instance_email_settings"
- label="Use system wide (transactional) email settings" />
+ </form>
+ @if (isCloud())
+ <div class="w-64 py-4">
+ <x-forms.checkbox instantSave="instantSaveInstance" id="useInstanceEmailSettings"
+ label="Use Hosted Email Service" />
</div>
@endif
- @if (!$team->use_instance_email_settings)
- <form class="flex flex-col items-end gap-2 pt-4 pb-4 xl:flex-row" wire:submit='submitFromFields'>
- <x-forms.input required id="team.smtp_from_name" helper="Name used in emails." label="From Name" />
- <x-forms.input required id="team.smtp_from_address" helper="Email address used in emails."
- label="From Address" />
- <x-forms.button type="submit">
- Save
- </x-forms.button>
- </form>
+ @if (!$useInstanceEmailSettings)
<div class="flex flex-col gap-4">
- <div class="p-4 border dark:border-coolgray-300">
- <h3>SMTP Server</h3>
+ <form wire:submit='submit' class="p-4 border dark:border-coolgray-300 flex flex-col gap-2">
+ <div class="flex items-center gap-2">
+ <h3>SMTP Server</h3>
+ <x-forms.button type="submit">
+ Save
+ </x-forms.button>
+ </div>
<div class="w-32">
- <x-forms.checkbox instantSave id="team.smtp_enabled" label="Enabled" />
+ <x-forms.checkbox instantSave="instantSaveSmtpEnabled" id="smtpEnabled" label="Enabled" />
</div>
- <form wire:submit='submit' class="flex flex-col">
+ <div class="flex flex-col">
<div class="flex flex-col gap-4">
<div class="flex flex-col w-full gap-2 xl:flex-row">
- <x-forms.input required id="team.smtp_host" placeholder="smtp.mailgun.org" label="Host" />
- <x-forms.input required id="team.smtp_port" placeholder="587" label="Port" />
- <x-forms.input id="team.smtp_encryption" helper="If SMTP uses SSL, set it to 'tls'."
+ <x-forms.input required id="smtpHost" placeholder="smtp.mailgun.org" label="Host" />
+ <x-forms.input required id="smtpPort" placeholder="587" label="Port" />
+ <x-forms.input id="smtpEncryption" helper="If SMTP uses SSL, set it to 'tls'."
placeholder="tls" label="Encryption" />
</div>
<div class="flex flex-col w-full gap-2 xl:flex-row">
- <x-forms.input id="team.smtp_username" label="SMTP Username" />
- <x-forms.input id="team.smtp_password" type="password" label="SMTP Password" />
- <x-forms.input id="team.smtp_timeout" helper="Timeout value for sending emails."
+ <x-forms.input id="smtpUsername" label="SMTP Username" />
+ <x-forms.input id="smtpPassword" type="password" label="SMTP Password" />
+ <x-forms.input id="smtpTimeout" helper="Timeout value for sending emails."
label="Timeout" />
</div>
</div>
- <div class="flex justify-end gap-4 pt-6">
- <x-forms.button type="submit">
- Save
- </x-forms.button>
- </div>
- </form>
- </div>
- <div class="p-4 border dark:border-coolgray-300">
- <h3>Resend</h3>
+ </div>
+ </form>
+ <form wire:submit='submit' class="p-4 border dark:border-coolgray-300 flex flex-col gap-2">
+ <div class="flex items-center gap-2">
+ <h3>Resend</h3>
+ <x-forms.button type="submit">
+ Save
+ </x-forms.button>
+ </div>
<div class="w-32">
- <x-forms.checkbox instantSave='instantSaveResend' id="team.resend_enabled" label="Enabled" />
+ <x-forms.checkbox instantSave='instantSaveResend' id="resendEnabled" label="Enabled" />
</div>
- <form wire:submit='submitResend' class="flex flex-col">
+ <div class="flex flex-col">
<div class="flex flex-col gap-4">
<div class="flex flex-col w-full gap-2 xl:flex-row">
- <x-forms.input required type="password" id="team.resend_api_key" placeholder="API key"
+ <x-forms.input required type="password" id="resendApiKey" placeholder="API key"
label="API Key" />
</div>
</div>
- <div class="flex justify-end gap-4 pt-6">
- <x-forms.button type="submit">
- Save
- </x-forms.button>
- </div>
- </form>
- </div>
+ </div>
+ </form>
</div>
@endif
- @if (isEmailEnabled($team) || data_get($team, 'use_instance_email_settings'))
+ @if (isEmailEnabled($team) || $useInstanceEmailSettings)
<h2 class="mt-4">Subscribe to events</h2>
<div class="w-64">
@if (isDev())
- <x-forms.checkbox instantSave="saveModel" id="team.smtp_notifications_test" label="Test" />
+ <x-forms.checkbox instantSave="saveModel" id="smtpNotificationsTest" label="Test" />
@endif
- <x-forms.checkbox instantSave="saveModel" id="team.smtp_notifications_status_changes"
+ <x-forms.checkbox instantSave="saveModel" id="smtpNotificationsStatusChanges"
label="Container Status Changes" />
- <x-forms.checkbox instantSave="saveModel" id="team.smtp_notifications_deployments"
+ <x-forms.checkbox instantSave="saveModel" id="smtpNotificationsDeployments"
label="Application Deployments" />
- <x-forms.checkbox instantSave="saveModel" id="team.smtp_notifications_database_backups"
- label="Backup Status" />
- <x-forms.checkbox instantSave="saveModel" id="team.smtp_notifications_scheduled_tasks"
+ <x-forms.checkbox instantSave="saveModel" id="smtpNotificationsDatabaseBackups" label="Backup Status" />
+ <x-forms.checkbox instantSave="saveModel" id="smtpNotificationsScheduledTasks"
label="Scheduled Tasks Status" />
- <x-forms.checkbox instantSave="saveModel" id="team.smtp_notifications_server_disk_usage"
- label="Server Disk Usage" />
+ <x-forms.checkbox instantSave="saveModel" id="smtpNotificationsServerDiskUsage" label="Server Disk Usage" />
</div>
@endif
</div>