8000 minor #47859 [Notifier] Check Discord embed limitations (alamirault) · symfony/symfony@c2d3dd8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c2d3dd8

Browse files
committed
minor #47859 [Notifier] Check Discord embed limitations (alamirault)
This PR was merged into the 6.2 branch. Discussion ---------- [Notifier] Check Discord embed limitations | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #47688 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Add missing check on Discord Embeds (length and number). See #47688 for description Official embed limits: https://discord.com/developers/docs/resources/channel#embed-object-embed-limits (unrelated phpunit failure) Commits ------- fa4a60c [Notifier] Check Discord embed limitations
2 parents 6351ab1 + fa4a60c commit c2d3dd8

File tree

9 files changed

+262
-0
lines changed

9 files changed

+262
-0
lines changed

src/Symfony/Component/Notifier/Bridge/Discord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Check embed limitations
8+
49
5.3
510
---
611

src/Symfony/Component/Notifier/Bridge/Discord/Embeds/DiscordAuthorEmbedObject.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,24 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;
1313

14+
use Symfony\Component\Notifier\Exception\LengthException;
15+
1416
/**
1517
* @author Karoly Gossler <connor@connor.hu>
1618
*/
1719
final class DiscordAuthorEmbedObject extends AbstractDiscordEmbedObject
1820
{
21+
private const NAME_LIMIT = 256;
22+
1923
/**
2024
* @return $this
2125
*/
2226
public function name(string $name): static
2327
{
28+
if (\strlen($name) > self::NAME_LIMIT) {
29+
throw new LengthException(sprintf('Maximum length for the name is %d characters.', self::NAME_LIMIT));
30+
}
31+
2432
$this->options['name'] = $name;
2533

2634
return $this;

src/Symfony/Component/Notifier/Bridge/Discord/Embeds/DiscordEmbed.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,26 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;
1313

14+
use Symfony\Component\Notifier\Exception\LengthException;
15+
1416
/**
1517
* @author Karoly Gossler <connor@connor.hu>
1618
*/
1719
final class DiscordEmbed extends AbstractDiscordEmbed
1820
{
21+
private const TITLE_LIMIT = 256;
22+
private const DESCRIPTION_LIMIT = 4096;
23+
private const FIELDS_LIMIT = 25;
24+
1925
/**
2026
* @return $this
2127
*/
2228
public function title(string $title): static
2329
{
30+
if (\strlen($title) > self::TITLE_LIMIT) {
31+
throw new LengthException(sprintf('Maximum length for the title is %d characters.', self::TITLE_LIMIT));
32+
}
33+
2434
$this->options['title'] = $title;
2535

2636
return $this;
@@ -31,6 +41,10 @@ public function title(string $title): static
3141
*/
3242
public function description(string $description): static
3343
{
44+
if (\strlen($description) > self::DESCRIPTION_LIMIT) {
45+
throw new LengthException(sprintf('Maximum length for the description is %d characters.', self::DESCRIPTION_LIMIT));
46+
}
47+
3448
$this->options['description'] = $description;
3549

3650
return $this;
@@ -111,6 +125,10 @@ public function author(DiscordAuthorEmbedObject $author): static
111125
*/
112126
public function addField(DiscordFieldEmbedObject $field): static
113127
{
128+
if (self::FIELDS_LIMIT === \count($this->options['fields'] ?? [])) {
129+
throw new LengthException(sprintf('Maximum number of fields should not exceed %d.', self::FIELDS_LIMIT));
130+
}
131+
114132
if (!isset($this->options['fields'])) {
115133
$this->options['fields'] = [];
116134
}

src/Symfony/Component/Notifier/Bridge/Discord/Embeds/DiscordFieldEmbedObject.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;
1313

14+
use Symfony\Component\Notifier\Exception\LengthException;
15+
1416
/**
1517
* @author Karoly Gossler <connor@connor.hu>
1618
*/
1719
final class DiscordFieldEmbedObject extends AbstractDiscordEmbedObject
1820
{
21+
private const NAME_LIMIT = 256;
22+
private const VALUE_LIMIT = 1024;
23+
1924
/**
2025
* @return $this
2126
*/
2227
public function name(string $name): static
2328
{
29+
if (\strlen($name) > self::NAME_LIMIT) {
30+
throw new LengthException(sprintf('Maximum length for the name is %d characters.', self::NAME_LIMIT));
31+
}
32+
2433
$this->options['name'] = $name;
2534

2635
return $this;
@@ -31,6 +40,10 @@ public function name(string $name): static
3140
*/
3241
public function value(string $value): static
3342
{
43+
if (\strlen($value) > self::VALUE_LIMIT) {
44+
throw new LengthException(sprintf('Maximum length for the value is %d characters.', self::VALUE_LIMIT));
45+
}
46+
3447
$this->options['value'] = $value;
3548

3649
return $this;

src/Symfony/Component/Notifier/Bridge/Discord/Embeds/DiscordFooterEmbedObject.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,24 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;
1313

14+
use Symfony\Component\Notifier\Exception\LengthException;
15+
1416
/**
1517
* @author Karoly Gossler <connor@connor.hu>
1618
*/
1719
final class DiscordFooterEmbedObject extends AbstractDiscordEmbedObject
1820
{
21+
private const TEXT_LIMIT = 2048;
22+
1923
/**
2024
* @return $this
2125
*/
2226
public function text(string $text): static
2327
{
28+
if (\strlen($text) > self::TEXT_LIMIT) {
29+
throw new LengthException(sprintf('Maximum length for the text is %d characters.', self::TEXT_LIMIT));
30+
}
31+
2432
$this->options['text'] = $text;
2533

2634
return $this;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord\Tests\Embeds;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordAuthorEmbedObject;
16+
use Symfony\Component\Notifier\Exception\LengthException;
17+
18+
final class DiscordAuthorEmbedObjectTest extends TestCase
19+
{
20+
public function testCanBeInstantiated()
21+
{
22+
$author = (new DiscordAuthorEmbedObject())
23+
->name('Doe')
24+
->url('http://ur.l')
25+
->iconUrl('http://icon-ur.l')
26+
->proxyIconUrl('http://proxy-icon-ur.l');
27+
28+
$this->assertSame([
29+
'name' => 'Doe',
30+
'url' => 'http://ur.l',
31+
'icon_url' => 'http://icon-ur.l',
32+
'proxy_icon_url' => 'http://proxy-icon-ur.l',
33+
], $author->toArray());
34+
}
35+
36+
public function testThrowsWhenNameExceedsCharacterLimit()
37+
{
38+
$this->expectException(LengthException::class);
39+
$this->expectExceptionMessage('Maximum length for the name is 256 characters.');
40+
41+
(new DiscordAuthorEmbedObject())->name(str_repeat('h', 257));
42+
}
43+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord\Tests\Embeds;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordEmbed;
16+
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordFieldEmbedObject;
17+
use Symfony\Component\Notifier\Exception\LengthException;
18+
19+
final class DiscordEmbedTest extends TestCase
20+
{
21+
public function testCanBeInstantiated()
22+
{
23+
$embed = (new DiscordEmbed())
24+
->title('foo')
25+
->description('bar')
26+
->addField((new DiscordFieldEmbedObject())
27+
->name('baz')
28+
->value('qux')
29+
);
30+
31+
$this->assertSame([
32+
'title' => 'foo',
33+
'description' => 'bar',
34+
'fields' => [
35+
[
36+
'name' => 'baz',
37+
'value' => 'qux',
38+
],
39+
],
40+
], $embed->toArray());
41+
}
42+
43+
public function testThrowsWhenTitleExceedsCharacterLimit()
44+
{
45+
$this->expectException(LengthException::class);
46+
$this->expectExceptionMessage('Maximum length for the title is 256 characters.');
47+
48+
(new DiscordEmbed())->title(str_repeat('h', 257));
49+
}
50+
51+
public function testThrowsWhenDescriptionExceedsCharacterLimit()
52+
{
53+
$this->expectException(LengthException::class);
54+
$this->expectExceptionMessage('Maximum length for the description is 4096 characters.');
55+
56+
(new DiscordEmbed())->description(str_repeat('h', 4097));
57+
}
58+
59+
public function testThrowsWhenFieldsLimitReached()
60+
{
61+
$embed = new DiscordEmbed();
62+
for ($i = 0; $i < 25; ++$i) {
63+
$embed->addField((new DiscordFieldEmbedObject())
64+
->name('baz')
65+
->value('qux')
66+
);
67+
}
68+
69+
$this->expectException(\LogicException::class);
70+
$this->expectExceptionMessage('Maximum number of fields should not exceed 25.');
71+
72+
$embed->addField((new DiscordFieldEmbedObject())
73+
->name('fail')
74+
->value('fail')
75+
);
76+
}
77+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord\Tests\Embeds;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordFieldEmbedObject;
16+
use Symfony\Component\Notifier\Exception\LengthException;
17+
18+
final class DiscordFieldEmbedObjectTest extends TestCase
19+
{
20+
public function testCanBeInstantiated()
21+
{
22+
$field = (new DiscordFieldEmbedObject())
23+
->name('foo')
24+
->value('bar')
25+
->inline(true);
26+
27+
$this->assertSame([
28+
'name' => 'foo',
29+
'value' => 'bar',
30+
'inline' => true,
31+
], $field->toArray());
32+
}
33+
34+
public function testThrowsWhenNameExceedsCharacterLimit()
35+
{
36+
$this->expectException(LengthException::class);
37+
$this->expectExceptionMessage('Maximum length for the name is 256 characters.');
38+
39+
(new DiscordFieldEmbedObject())->name(str_repeat('h', 257));
40+
}
41+
42+
public function testThrowsWhenValueExceedsCharacterLimit()
43+
{
44+
$this->expectException(LengthException::class);
45+
$this->expectExceptionMessage('Maximum length for the value is 1024 characters.');
46+
47+
(new DiscordFieldEmbedObject())->value(str_repeat('h', 1025));
48+
}
49+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2< 10000 /code>+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord\Tests\Embeds;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordFooterEmbedObject;
16+
use Symfony\Component\Notifier\Exception\LengthException;
17+
18+
final class DiscordFooterEmbedObjectTest extends TestCase
19+
{
20+
public function testCanBeInstantiated()
21+
{
22+
$author = (new DiscordFooterEmbedObject())
23+
->text('foo')
24+
->iconUrl('http://icon-ur.l')
25+
->proxyIconUrl('http://proxy-icon-ur.l');
26+
27+
$this->assertSame([
28+
'text' => 'foo',
29+
'icon_url' => 'http://icon-ur.l',
30+
'proxy_icon_url' => 'http://proxy-icon-ur.l',
31+
], $author->toArray());
32+
}
33+
34+
public function testThrowsWhenTextExceedsCharacterLimit()
35+
{
36+
$this->expectException(LengthException::class);
37+
$this->expectExceptionMessage('Maximum length for the text is 2048 characters.');
38+
39+
(new DiscordFooterEmbedObject())->text(str_repeat('h', 2049));
40+
}
41+
}

0 commit comments

Comments
 (0)
0