From af1930dc8d035b3e328315a0efb3aedfc5e2e16b Mon Sep 17 00:00:00 2001 From: Hocuri Date: Fri, 10 Apr 2020 16:10:26 +0200 Subject: [PATCH] Improving mailing lists and fixing things: Remove additional chat type MailingList, make them GroupLists with a special param, make can_send() return false, code style and deduplication --- src/chat.rs | 6 ++++- src/constants.rs | 1 - src/dc_receive_imf.rs | 60 ++++++++++++++++++++++--------------------- src/param.rs | 2 ++ 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 908a28282..4c3a65217 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -574,9 +574,13 @@ impl Chat { self.param.exists(Param::Devicetalk) } + pub fn is_mailing_list(&self) -> bool { + self.param.exists(Param::MailingList) + } + /// Returns true if user can send messages to this chat. pub fn can_send(&self) -> bool { - !self.id.is_special() && !self.is_device_talk() + !self.id.is_special() && !self.is_device_talk() && !self.is_mailing_list() } pub fn update_param(&mut self, context: &Context) -> Result<(), Error> { diff --git a/src/constants.rs b/src/constants.rs index ce71a8038..56d289f49 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -126,7 +126,6 @@ pub enum Chattype { Single = 100, Group = 120, VerifiedGroup = 130, - MailingList = 140, } impl Default for Chattype { diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index ff82149ef..3a2855976 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -1131,7 +1131,10 @@ fn create_or_lookup_mailinglist( let re = Regex::new(r"^(.*.)<(.*.)>$").unwrap(); let (name, listid) = match re.captures(list_id_header) { Some(cap) => (cap[1].trim().to_string(), cap[2].trim().to_string()), - None => (list_id_header.trim().to_string(), list_id_header.trim().to_string()), + None => ( + list_id_header.trim().to_string(), + list_id_header.trim().to_string(), + ), }; match chat::get_chat_id_by_mailinglistid(context, &listid) { @@ -1145,7 +1148,18 @@ fn create_or_lookup_mailinglist( return Ok((ChatId::new(0), Blocked::Not)); } - let chat_id = create_mailinglist_record(context, &listid, &name, create_blocked); + let chat_id = create_mailinglist_record(context, &listid, &name, create_blocked) + .unwrap_or_else(|e| { + warn!( + context, + "Failed to create group '{}' for grpid={}: {}", + &name, + &listid, + e.to_string() + ); + ChatId::new(0) + }); + println!("err: new chatid {:?}", chat_id); Ok((chat_id, create_blocked)) } } @@ -1318,32 +1332,14 @@ fn create_mailinglist_record( listid: impl AsRef, name: impl AsRef, create_blocked: Blocked, -) -> ChatId { - if let Err(e) = sql::execute( +) -> Result { + let chat_id = create_group_record( context, - &context.sql, - "INSERT INTO chats (type, name, grpid, blocked, created_timestamp) VALUES(?, ?, ?, ?, ?);", - params![ - Chattype::MailingList, - name.as_ref(), - listid.as_ref(), - create_blocked, - time(), - ], - ) - { - println!("{:?}", e); - warn!( - context, - "Failed to create mailinglist '{}' for listid={}", - name.as_ref(), - listid.as_ref() - ); - return ChatId::new(0); - } - let row_id = sql::get_rowid(context, &context.sql, "chats", "grpid", listid.as_ref()); - println!("row_id {}", row_id); - let chat_id = ChatId::new(row_id); + &listid, + &name, + create_blocked, + VerifiedStatus::Unverified, + ); info!( context, "Created mailinglist '{}' listid={} as {}", @@ -1351,7 +1347,12 @@ fn create_mailinglist_record( listid.as_ref(), chat_id ); - chat_id + let mut chat = Chat::load_from_db(context, chat_id)?; + + chat.param.set(Param::MailingList, "true"); + chat.update_param(context)?; + + Ok(chat_id) } fn create_adhoc_grp_id(context: &Context, member_ids: &[u32]) -> String { @@ -1778,7 +1779,8 @@ mod tests { assert_eq!(chats.len(), 1); let chat_id = chat::create_by_msg_id(&t.ctx, chats.get_msg_id(0).unwrap()).unwrap(); let chat = chat::Chat::load_from_db(&t.ctx, chat_id).unwrap(); - assert_eq!(chat.typ, Chattype::MailingList); + assert!(chat.is_mailing_list()); + assert_eq!(chat.can_send(), false); assert_eq!(chat.name, "deltachat/deltachat-core-rust"); assert_eq!(chat::get_chat_contacts(&t.ctx, chat_id).len(), 0); } diff --git a/src/param.rs b/src/param.rs index 8fe9ce97c..fc0d02ae3 100644 --- a/src/param.rs +++ b/src/param.rs @@ -117,6 +117,8 @@ pub enum Param { /// For MDN-sending job MsgId = b'I', + + MailingList = b't' } /// Possible values for `Param::ForcePlaintext`.