Skip to content

Commit 1ffb5ac

Browse files
stanjgDaneEveritt
authored andcommitted
Send an email when a server is marked as installed (pterodactyl#1213)
Co-authored-by: @stanjg
1 parent c42605e commit 1ffb5ac

File tree

7 files changed

+141
-1
lines changed

7 files changed

+141
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1616
* Fixed `pterodactyl.environment_variables` to be used correctly for global environment variables. The wrong config variable name was being using previously.
1717
* Fixes tokens being sent to users when their account is created to actually work. Implements Laravel's internal token creation mechanisms rather than trying to do it custom.
1818
* Updates some eggs to ensure they have the correct data and will continue working down the road. Fixes autoupdating on some source servers and MC related download links.
19+
* Emails should send properly now when a server is marked as installed to let the owner know it is ready for action.
1920

2021
### Changed
2122
* Attempting to upload a folder via the web file manager will now display a warning telling the user to use SFTP.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Pterodactyl\Contracts\Core;
4+
5+
use Pterodactyl\Events\Event;
6+
7+
interface ReceivesEvents
8+
{
9+
/**
10+
* Handles receiving an event from the application.
11+
*
12+
* @param \Pterodactyl\Events\Event $notification
13+
*/
14+
public function handle(Event $notification): void;
15+
}

app/Events/Server/Installed.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Pterodactyl\Events\Server;
4+
5+
use Pterodactyl\Events\Event;
6+
use Pterodactyl\Models\Server;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
class Installed extends Event
10+
{
11+
use SerializesModels;
12+
13+
/**
14+
* @var \Pterodactyl\Models\Server
15+
*/
16+
public $server;
17+
18+
/**
19+
* Create a new event instance.
20+
*
21+
* @var \Pterodactyl\Models\Server
22+
*/
23+
public function __construct(Server $server)
24+
{
25+
$this->server = $server;
26+
}
27+
}

app/Http/Controllers/Daemon/ActionController.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,26 @@
77
use Pterodactyl\Models\Node;
88
use Pterodactyl\Models\Server;
99
use Pterodactyl\Http\Controllers\Controller;
10+
use Pterodactyl\Events\Server\Installed as ServerInstalled;
11+
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
1012

1113
class ActionController extends Controller
1214
{
15+
/**
16+
* @var \Illuminate\Contracts\Events\Dispatcher
17+
*/
18+
private $eventDispatcher;
19+
20+
/**
21+
* ActionController constructor.
22+
*
23+
* @param \Illuminate\Contracts\Events\Dispatcher $eventDispatcher
24+
*/
25+
public function __construct(EventDispatcher $eventDispatcher)
26+
{
27+
$this->eventDispatcher = $eventDispatcher;
28+
}
29+
1330
/**
1431
* Handles install toggle request from daemon.
1532
*
@@ -37,6 +54,11 @@ public function markInstall(Request $request)
3754
$server->installed = ($status === 'installed') ? 1 : 2;
3855
$server->save();
3956

57+
// Only fire event if server installed successfully.
58+
if ($server->installed === 1) {
59+
$this->eventDispatcher->dispatch(new ServerInstalled($server));
60+
}
61+
4062
return response()->json([]);
4163
}
4264

app/Listeners/.gitkeep

Whitespace-only changes.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Pterodactyl\Notifications;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Pterodactyl\Events\Event;
7+
use Illuminate\Container\Container;
8+
use Illuminate\Notifications\Notification;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Pterodactyl\Contracts\Core\ReceivesEvents;
11+
use Illuminate\Contracts\Notifications\Dispatcher;
12+
use Illuminate\Notifications\Messages\MailMessage;
13+
14+
class ServerInstalled extends Notification implements ShouldQueue, ReceivesEvents
15+
{
16+
use Queueable;
17+
18+
/**
19+
* @var \Pterodactyl\Models\Server
20+
*/
21+
public $server;
22+
23+
/**
24+
* @var \Pterodactyl\Models\User
25+
*/
26+
public $user;
27+
28+
/**
29+
* Handle a direct call to this notification from the server installed event. This is configured
30+
* in the event service provider.
31+
*
32+
* @param \Pterodactyl\Events\Event|\Pterodactyl\Events\Server\Installed $event
33+
*/
34+
public function handle(Event $event): void
35+
{
36+
$event->server->loadMissing('user');
37+
38+
$this->server = $event->server;
39+
$this->user = $event->server->user;
40+
41+
// Since we are calling this notification directly from an event listener we need to fire off the dispatcher
42+
// to send the email now. Don't use send() or you'll end up firing off two different events.
43+
Container::getInstance()->make(Dispatcher::class)->sendNow($this->user, $this);
44+
}
45+
46+
/**
47+
* Get the notification's delivery channels.
48+
*
49+
* @return array
50+
*/
51+
public function via()
52+
{
53+
return ['mail'];
54+
}
55+
56+
/**
57+
* Get the mail representation of the notification.
58+
*
59+
* @return \Illuminate\Notifications\Messages\MailMessage
60+
*/
61+
public function toMail()
62+
{
63+
return (new MailMessage)
64+
->greeting('Hello ' . $this->user->username . '.')
65+
->line('Your server has finished installing and is now ready for you to use.')
66+
->line('Server Name: ' . $this->server->name)
67+
->action('Login and Begin Using', route('index'));
68+
}
69+
}

app/Providers/EventServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Pterodactyl\Providers;
44

5+
use Pterodactyl\Events\Server\Installed as ServerInstalledEvent;
6+
use Pterodactyl\Notifications\ServerInstalled as ServerInstalledNotification;
57
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
68

79
class EventServiceProvider extends ServiceProvider
@@ -11,5 +13,9 @@ class EventServiceProvider extends ServiceProvider
1113
*
1214
* @var array
1315
*/
14-
protected $listen = [];
16+
protected $listen = [
17+
ServerInstalledEvent::class => [
18+
ServerInstalledNotification::class,
19+
],
20+
];
1521
}

0 commit comments

Comments
 (0)