refactor: 🚚 Rename pterodactyl application api accordingly

This commit is contained in:
IceToast 2023-04-04 01:16:39 +02:00
parent 1784358fc2
commit ce544019eb
No known key found for this signature in database
GPG key ID: 1464353E063A5B97
6 changed files with 40 additions and 61 deletions

View file

@ -24,19 +24,18 @@ class PterodactylClient
public PendingRequest $client; public PendingRequest $client;
public PendingRequest $client_admin; public PendingRequest $application;
public function __construct(PterodactylSettings $ptero_settings) public function __construct(PterodactylSettings $ptero_settings)
{ {
$server_settings = new ServerSettings(); $server_settings = new ServerSettings();
try { try {
$this->client = $this->client($ptero_settings); $this->client = $this->client($ptero_settings);
$this->client_admin = $this->clientAdmin($ptero_settings); $this->application = $this->clientAdmin($ptero_settings);
$this->per_page_limit = $ptero_settings->per_page_limit; $this->per_page_limit = $ptero_settings->per_page_limit;
$this->allocation_limit = $server_settings->allocation_limit; $this->allocation_limit = $server_settings->allocation_limit;
} } catch (Exception $exception) {
catch (Exception $exception) {
logger('Failed to construct Pterodactyl client, Settings table not available?', ['exception' => $exception]); logger('Failed to construct Pterodactyl client, Settings table not available?', ['exception' => $exception]);
} }
} }
@ -94,7 +93,7 @@ class PterodactylClient
public function getEggs(Nest $nest) public function getEggs(Nest $nest)
{ {
try { try {
$response = $this->client_admin->get("application/nests/{$nest->id}/eggs?include=nest,variables&per_page=" . $this->per_page_limit); $response = $this->application->get("application/nests/{$nest->id}/eggs?include=nest,variables&per_page=" . $this->per_page_limit);
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -113,7 +112,7 @@ class PterodactylClient
public function getNodes() public function getNodes()
{ {
try { try {
$response = $this->client_admin->get('application/nodes?per_page=' . $this->per_page_limit); $response = $this->application->get('application/nodes?per_page=' . $this->per_page_limit);
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -133,7 +132,7 @@ class PterodactylClient
public function getNode($id) public function getNode($id)
{ {
try { try {
$response = $this->client_admin->get('application/nodes/' . $id); $response = $this->application->get('application/nodes/' . $id);
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -147,7 +146,7 @@ class PterodactylClient
public function getServers() public function getServers()
{ {
try { try {
$response = $this->client_admin->get('application/servers?per_page=' . $this->per_page_limit); $response = $this->application->get('application/servers?per_page=' . $this->per_page_limit);
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -166,7 +165,7 @@ class PterodactylClient
public function getNests() public function getNests()
{ {
try { try {
$response = $this->client_admin->get('application/nests?per_page=' . $this->per_page_limit); $response = $this->application->get('application/nests?per_page=' . $this->per_page_limit);
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -185,7 +184,7 @@ class PterodactylClient
public function getLocations() public function getLocations()
{ {
try { try {
$response = $this->client_admin->get('application/locations?per_page=' . $this->per_page_limit); $response = $this->application->get('application/locations?per_page=' . $this->per_page_limit);
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -240,7 +239,7 @@ class PterodactylClient
public function getAllocations(Node $node) public function getAllocations(Node $node)
{ {
try { try {
$response = $this->client_admin->get("application/nodes/{$node->id}/allocations?per_page={$this->allocation_limit}"); $response = $this->application->get("application/nodes/{$node->id}/allocations?per_page={$this->allocation_limit}");
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -259,7 +258,7 @@ class PterodactylClient
*/ */
public function createServer(Server $server, Egg $egg, int $allocationId) public function createServer(Server $server, Egg $egg, int $allocationId)
{ {
return $this->client_admin->post('application/servers', [ return $this->application->post('application/servers', [
'name' => $server->name, 'name' => $server->name,
'external_id' => $server->id, 'external_id' => $server->id,
'user' => $server->user->pterodactyl_id, 'user' => $server->user->pterodactyl_id,
@ -288,7 +287,7 @@ class PterodactylClient
public function suspendServer(Server $server) public function suspendServer(Server $server)
{ {
try { try {
$response = $this->client_admin->post("application/servers/$server->pterodactyl_id/suspend"); $response = $this->application->post("application/servers/$server->pterodactyl_id/suspend");
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -302,7 +301,7 @@ class PterodactylClient
public function unSuspendServer(Server $server) public function unSuspendServer(Server $server)
{ {
try { try {
$response = $this->client_admin->post("application/servers/$server->pterodactyl_id/unsuspend"); $response = $this->application->post("application/servers/$server->pterodactyl_id/unsuspend");
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -322,7 +321,7 @@ class PterodactylClient
public function getUser(int $pterodactylId) public function getUser(int $pterodactylId)
{ {
try { try {
$response = $this->client_admin->get("application/users/{$pterodactylId}"); $response = $this->application->get("application/users/{$pterodactylId}");
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -342,7 +341,7 @@ class PterodactylClient
public function getServerAttributes(int $pterodactylId, bool $deleteOn404 = false) public function getServerAttributes(int $pterodactylId, bool $deleteOn404 = false)
{ {
try { try {
$response = $this->client_admin->get("application/servers/{$pterodactylId}?include=egg,node,nest,location"); $response = $this->application->get("application/servers/{$pterodactylId}?include=egg,node,nest,location");
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }
@ -371,7 +370,7 @@ class PterodactylClient
*/ */
public function updateServer(Server $server, Product $product) public function updateServer(Server $server, Product $product)
{ {
return $this->client_admin->patch("application/servers/{$server->pterodactyl_id}/build", [ return $this->application->patch("application/servers/{$server->pterodactyl_id}/build", [
'allocation' => $server->allocation, 'allocation' => $server->allocation,
'memory' => $product->memory, 'memory' => $product->memory,
'swap' => $product->swap, 'swap' => $product->swap,
@ -396,7 +395,7 @@ class PterodactylClient
*/ */
public function updateServerOwner(Server $server, int $userId) public function updateServerOwner(Server $server, int $userId)
{ {
return $this->client_admin->patch("application/servers/{$server->pterodactyl_id}/details", [ return $this->application->patch("application/servers/{$server->pterodactyl_id}/details", [
'name' => $server->name, 'name' => $server->name,
'user' => $userId, 'user' => $userId,
]); ]);
@ -435,7 +434,7 @@ class PterodactylClient
public function checkNodeResources(Node $node, int $requireMemory, int $requireDisk) public function checkNodeResources(Node $node, int $requireMemory, int $requireDisk)
{ {
try { try {
$response = $this->client_admin->get("application/nodes/{$node->id}"); $response = $this->application->get("application/nodes/{$node->id}");
} catch (Exception $e) { } catch (Exception $e) {
throw self::getException($e->getMessage()); throw self::getException($e->getMessage());
} }

View file

@ -43,7 +43,7 @@ class UserController extends Controller
{ {
$this->pterodactyl = new PterodactylClient($ptero_settings); $this->pterodactyl = new PterodactylClient($ptero_settings);
} }
/** /**
* Display a listing of the resource. * Display a listing of the resource.
* *
@ -105,7 +105,7 @@ class UserController extends Controller
//Update Users Password on Pterodactyl //Update Users Password on Pterodactyl
//Username,Mail,First and Lastname are required aswell //Username,Mail,First and Lastname are required aswell
$response = $this->pterodactyl->client_admin->patch('/application/users/' . $user->pterodactyl_id, [ $response = $this->pterodactyl->application->patch('/application/users/' . $user->pterodactyl_id, [
'username' => $request->name, 'username' => $request->name,
'first_name' => $request->name, 'first_name' => $request->name,
'last_name' => $request->name, 'last_name' => $request->name,
@ -280,7 +280,7 @@ class UserController extends Controller
'referral_code' => $this->createReferralCode(), 'referral_code' => $this->createReferralCode(),
]); ]);
$response = $this->pterodactyl->client_admin->post('/application/users', [ $response = $this->pterodactyl->application->post('/application/users', [
'external_id' => App::environment('local') ? Str::random(16) : (string) $user->id, 'external_id' => App::environment('local') ? Str::random(16) : (string) $user->id,
'username' => $user->name, 'username' => $user->name,
'email' => $user->email, 'email' => $user->email,

View file

@ -138,7 +138,7 @@ class RegisterController extends Controller
]); ]);
$response = $this->pterodactyl->client_admin->post('/application/users', [ $response = $this->pterodactyl->application->post('/application/users', [
'external_id' => App::environment('local') ? Str::random(16) : (string) $user->id, 'external_id' => App::environment('local') ? Str::random(16) : (string) $user->id,
'username' => $user->name, 'username' => $user->name,
'email' => $user->email, 'email' => $user->email,

View file

@ -78,15 +78,15 @@ class ProfileController extends Controller
$user = User::findOrFail($id); $user = User::findOrFail($id);
//update password if necessary //update password if necessary
if (! is_null($request->input('new_password'))) { if (!is_null($request->input('new_password'))) {
//validate password request //validate password request
$request->validate([ $request->validate([
'current_password' => [ 'current_password' => [
'required', 'required',
function ($attribute, $value, $fail) use ($user) { function ($attribute, $value, $fail) use ($user) {
if (! Hash::check($value, $user->password)) { if (!Hash::check($value, $user->password)) {
$fail('The '.$attribute.' is invalid.'); $fail('The ' . $attribute . ' is invalid.');
} }
}, },
], ],
@ -96,7 +96,7 @@ class ProfileController extends Controller
//Update Users Password on Pterodactyl //Update Users Password on Pterodactyl
//Username,Mail,First and Lastname are required aswell //Username,Mail,First and Lastname are required aswell
$response = $this->pterodactyl->client_admin->patch('/application/users/' . $user->pterodactyl_id, [ $response = $this->pterodactyl->application->patch('/application/users/' . $user->pterodactyl_id, [
'password' => $request->input('new_password'), 'password' => $request->input('new_password'),
'username' => $request->input('name'), 'username' => $request->input('name'),
'first_name' => $request->input('name'), 'first_name' => $request->input('name'),
@ -118,13 +118,13 @@ class ProfileController extends Controller
//validate request //validate request
$request->validate([ $request->validate([
'name' => 'required|min:4|max:30|alpha_num|unique:users,name,'.$id.',id', 'name' => 'required|min:4|max:30|alpha_num|unique:users,name,' . $id . ',id',
'email' => 'required|email|max:64|unique:users,email,'.$id.',id', 'email' => 'required|email|max:64|unique:users,email,' . $id . ',id',
'avatar' => 'nullable', 'avatar' => 'nullable',
]); ]);
//update avatar //update avatar
if (! is_null($request->input('avatar'))) { if (!is_null($request->input('avatar'))) {
$avatar = json_decode($request->input('avatar')); $avatar = json_decode($request->input('avatar'));
if ($avatar->input->size > 3000000) { if ($avatar->input->size > 3000000) {
abort(500); abort(500);
@ -140,7 +140,7 @@ class ProfileController extends Controller
} }
//update name and email on Pterodactyl //update name and email on Pterodactyl
$response = $this->pterodactyl->client_admin->patch('/application/users/' . $user->pterodactyl_id, [ $response = $this->pterodactyl->application->patch('/application/users/' . $user->pterodactyl_id, [
'username' => $request->input('name'), 'username' => $request->input('name'),
'first_name' => $request->input('name'), 'first_name' => $request->input('name'),
'last_name' => $request->input('name'), 'last_name' => $request->input('name'),

View file

@ -28,9 +28,9 @@ class Server extends Model
public function getActivitylogOptions(): LogOptions public function getActivitylogOptions(): LogOptions
{ {
return LogOptions::defaults() return LogOptions::defaults()
-> logOnlyDirty() ->logOnlyDirty()
-> logOnly(['*']) ->logOnly(['*'])
-> dontSubmitEmptyLogs(); ->dontSubmitEmptyLogs();
} }
/** /**
@ -84,8 +84,8 @@ class Server extends Model
}); });
static::deleting(function (Server $server) { static::deleting(function (Server $server) {
$response = $server->pterodactyl->client_admin->delete("/application/servers/{$server->pterodactyl_id}"); $response = $server->pterodactyl->application->delete("/application/servers/{$server->pterodactyl_id}");
if ($response->failed() && ! is_null($server->pterodactyl_id)) { if ($response->failed() && !is_null($server->pterodactyl_id)) {
//only return error when it's not a 404 error //only return error when it's not a 404 error
if ($response['errors'][0]['status'] != '404') { if ($response['errors'][0]['status'] != '404') {
throw new Exception($response['errors'][0]['code']); throw new Exception($response['errors'][0]['code']);
@ -99,7 +99,7 @@ class Server extends Model
*/ */
public function isSuspended() public function isSuspended()
{ {
return ! is_null($this->suspended); return !is_null($this->suspended);
} }
/** /**
@ -107,7 +107,7 @@ class Server extends Model
*/ */
public function getPterodactylServer() public function getPterodactylServer()
{ {
return $this->pterodactyl->client_admin->get("/application/servers/{$this->pterodactyl_id}"); return $this->pterodactyl->application->get("/application/servers/{$this->pterodactyl_id}");
} }
/** /**

View file

@ -122,7 +122,7 @@ class User extends Authenticatable implements MustVerifyEmail
$user->discordUser()->delete(); $user->discordUser()->delete();
$user->pterodactyl->client_admin->delete("/application/users/{$user->pterodactyl_id}"); $user->pterodactyl->application->delete("/application/users/{$user->pterodactyl_id}");
}); });
} }
@ -195,9 +195,6 @@ class User extends Authenticatable implements MustVerifyEmail
return $this->suspended; return $this->suspended;
} }
/**
* @throws Exception
*/
public function suspend() public function suspend()
{ {
foreach ($this->servers as $server) { foreach ($this->servers as $server) {
@ -211,9 +208,6 @@ class User extends Authenticatable implements MustVerifyEmail
return $this; return $this;
} }
/**
* @throws Exception
*/
public function unSuspend() public function unSuspend()
{ {
foreach ($this->getServersWithProduct() as $server) { foreach ($this->getServersWithProduct() as $server) {
@ -241,23 +235,9 @@ class User extends Authenticatable implements MustVerifyEmail
*/ */
public function getAvatar() public function getAvatar()
{ {
//TODO loading the images to confirm they exist is causing to much load time. alternative has to be found :) maybe onerror tag on the <img tags>
// if ($this->discordUser()->exists()) {
// if(@getimagesize($this->discordUser->getAvatar())) {
// $avatar = $this->discordUser->getAvatar();
// } else {
// $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
// }
// } else {
// $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
// }
return 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->email))); return 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->email)));
} }
/**
* @return string
*/
public function creditUsage() public function creditUsage()
{ {
$usage = 0; $usage = 0;