diff --git a/src/contact.rs b/src/contact.rs index 3bb0dca7a..df0c76970 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -15,6 +15,7 @@ use crate::events::Event; use crate::key::*; use crate::login_param::LoginParam; use crate::message::{MessageState, MsgId}; +use crate::mimeparser::ImageAction; use crate::param::*; use crate::peerstate::*; use crate::sql; @@ -965,17 +966,26 @@ fn set_block_contact(context: &Context, contact_id: u32, new_blocking: bool) { pub fn set_profile_image( context: &Context, contact_id: u32, - profile_image: Option, + profile_image: ImageAction, ) -> Result<()> { // the given profile image is expected to be already in the blob directory // as profile images can be set only by receiving messages, this should be always the case, however. let mut contact = Contact::load_from_db(context, contact_id)?; - match profile_image { - Some(profile_image) => contact.param.set(Param::ProfileImage, profile_image), - None => contact.param.remove(Param::ProfileImage), + let changed = match profile_image { + ImageAction::Change(profile_image) => { + contact.param.set(Param::ProfileImage, profile_image); + true + } + ImageAction::Delete => { + contact.param.remove(Param::ProfileImage); + true + } + ImageAction::None => false, }; - contact.update_param(context)?; - context.call_cb(Event::ContactsChanged(Some(contact_id))); + if changed { + contact.update_param(context)?; + context.call_cb(Event::ContactsChanged(Some(contact_id))); + } Ok(()) } diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index ae687705b..ddb3c1e86 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -238,8 +238,8 @@ pub fn dc_receive_imf( ); } - if let Some(profile_image) = mime_parser.profile_image { - match contact::set_profile_image(&context, from_id, profile_image) { + if mime_parser.profile_image != ImageAction::None { + match contact::set_profile_image(&context, from_id, mime_parser.profile_image) { Ok(()) => { context.call_cb(Event::ChatModified(chat_id)); true diff --git a/src/mimeparser.rs b/src/mimeparser.rs index 6ba76305f..ea4a05bc8 100644 --- a/src/mimeparser.rs +++ b/src/mimeparser.rs @@ -37,12 +37,19 @@ pub struct MimeParser<'a> { pub is_system_message: SystemMessage, pub location_kml: Option, pub message_kml: Option, - pub profile_image: Option>, + pub profile_image: ImageAction, reports: Vec, mdns_enabled: bool, parsed_protected_headers: bool, } +#[derive(Debug, PartialEq)] +pub enum ImageAction { + None, + Delete, + Change(String), +} + #[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, ToSql, FromSql)] #[repr(i32)] pub enum SystemMessage { @@ -84,7 +91,7 @@ impl<'a> MimeParser<'a> { is_system_message: SystemMessage::Unknown, location_kml: None, message_kml: None, - profile_image: None, + profile_image: ImageAction::None, mdns_enabled, parsed_protected_headers: false, }; @@ -195,7 +202,7 @@ impl<'a> MimeParser<'a> { if let Some(header_value) = self.get(HeaderDef::ChatProfileImage) { if header_value == "0" { - self.profile_image = Some(None); + self.profile_image = ImageAction::Delete; } else { let mut i = 0; while i != self.parts.len() { @@ -203,7 +210,7 @@ impl<'a> MimeParser<'a> { if let Some(part_filename) = &part.org_filename { if part_filename == header_value { if let Some(blob) = part.param.get(Param::File) { - self.profile_image = Some(Some(blob.to_string())); + self.profile_image = ImageAction::Change(blob.to_string()); self.parts.remove(i); } break; @@ -1157,16 +1164,19 @@ mod tests { fn test_mimeparser_with_profile_image() { let t = dummy_context(); + let raw = include_bytes!("../test-data/message/mail_attach_txt.eml"); + let mimeparser = MimeParser::from_bytes(&t.ctx, &raw[..]).unwrap(); + assert_eq!(mimeparser.profile_image, ImageAction::None); + let raw = include_bytes!("../test-data/message/mail_with_profile_image.eml"); let mimeparser = MimeParser::from_bytes(&t.ctx, &raw[..]).unwrap(); assert_eq!(mimeparser.parts.len(), 1); - assert!(mimeparser.profile_image.is_some()); - assert!(mimeparser.profile_image.unwrap().is_some()); + assert_ne!(mimeparser.profile_image, ImageAction::None); + assert_ne!(mimeparser.profile_image, ImageAction::Delete); let raw = include_bytes!("../test-data/message/mail_with_profile_image_deleted.eml"); let mimeparser = MimeParser::from_bytes(&t.ctx, &raw[..]).unwrap(); assert_eq!(mimeparser.parts.len(), 1); - assert!(mimeparser.profile_image.is_some()); - assert!(mimeparser.profile_image.unwrap().is_none()); + assert_eq!(mimeparser.profile_image, ImageAction::Delete); } }