From f6ef42a67ecad9c93615c47ea6656b4d5a7c522b Mon Sep 17 00:00:00 2001 From: mho22 Date: Fri, 30 Aug 2024 11:21:27 +0200 Subject: [PATCH 1/3] Added limits in embed structures in discord notifications messages --- .../Channels/Discord/DiscordMessage.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Notifications/Channels/Discord/DiscordMessage.php b/src/Notifications/Channels/Discord/DiscordMessage.php index fda49647..c084c94e 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::limi( $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(), ], From 941108259df60cbe449a502d92033ed773a1f849 Mon Sep 17 00:00:00 2001 From: mho22 Date: Fri, 30 Aug 2024 11:23:37 +0200 Subject: [PATCH 2/3] Added limits in embed structures in discord notifications messages --- .../Channels/Discord/DiscordMessage.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Notifications/Channels/Discord/DiscordMessage.php b/src/Notifications/Channels/Discord/DiscordMessage.php index c084c94e..afdb7f06 100644 --- a/src/Notifications/Channels/Discord/DiscordMessage.php +++ b/src/Notifications/Channels/Discord/DiscordMessage.php @@ -104,8 +104,8 @@ public function fields(array $fields, bool $inline = true): self { foreach ($fields as $label => $value) { $this->fields[] = [ - 'name' => Str::limit( $label, 250 ), - 'value' => Str::limi( $value, 1000 ), + 'name' => Str::limit($label, 250), + 'value' => Str::limi($value, 1000), 'inline' => $inline, ]; } @@ -119,14 +119,14 @@ public function toArray(): array 'avatar_url' => $this->avatarUrl, 'embeds' => [ [ - 'title' => Str::limit( $this->title, 250 ), + 'title' => Str::limit($this->title, 250), 'url' => $this->url, 'type' => 'rich', - 'description' => Str::limit( $this->description, 4000 ), - 'fields' => array_slice( $this->fields, 0, 25 ), + 'description' => Str::limit($this->description, 4000), + 'fields' => array_slice($this->fields, 0, 25), 'color' => hexdec((string) $this->color), 'footer' => [ - 'text' => $this->footer ? Str::limit( $this->footer, 2000 ): '', + 'text' => $this->footer ? Str::limit($this->footer, 2000) : '', ], 'timestamp' => $this->timestamp ?? now(), ], From 4a55bd75813970679292a6b4d7f367bd72167c78 Mon Sep 17 00:00:00 2001 From: mho22 Date: Fri, 3 Jan 2025 16:27:07 +0100 Subject: [PATCH 3/3] Added limits in embed structures in discord notifications messages --- .../Channels/Discord/DiscordMessage.php | 2 +- .../Channels/Discord/DiscordMessageTest.php | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) 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 afdb7f06..5cd69c53 100644 --- a/src/Notifications/Channels/Discord/DiscordMessage.php +++ b/src/Notifications/Channels/Discord/DiscordMessage.php @@ -105,7 +105,7 @@ public function fields(array $fields, bool $inline = true): self foreach ($fields as $label => $value) { $this->fields[] = [ 'name' => Str::limit($label, 250), - 'value' => Str::limi($value, 1000), + 'value' => Str::limit($value, 1000), 'inline' => $inline, ]; } 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); +});