This commit is contained in:
Andras Bacsai 2023-06-01 13:24:20 +02:00
parent 0aa816b4f2
commit 6e094eaf42
10 changed files with 65 additions and 64 deletions

View File

@ -13,11 +13,11 @@ class DiscordSettings extends Component
public Team|Server $model;
protected $rules = [
'model.smtp_attributes.discord_active' => 'nullable|boolean',
'model.smtp_attributes.discord_webhook' => 'required|url',
'model.extra_attributes.discord_active' => 'nullable|boolean',
'model.extra_attributes.discord_webhook' => 'required|url',
];
protected $validationAttributes = [
'model.smtp_attributes.discord_webhook' => 'Discord Webhook',
'model.extra_attributes.discord_webhook' => 'Discord Webhook',
];
public function mount($model)
{
@ -28,8 +28,8 @@ public function instantSave()
try {
$this->submit();
} catch (\Exception $e) {
$this->model->smtp_attributes->discord_active = false;
$this->addError('model.smtp_attributes.discord_webhook', $e->getMessage());
$this->model->extra_attributes->discord_active = false;
$this->validate();
}
}
private function saveModel()

View File

@ -13,27 +13,27 @@ class EmailSettings extends Component
public Team|Server $model;
protected $rules = [
'model.smtp_attributes.smtp_active' => 'nullable|boolean',
'model.smtp_attributes.from_address' => 'required',
'model.smtp_attributes.from_name' => 'required',
'model.smtp_attributes.recipients' => 'required',
'model.smtp_attributes.smtp_host' => 'required',
'model.smtp_attributes.smtp_port' => 'required',
'model.smtp_attributes.smtp_encryption' => 'nullable',
'model.smtp_attributes.smtp_username' => 'nullable',
'model.smtp_attributes.smtp_password' => 'nullable',
'model.smtp_attributes.smtp_timeout' => 'nullable',
'model.smtp_attributes.test_address' => 'nullable',
'model.extra_attributes.smtp_active' => 'nullable|boolean',
'model.extra_attributes.from_address' => 'required|email',
'model.extra_attributes.from_name' => 'required',
'model.extra_attributes.recipients' => 'required',
'model.extra_attributes.smtp_host' => 'required',
'model.extra_attributes.smtp_port' => 'required',
'model.extra_attributes.smtp_encryption' => 'nullable',
'model.extra_attributes.smtp_username' => 'nullable',
'model.extra_attributes.smtp_password' => 'nullable',
'model.extra_attributes.smtp_timeout' => 'nullable',
'model.extra_attributes.test_notification_email' => 'nullable|email',
];
protected $validationAttributes = [
'model.smtp_attributes.from_address' => 'From Address',
'model.smtp_attributes.from_name' => 'From Name',
'model.smtp_attributes.recipients' => 'Recipients',
'model.smtp_attributes.smtp_host' => 'Host',
'model.smtp_attributes.smtp_port' => 'Port',
'model.smtp_attributes.smtp_encryption' => 'Encryption',
'model.smtp_attributes.smtp_username' => 'Username',
'model.smtp_attributes.smtp_password' => 'Password',
'model.extra_attributes.from_address' => 'From Address',
'model.extra_attributes.from_name' => 'From Name',
'model.extra_attributes.recipients' => 'Recipients',
'model.extra_attributes.smtp_host' => 'Host',
'model.extra_attributes.smtp_port' => 'Port',
'model.extra_attributes.smtp_encryption' => 'Encryption',
'model.extra_attributes.smtp_username' => 'Username',
'model.extra_attributes.smtp_password' => 'Password',
];
public function mount($model)
{
@ -54,6 +54,11 @@ private function saveModel()
}
public function instantSave()
{
$this->saveModel();
try {
$this->submit();
} catch (\Exception $e) {
$this->model->extra_attributes->smtp_active = false;
$this->validate();
}
}
}

View File

