Skip to content

Commit

Permalink
Add length limits to EmbedBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
l7ssha committed Jan 31, 2019
1 parent b9bf12b commit 4532f5d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
5 changes: 5 additions & 0 deletions lib/src/nyxx/builders/EmbedAuthorBuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ class EmbedAuthorBuilder implements Builder {
/// Author icon url
String iconUrl;

int get length => name.length;

@override

/// Builds object to Map() instance;
Map<String, dynamic> _build() {
if(this.length > 256)
throw new Exception("Author name is too long. (256 characters limit)");

Map<String, dynamic> tmp = Map();

if (name != null) tmp["name"] = name;
Expand Down
26 changes: 21 additions & 5 deletions lib/src/nyxx/builders/EmbedBuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class EmbedBuilder implements Builder {
EmbedAuthorBuilder author;

/// Embed custom fields;
List<Map<String, dynamic>> _fields;
List<EmbedFieldBuilder> _fields;

EmbedBuilder() {
_fields = List();
Expand All @@ -59,24 +59,40 @@ class EmbedBuilder implements Builder {
Function(EmbedFieldBuilder field) builder,
EmbedFieldBuilder field}) {
if (field != null) {
_fields.add(field._build());
_fields.add(field);
return;
}

if (builder != null) {
var tmp = EmbedFieldBuilder();
builder(tmp);
_fields.add(tmp._build());
_fields.add(tmp);
return;
}

_fields.add(EmbedFieldBuilder(name, content, inline)._build());
_fields.add(EmbedFieldBuilder(name, content, inline));
}

int get length {
return (this.title?.length ?? 0) + (this.description?.length ?? 0) + (this.footer?.length ?? 0) + (this.author?.length ?? 0) + _fields.map((embed) => embed.length).reduce((f, s) => f + s);
}

@override

/// Builds object to Map() instance;
Map<String, dynamic> _build() {
if(this.title != null && this.title.length > 256)
throw new Exception("Embed title is too long (256 characters limit)");

if(this.description != null && this.description.length > 2048)
throw new Exception("Embed description is too long (2048 characters limit)");

if(this._fields.length > 25)
throw new Exception("Embed cannot contain more than 25 fields");

if(this.length > 6000)
throw new Exception("Total length of embed cannot exceed 6000 characters");

Map<String, dynamic> tmp = Map();

if (title != null) tmp["title"] = title;
Expand All @@ -90,7 +106,7 @@ class EmbedBuilder implements Builder {
if (thumbnailUrl != null)
tmp["thumbnail"] = <String, dynamic>{"url": thumbnailUrl};
if (author != null) tmp["author"] = author._build();
if (_fields.length > 0) tmp["fields"] = _fields;
if (_fields.length > 0) tmp["fields"] = _fields.map((builder) => builder._build()).toList();

return tmp;
}
Expand Down
10 changes: 10 additions & 0 deletions lib/src/nyxx/builders/EmbedFieldBuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ class EmbedFieldBuilder implements Builder {
/// Constructs new instance of Field
EmbedFieldBuilder([this.name, this.content, this.inline]);

int get length {
return name.toString().length + content.toString().length;
}

@override

/// Builds object to Map() instance;
Map<String, dynamic> _build() {
if(this.name.toString().length > 256)
throw new Exception("Field name is too long. (256 characters limit)");

if(this.content.toString().length > 1024)
throw new Exception("Field content is too long. (1024 characters limit)");

Map<String, dynamic> tmp = Map();

if (name != null)
Expand Down
8 changes: 7 additions & 1 deletion lib/src/nyxx/builders/EmbedFooterBuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ part of nyxx;

/// Build new instance of Embed's footer
class EmbedFooterBuilder implements Builder {
/// Fotter text
/// Footer text
String text;

/// Url of footer icon. Supports only http(s) for now
String iconUrl;

/// Length of footer
int get length => text.length;

@override

/// Builds object to Map() instance;
Map<String, dynamic> _build() {
if(this.length > 2048)
throw new Exception("Footer text is too long. (1024 characters limit)");

Map<String, dynamic> tmp = Map();

if (text != null) tmp["text"] = text;
Expand Down

0 comments on commit 4532f5d

Please sign in to comment.