Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord Notifications - Add limits in embed structures in discord notifications messages #1864

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/Notifications/Channels/Discord/DiscordMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Backup\Notifications\Channels\Discord;

use Carbon\Carbon;
use Illuminate\Support\Str;

class DiscordMessage
{
Expand Down Expand Up @@ -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,
];
}
Expand All @@ -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(),
],
Expand Down
78 changes: 78 additions & 0 deletions tests/Notifications/Channels/Discord/DiscordMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

use Spatie\Backup\Notifications\Channels\Discord\DiscordMessage;

beforeEach(function () {
$this->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);
});