From 47bd7289b1e7184ea153aaf3484345b6468c5e5e Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Thu, 7 Nov 2024 17:26:47 -0500 Subject: [PATCH] Clear webhook cache when webhooks are deleted (#695) * Clear webhook cache when webhooks are deleted * fix: type casts --------- Co-authored-by: Vehikl --- app/Models/WebhookConfiguration.php | 20 +++++++++---- .../{ => Webhooks}/DispatchWebhooksTest.php | 28 ++++++++++++++++++- .../{ => Webhooks}/ProcessWebhooksTest.php | 2 +- 3 files changed, 43 insertions(+), 7 deletions(-) rename tests/Feature/{ => Webhooks}/DispatchWebhooksTest.php (68%) rename tests/Feature/{ => Webhooks}/ProcessWebhooksTest.php (99%) diff --git a/app/Models/WebhookConfiguration.php b/app/Models/WebhookConfiguration.php index d5a1b0ba0..60eade1ab 100644 --- a/app/Models/WebhookConfiguration.php +++ b/app/Models/WebhookConfiguration.php @@ -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 diff --git a/tests/Feature/DispatchWebhooksTest.php b/tests/Feature/Webhooks/DispatchWebhooksTest.php similarity index 68% rename from tests/Feature/DispatchWebhooksTest.php rename to tests/Feature/Webhooks/DispatchWebhooksTest.php index 5c032761c..7fcb1e326 100644 --- a/tests/Feature/DispatchWebhooksTest.php +++ b/tests/Feature/Webhooks/DispatchWebhooksTest.php @@ -1,6 +1,6 @@ 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(); diff --git a/tests/Feature/ProcessWebhooksTest.php b/tests/Feature/Webhooks/ProcessWebhooksTest.php similarity index 99% rename from tests/Feature/ProcessWebhooksTest.php rename to tests/Feature/Webhooks/ProcessWebhooksTest.php index 7eb7a8d9a..c15683d29 100644 --- a/tests/Feature/ProcessWebhooksTest.php +++ b/tests/Feature/Webhooks/ProcessWebhooksTest.php @@ -1,6 +1,6 @@