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:
Lance Pioch 2024-11-07 17:26:47 -05:00 committed by GitHub
parent a9b76a0f51
commit 47bd7289b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 7 deletions

View File

@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
class WebhookConfiguration extends Model class WebhookConfiguration extends Model
@ -40,12 +41,21 @@ class WebhookConfiguration extends Model
...$webhookConfiguration->getOriginal('events', '[]'), ...$webhookConfiguration->getOriginal('events', '[]'),
])->unique(); ])->unique();
$changedEvents->each(function (string $event) { 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("webhooks.$event", WebhookConfiguration::query()->whereJsonContains('events', $event)->get());
}); });
cache()->forever('watchedWebhooks', WebhookConfiguration::pluck('events')->flatten()->unique()->values()->all()); cache()->forever('watchedWebhooks', WebhookConfiguration::pluck('events')->flatten()->unique()->values()->all());
});
} }
public function webhooks(): HasMany public function webhooks(): HasMany

View File

@ -1,6 +1,6 @@
<?php <?php
namespace App\Tests\Feature; namespace App\Tests\Feature\Webhooks;
use App\Jobs\ProcessWebhook; use App\Jobs\ProcessWebhook;
use App\Models\Server; use App\Models\Server;
@ -62,6 +62,32 @@ class DispatchWebhooksTest extends TestCase
Queue::assertPushed(ProcessWebhook::class, 1); 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 public function createServer(): Server
{ {
return Server::factory()->withNode()->create(); return Server::factory()->withNode()->create();

View File

@ -1,6 +1,6 @@
<?php <?php
namespace App\Tests\Feature; namespace App\Tests\Feature\Webhooks;
use App\Events\Server\Installed; use App\Events\Server\Installed;
use App\Jobs\ProcessWebhook; use App\Jobs\ProcessWebhook;