@ -23,22 +23,16 @@ protected static function booted()
'team_id',
'private_key_id',
'extra_attributes',
'smtp_attributes',
];
public $casts = [
'extra_attributes' => SchemalessAttributes::class,
'smtp_attributes' => SchemalessAttributes::class,
];
public function scopeWithExtraAttributes(): Builder
{
return $this->extra_attributes->modelScope();
}
public function scopeWithSmtpAttributes(): Builder
{
return $this->smtp_attributes->modelScope();
}
public function standaloneDockers()
{

View File

@ -13,30 +13,32 @@ class Team extends BaseModel implements SendsDiscord, SendsEmail
use Notifiable;
protected $casts = [
'smtp_attributes' => SchemalessAttributes::class,
'extra_attributes' => SchemalessAttributes::class,
'personal_team' => 'boolean',
];
protected $fillable = [
'id',
'name',
'personal_team',
'smtp_attributes',
'extra_attributes',
];
public function routeNotificationForDiscord()
{
return $this->smtp_attributes->get('discord_webhook');
return $this->extra_attributes->get('discord_webhook');
}
public function routeNotificationForEmail(string $attribute = 'recipients')
{
$recipients = $this->smtp_attributes->get($attribute, '');
return explode(PHP_EOL, $recipients);
$recipients = $this->extra_attributes->get($attribute, '');
if (is_null($recipients) || $recipients === '') {
return [];
}
return explode(',', $recipients);
}
public function scopeWithExtraAttributes(): Builder
{
return $this->smtp_attributes->modelScope();
return $this->extra_attributes->modelScope();
}
public function projects()

View File

@ -15,8 +15,8 @@ public function send(SendsEmail $notifiable, Notification $notification): void
{
$this->bootConfigs($notifiable);
if ($notification instanceof \App\Notifications\TestNotification) {
$bcc = $notifiable->routeNotificationForEmail('test_address');
if (count($bcc) === 1) {
$bcc = $notifiable->routeNotificationForEmail('test_notification_email');
if (count($bcc) === 0) {
$bcc = $notifiable->routeNotificationForEmail();
}
} else {
@ -29,8 +29,8 @@ public function send(SendsEmail $notifiable, Notification $notification): void
[],
fn (Message $message) => $message
->from(
$notifiable->smtp_attributes?->get('from_address'),
$notifiable->smtp_attributes?->get('from_name')
$notifiable->extra_attributes?->get('from_address'),
$notifiable->extra_attributes?->get('from_name')
)
->cc($bcc)
->bcc($bcc)
@ -44,12 +44,12 @@ private function bootConfigs($notifiable): void
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
"host" => $notifiable->smtp_attributes?->get('smtp_host'),
"port" => $notifiable->smtp_attributes?->get('smtp_port'),
"encryption" => $notifiable->smtp_attributes?->get('smtp_encryption'),
"username" => $notifiable->smtp_attributes?->get('smtp_username'),
"password" => $notifiable->smtp_attributes?->get('smtp_password'),
"timeout" => $notifiable->smtp_attributes?->get('smtp_timeout'),
"host" => $notifiable->extra_attributes?->get('smtp_host'),
"port" => $notifiable->extra_attributes?->get('smtp_port'),
"encryption" => $notifiable->extra_attributes?->get('smtp_encryption'),
"username" => $notifiable->extra_attributes?->get('smtp_username'),
"password" => $notifiable->extra_attributes?->get('smtp_password'),
"timeout" => $notifiable->extra_attributes?->get('smtp_timeout'),
"local_domain" => null,
]);
}

View File

@ -29,8 +29,8 @@ public function __construct()
public function via(object $notifiable): array
{
$channels = [];
$notifiable->smtp_attributes?->get('smtp_active') && $channels[] = EmailChannel::class;
$notifiable->smtp_attributes?->get('discord_active') && $channels[] = DiscordChannel::class;
$notifiable->extra_attributes?->get('smtp_active') && $channels[] = EmailChannel::class;
$notifiable->extra_attributes?->get('discord_active') && $channels[] = DiscordChannel::class;
return $channels;
}

View File

@ -16,7 +16,7 @@ public function up(): void
$table->string('uuid')->unique();
$table->string('name');
$table->boolean('personal_team')->default(false);
$table->schemalessAttributes('smtp_attributes');
$table->schemalessAttributes('extra_attributes');
$table->timestamps();
});
}

View File

@ -22,7 +22,6 @@ public function up(): void
$table->foreignId('team_id');
$table->foreignId('private_key_id');
$table->schemalessAttributes('extra_attributes');
$table->schemalessAttributes('smtp_attributes');
$table->timestamps();
});
}

View File

@ -7,10 +7,10 @@
</x-forms.button>
</div>
<div class="flex flex-col gap-2 xl:flex-row w-96">
<x-forms.checkbox instantSave id="model.smtp_attributes.discord_active" label="Notification Enabled" />
<x-forms.checkbox instantSave id="model.extra_attributes.discord_active" label="Notification Enabled" />
</div>
<div class="flex flex-col gap-2 xl:flex-row w-96">
<x-forms.input required id="model.smtp_attributes.discord_webhook" label="Webhook" />
<x-forms.input required id="model.extra_attributes.discord_webhook" label="Webhook" />
</div>
<div>

View File

@ -7,28 +7,29 @@
</x-forms.button>
</div>
<div class="flex flex-col w-96">
<x-forms.checkbox instantSave id="model.smtp_attributes.smtp_active" label="Notification Enabled" />
<x-forms.checkbox instantSave id="model.extra_attributes.smtp_active" label="Notification Enabled" />
</div>
<x-forms.input id="model.extra_attributes.test_notification_email" label="Test Notification Email" />
<div class="flex flex-col gap-2 xl:flex-row">
<div class="flex flex-col w-96">
<x-forms.textarea required id="model.smtp_attributes.recipients" helper="E-mails, one per line"
<x-forms.input required id="model.extra_attributes.recipients" helper="Emails separated by comma"
label="Recipients" />
</div>
</div>
<div class="flex flex-col gap-2 xl:flex-row">
<div class="flex flex-col w-96">
<x-forms.input required id="model.smtp_attributes.smtp_host" label="Host" />
<x-forms.input required id="model.smtp_attributes.smtp_port" label="Port" />
<x-forms.input id="model.smtp_attributes.smtp_encryption" label="Encryption" />
<x-forms.input required id="model.extra_attributes.smtp_host" label="Host" />
<x-forms.input required id="model.extra_attributes.smtp_port" label="Port" />
<x-forms.input id="model.extra_attributes.smtp_encryption" label="Encryption" />
</div>
<div class="flex flex-col w-96">
<x-forms.input id="model.smtp_attributes.smtp_username" label="Username" />
<x-forms.input id="model.smtp_attributes.smtp_password" label="Password" />
<x-forms.input id="model.smtp_attributes.smtp_timeout" label="Timeout" />
<x-forms.input id="model.extra_attributes.smtp_username" label="Username" />
<x-forms.input id="model.extra_attributes.smtp_password" label="Password" />
<x-forms.input id="model.extra_attributes.smtp_timeout" label="Timeout" />
</div>
<div class="flex flex-col w-96">
<x-forms.input required id="model.smtp_attributes.from_address" label="From Address" />
<x-forms.input required id="model.smtp_attributes.from_name" label="From Name" />
<x-forms.input required id="model.extra_attributes.from_address" label="From Address" />
<x-forms.input required id="model.extra_attributes.from_name" label="From Name" />
</div>
</div>
</form>