Skip to content

Commit

Permalink
Add DatabaseHost webhook configuration and enhance settings
Browse files Browse the repository at this point in the history
- 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
Poseidon281 committed Aug 3, 2024
1 parent 1dc31a8 commit c0aca8e
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 1 deletion.
19 changes: 19 additions & 0 deletions app/Events/DatabaseHost/Created.php
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)
{
//
}
}
19 changes: 19 additions & 0 deletions app/Events/DatabaseHost/Deleted.php
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)
{
//
}
}
68 changes: 67 additions & 1 deletion app/Filament/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,14 @@ private function webhookSettings(): array
->helperText('All Node related events will be logged to this webhook')
->default(env('NODE_WEBHOOK')),

TextInput::make('DATABASE_HOST_WEBHOOK')
->label('DatabaseHost Webhook')
->visible(fn (Get $get) => $get('WEBHOOK_TYPE') === 'json')
->hintAction($this->getDatabaseHostHintAction())
->url()
->helperText('All DatabaseHost related events will be logged to this webhook')
->default(env('DATABASE_HOST_WEBHOOK')),

TextInput::make('MAIN_WEBHOOK_DISCORD')
->label('Main Webhook')
->visible(fn (Get $get) => $get('WEBHOOK_TYPE') === 'discord')
Expand Down Expand Up @@ -426,6 +434,15 @@ private function webhookSettings(): array
->placeholder('https://discord.com/api/webhooks/')
->helperText('All Node related events will be logged to this webhook')
->default(env('NODE_WEBHOOK_DISCORD')),

TextInput::make('DATABASE_HOST_WEBHOOK_DISCORD')
->label('DatabaseHost Webhook')
->visible(fn (Get $get) => $get('WEBHOOK_TYPE') === 'discord')
->hintAction($this->getDatabaseHostHintAction())
->url()
->placeholder('https://discord.com/api/webhooks/')
->helperText('All DatabaseHost related events will be logged to this webhook')
->default(env('DATABASE_HOST_WEBHOOK_DISCORD')),
]),

Section::make('Discord Embed')
Expand Down Expand Up @@ -927,7 +944,7 @@ protected function getNodeHintAction()
Toggle::make('NODE_ID')
->label('ID')
->inline()
->default(env('USER_ID', true)),
->default(env('NODE_ID', true)),
Toggle::make('NODE_UUID')
->label('UUID')
->inline()
Expand Down Expand Up @@ -1001,4 +1018,53 @@ protected function getNodeHintAction()
->send();
});
}

protected function getDatabaseHostHintAction()
{
return
FormAction::make('DATABASE_HOST_CUSTOM_PAYLOAD')
->label('Customize Content')
->modalDescription('Customize what data you would like to send to your webhook')
->form([
Toggle::make('DATABASE_HOST_ID')
->label('ID')
->inline()
->default(env('DATABASE_HOST_ID', true)),
Toggle::make('DATABASE_HOST_NAME')
->label('Name')
->inline()
->default(env('DATABASE_HOST_NAME', true)),
Toggle::make('DATABASE_HOST_HOST')
->label('Host')
->inline()
->default(env('DATABASE_HOST_HOST', true)),
Toggle::make('DATABASE_HOST_PORT')
->label('Port')
->inline()
->default(env('DATABASE_HOST_PORT', true)),
Toggle::make('DATABASE_HOST_MAX_DB')
->label('Maximum Databases')
->inline()
->default(env('DATABASE_HOST_MAX_DB', true)),
Toggle::make('DATABASE_HOST_NODE_ID')
->label('Node ID')
->inline()
->default(env('DATABASE_HOST_NODE_ID', true)),
])
->action(function (array $data): void {
$data = array_map(fn ($value) => is_bool($value) ? ($value ? 'true' : 'false') : $value, $data);

$this->writeToEnvironment($data);

Artisan::call('config:clear');
Artisan::call('queue:restart');

$this->rememberData();

Notification::make()
->title('Content saved')
->success()
->send();
});
}
}
156 changes: 156 additions & 0 deletions app/Listeners/Webhook/DatabaseHostWebhookListener.php
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,
];
}
}
49 changes: 49 additions & 0 deletions app/Observers/DatabaseHostObserver.php
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));
}
}
7 changes: 7 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

namespace App\Providers;

use App\Events\DatabaseHost as DatabaseHostEvents;
use App\Events\Egg as EggEvents;
use App\Events\Node as NodeEvents;
use App\Events\Server as ServerEvents;
use App\Events\User as UserEvents;
use App\Listeners\Webhook\DatabaseHostWebhookListener;
use App\Listeners\Webhook\EggWebhookListener;
use App\Listeners\Webhook\NodeWebhookListener;
use App\Listeners\Webhook\ServerWebhookListener;
use App\Listeners\Webhook\UserWebhookListener;
use App\Models\DatabaseHost;
use App\Models\Egg;
use App\Models\Node;
use App\Models\User;
use App\Models\Server;
use App\Models\Subuser;
use App\Models\EggVariable;
use App\Observers\DatabaseHostObserver;
use App\Observers\EggObserver;
use App\Observers\NodeObserver;
use App\Observers\UserObserver;
Expand All @@ -33,6 +37,8 @@ class EventServiceProvider extends ServiceProvider
*/
protected $listen = [
ServerInstalledEvent::class => [ServerInstalledNotification::class],
DatabaseHostEvents\Created::class => [DatabaseHostWebhookListener::class],
DatabaseHostEvents\Deleted::class => [DatabaseHostWebhookListener::class],
EggEvents\Created::class => [EggWebhookListener::class],
EggEvents\Deleted::class => [EggWebhookListener::class],
NodeEvents\Created::class => [NodeWebhookListener::class],
Expand All @@ -50,6 +56,7 @@ public function boot(): void
{
parent::boot();

DatabaseHost::observe(DatabaseHostObserver::class);
Egg::observe(EggObserver::class);
EggVariable::observe(EggVariableObserver::class);
Node::observe(NodeObserver::class);
Expand Down
2 changes: 2 additions & 0 deletions app/Traits/SendWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private static function getWebhook($webhook)
try {
if (env('WEBHOOK_TYPE') === 'json') {
$webhookCategories = [
'databasehost' => 'DATABASE_HOST_WEBHOOK',
'egg' => 'EGG_WEBHOOK',
'node' => 'NODE_WEBHOOK',
'server' => 'SERVER_WEBHOOK',
Expand All @@ -24,6 +25,7 @@ private static function getWebhook($webhook)
];
} elseif (env('WEBHOOK_TYPE') === 'discord') {
$webhookCategories = [
'databasehost' => 'DATABASE_HOST_WEBHOOK_DISCORD',
'egg' => 'EGG_WEBHOOK_DISCORD',
'node' => 'NODE_WEBHOOK_DISCORD',
'server' => 'SERVER_WEBHOOK_DISCORD',
Expand Down

0 comments on commit c0aca8e

Please sign in to comment.