From 3d91654b3017ae57386d4a70c16c0b76476eed9b Mon Sep 17 00:00:00 2001 From: Yannick Decat <17177411+mho22@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:19:03 +0100 Subject: [PATCH] Discord Notifications - Add limits in embed structures in discord notifications messages (#1864) * Added limits in embed structures in discord notifications messages * Added limits in embed structures in discord notifications messages * Added limits in embed structures in discord notifications messages --- .../Channels/Discord/DiscordMessage.php | 13 ++-- .../Channels/Discord/DiscordMessageTest.php | 78 +++++++++++++++++++ 2 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 tests/Notifications/Channels/Discord/DiscordMessageTest.php diff --git a/src/Notifications/Channels/Discord/DiscordMessage.php b/src/Notifications/Channels/Discord/DiscordMessage.php index fda49647..5cd69c53 100644 --- a/src/Notifications/Channels/Discord/DiscordMessage.php +++ b/src/Notifications/Channels/Discord/DiscordMessage.php @@ -3,6 +3,7 @@ namespace Spatie\Backup\Notifications\Channels\Discord; use Carbon\Carbon; +use Illuminate\Support\Str; class DiscordMessage { @@ -103,8 +104,8 @@ public function fields(array $fields, bool $inline = true): self { foreach ($fields as $label => $value) { $this->fields[] = [ - 'name' => $label, - 'value' => $value, + 'name' => Str::limit($label, 250), + 'value' => Str::limit($value, 1000), 'inline' => $inline, ]; } @@ -118,14 +119,14 @@ public function toArray(): array 'avatar_url' => $this->avatarUrl, 'embeds' => [ [ - 'title' => $this->title, + 'title' => Str::limit($this->title, 250), 'url' => $this->url, 'type' => 'rich', - 'description' => $this->description, - 'fields' => $this->fields, + 'description' => Str::limit($this->description, 4000), + 'fields' => array_slice($this->fields, 0, 25), 'color' => hexdec((string) $this->color), 'footer' => [ - 'text' => $this->footer ?? '', + 'text' => $this->footer ? Str::limit($this->footer, 2000) : '', ], 'timestamp' => $this->timestamp ?? now(), ], diff --git a/tests/Notifications/Channels/Discord/DiscordMessageTest.php b/tests/Notifications/Channels/Discord/DiscordMessageTest.php new file mode 100644 index 00000000..52b08544 --- /dev/null +++ b/tests/Notifications/Channels/Discord/DiscordMessageTest.php @@ -0,0 +1,78 @@ +message = new DiscordMessage; +}); + +it('resizes embed title if character count is greater than 256', function () { + $string = fake()->regexify('[A-Za-z0-9]{300}'); + $count = strlen($string); + + expect($count)->toBeGreaterThan(256); + + $this->message->title($string); + $title = $this->message->toArray()['embeds'][0]['title']; + $count = strlen($title); + + expect($count)->toBeLessThan(256); +}); + +it('resizes embed description if character count is greater than 4096', function () { + $string = fake()->regexify('[A-Za-z0-9]{5000}'); + $count = strlen($string); + + expect($count)->toBeGreaterThan(4096); + + $this->message->title($string); + $description = $this->message->toArray()['embeds'][0]['description']; + $count = strlen($description); + + expect($count)->toBeLessThan(4096); +}); + +it('resizes fields if fields count is greater than 25', function () { + $array = array_map(fn() => fake()->word(), range(1, 50)); + $count = count($array); + + expect($count)->toBeGreaterThan(25); + + $this->message->fields($array); + $fields = $this->message->toArray()['embeds'][0]['fields']; + $count = count($fields); + + expect($count)->toEqual(25); +}); + +it('resizes field name if character count is greater than 256 and value if character count is greater than 1024', function () { + $name = fake()->regexify('[A-Za-z0-9]{300}'); + $value = fake()->regexify('[A-Za-z0-9]{2000}'); + $array = [$name => $value]; + $nameCount = strlen($name); + $valueCount = strlen($value); + + expect($nameCount)->toBeGreaterThan(256); + expect($valueCount)->toBeGreaterThan(1024); + + $this->message->fields($array); + $fields = $this->message->toArray()['embeds'][0]['fields'][0]; + $nameCount = strlen($fields['name']); + $valueCount = strlen($fields['value']); + + expect($nameCount)->toBeLessThan(256); + expect($valueCount)->toBeLessThan(1024); +}); + +it('resizes footer text if character count is greater than 2048', function () { + $string = fake()->regexify('[A-Za-z0-9]{3000}'); + $count = strlen($string); + + expect($count)->toBeGreaterThan(2048); + + $this->message->footer($string); + $footer = $this->message->toArray()['embeds'][0]['footer']['text']; + $count = strlen($footer); + + expect($count)->toBeLessThan(2048); +});