mirror of
https://github.com/chatmail/core.git
synced 2026-05-09 01:46:30 +03:00
use tri-state ImageAction instead of Option<Option>
This commit is contained in:
committed by
holger krekel
parent
c62532a665
commit
c3fd0889e2
@@ -15,6 +15,7 @@ use crate::events::Event;
|
|||||||
use crate::key::*;
|
use crate::key::*;
|
||||||
use crate::login_param::LoginParam;
|
use crate::login_param::LoginParam;
|
||||||
use crate::message::{MessageState, MsgId};
|
use crate::message::{MessageState, MsgId};
|
||||||
|
use crate::mimeparser::ImageAction;
|
||||||
use crate::param::*;
|
use crate::param::*;
|
||||||
use crate::peerstate::*;
|
use crate::peerstate::*;
|
||||||
use crate::sql;
|
use crate::sql;
|
||||||
@@ -965,17 +966,26 @@ fn set_block_contact(context: &Context, contact_id: u32, new_blocking: bool) {
|
|||||||
pub fn set_profile_image(
|
pub fn set_profile_image(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
contact_id: u32,
|
contact_id: u32,
|
||||||
profile_image: Option<String>,
|
profile_image: ImageAction,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// the given profile image is expected to be already in the blob directory
|
// 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.
|
// 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)?;
|
let mut contact = Contact::load_from_db(context, contact_id)?;
|
||||||
match profile_image {
|
let changed = match profile_image {
|
||||||
Some(profile_image) => contact.param.set(Param::ProfileImage, profile_image),
|
ImageAction::Change(profile_image) => {
|
||||||
None => contact.param.remove(Param::ProfileImage),
|
contact.param.set(Param::ProfileImage, profile_image);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
ImageAction::Delete => {
|
||||||
|
contact.param.remove(Param::ProfileImage);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
ImageAction::None => false,
|
||||||
};
|
};
|
||||||
|
if changed {
|
||||||
contact.update_param(context)?;
|
contact.update_param(context)?;
|
||||||
context.call_cb(Event::ContactsChanged(Some(contact_id)));
|
context.call_cb(Event::ContactsChanged(Some(contact_id)));
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -238,8 +238,8 @@ pub fn dc_receive_imf(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(profile_image) = mime_parser.profile_image {
|
if mime_parser.profile_image != ImageAction::None {
|
||||||
match contact::set_profile_image(&context, from_id, profile_image) {
|
match contact::set_profile_image(&context, from_id, mime_parser.profile_image) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
context.call_cb(Event::ChatModified(chat_id));
|
context.call_cb(Event::ChatModified(chat_id));
|
||||||
true
|
true
|
||||||
|
|||||||
@@ -37,12 +37,19 @@ pub struct MimeParser<'a> {
|
|||||||
pub is_system_message: SystemMessage,
|
pub is_system_message: SystemMessage,
|
||||||
pub location_kml: Option<location::Kml>,
|
pub location_kml: Option<location::Kml>,
|
||||||
pub message_kml: Option<location::Kml>,
|
pub message_kml: Option<location::Kml>,
|
||||||
pub profile_image: Option<Option<String>>,
|
pub profile_image: ImageAction,
|
||||||
reports: Vec<Report>,
|
reports: Vec<Report>,
|
||||||
mdns_enabled: bool,
|
mdns_enabled: bool,
|
||||||
parsed_protected_headers: 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)]
|
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, ToSql, FromSql)]
|
||||||
#[repr(i32)]
|
#[repr(i32)]
|
||||||
pub enum SystemMessage {
|
pub enum SystemMessage {
|
||||||
@@ -84,7 +91,7 @@ impl<'a> MimeParser<'a> {
|
|||||||
is_system_message: SystemMessage::Unknown,
|
is_system_message: SystemMessage::Unknown,
|
||||||
location_kml: None,
|
location_kml: None,
|
||||||
message_kml: None,
|
message_kml: None,
|
||||||
profile_image: None,
|
profile_image: ImageAction::None,
|
||||||
mdns_enabled,
|
mdns_enabled,
|
||||||
parsed_protected_headers: false,
|
parsed_protected_headers: false,
|
||||||
};
|
};
|
||||||
@@ -195,7 +202,7 @@ impl<'a> MimeParser<'a> {
|
|||||||
|
|
||||||
if let Some(header_value) = self.get(HeaderDef::ChatProfileImage) {
|
if let Some(header_value) = self.get(HeaderDef::ChatProfileImage) {
|
||||||
if header_value == "0" {
|
if header_value == "0" {
|
||||||
self.profile_image = Some(None);
|
self.profile_image = ImageAction::Delete;
|
||||||
} else {
|
} else {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i != self.parts.len() {
|
while i != self.parts.len() {
|
||||||
@@ -203,7 +210,7 @@ impl<'a> MimeParser<'a> {
|
|||||||
if let Some(part_filename) = &part.org_filename {
|
if let Some(part_filename) = &part.org_filename {
|
||||||
if part_filename == header_value {
|
if part_filename == header_value {
|
||||||
if let Some(blob) = part.param.get(Param::File) {
|
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);
|
self.parts.remove(i);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -1157,16 +1164,19 @@ mod tests {
|
|||||||
fn test_mimeparser_with_profile_image() {
|
fn test_mimeparser_with_profile_image() {
|
||||||
let t = dummy_context();
|
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 raw = include_bytes!("../test-data/message/mail_with_profile_image.eml");
|
||||||
let mimeparser = MimeParser::from_bytes(&t.ctx, &raw[..]).unwrap();
|
let mimeparser = MimeParser::from_bytes(&t.ctx, &raw[..]).unwrap();
|
||||||
assert_eq!(mimeparser.parts.len(), 1);
|
assert_eq!(mimeparser.parts.len(), 1);
|
||||||
assert!(mimeparser.profile_image.is_some());
|
assert_ne!(mimeparser.profile_image, ImageAction::None);
|
||||||
assert!(mimeparser.profile_image.unwrap().is_some());
|
assert_ne!(mimeparser.profile_image, ImageAction::Delete);
|
||||||
|
|
||||||
let raw = include_bytes!("../test-data/message/mail_with_profile_image_deleted.eml");
|
let raw = include_bytes!("../test-data/message/mail_with_profile_image_deleted.eml");
|
||||||
let mimeparser = MimeParser::from_bytes(&t.ctx, &raw[..]).unwrap();
|
let mimeparser = MimeParser::from_bytes(&t.ctx, &raw[..]).unwrap();
|
||||||
assert_eq!(mimeparser.parts.len(), 1);
|
assert_eq!(mimeparser.parts.len(), 1);
|
||||||
assert!(mimeparser.profile_image.is_some());
|
assert_eq!(mimeparser.profile_image, ImageAction::Delete);
|
||||||
assert!(mimeparser.profile_image.unwrap().is_none());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user