subscribe to events

This commit is contained in:
Andras Bacsai 2023-06-19 14:31:42 +02:00
parent 631968ee5b
commit f7dd110a49
15 changed files with 155 additions and 25 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace App\Data;
use Spatie\LaravelData\Data;
class SmtpConfiguration extends Data
{
public function __construct(
public bool $smtp_active = false,
public string $smtp_host,
public int $smtp_port,
public ?string $smtp_encryption,
public ?string $smtp_username,
public ?string $smtp_password,
public ?int $smtp_timeout,
public string $smtp_from_address,
public string $smtp_from_name,
public ?string $smtp_recipients,
public ?string $smtp_test_recipients,
) {
}
}

View File

@ -15,11 +15,16 @@ class DiscordSettings extends Component
protected $rules = [
'model.extra_attributes.discord_active' => 'nullable|boolean',
'model.extra_attributes.discord_webhook' => 'required|url',
'model.extra_attributes.notifications_test' => 'nullable|boolean',
'model.extra_attributes.notifications_deployments' => 'nullable|boolean',
];
protected $validationAttributes = [
'model.extra_attributes.discord_webhook' => 'Discord Webhook',
];
public function instantSaveEvents()
{
$this->saveModel();
}
public function instantSave()
{
try {
@ -35,6 +40,7 @@ private function saveModel()
if (is_a($this->model, Team::class)) {
session(['currentTeam' => $this->model]);
}
$this->emit('success', 'Settings saved.');
}
public function submit()
{

View File

@ -24,6 +24,8 @@ 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_deployments' => 'nullable|boolean',
'model.extra_attributes.notifications_test' => 'nullable|boolean',
];
protected $validationAttributes = [
'model.extra_attributes.smtp_from_address' => 'From Address',
@ -66,11 +68,16 @@ private function saveModel()
if (is_a($this->model, Team::class)) {
session(['currentTeam' => $this->model]);
}
$this->emit('success', 'Settings saved.');
}
public function sendTestNotification()
{
Notification::send($this->model, new TestNotification);
}
public function instantSaveEvents()
{
$this->saveModel();
}
public function instantSave()
{
try {

View File

@ -9,12 +9,14 @@
use App\Models\Application;
use App\Models\ApplicationDeploymentQueue;
use App\Models\ApplicationPreview;
use App\Notifications\Notifications\ApplicationDeployedNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
use Spatie\Activitylog\Models\Activity;
use Symfony\Component\Yaml\Yaml;
@ -315,6 +317,7 @@ private function next(string $status)
dispatch(new InstanceProxyCheckJob());
}
queue_next_deployment($this->application);
Notification::send($this->application->environment->project->team, new ApplicationDeployedNotification($this->application, $this->deployment_uuid));
}
private function execute_in_builder(string $command)
{

View File

@ -26,7 +26,10 @@ static public function ownedByCurrentTeam()
{
return Project::whereTeamId(session('currentTeam')->id);
}
public function team()
{
return $this->belongsTo(Team::class);
}
public function environments()
{
return $this->hasMany(Environment::class);

View File

@ -11,20 +11,14 @@ class EmailChannel
public function send(SendsEmail $notifiable, Notification $notification): void
{
$this->bootConfigs($notifiable);
$is_test_notification = $notification instanceof \App\Notifications\Notifications\TestNotification;
if ($is_test_notification) {
$bcc = $notifiable->routeNotificationForEmail('smtp_test_recipients');
if (count($bcc) === 0) {
if ($notifiable instanceof \App\Models\Team) {
$bcc = $notifiable->members()->pluck('email')->toArray();
}
$bcc = $notifiable->routeNotificationForEmail('smtp_test_recipients');
if (count($bcc) === 0) {
if ($notifiable instanceof \App\Models\Team) {
$bcc = $notifiable->members()->pluck('email')->toArray();
}
} else {
$bcc = $notifiable->routeNotificationForEmail();
}
$mailMessage = $notification->toMail($notifiable);
Mail::send(
[],
[],

View File

@ -0,0 +1,67 @@
<?php
namespace App\Notifications\Notifications;
use App\Models\Application;
use App\Models\Team;
use App\Notifications\Channels\EmailChannel;
use App\Notifications\Channels\DiscordChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Str;
class ApplicationDeployedNotification extends Notification implements ShouldQueue
{
use Queueable;
public Application $application;
public string $application_name;
public string $deployment_uuid;
public string|null $deployment_url = null;
public string $project_uuid;
public string $environment_name;
public string $fqdn;
public function __construct(Application $application, string $deployment_uuid)
{
$this->application = $application;
$this->application_name = data_get($application, 'name');
$this->deployment_uuid = $deployment_uuid;
$this->project_uuid = data_get($application, 'environment.project.uuid');
$this->environment_name = data_get($application, 'environment.name');
$this->fqdn = data_get($application, 'fqdn');
if (Str::of($this->fqdn)->explode(',')->count() > 1) {
$this->fqdn = Str::of($this->fqdn)->explode(',')->first();
}
$this->deployment_url = base_url() . "/project/{$this->project_uuid}/{$this->environment_name}/application/{$this->application->uuid}/deployment/{$this->deployment_uuid}";
}
public function via(object $notifiable): array
{
$channels = [];
if ($notifiable->extra_attributes?->get('email_active') && $notifiable->extra_attributes?->get('notifications_deployments')) {
$channels[] = EmailChannel::class;
}
if ($notifiable->extra_attributes?->get('discord_active') && $notifiable->extra_attributes?->get('notifications_deployments')) {
$channels[] = DiscordChannel::class;
}
return $channels;
}
public function toMail(Team $team): MailMessage
{
$mail = new MailMessage();
$mail->subject("New version is deployed of {$this->application_name}");
$mail->view('emails.application-deployed', [
'name' => $this->application_name,
'fqdn' => $this->fqdn,
'url' => $this->deployment_url,
]);
return $mail;
}
public function toDiscord(): string
{
return '⚒️ A new version has been deployed of **' . $this->application_name . '**.
[Application Link](' . $this->fqdn . ') | [Deployment logs](' . $this->deployment_url . ')';
}
}

View File

@ -15,20 +15,26 @@ class TestNotification extends Notification implements ShouldQueue
public function via(object $notifiable): array
{
$channels = [];
$notifiable->extra_attributes?->get('smtp_active') && $channels[] = EmailChannel::class;
$notifiable->extra_attributes?->get('discord_active') && $channels[] = DiscordChannel::class;
if ($notifiable->extra_attributes?->get('email_active') && $notifiable->extra_attributes?->get('notifications_test')) {
$channels[] = EmailChannel::class;
}
if ($notifiable->extra_attributes?->get('discord_active') && $notifiable->extra_attributes?->get('notifications_test')) {
$channels[] = DiscordChannel::class;
}
return $channels;
}
public function toMail(): MailMessage
{
return (new MailMessage)
->subject('Coolify Test Notification')
->line('Congratulations!')
->line('You have successfully received a test Email notification from Coolify. 🥳');
$mail = new MailMessage();
$mail->subject("Coolify Test Notification");
$mail->view('emails.test');
return $mail;
}
public function toDiscord(): string
{
return 'You have successfully received a test Discord notification from Coolify. 🥳 [Go to your dashboard](' . base_url() . ')';
return 'This is a test Discord notification from Coolify.
[Go to your dashboard](' . base_url() . ')';
}
}

View File

@ -2,6 +2,7 @@
namespace Database\Seeders;
use App\Data\SmtpConfiguration;
use App\Models\InstanceSettings;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Process;
@ -16,14 +17,14 @@ public function run(): void
InstanceSettings::create([
'id' => 0,
'is_registration_enabled' => true,
'extra_attributes' => [
'extra_attributes' => SmtpConfiguration::from([
'smtp_active' => true,
'smtp_test_recipients' => 'test@example.com,test2@example.com',
'smtp_host' => 'coolify-mail',
'smtp_port' => 1025,
'smtp_from_address' => 'hi@localhost.com',
'smtp_from_name' => 'Coolify',
]
])
]);
try {
$ipv4 = Process::run('curl -4s https://ifconfig.io')->output();

View File

@ -35,5 +35,5 @@ class="w-4 h-4 stroke-current">
<div class="flex-1"></div>
<input type="checkbox" @if ($disabled !== null) disabled @endif name={{ $id }}
@if (!$noDirty) wire:dirty.class="input-warning" @endif
@if ($instantSave) wire:click='instantSave' wire:model.defer={{ $id }} @else wire:model.defer={{ $value ?? $id }} @endif />
@if ($instantSave) wire:click='{{ $instantSave === 'instantSave' || $instantSave == '1' ? 'instantSave' : $instantSave }}' wire:model.defer={{ $id }} @else wire:model.defer={{ $value ?? $id }} @endif />
</div>

View File

@ -0,0 +1,9 @@
<h4 class="mt-4">Subscribe to events</h4>
<div class="w-64 ">
@if (isDev())
<x-forms.checkbox instantSave="instantSaveEvents" id="model.extra_attributes.notifications_test"
label="Test Notifications" />
@endif
<x-forms.checkbox instantSave="instantSaveEvents" id="model.extra_attributes.notifications_deployments"
label="New Deployments" />
</div>

View File

@ -0,0 +1,7 @@
Hello,<br><br>
A new version of your application "{{ $name }}" has been deployed to <a target="_blank"
href="{{ $fqdn }}">{{ $fqdn }}</a><br><br>
Click the following link to view the deployment logs: <a target="_blank" href="{{ $url }}">View
Deployment</a><br><br>

View File

@ -1 +1,3 @@
Hello from test email. If you are seeing this, it means that your SMTP settings are working.
Hello,<br><br>
If you are seeing this, it means that your SMTP settings are correct.

View File

@ -12,11 +12,12 @@
</x-forms.button>
@endif
</div>
<div class="flex flex-col gap-2 xl:flex-row w-96">
<div class="w-48">
<x-forms.checkbox instantSave id="model.extra_attributes.discord_active" 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" />
</form>
<x-notification-subscription />
</div>

View File

@ -17,7 +17,7 @@
</x-forms.button>
@endif
</div>
<div class="flex flex-col">
<div class="w-48">
<x-forms.checkbox instantSave id="model.extra_attributes.smtp_active" label="Notification Enabled" />
</div>
@ -53,4 +53,5 @@
</div>
</div>
</form>
<x-notification-subscription />
</div>