diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 3c2fc96fad..8b49a43d81 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -9,6 +9,7 @@ use App\Console\Commands\Schedule\ProcessRunnableCommand; use App\Jobs\NodeStatistics; use App\Models\ActivityLog; +use App\Models\Webhook; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Database\Console\PruneCommand; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -48,5 +49,9 @@ protected function schedule(Schedule $schedule): void if (config('activity.prune_days')) { $schedule->command(PruneCommand::class, ['--model' => [ActivityLog::class]])->daily(); } + + if (config('panel.webhook.prune_days')) { + $schedule->command(PruneCommand::class, ['--model' => [Webhook::class]])->daily(); + } } } diff --git a/app/Filament/Pages/Settings.php b/app/Filament/Pages/Settings.php index f8d18a01c2..7043fe9d7d 100644 --- a/app/Filament/Pages/Settings.php +++ b/app/Filament/Pages/Settings.php @@ -540,7 +540,21 @@ private function miscSettings(): array ->afterStateUpdated(fn ($state, Set $set) => $set('PANEL_EDITABLE_SERVER_DESCRIPTIONS', (bool) $state)) ->default(env('PANEL_EDITABLE_SERVER_DESCRIPTIONS', config('panel.editable_server_descriptions'))), ]), - + Section::make('Webhook') + ->description('Configure how often old webhook logs should be pruned.') + ->columns() + ->collapsible() + ->collapsed() + ->schema([ + TextInput::make('APP_WEBHOOK_PRUNE_DAYS') + ->label('Prune age') + ->required() + ->numeric() + ->minValue(1) + ->maxValue(365) + ->suffix('Days') + ->default(env('APP_WEBHOOK_PRUNE_DAYS', config('panel.webhook.prune_days'))), + ]), ]; } diff --git a/app/Models/Webhook.php b/app/Models/Webhook.php index 28c029122d..7a8e78ca8c 100644 --- a/app/Models/Webhook.php +++ b/app/Models/Webhook.php @@ -2,12 +2,15 @@ namespace App\Models; +use Carbon\Carbon; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\MassPrunable; use Illuminate\Database\Eloquent\Model; class Webhook extends Model { - use HasFactory; + use HasFactory, MassPrunable; protected $fillable = ['payload', 'successful_at', 'event', 'endpoint']; @@ -18,4 +21,9 @@ public function casts() 'successful_at' => 'datetime', ]; } + + public function prunable(): Builder + { + return static::where('created_at', '<=', Carbon::now()->subDays(config('panel.webhook.prune_days'))); + } } diff --git a/app/Models/WebhookConfiguration.php b/app/Models/WebhookConfiguration.php index 1f38030460..d5a1b0ba0e 100644 --- a/app/Models/WebhookConfiguration.php +++ b/app/Models/WebhookConfiguration.php @@ -12,6 +12,13 @@ class WebhookConfiguration extends Model { use HasFactory, SoftDeletes; + /** + * Blacklisted events. + */ + protected static array $eventBlacklist = [ + 'eloquent.created: App\Models\Webhook', + ]; + protected $fillable = [ 'endpoint', 'description', @@ -48,7 +55,11 @@ public function webhooks(): HasMany public static function allPossibleEvents(): array { - return static::discoverCustomEvents() + static::allModelEvents(); + return collect(static::discoverCustomEvents()) + ->merge(static::allModelEvents()) + ->unique() + ->filter(fn ($event) => !in_array($event, static::$eventBlacklist)) + ->all(); } public static function filamentCheckboxList(): array diff --git a/config/panel.php b/config/panel.php index 5d0cabd0c1..83c1859e2d 100644 --- a/config/panel.php +++ b/config/panel.php @@ -168,8 +168,28 @@ 'editable_server_descriptions' => env('PANEL_EDITABLE_SERVER_DESCRIPTIONS', true), + /* + |-------------------------------------------------------------------------- + | API Settings + |-------------------------------------------------------------------------- + | + | This section controls Api Key configurations + */ + 'api' => [ 'key_limit' => env('API_KEYS_LIMIT', 25), 'key_expire_time' => env('API_KEYS_EXPIRE_TIME', 720), ], + + /* + |-------------------------------------------------------------------------- + | Webhook Settings + |-------------------------------------------------------------------------- + | + | This section controls Webhook configurations + */ + + 'webhook' => [ + 'prune_days' => env('APP_WEBHOOK_PRUNE_DAYS', 30), + ], ];