Skip to content

Commit 8e1a215

Browse files
authored
server: add configuration for install notifications (pterodactyl#4331)
* server: track `installed_at`, only send install notification on first install * server: add configuration for install notifications
1 parent 23124c9 commit 8e1a215

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
44

5+
use Carbon\CarbonImmutable;
56
use Illuminate\Http\Request;
67
use Illuminate\Http\Response;
78
use Pterodactyl\Models\Server;
@@ -40,7 +41,7 @@ public function __construct(ServerRepository $repository, EventDispatcher $event
4041
*
4142
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
4243
*/
43-
public function index(Request $request, string $uuid)
44+
public function index(Request $request, string $uuid): JsonResponse
4445
{
4546
$server = $this->repository->getByUuid($uuid);
4647
$egg = $server->egg;
@@ -60,7 +61,7 @@ public function index(Request $request, string $uuid)
6061
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
6162
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
6263
*/
63-
public function store(InstallationDataRequest $request, string $uuid)
64+
public function store(InstallationDataRequest $request, string $uuid): JsonResponse
6465
{
6566
$server = $this->repository->getByUuid($uuid);
6667

@@ -69,10 +70,14 @@ public function store(InstallationDataRequest $request, string $uuid)
6970
$status = Server::STATUS_SUSPENDED;
7071
}
7172

72-
$this->repository->update($server->id, ['status' => $status], true, true);
73+
$this->repository->update($server->id, ['status' => $status, 'installed_at' => CarbonImmutable::now()], true, true);
7374

7475
// If the server successfully installed, fire installed event.
75-
if ($status === null) {
76+
// This logic allows individually disabling install and reinstall notifications separately.
77+
$isInitialInstall = is_null($server->installed_at);
78+
if ($isInitialInstall && config()->get('pterodactyl.email.send_install_notification', true)) {
79+
$this->eventDispatcher->dispatch(new ServerInstalled($server));
80+
} elseif (! $isInitialInstall && config()->get('pterodactyl.email.send_reinstall_notification', true)) {
7681
$this->eventDispatcher->dispatch(new ServerInstalled($server));
7782
}
7883

app/Models/Server.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @property int $backup_limit
3939
* @property \Illuminate\Support\Carbon|null $created_at
4040
* @property \Illuminate\Support\Carbon|null $updated_at
41+
* @property \Illuminate\Support\Carbon|null $installed_at
4142
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\ActivityLog[] $activity
4243
* @property int|null $activity_count
4344
* @property \Pterodactyl\Models\Allocation|null $allocation
@@ -128,6 +129,7 @@ class Server extends Model
128129
protected $attributes = [
129130
'status' => self::STATUS_INSTALLING,
130131
'oom_disabled' => true,
132+
'installed_at' => null,
131133
];
132134

133135
/**
@@ -142,14 +144,14 @@ class Server extends Model
142144
*
143145
* @var array
144146
*/
145-
protected $dates = [self::CREATED_AT, self::UPDATED_AT, 'deleted_at'];
147+
protected $dates = [self::CREATED_AT, self::UPDATED_AT, 'deleted_at', 'installed_at'];
146148

147149
/**
148150
* Fields that are not mass assignable.
149151
*
150152
* @var array
151153
*/
152-
protected $guarded = ['id', self::CREATED_AT, self::UPDATED_AT, 'deleted_at'];
154+
protected $guarded = ['id', self::CREATED_AT, self::UPDATED_AT, 'deleted_at', 'installed_at'];
153155

154156
/**
155157
* @var array

config/pterodactyl.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,18 @@
205205
'assets' => [
206206
'use_hash' => env('PTERODACTYL_USE_ASSET_HASH', false),
207207
],
208+
209+
/*
210+
|--------------------------------------------------------------------------
211+
| Email Notification Settings
212+
|--------------------------------------------------------------------------
213+
|
214+
| This section controls what notifications are sent to users.
215+
*/
216+
'email' => [
217+
// Should an email be sent to a server owner once their server has completed it's first install process?
218+
'send_install_notification' => env('PTERODACTYL_SEND_INSTALL_NOTIFICATION', true),
219+
// Should an email be sent to a server owner whenever their server is reinstalled?
220+
'send_reinstall_notification' => env('PTERODACTYL_SEND_REINSTALL_NOTIFICATION', true),
221+
],
208222
];
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddInstalledAtColumnToServersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('servers', function (Blueprint $table) {
17+
$table->timestamp('installed_at')->nullable();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('servers', function (Blueprint $table) {
29+
$table->dropColumn('installed_at');
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)