mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 01:44:45 +02:00
Clear webhook cache when webhooks are deleted (#695)
* Clear webhook cache when webhooks are deleted * fix: type casts --------- Co-authored-by: Vehikl <go@vehikl.com>
This commit is contained in:
parent
a9b76a0f51
commit
47bd7289b1
@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\File;
|
||||
|
||||
class WebhookConfiguration extends Model
|
||||
@ -40,12 +41,21 @@ class WebhookConfiguration extends Model
|
||||
...$webhookConfiguration->getOriginal('events', '[]'),
|
||||
])->unique();
|
||||
|
||||
$changedEvents->each(function (string $event) {
|
||||
cache()->forever("webhooks.$event", WebhookConfiguration::query()->whereJsonContains('events', $event)->get());
|
||||
});
|
||||
|
||||
cache()->forever('watchedWebhooks', WebhookConfiguration::pluck('events')->flatten()->unique()->values()->all());
|
||||
self::updateCache($changedEvents);
|
||||
});
|
||||
|
||||
self::deleted(static function (self $webhookConfiguration): void {
|
||||
self::updateCache(collect((array) $webhookConfiguration->events));
|
||||
});
|
||||
}
|
||||
|
||||
private static function updateCache(Collection $eventList): void
|
||||
{
|
||||
$eventList->each(function (string $event) {
|
||||
cache()->forever("webhooks.$event", WebhookConfiguration::query()->whereJsonContains('events', $event)->get());
|
||||
});
|
||||
|
||||
cache()->forever('watchedWebhooks', WebhookConfiguration::pluck('events')->flatten()->unique()->values()->all());
|
||||
}
|
||||
|
||||
public function webhooks(): HasMany
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Feature;
|
||||
namespace App\Tests\Feature\Webhooks;
|
||||
|
||||
use App\Jobs\ProcessWebhook;
|
||||
use App\Models\Server;
|
||||
@ -62,6 +62,32 @@ class DispatchWebhooksTest extends TestCase
|
||||
Queue::assertPushed(ProcessWebhook::class, 1);
|
||||
}
|
||||
|
||||
public function test_it_does_not_call_removed_events()
|
||||
{
|
||||
$webhookConfig = WebhookConfiguration::factory()->create([
|
||||
'events' => ['eloquent.created: '.Server::class],
|
||||
]);
|
||||
|
||||
$webhookConfig->update(['events' => 'eloquent.deleted: '.Server::class]);
|
||||
|
||||
$this->createServer();
|
||||
|
||||
Queue::assertNothingPushed();
|
||||
}
|
||||
|
||||
public function test_it_does_not_call_deleted_webhooks()
|
||||
{
|
||||
$webhookConfig = WebhookConfiguration::factory()->create([
|
||||
'events' => ['eloquent.created: '.Server::class],
|
||||
]);
|
||||
|
||||
$webhookConfig->delete();
|
||||
|
||||
$this->createServer();
|
||||
|
||||
Queue::assertNothingPushed();
|
||||
}
|
||||
|
||||
public function createServer(): Server
|
||||
{
|
||||
return Server::factory()->withNode()->create();
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Feature;
|
||||
namespace App\Tests\Feature\Webhooks;
|
||||
|
||||
use App\Events\Server\Installed;
|
||||
use App\Jobs\ProcessWebhook;
|
Loading…
x
Reference in New Issue
Block a user