Fixed webhooks on v4 and nested values (#1704)

Co-authored-by: MartinOscar <40749467+rmartinoscar@users.noreply.github.com>
This commit is contained in:
JoanFo 2025-09-18 16:40:24 +02:00 committed by GitHub
parent 68f8244298
commit 6db1d82738
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 272 additions and 58 deletions

View File

@ -41,7 +41,7 @@ class EditWebhookConfiguration extends EditRecord
protected function mutateFormDataBeforeSave(array $data): array protected function mutateFormDataBeforeSave(array $data): array
{ {
if (($data['type'] ?? null) === WebhookType::Discord->value) { if (($data['type'] ?? null) === WebhookType::Discord) {
$embeds = data_get($data, 'embeds', []); $embeds = data_get($data, 'embeds', []);
foreach ($embeds as &$embed) { foreach ($embeds as &$embed) {
@ -68,7 +68,7 @@ class EditWebhookConfiguration extends EditRecord
$data['payload'] = $tmp; $data['payload'] = $tmp;
} }
if (($data['type'] ?? null) === WebhookType::Regular->value && isset($data['headers']) && is_array($data['headers'])) { if (($data['type'] ?? null) === WebhookType::Regular && isset($data['headers'])) {
$newHeaders = []; $newHeaders = [];
foreach ($data['headers'] as $key => $value) { foreach ($data['headers'] as $key => $value) {
$newKey = str_replace(' ', '-', $key); $newKey = str_replace(' ', '-', $key);
@ -84,7 +84,6 @@ class EditWebhookConfiguration extends EditRecord
{ {
if (($data['type'] ?? null) === WebhookType::Discord->value) { if (($data['type'] ?? null) === WebhookType::Discord->value) {
$embeds = data_get($data, 'payload.embeds', []); $embeds = data_get($data, 'payload.embeds', []);
foreach ($embeds as &$embed) { foreach ($embeds as &$embed) {
$embed['color'] = '#' . dechex(data_get($embed, 'color')); $embed['color'] = '#' . dechex(data_get($embed, 'color'));
$embed = collect($embed)->filter(fn ($key) => is_array($key) ? array_filter($key, fn ($arr_key) => !empty($arr_key)) : !empty($key))->all(); $embed = collect($embed)->filter(fn ($key) => is_array($key) ? array_filter($key, fn ($arr_key) => !empty($arr_key)) : !empty($key))->all();

View File

@ -133,7 +133,7 @@ class WebhookResource extends Resource
->live() ->live()
->inline() ->inline()
->options(WebhookType::class) ->options(WebhookType::class)
->default(WebhookType::Regular->value), ->default(WebhookType::Regular),
TextInput::make('description') TextInput::make('description')
->label(trans('admin/webhook.description')) ->label(trans('admin/webhook.description'))
->required(), ->required(),
@ -144,7 +144,6 @@ class WebhookResource extends Resource
->afterStateUpdated(fn (string $state, Set $set) => $set('type', str($state)->contains('discord.com') ? WebhookType::Discord : WebhookType::Regular)), ->afterStateUpdated(fn (string $state, Set $set) => $set('type', str($state)->contains('discord.com') ? WebhookType::Discord : WebhookType::Regular)),
Section::make(trans('admin/webhook.regular')) Section::make(trans('admin/webhook.regular'))
->hidden(fn (Get $get) => $get('type') === WebhookType::Discord) ->hidden(fn (Get $get) => $get('type') === WebhookType::Discord)
->dehydratedWhenHidden()
->schema(fn () => self::getRegularFields()) ->schema(fn () => self::getRegularFields())
->headerActions([ ->headerActions([
Action::make('reset_headers') Action::make('reset_headers')
@ -158,7 +157,6 @@ class WebhookResource extends Resource
->formBefore(), ->formBefore(),
Section::make(trans('admin/webhook.discord')) Section::make(trans('admin/webhook.discord'))
->hidden(fn (Get $get) => $get('type') === WebhookType::Regular) ->hidden(fn (Get $get) => $get('type') === WebhookType::Regular)
->dehydratedWhenHidden()
->afterStateUpdated(fn (Livewire $livewire) => $livewire->dispatch('refresh-widget')) ->afterStateUpdated(fn (Livewire $livewire) => $livewire->dispatch('refresh-widget'))
->schema(fn () => self::getDiscordFields()) ->schema(fn () => self::getDiscordFields())
->view('filament.components.webhooksection') ->view('filament.components.webhooksection')

View File

@ -8,7 +8,6 @@ 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\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Livewire\Features\SupportEvents\HandlesEvents; use Livewire\Features\SupportEvents\HandlesEvents;
@ -181,7 +180,7 @@ class WebhookConfiguration extends Model
function ($matches) use ($replacement) { function ($matches) use ($replacement) {
$trimmed = trim($matches[1]); $trimmed = trim($matches[1]);
return Arr::get($replacement, $trimmed, $trimmed); return data_get($replacement, $trimmed, $trimmed);
}, },
$subject $subject
); );
@ -191,7 +190,7 @@ class WebhookConfiguration extends Model
public function run(?string $eventName = null, ?array $eventData = null): void public function run(?string $eventName = null, ?array $eventData = null): void
{ {
$eventName ??= 'eloquent.created: '.Server::class; $eventName ??= 'eloquent.created: '.Server::class;
$eventData ??= $this->getWebhookSampleData(); $eventData ??= static::getWebhookSampleData();
ProcessWebhook::dispatch($this, $eventName, [$eventData]); ProcessWebhook::dispatch($this, $eventName, [$eventData]);
} }
@ -199,37 +198,201 @@ class WebhookConfiguration extends Model
/** /**
* @return array<string, mixed> * @return array<string, mixed>
*/ */
public function getWebhookSampleData(): array public static function getWebhookSampleData(): array
{ {
return [ return [
'status' => 'installing', 'id' => 4,
'oom_killer' => false, 'uuid' => '4864a058-9a3b-44a9-a6cf-c1355e89406e',
'installed_at' => null, 'uuid_short' => '4864a058',
'external_id' => 10,
'uuid' => '651fgbc1-dee6-4250-814e-10slda13f1e',
'uuid_short' => '651fgbc1',
'node_id' => 1, 'node_id' => 1,
'name' => 'Eagle', 'name' => 'Example Server',
'owner_id' => 1,
'memory' => 6144,
'swap' => 0,
'disk' => 20480,
'io' => 500,
'cpu' => 300,
'egg_id' => 1,
'startup' => 'java -Xms128M -XX:MaxRAMPercentage=95.0 -Dterminal.jline=false -Dterminal.ansi=true -jar {{SERVER_JARFILE}}',
'created_at' => '2025-09-05T01:15:43.000000Z',
'updated_at' => '2025-09-11T22:45:14.000000Z',
'allocation_id' => 4,
'image' => 'ghcr.io/parkervcp/yolks:java_21',
'description' => 'This is an example server description.', 'description' => 'This is an example server description.',
'skip_scripts' => false, 'skip_scripts' => false,
'owner_id' => 1, 'external_id' => null,
'memory' => 2048, 'database_limit' => 5,
'swap' => 128,
'disk' => 10240,
'io' => 500,
'cpu' => 100,
'threads' => '1,3,5',
'allocation_id' => 4,
'egg_id' => 2,
'startup' => 'java -Xms128M -XX:MaxRAMPercentage=95.0 -jar {{SERVER_JARFILE}}',
'image' => 'ghcr.io/parkervcp/yolks:java_21',
'database_limit' => 1,
'allocation_limit' => 5, 'allocation_limit' => 5,
'backup_limit' => 3, 'threads' => null,
'backup_limit' => 5,
'status' => null,
'installed_at' => '2025-09-06T03:02:31.000000Z',
'oom_killer' => false,
'docker_labels' => [], 'docker_labels' => [],
'created_at' => '2025-03-17T15:20:32.000000Z', 'allocation' => [
'updated_at' => '2025-05-12T17:53:12.000000Z', 'id' => 4,
'id' => 2, 'node_id' => 1,
'ip' => '0.0.0.0',
'port' => 25565,
'server_id' => 4,
'created_at' => '2025-07-01T20:12:41.000000Z',
'updated_at' => '2025-09-09T17:47:22.000000Z',
'ip_alias' => null,
'notes' => null,
],
'variables' => [
[
'id' => 1,
'egg_id' => 1,
'name' => 'Build Number',
'description' => 'The build number for the paper release.\r\n\r\nLeave at latest to always get the latest version. Invalid versions will default to latest.',
'env_variable' => 'BUILD_NUMBER',
'default_value' => 'latest',
'user_viewable' => true,
'user_editable' => true,
'rules' => ['required', 'string', 'max:20'],
'created_at' => '2025-09-05T01:15:43.000000Z',
'updated_at' => '2025-09-05T01:15:43.000000Z',
'sort' => 4,
'server_value' => 'latest',
],
[
'id' => 2,
'egg_id' => 1,
'name' => 'Download Path',
'description' => 'A URL to use to download a server.jar rather than the ones in the install script. This is not user\nviewable.',
'env_variable' => 'DL_PATH',
'default_value' => '',
'user_viewable' => false,
'user_editable' => false,
'rules' => ['nullable', 'string'],
'created_at' => '2025-09-05T01:15:43.000000Z',
'updated_at' => '2025-09-05T01:15:43.000000Z',
'sort' => 3,
'server_value' => '',
],
[
'id' => 3,
'egg_id' => 1,
'name' => 'Minecraft Version',
'description' => 'The version of minecraft to download. \r\n\r\nLeave at latest to always get the latest version. Invalid versions will default to latest.',
'env_variable' => 'MINECRAFT_VERSION',
'default_value' => 'latest',
'user_viewable' => true,
'user_editable' => true,
'rules' => ['nullable', 'string', 'max:20'],
'created_at' => '2025-09-05T01:15:43.000000Z',
'updated_at' => '2025-09-05T01:15:43.000000Z',
'sort' => 1,
'server_value' => '1.21.8',
],
[
'id' => 4,
'egg_id' => 1,
'name' => 'Server Jar File',
'description' => 'The name of the server jarfile to run the server with.',
'env_variable' => 'SERVER_JARFILE',
'default_value' => 'server.jar',
'user_viewable' => true,
'user_editable' => true,
'rules' => ['required', 'regex:/^([\w\d._-]+)(\.jar)$/'],
'created_at' => '2025-09-05T01:15:43.000000Z',
'updated_at' => '2025-09-05T01:15:43.000000Z',
'sort' => 2,
'server_value' => 'server.jar',
],
],
'server_variables' => [
'record-21' => [
'id' => 21,
'server_id' => 4,
'variable_id' => 3,
'variable_value' => '1.21.8',
'created_at' => '2025-09-06T06:00:58.000000Z',
'updated_at' => '2025-09-09T17:59:40.000000Z',
'variable' => [
'id' => 3,
'egg_id' => 1,
'name' => 'Minecraft Version',
'description' => 'The version of minecraft to download. \r\n\r\nLeave at latest to always get the latest version. Invalid versions will default to latest.',
'env_variable' => 'MINECRAFT_VERSION',
'default_value' => 'latest',
'user_viewable' => true,
'user_editable' => true,
'rules' => ['nullable', 'string', 'max:20'],
'created_at' => '2025-09-05T01:15:43.000000Z',
'updated_at' => '2025-09-05T01:15:43.000000Z',
'sort' => 1,
],
],
'record-22' => [
'id' => 22,
'server_id' => 4,
'variable_id' => 4,
'variable_value' => 'server.jar',
'created_at' => '2025-09-06T06:00:58.000000Z',
'updated_at' => '2025-09-06T06:01:05.000000Z',
'variable' => [
'id' => 4,
'egg_id' => 1,
'name' => 'Server Jar File',
'description' => 'The name of the server jarfile to run the server with.',
'env_variable' => 'SERVER_JARFILE',
'default_value' => 'server.jar',
'user_viewable' => true,
'user_editable' => true,
'rules' => ['required', 'regex:/^([\w\d._-]+)(\.jar)$/'],
'created_at' => '2025-09-05T01:15:43.000000Z',
'updated_at' => '2025-09-05T01:15:43.000000Z',
'sort' => 2,
],
],
'record-20' => [
'id' => 20,
'server_id' => 4,
'variable_id' => 2,
'variable_value' => '',
'created_at' => '2025-09-06T06:00:58.000000Z',
'updated_at' => '2025-09-06T06:00:58.000000Z',
'variable' => [
'id' => 2,
'egg_id' => 1,
'name' => 'Download Path',
'description' => 'A URL to use to download a server.jar rather than the ones in the install script. This is not user\nviewable.',
'env_variable' => 'DL_PATH',
'default_value' => '',
'user_viewable' => false,
'user_editable' => false,
'rules' => ['nullable', 'string'],
'created_at' => '2025-09-05T01:15:43.000000Z',
'updated_at' => '2025-09-05T01:15:43.000000Z',
'sort' => 3,
],
],
'record-19' => [
'id' => 19,
'server_id' => 4,
'variable_id' => 1,
'variable_value' => 'latest',
'created_at' => '2025-09-06T06:00:58.000000Z',
'updated_at' => '2025-09-06T06:00:58.000000Z',
'variable' => [
'id' => 1,
'egg_id' => 1,
'name' => 'Build Number',
'description' => 'The build number for the paper release.\r\n\r\nLeave at latest to always get the latest version. Invalid versions will default to latest.',
'env_variable' => 'BUILD_NUMBER',
'default_value' => 'latest',
'user_viewable' => true,
'user_editable' => true,
'rules' => ['required', 'string', 'max:20'],
'created_at' => '2025-09-05T01:15:43.000000Z',
'updated_at' => '2025-09-05T01:15:43.000000Z',
'sort' => 4,
],
],
],
'event' => 'updated: Server',
]; ];
} }
} }

View File

@ -1,26 +1,80 @@
<x-filament::section @php
:aside="$isAside()" $afterHeader = $getChildSchema($schemaComponent::AFTER_HEADER_SCHEMA_KEY)?->toHtmlString();
:collapsed="$isCollapsed()" $isAside = $isAside();
:collapsible="$isCollapsible() && (! $isAside)" $isCollapsed = $isCollapsed();
:compact="$isCompact()" $isCollapsible = $isCollapsible();
:content-before="$isFormBefore()" $isCompact = $isCompact();
:description="$getDescription()" $isContained = $isContained();
:footer-actions="$getFooterActions()" $isDivided = $isDivided();
:footer-actions-alignment="$getFooterActionsAlignment()" $isFormBefore = $isFormBefore();
:header-actions="$getHeaderActions()" $description = $getDescription();
:heading="$getHeading()" $footer = $getChildSchema($schemaComponent::FOOTER_SCHEMA_KEY)?->toHtmlString();
:icon="$getIcon()" $heading = $getHeading();
:icon-color="$getIconColor()" $headingTag = $getHeadingTag();
:icon-size="$getIconSize()" $icon = $getIcon();
:persist-collapsed="$shouldPersistCollapsed()" $iconColor = $getIconColor();
:attributes=" $iconSize = $getIconSize();
\Filament\Support\prepare_inherited_attributes($attributes) $shouldPersistCollapsed = $shouldPersistCollapsed();
->merge(['id' => $getId()], escape: false) $isSecondary = $isSecondary();
" $id = $getId();
> @endphp
<x-slot name="heading">
@livewire(App\Filament\Admin\Widgets\DiscordPreview::class, ['record' => $getRecord(), 'pollingInterval' => $pollingInterval ?? null])
</x-slot>
{{ $getChildComponentContainer() }} <div
</x-filament::section> {{
$attributes
->merge([
'id' => $id,
], escape: false)
->merge($getExtraAttributes(), escape: false)
->merge($getExtraAlpineAttributes(), escape: false)
->class(['fi-sc-section'])
}}
>
@if (filled($label = $getLabel()))
<div class="fi-sc-section-label-ctn">
{{ $getChildSchema($schemaComponent::BEFORE_LABEL_SCHEMA_KEY) }}
<div class="fi-sc-section-label">
{{ $label }}
</div>
{{ $getChildSchema($schemaComponent::AFTER_LABEL_SCHEMA_KEY) }}
</div>
@endif
@if ($aboveContentContainer = $getChildSchema($schemaComponent::ABOVE_CONTENT_SCHEMA_KEY)?->toHtmlString())
{{ $aboveContentContainer }}
@endif
<x-filament::section
:after-header="$afterHeader"
:aside="$isAside"
:collapsed="$isCollapsed"
:collapse-id="$id"
:collapsible="$isCollapsible && (! $isAside)"
:compact="$isCompact"
:contained="$isContained"
:content-before="$isFormBefore"
:description="$description"
:divided="$isDivided"
:footer="$footer"
:has-content-el="false"
:heading="$heading"
:heading-tag="$headingTag"
:icon="$icon"
:icon-color="$iconColor"
:icon-size="$iconSize"
:persist-collapsed="$shouldPersistCollapsed"
:secondary="$isSecondary"
>
<x-slot name="heading">
@livewire(App\Filament\Admin\Widgets\DiscordPreview::class, ['record' => $getRecord(), 'pollingInterval' => $pollingInterval ?? null])
</x-slot>
{{ $getChildSchema()->gap(! $isDivided)->extraAttributes(['class' => 'fi-section-content']) }}
</x-filament::section>
@if ($belowContentContainer = $getChildSchema($schemaComponent::BELOW_CONTENT_SCHEMA_KEY)?->toHtmlString())
{{ $belowContentContainer }}
@endif
</div>