merge: pull request #251 from revoltchat/fix/zomatree/send-message-null

This commit is contained in:
Paul Makles
2023-06-04 19:29:07 +01:00
committed by GitHub
7 changed files with 31 additions and 26 deletions

View File

@@ -53,11 +53,7 @@ pub async fn req(
return Err(Error::CannotEditMessage); return Err(Error::CannotEditMessage);
} }
if let Some(new_embeds) = &edit.embeds { Message::validate_sum(&edit.content, edit.embeds.as_deref().unwrap_or_default())?;
Message::validate_sum(&edit.content, new_embeds)?;
} else {
Message::validate_sum(&edit.content, &vec![])?;
}
message.edited = Some(Timestamp::now_utc()); message.edited = Some(Timestamp::now_utc());
let mut partial = PartialMessage { let mut partial = PartialMessage {
@@ -85,7 +81,7 @@ pub async fn req(
new_embeds.clear(); new_embeds.clear();
for embed in embeds { for embed in embeds {
new_embeds.push(embed.clone().into_embed(db, message.id.clone()).await?); new_embeds.push(embed.clone().into_embed(db, &message.id).await?);
} }
} }

View File

@@ -47,14 +47,14 @@ pub async fn message_send(
} }
// Check permissions for embeds // Check permissions for embeds
if !data.embeds.is_empty() { if data.embeds.as_ref().is_some_and(|v| !v.is_empty()) {
permissions permissions
.throw_permission(db, Permission::SendEmbeds) .throw_permission(db, Permission::SendEmbeds)
.await?; .await?;
} }
// Check permissions for files // Check permissions for files
if !data.attachments.is_empty() { if data.attachments.as_ref().is_some_and(|v| !v.is_empty()) {
permissions permissions
.throw_permission(db, Permission::UploadFiles) .throw_permission(db, Permission::UploadFiles)
.await?; .await?;

View File

@@ -1065,7 +1065,7 @@ pub async fn webhook_execute_github(
let message_id = Ulid::new().to_string(); let message_id = Ulid::new().to_string();
let embed = sendable_embed let embed = sendable_embed
.into_embed(legacy_db, message_id.clone()) .into_embed(legacy_db, &message_id)
.await?; .await?;
let mut message = Message { let mut message = Message {

View File

@@ -11,7 +11,7 @@ use crate::{
}, },
tasks::{ack::AckEvent, process_embeds}, tasks::{ack::AckEvent, process_embeds},
types::push::MessageAuthor, types::push::MessageAuthor,
variables::delta::{MAX_ATTACHMENT_COUNT, MAX_REPLY_COUNT}, variables::delta::{MAX_ATTACHMENT_COUNT, MAX_REPLY_COUNT, MAX_EMBED_COUNT},
web::idempotency::IdempotencyKey, web::idempotency::IdempotencyKey,
Database, Error, OverrideField, Ref, Result, Database, Error, OverrideField, Ref, Result,
}; };
@@ -411,14 +411,14 @@ impl Channel {
mut idempotency: IdempotencyKey, mut idempotency: IdempotencyKey,
generate_embeds: bool, generate_embeds: bool,
) -> Result<Message> { ) -> Result<Message> {
Message::validate_sum(&data.content, &data.embeds)?; Message::validate_sum(&data.content, data.embeds.as_deref().unwrap_or_default())?;
idempotency.consume_nonce(data.nonce).await?; idempotency.consume_nonce(data.nonce).await?;
// Check the message is not empty // Check the message is not empty
if (data.content.as_ref().map_or(true, |v| v.is_empty())) if (data.content.as_ref().map_or(true, |v| v.is_empty()))
&& (data.attachments.is_empty()) && (data.attachments.as_ref().map_or(true, |v| v.is_empty()))
&& (data.embeds.is_empty()) && (data.embeds.as_ref().map_or(true, |v| v.is_empty()))
{ {
return Err(Error::EmptyMessage); return Err(Error::EmptyMessage);
} }
@@ -497,15 +497,21 @@ impl Channel {
// Add attachments to message. // Add attachments to message.
let mut attachments = vec![]; let mut attachments = vec![];
if data.attachments.len() > *MAX_ATTACHMENT_COUNT { if data.attachments.as_ref().is_some_and(|v| v.len() > *MAX_ATTACHMENT_COUNT) {
return Err(Error::TooManyAttachments { return Err(Error::TooManyAttachments {
max: *MAX_ATTACHMENT_COUNT, max: *MAX_ATTACHMENT_COUNT,
}); });
} }
for attachment_id in data.attachments { if data.embeds.as_ref().is_some_and(|v| v.len() > *MAX_EMBED_COUNT) {
return Err(Error::TooManyEmbeds {
max: *MAX_EMBED_COUNT
})
}
for attachment_id in data.attachments.as_deref().unwrap_or_default() {
attachments.push( attachments.push(
db.find_and_use_attachment(&attachment_id, "attachments", "message", &message_id) db.find_and_use_attachment(attachment_id, "attachments", "message", &message_id)
.await?, .await?,
); );
} }
@@ -516,8 +522,8 @@ impl Channel {
// Process included embeds. // Process included embeds.
let mut embeds = vec![]; let mut embeds = vec![];
for sendable_embed in data.embeds { for sendable_embed in data.embeds.unwrap_or_default() {
embeds.push(sendable_embed.into_embed(db, message_id.clone()).await?) embeds.push(sendable_embed.into_embed(db, &message_id).await?)
} }
if !embeds.is_empty() { if !embeds.is_empty() {

View File

@@ -170,7 +170,7 @@ impl Message {
} }
/// Validate the sum of content of a message is under threshold /// Validate the sum of content of a message is under threshold
pub fn validate_sum(content: &Option<String>, embeds: &Vec<SendableEmbed>) -> Result<()> { pub fn validate_sum(content: &Option<String>, embeds: &[SendableEmbed]) -> Result<()> {
let mut running_total = 0; let mut running_total = 0;
if let Some(content) = content { if let Some(content) = content {
running_total += content.len(); running_total += content.len();
@@ -353,13 +353,13 @@ impl From<SystemMessage> for String {
} }
impl SendableEmbed { impl SendableEmbed {
pub async fn into_embed(self, db: &Database, message_id: String) -> Result<Embed> { pub async fn into_embed(self, db: &Database, message_id: &str) -> Result<Embed> {
self.validate() self.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
let media = if let Some(id) = self.media { let media = if let Some(id) = self.media {
Some( Some(
db.find_and_use_attachment(&id, "attachments", "message", &message_id) db.find_and_use_attachment(&id, "attachments", "message", message_id)
.await?, .await?,
) )
} else { } else {

View File

@@ -269,16 +269,15 @@ pub struct DataMessageSend {
#[validate(length(min = 0, max = 2000))] #[validate(length(min = 0, max = 2000))]
pub content: Option<String>, pub content: Option<String>,
/// Attachments to include in message /// Attachments to include in message
#[serde(default)]
pub attachments: Vec<String>, pub attachments: Option<Vec<String>>,
/// Messages to reply to /// Messages to reply to
pub replies: Option<Vec<Reply>>, pub replies: Option<Vec<Reply>>,
/// Embeds to include in message /// Embeds to include in message
/// ///
/// Text embed content contributes to the content length cap /// Text embed content contributes to the content length cap
#[serde(default)] #[validate]
#[validate(length(min = 0, max = 10))] pub embeds: Option<Vec<SendableEmbed>>,
pub embeds: Vec<SendableEmbed>,
/// Masquerade to apply to this message /// Masquerade to apply to this message
#[validate] #[validate]
pub masquerade: Option<Masquerade>, pub masquerade: Option<Masquerade>,

View File

@@ -53,6 +53,9 @@ pub enum Error {
TooManyChannels { TooManyChannels {
max: usize, max: usize,
}, },
TooManyEmbeds {
max: usize,
},
EmptyMessage, EmptyMessage,
PayloadTooLarge, PayloadTooLarge,
CannotRemoveYourself, CannotRemoveYourself,
@@ -191,6 +194,7 @@ impl<'r> Responder<'r, 'static> for Error {
Error::TooManyEmoji { .. } => Status::BadRequest, Error::TooManyEmoji { .. } => Status::BadRequest,
Error::TooManyChannels { .. } => Status::BadRequest, Error::TooManyChannels { .. } => Status::BadRequest,
Error::TooManyRoles { .. } => Status::BadRequest, Error::TooManyRoles { .. } => Status::BadRequest,
Error::TooManyEmbeds { .. } => Status::BadRequest,
Error::ReachedMaximumBots => Status::BadRequest, Error::ReachedMaximumBots => Status::BadRequest,
Error::IsBot => Status::BadRequest, Error::IsBot => Status::BadRequest,