feat(messaging): cap total text content to 2k characters
This commit is contained in:
@@ -36,6 +36,8 @@ pub async fn req(
|
|||||||
edit.validate()
|
edit.validate()
|
||||||
.map_err(|error| Error::FailedValidation { error })?;
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|
||||||
|
Message::validate_sum(&edit.content, &edit.embeds)?;
|
||||||
|
|
||||||
let mut message = msg.as_message(db).await?;
|
let mut message = msg.as_message(db).await?;
|
||||||
if message.channel != target {
|
if message.channel != target {
|
||||||
return Err(Error::NotFound);
|
return Err(Error::NotFound);
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ pub struct DataMessageSend {
|
|||||||
/// Messages to reply to
|
/// Messages to reply to
|
||||||
replies: Option<Vec<Reply>>,
|
replies: Option<Vec<Reply>>,
|
||||||
/// Embeds to include in message
|
/// Embeds to include in message
|
||||||
|
///
|
||||||
|
/// Text embed content contributes to the content length cap
|
||||||
#[validate(length(min = 1, max = 10))]
|
#[validate(length(min = 1, max = 10))]
|
||||||
embeds: Option<Vec<SendableEmbed>>,
|
embeds: Option<Vec<SendableEmbed>>,
|
||||||
/// Masquerade to apply to this message
|
/// Masquerade to apply to this message
|
||||||
@@ -61,6 +63,8 @@ pub async fn message_send(
|
|||||||
data.validate()
|
data.validate()
|
||||||
.map_err(|error| Error::FailedValidation { error })?;
|
.map_err(|error| Error::FailedValidation { error })?;
|
||||||
|
|
||||||
|
Message::validate_sum(&data.content, &data.embeds)?;
|
||||||
|
|
||||||
idempotency.consume_nonce(data.nonce).await?;
|
idempotency.consume_nonce(data.nonce).await?;
|
||||||
|
|
||||||
let channel = target.as_channel(db).await?;
|
let channel = target.as_channel(db).await?;
|
||||||
|
|||||||
@@ -166,6 +166,31 @@ impl Message {
|
|||||||
.await;
|
.await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Validate the sum of content of a message is under threshold
|
||||||
|
pub fn validate_sum(
|
||||||
|
content: &Option<String>,
|
||||||
|
embeds: &Option<Vec<SendableEmbed>>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let mut running_total = 0;
|
||||||
|
if let Some(content) = content {
|
||||||
|
running_total += content.len();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(embeds) = embeds {
|
||||||
|
for embed in embeds {
|
||||||
|
if let Some(desc) = &embed.description {
|
||||||
|
running_total += desc.len();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if running_total <= 2000 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(Error::PayloadTooLarge)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait IntoUsers {
|
pub trait IntoUsers {
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ pub enum Error {
|
|||||||
TooManyAttachments,
|
TooManyAttachments,
|
||||||
TooManyReplies,
|
TooManyReplies,
|
||||||
EmptyMessage,
|
EmptyMessage,
|
||||||
|
PayloadTooLarge,
|
||||||
CannotRemoveYourself,
|
CannotRemoveYourself,
|
||||||
GroupTooLarge {
|
GroupTooLarge {
|
||||||
max: usize,
|
max: usize,
|
||||||
@@ -145,6 +146,7 @@ impl<'r> Responder<'r, 'static> for Error {
|
|||||||
Error::TooManyAttachments => Status::BadRequest,
|
Error::TooManyAttachments => Status::BadRequest,
|
||||||
Error::TooManyReplies => Status::BadRequest,
|
Error::TooManyReplies => Status::BadRequest,
|
||||||
Error::EmptyMessage => Status::UnprocessableEntity,
|
Error::EmptyMessage => Status::UnprocessableEntity,
|
||||||
|
Error::PayloadTooLarge => Status::UnprocessableEntity,
|
||||||
Error::CannotRemoveYourself => Status::BadRequest,
|
Error::CannotRemoveYourself => Status::BadRequest,
|
||||||
Error::GroupTooLarge { .. } => Status::Forbidden,
|
Error::GroupTooLarge { .. } => Status::Forbidden,
|
||||||
Error::AlreadyInGroup => Status::Conflict,
|
Error::AlreadyInGroup => Status::Conflict,
|
||||||
|
|||||||
Reference in New Issue
Block a user