Skip to content

Commit

Permalink
Clear webhook cache when webhooks are deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
go-vehikl committed Oct 31, 2024
1 parent 41ddae1 commit 72208cf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
17 changes: 13 additions & 4 deletions app/Models/WebhookConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,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($webhookConfiguration->events));

Check failure on line 47 in app/Models/WebhookConfiguration.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $value of function collect expects Illuminate\Contracts\Support\Arrayable<(int|string), mixed>|iterable<(int|string), mixed>|null, string given.
});
}

cache()->forever('watchedWebhooks', WebhookConfiguration::pluck('events')->flatten()->unique()->values()->all());
private static function updateCache($eventList): void

Check failure on line 51 in app/Models/WebhookConfiguration.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method App\Models\WebhookConfiguration::updateCache() has parameter $eventList with no type specified.
{
$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 72208cf

Please sign in to comment.