Add support for automatically pruning activity logs

This commit is contained in:
DaneEveritt 2022-05-29 19:45:00 -04:00
parent 9b7af02690
commit e15985ea39
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 41 additions and 6 deletions

View file

@ -2,9 +2,12 @@
namespace Pterodactyl\Models;
use Carbon\Carbon;
use LogicException;
use Illuminate\Support\Facades\Event;
use Pterodactyl\Events\ActivityLogged;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\MassPrunable;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Model as IlluminateModel;
@ -42,6 +45,8 @@ use Illuminate\Database\Eloquent\Model as IlluminateModel;
*/
class ActivityLog extends Model
{
use MassPrunable;
public $timestamps = false;
protected $guarded = [
@ -86,6 +91,20 @@ class ActivityLog extends Model
return $builder->whereMorphedTo('actor', $actor);
}
/**
* Returns models to be pruned.
*
* @see https://laravel.com/docs/9.x/eloquent#pruning-models
*/
public function prunable()
{
if (is_null(config('activity.prune_days'))) {
throw new LogicException('Cannot prune activity logs: no "prune_days" configuration value is set.');
}
return static::where('timestamp', '<=', Carbon::now()->subDays(config('activity.prune_days')));
}
/**
* Boots the model event listeners. This will trigger an activity log event every
* time a new model is inserted which can then be captured and worked with as needed.