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 @@ class DiscordSettings extends Component
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 @@ class EmailSettings extends Component
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\Enums\ProcessStatus;
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 @@ COPY --from=$this->build_image_name /app/{$this->application->publish_directory}
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 @@ class Project extends BaseModel
{
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() . ')';
}
}