Skip to content

Commit

Permalink
Add prune & event blacklist (#682)
Browse files Browse the repository at this point in the history
* Add prune & event blacklist

* Pinted 3times with --dirty bruh

* Add to Settings

* Fix prune & description

* Prune Logs not Configuration
  • Loading branch information
RMartinOscar authored Oct 28, 2024
1 parent bc2df22 commit 3f9c1db
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
16 changes: 15 additions & 1 deletion app/Filament/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))),
]),
];
}

Expand Down
10 changes: 9 additions & 1 deletion app/Models/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -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')));
}
}
13 changes: 12 additions & 1 deletion app/Models/WebhookConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions config/panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
];

0 comments on commit 3f9c1db

Please sign in to comment.