Skip to content

Commit

Permalink
Clear webhook cache when webhooks are deleted (#695)
Browse files Browse the repository at this point in the history
* Clear webhook cache when webhooks are deleted

* fix: type casts

---------

Co-authored-by: Vehikl <go@vehikl.com>
  • Loading branch information
lancepioch and go-vehikl authored Nov 7, 2024
1 parent a9b76a0 commit 47bd728
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
18 changes: 14 additions & 4 deletions app/Models/WebhookConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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
Expand Down Expand Up @@ -40,12 +41,21 @@ protected static function booted(): void
...$webhookConfiguration->getOriginal('events', '[]'),
])->unique();

$changedEvents->each(function (string $event) {
cache()->forever("webhooks.$event", WebhookConfiguration::query()->whereJsonContains('events', $event)->get());
});
self::updateCache($changedEvents);
});

self::deleted(static function (self $webhookConfiguration): void {
self::updateCache(collect((array) $webhookConfiguration->events));
});
}

cache()->forever('watchedWebhooks', WebhookConfiguration::pluck('events')->flatten()->unique()->values()->all());
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Tests\Feature;
namespace App\Tests\Feature\Webhooks;

use App\Jobs\ProcessWebhook;
use App\Models\Server;
Expand Down Expand Up @@ -62,6 +62,32 @@ public function test_it_sends_some_webhooks()
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Tests\Feature;
namespace App\Tests\Feature\Webhooks;

use App\Events\Server\Installed;
use App\Jobs\ProcessWebhook;
Expand Down

0 comments on commit 47bd728

Please sign in to comment.