From 8e1a21563e05e5233c12196c41b18351d5614e54 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sun, 25 Sep 2022 13:16:58 -0600 Subject: [PATCH] server: add configuration for install notifications (#4331) * server: track `installed_at`, only send install notification on first install * server: add configuration for install notifications --- .../Servers/ServerInstallController.php | 13 +++++--- app/Models/Server.php | 6 ++-- config/pterodactyl.php | 14 ++++++++ ...d_installed_at_column_to_servers_table.php | 32 +++++++++++++++++++ 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 database/migrations/2022_08_16_230204_add_installed_at_column_to_servers_table.php diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php b/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php index 0e24f71be..1c82e6d88 100644 --- a/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php +++ b/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php @@ -2,6 +2,7 @@ namespace Pterodactyl\Http\Controllers\Api\Remote\Servers; +use Carbon\CarbonImmutable; use Illuminate\Http\Request; use Illuminate\Http\Response; use Pterodactyl\Models\Server; @@ -40,7 +41,7 @@ class ServerInstallController extends Controller * * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ - public function index(Request $request, string $uuid) + public function index(Request $request, string $uuid): JsonResponse { $server = $this->repository->getByUuid($uuid); $egg = $server->egg; @@ -60,7 +61,7 @@ class ServerInstallController extends Controller * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Model\DataValidationException */ - public function store(InstallationDataRequest $request, string $uuid) + public function store(InstallationDataRequest $request, string $uuid): JsonResponse { $server = $this->repository->getByUuid($uuid); @@ -69,10 +70,14 @@ class ServerInstallController extends Controller $status = Server::STATUS_SUSPENDED; } - $this->repository->update($server->id, ['status' => $status], true, true); + $this->repository->update($server->id, ['status' => $status, 'installed_at' => CarbonImmutable::now()], true, true); // If the server successfully installed, fire installed event. - if ($status === null) { + // This logic allows individually disabling install and reinstall notifications separately. + $isInitialInstall = is_null($server->installed_at); + if ($isInitialInstall && config()->get('pterodactyl.email.send_install_notification', true)) { + $this->eventDispatcher->dispatch(new ServerInstalled($server)); + } elseif (! $isInitialInstall && config()->get('pterodactyl.email.send_reinstall_notification', true)) { $this->eventDispatcher->dispatch(new ServerInstalled($server)); } diff --git a/app/Models/Server.php b/app/Models/Server.php index 3f67c396d..fa690d353 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -38,6 +38,7 @@ use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException; * @property int $backup_limit * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at + * @property \Illuminate\Support\Carbon|null $installed_at * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\ActivityLog[] $activity * @property int|null $activity_count * @property \Pterodactyl\Models\Allocation|null $allocation @@ -128,6 +129,7 @@ class Server extends Model protected $attributes = [ 'status' => self::STATUS_INSTALLING, 'oom_disabled' => true, + 'installed_at' => null, ]; /** @@ -142,14 +144,14 @@ class Server extends Model * * @var array */ - protected $dates = [self::CREATED_AT, self::UPDATED_AT, 'deleted_at']; + protected $dates = [self::CREATED_AT, self::UPDATED_AT, 'deleted_at', 'installed_at']; /** * Fields that are not mass assignable. * * @var array */ - protected $guarded = ['id', self::CREATED_AT, self::UPDATED_AT, 'deleted_at']; + protected $guarded = ['id', self::CREATED_AT, self::UPDATED_AT, 'deleted_at', 'installed_at']; /** * @var array diff --git a/config/pterodactyl.php b/config/pterodactyl.php index e21d1859f..5bca4a3eb 100644 --- a/config/pterodactyl.php +++ b/config/pterodactyl.php @@ -205,4 +205,18 @@ return [ 'assets' => [ 'use_hash' => env('PTERODACTYL_USE_ASSET_HASH', false), ], + + /* + |-------------------------------------------------------------------------- + | Email Notification Settings + |-------------------------------------------------------------------------- + | + | This section controls what notifications are sent to users. + */ + 'email' => [ + // Should an email be sent to a server owner once their server has completed it's first install process? + 'send_install_notification' => env('PTERODACTYL_SEND_INSTALL_NOTIFICATION', true), + // Should an email be sent to a server owner whenever their server is reinstalled? + 'send_reinstall_notification' => env('PTERODACTYL_SEND_REINSTALL_NOTIFICATION', true), + ], ]; diff --git a/database/migrations/2022_08_16_230204_add_installed_at_column_to_servers_table.php b/database/migrations/2022_08_16_230204_add_installed_at_column_to_servers_table.php new file mode 100644 index 000000000..50a5e23f8 --- /dev/null +++ b/database/migrations/2022_08_16_230204_add_installed_at_column_to_servers_table.php @@ -0,0 +1,32 @@ +timestamp('installed_at')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('servers', function (Blueprint $table) { + $table->dropColumn('installed_at'); + }); + } +}