forked from pelican-dev/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DatabaseHost webhook configuration and enhance settings
- Introduced new `DATABASE_HOST_WEBHOOK` setting for JSON webhooks and `DATABASE_HOST_WEBHOOK_DISCORD` for Discord webhooks in the `Settings.php` file. - Added `DATABASE_HOST_WEBHOOK` and `DATABASE_HOST_WEBHOOK_DISCORD` fields to manage webhooks related to DatabaseHost events. - Implemented `getDatabaseHostHintAction()` method in `Settings.php` to customize webhook payloads for DatabaseHost-related events. - Registered `DatabaseHostWebhookListener` for handling DatabaseHost-related events and added `DatabaseHostObserver` for observing changes to the DatabaseHost model. - Updated `EventServiceProvider` to include event listeners for DatabaseHost events. - Enhanced the `SendWebhook` trait to include support for `DATABASE_HOST_WEBHOOK` and `DATABASE_HOST_WEBHOOK_DISCORD` based on the webhook type. These changes introduce flexible webhook configurations for DatabaseHost-related events and improve the overall management of webhook settings.
- Loading branch information
1 parent
1dc31a8
commit c0aca8e
Showing
7 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Events\DatabaseHost; | ||
|
||
use App\Models\DatabaseHost; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class Created | ||
{ | ||
use SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
*/ | ||
public function __construct(public DatabaseHost $databaseHost) | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Events\DatabaseHost; | ||
|
||
use App\Models\DatabaseHost; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class Deleted | ||
{ | ||
use SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
*/ | ||
public function __construct(public DatabaseHost $databaseHost) | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
|
||
namespace App\Listeners\Webhook; | ||
|
||
use App\Events\DatabaseHost as DatabaseHostEvents; | ||
use App\Traits\SendWebhook; | ||
use Carbon\Carbon; | ||
|
||
class DatabaseHostWebhookListener | ||
{ | ||
use SendWebhook; | ||
|
||
/** | ||
* Create the event listener. | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the event. | ||
*/ | ||
public function handle(object $event): void | ||
{ | ||
if ($event instanceof DatabaseHostEvents\Created) { | ||
$this->handleDatabaseHostCreated($event); | ||
} elseif ($event instanceof DatabaseHostEvents\Deleted) { | ||
$this->handleDatabaseHostDeleted($event); | ||
} | ||
} | ||
|
||
protected function handleDatabaseHostCreated($event) | ||
{ | ||
$settings = $this->getDatabaseHostSettings(); | ||
$ID = $event->egg->id; | ||
$admin = auth()->check() ? auth()->user()->username : 'Unknown'; | ||
$appName = env('APP_NAME'); | ||
$APP_URL = env('APP_URL'); | ||
$Url = "$APP_URL/admin/eggs/$ID/edit"; | ||
$currentTime = Carbon::now()->toDateTimeString(); | ||
$extra = "\nAdded by: $admin"; | ||
|
||
$data = $this->getDatabaseHostData($event); | ||
|
||
$message = []; | ||
foreach ($settings as $key => $isEnabled) { | ||
if ($isEnabled && array_key_exists($key, $data)) { | ||
$message[ucwords(str_replace('_', ' ', $key))] = $data[$key]; | ||
} | ||
} | ||
$message['Added by'] = $admin; | ||
|
||
if (env('WEBHOOK_TYPE') === 'json') { | ||
$this->send( | ||
'egg', | ||
[ | ||
'event' => 'DatabaseHost Added', | ||
'triggered_at' => $currentTime, | ||
'data' => $message, | ||
] | ||
); | ||
} elseif (env('WEBHOOK_TYPE') === 'discord') { | ||
$embed = [ | ||
[ | ||
'author' => [ | ||
'name' => $appName, | ||
'url' => $Url, | ||
], | ||
'title' => 'DatabaseHost Added', | ||
'description' => $this->buildDiscordDescription($settings, $data, $extra), | ||
'footer' => [ | ||
'text' => "Current Time: $currentTime", | ||
], | ||
], | ||
]; | ||
|
||
$this->send('databasehost', [ | ||
'embeds' => $embed, | ||
]); | ||
} | ||
} | ||
|
||
protected function handleDatabaseHostDeleted($event) | ||
{ | ||
$settings = $this->getDatabaseHostSettings(); | ||
$admin = auth()->check() ? auth()->user()->username : 'Unknown'; | ||
$appName = env('APP_NAME'); | ||
$Url = env('APP_URL'); | ||
$currentTime = Carbon::now()->toDateTimeString(); | ||
$extra = "\nDeleted by: $admin"; | ||
|
||
$data = $this->getDatabaseHostData($event); | ||
|
||
$message = []; | ||
foreach ($settings as $key => $isEnabled) { | ||
if ($isEnabled && array_key_exists($key, $data)) { | ||
$message[ucwords(str_replace('_', ' ', $key))] = $data[$key]; | ||
} | ||
} | ||
$message['Deleted by'] = $admin; | ||
|
||
if (env('WEBHOOK_TYPE') === 'json') { | ||
$this->send( | ||
'egg', | ||
[ | ||
'event' => 'DatabaseHost Deleted', | ||
'triggered_at' => $currentTime, | ||
'data' => $message, | ||
] | ||
); | ||
} elseif (env('WEBHOOK_TYPE') === 'discord') { | ||
$embed = [ | ||
[ | ||
'author' => [ | ||
'name' => $appName, | ||
'url' => $Url, | ||
], | ||
'title' => 'DatabaseHost Deleted', | ||
'description' => $this->buildDiscordDescription($settings, $data, $extra), | ||
'footer' => [ | ||
'text' => "Current Time: $currentTime", | ||
], | ||
], | ||
]; | ||
|
||
$this->send('databasehost', [ | ||
'embeds' => $embed, | ||
]); | ||
} | ||
} | ||
|
||
private function getDatabaseHostSettings(): array | ||
{ | ||
return [ | ||
'id' => env('DATABASE_HOST_ID', true) === true, | ||
'name' => env('DATABASE_HOST_NAME', true) === true, | ||
'host' => env('DATABASE_HOST_HOST', true) === true, | ||
'port' => env('DATABASE_HOST_PORT', true) === true, | ||
'max_databases' => env('DATABASE_HOST_MAX_DB', true) === true, | ||
'node_id' => env('DATABASE_HOST_NODE_ID', true) === true, | ||
]; | ||
} | ||
|
||
private function getDatabaseHostData($event): array | ||
{ | ||
return [ | ||
'id' => $event->databaseHost->id, | ||
'name' => $event->databaseHost->name, | ||
'host' => $event->databaseHost->host, | ||
'port' => $event->databaseHost->port, | ||
'max_databases' => $event->databaseHost->max_databases, | ||
'node_id' => $event->databaseHost->node_id, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App\Observers; | ||
|
||
use App\Models\DatabaseHost; | ||
use App\Events\DatabaseHost as DatabaseHostEvents; | ||
|
||
class DatabaseHostObserver | ||
{ | ||
/** | ||
* Handle the DatabaseHost "created" event. | ||
*/ | ||
public function created(DatabaseHost $databaseHost): void | ||
{ | ||
event(new DatabaseHostEvents\Created($databaseHost)); | ||
} | ||
|
||
/** | ||
* Handle the DatabaseHost "updated" event. | ||
*/ | ||
public function updated(DatabaseHost $databaseHost): void | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the DatabaseHost "deleted" event. | ||
*/ | ||
public function deleted(DatabaseHost $databaseHost): void | ||
{ | ||
event(new DatabaseHostEvents\Deleted($databaseHost)); | ||
} | ||
|
||
/** | ||
* Handle the DatabaseHost "restored" event. | ||
*/ | ||
public function restored(DatabaseHost $databaseHost): void | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the DatabaseHost "force deleted" event. | ||
*/ | ||
public function forceDeleted(DatabaseHost $databaseHost): void | ||
{ | ||
event(new DatabaseHostEvents\Deleted($databaseHost)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters