Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/Events/Backup/BackupCompleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Events\Backup;

use App\Events\Event;
use App\Models\Backup;
use Illuminate\Queue\SerializesModels;

class BackupCompleted extends Event
{
use SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public Backup $backup, public bool $successful) {}
}
10 changes: 10 additions & 0 deletions app/Filament/Admin/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,16 @@ private function miscSettings(): array
->formatStateUsing(fn ($state): bool => (bool) $state)
->afterStateUpdated(fn ($state, Set $set) => $set('PANEL_SEND_REINSTALL_NOTIFICATION', (bool) $state))
->default(env('PANEL_SEND_REINSTALL_NOTIFICATION', config('panel.email.send_reinstall_notification'))),
Toggle::make('PANEL_SEND_BACKUP_NOTIFICATION')
->label(trans('admin/setting.misc.mail_notifications.backup_completed'))
->onIcon('tabler-check')
->offIcon('tabler-x')
->onColor('success')
->offColor('danger')
->live()
->formatStateUsing(fn ($state): bool => (bool) $state)
->afterStateUpdated(fn ($state, Set $set) => $set('PANEL_SEND_BACKUP_NOTIFICATION', (bool) $state))
->default(env('PANEL_SEND_BACKUP_NOTIFICATION', config('panel.email.send_backup_notification'))),
]),
Section::make(trans('admin/setting.misc.connections.title'))
->description(trans('admin/setting.misc.connections.helper'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Api\Remote\Backups;

use App\Events\Backup\BackupCompleted;
use App\Exceptions\DisplayException;
use App\Exceptions\Http\HttpForbiddenException;
use App\Extensions\Backups\BackupManager;
Expand Down Expand Up @@ -57,9 +58,9 @@ public function index(ReportBackupCompleteRequest $request, string $backup): Jso
$action = $request->boolean('successful') ? 'server:backup.complete' : 'server:backup.fail';
$log = Activity::event($action)->subject($model, $model->server)->property('name', $model->name);

$log->transaction(function () use ($model, $request) {
$successful = $request->boolean('successful');
$successful = $request->boolean('successful');

$log->transaction(function () use ($model, $request, $successful) {
$model->fill([
'is_successful' => $successful,
// Change the lock state to unlocked if this was a failed backup so that it can be
Expand All @@ -79,6 +80,8 @@ public function index(ReportBackupCompleteRequest $request, string $backup): Jso
}
});

event(new BackupCompleted($model, $successful));

return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}

Expand Down
39 changes: 39 additions & 0 deletions app/Listeners/Backup/BackupCompletedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Listeners\Backup;

use App\Events\Backup\BackupCompleted;
use App\Filament\Server\Resources\Backups\Pages\ListBackups;
use App\Notifications\BackupCompleted as BackupCompletedNotification;
use Filament\Actions\Action;
use Filament\Notifications\Notification;

class BackupCompletedListener
{
public function handle(BackupCompleted $event): void
{
$event->backup->loadMissing(['server', 'server.user']);

$user = $event->backup->server->user;
$locale = $user->language ?? 'en';

// Always send panel notification
Notification::make()
->status($event->successful ? 'success' : 'danger')
->title(trans('notifications.backup_' . ($event->successful ? 'completed' : 'failed'), locale: $locale))
->body(trans('notifications.backup_body', ['name' => $event->backup->name, 'server' => $event->backup->server->name], $locale))
->actions([
Action::make('view')
->button()
->label(trans('notifications.view_backups', locale: $locale))
->markAsRead()
->url(fn () => ListBackups::getUrl(panel: 'server', tenant: $event->backup->server)),
])
->sendToDatabase($user);

// Send email notification if enabled and backup was successful
if ($event->successful && config()->get('panel.email.send_backup_notification', true)) {
$user->notify(new BackupCompletedNotification($event->backup));
}
}
}
34 changes: 34 additions & 0 deletions app/Notifications/BackupCompleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Notifications;

use App\Filament\Server\Resources\Backups\Pages\ListBackups;
use App\Models\Backup;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class BackupCompleted extends Notification implements ShouldQueue
{
use Queueable;

public function __construct(public Backup $backup) {}

/** @return string[] */
public function via(): array
{
return ['mail'];
}

public function toMail(User $notifiable): MailMessage
{
return (new MailMessage())
->greeting('Hello ' . $notifiable->username . '.')
->line('Your backup has finished and is now ready.')
->line('Backup Name: ' . $this->backup->name)
->line('Server Name: ' . $this->backup->server->name)
->action('View Backups', ListBackups::getUrl(panel: 'server', tenant: $this->backup->server));
}
}
5 changes: 5 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Events\Backup\BackupCompleted;
use App\Listeners\Backup\BackupCompletedListener;
use App\Listeners\DispatchWebhooks;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

Expand All @@ -15,5 +17,8 @@ class EventServiceProvider extends ServiceProvider
'eloquent.created*' => [DispatchWebhooks::class],
'eloquent.deleted*' => [DispatchWebhooks::class],
'eloquent.updated*' => [DispatchWebhooks::class],
BackupCompleted::class => [
BackupCompletedListener::class,
],
];
}
2 changes: 2 additions & 0 deletions config/panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
'send_install_notification' => env('PANEL_SEND_INSTALL_NOTIFICATION', true),
// Should an email be sent to a server owner whenever their server is reinstalled?
'send_reinstall_notification' => env('PANEL_SEND_REINSTALL_NOTIFICATION', true),
// Should an email be sent to a server owner whenever their backup is completed?
'send_backup_notification' => env('PANEL_SEND_BACKUP_NOTIFICATION', true),
],

'filament' => [
Expand Down
1 change: 1 addition & 0 deletions lang/en/admin/setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
'helper' => 'Toggle which mail notifications should be sent to Users.',
'server_installed' => 'Server Installed',
'server_reinstalled' => 'Server Reinstalled',
'backup_completed' => 'Backup Completed',
],
'connections' => [
'title' => 'Connections',
Expand Down
4 changes: 4 additions & 0 deletions lang/en/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
'installation_failed' => 'Server Installation Failed',
'reinstallation_completed' => 'Server Reinstallation Completed',
'reinstallation_failed' => 'Server Reinstallation Failed',
'backup_completed' => 'Backup Completed',
'backup_failed' => 'Backup Failed',
'backup_body' => 'Backup ":name" for server ":server"',
'view_backups' => 'View Backups',
'failed' => 'Failed',
'user_added' => [
'title' => 'Added to Server',
Expand Down