From b771e83e9773fee5b45bdc9c0996dc89d5487720 Mon Sep 17 00:00:00 2001 From: Akinator31 Date: Thu, 27 Nov 2025 22:20:57 +0100 Subject: [PATCH] fix(thread): add warning when attachments are too big --- .../src/handlers/guild_messages_handler.rs | 20 +++++++++++++++++++ rustmail/src/i18n/language/en.rs | 4 ++++ rustmail/src/i18n/language/fr.rs | 4 ++++ rustmail/src/modules/threads.rs | 19 ++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/rustmail/src/handlers/guild_messages_handler.rs b/rustmail/src/handlers/guild_messages_handler.rs index 9b2210df..06b10cab 100644 --- a/rustmail/src/handlers/guild_messages_handler.rs +++ b/rustmail/src/handlers/guild_messages_handler.rs @@ -143,6 +143,26 @@ async fn manage_incoming_message( let channel_id = ChannelId::new(channel_id_num); + const MAX_ATTACHMENT_SIZE: u32 = 8 * 1024 * 1024; + for attachment in &msg.attachments { + if attachment.size > MAX_ATTACHMENT_SIZE { + let _ = MessageBuilder::system_message(ctx, config) + .translated_content( + "discord.attachment_too_large", + None, + Some(msg.author.id), + None, + ) + .await + .to_user(msg.author.id) + .send(true) + .await; + + drop(guard); + return Ok(()); + } + } + if let Err(e) = send_to_thread(ctx, channel_id, msg, config, false).await { let error = validation_failed(&format!("Failed to forward message: {}", e)); let _ = error_handler diff --git a/rustmail/src/i18n/language/en.rs b/rustmail/src/i18n/language/en.rs index af3c327e..f5026f08 100644 --- a/rustmail/src/i18n/language/en.rs +++ b/rustmail/src/i18n/language/en.rs @@ -43,6 +43,10 @@ pub fn load_english_messages(dict: &mut ErrorDictionary) { DictionaryMessage::new("Discord API error") .with_description("An error occurred while communicating with Discord"), ); + dict.messages.insert( + "discord.attachment_too_large".to_string(), + DictionaryMessage::new("Your attachment is too large! Discord has a file size limit of 8 MB for attachments. Please reduce the file size or send a link."), + ); dict.messages.insert( "discord.user_is_a_bot".to_string(), DictionaryMessage::new("The specified user is a rustmail."), diff --git a/rustmail/src/i18n/language/fr.rs b/rustmail/src/i18n/language/fr.rs index 4135a6a4..7d4cc5b3 100644 --- a/rustmail/src/i18n/language/fr.rs +++ b/rustmail/src/i18n/language/fr.rs @@ -41,6 +41,10 @@ pub fn load_french_messages(dict: &mut ErrorDictionary) { DictionaryMessage::new("Erreur de l'API Discord") .with_description("Une erreur s'est produite lors de la communication avec Discord"), ); + dict.messages.insert( + "discord.attachment_too_large".to_string(), + DictionaryMessage::new("Votre pièce jointe est trop volumineuse ! Discord a une limite de taille de fichier de 8 Mo pour les pièces jointes. Veuillez réduire la taille du fichier ou envoyer un lien."), + ); dict.messages.insert( "discord.user_is_a_bot".to_string(), DictionaryMessage::new("L'utilisateur spécifié est un rustmail"), diff --git a/rustmail/src/modules/threads.rs b/rustmail/src/modules/threads.rs index 71501c14..57adcab5 100644 --- a/rustmail/src/modules/threads.rs +++ b/rustmail/src/modules/threads.rs @@ -140,6 +140,25 @@ pub async fn create_channel(ctx: &Context, msg: &Message, config: &Config) { return; } + const MAX_ATTACHMENT_SIZE: u32 = 8 * 1024 * 1024; + for attachment in &msg.attachments { + if attachment.size > MAX_ATTACHMENT_SIZE { + let _ = MessageBuilder::system_message(ctx, config) + .translated_content( + "discord.attachment_too_large", + None, + Some(msg.author.id), + None, + ) + .await + .to_user(msg.author.id) + .send(true) + .await; + + return; + } + } + let (target_channel_id, _is_new_thread) = match create_or_get_thread_for_user(ctx, config, msg.author.id).await { Ok(res) => res,