Join recipients_names and recipients_addr into one vector

This change ensures on the type level that number of names and addresses
is the same.
This commit is contained in:
Alexander Krotov
2020-01-08 20:38:27 +03:00
parent 5b424aec22
commit bd70765b7d
2 changed files with 33 additions and 35 deletions

View File

@@ -25,7 +25,7 @@ use crate::location;
use crate::login_param::LoginParam; use crate::login_param::LoginParam;
use crate::message::MsgId; use crate::message::MsgId;
use crate::message::{self, Message, MessageState}; use crate::message::{self, Message, MessageState};
use crate::mimefactory::{vec_contains_lowercase, MimeFactory, RenderedEmail}; use crate::mimefactory::{MimeFactory, RenderedEmail};
use crate::param::*; use crate::param::*;
use crate::sql; use crate::sql;
@@ -683,8 +683,12 @@ pub fn job_send_msg(context: &Context, msg_id: MsgId) -> Result<()> {
); );
} }
let lowercase_from = rendered_msg.from.to_lowercase();
if context.get_config_bool(Config::BccSelf) if context.get_config_bool(Config::BccSelf)
&& !vec_contains_lowercase(&rendered_msg.recipients, &rendered_msg.from) && !rendered_msg
.recipients
.iter()
.any(|x| x.to_lowercase() == lowercase_from)
{ {
rendered_msg.recipients.push(rendered_msg.from.clone()); rendered_msg.recipients.push(rendered_msg.from.clone());
} }

View File

@@ -30,8 +30,10 @@ pub struct MimeFactory<'a, 'b> {
from_addr: String, from_addr: String,
from_displayname: String, from_displayname: String,
selfstatus: String, selfstatus: String,
recipients_names: Vec<String>,
recipients_addr: Vec<String>, /// Vector of pairs of recipient name and address
recipients: Vec<(String, String)>,
timestamp: i64, timestamp: i64,
loaded: Loaded, loaded: Loaded,
msg: &'b Message, msg: &'b Message,
@@ -74,13 +76,11 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
.get_config(Config::ConfiguredAddr) .get_config(Config::ConfiguredAddr)
.unwrap_or_default(); .unwrap_or_default();
let from_displayname = context.get_config(Config::Displayname).unwrap_or_default(); let from_displayname = context.get_config(Config::Displayname).unwrap_or_default();
let mut recipients_names = Vec::with_capacity(5); let mut recipients = Vec::with_capacity(5);
let mut recipients_addr = Vec::with_capacity(5);
let mut req_mdn = false; let mut req_mdn = false;
if chat.is_self_talk() { if chat.is_self_talk() {
recipients_names.push(from_displayname.to_string()); recipients.push((from_displayname.to_string(), from_addr.to_string()));
recipients_addr.push(from_addr.to_string());
} else { } else {
context.sql.query_map( context.sql.query_map(
"SELECT c.authname, c.addr \ "SELECT c.authname, c.addr \
@@ -96,9 +96,8 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
|rows| { |rows| {
for row in rows { for row in rows {
let (authname, addr) = row?; let (authname, addr) = row?;
if !vec_contains_lowercase(&recipients_addr, &addr) { if !recipients_contain_addr(&recipients, &addr) {
recipients_addr.push(addr); recipients.push((authname, addr));
recipients_names.push(authname);
} }
} }
Ok(()) Ok(())
@@ -117,10 +116,9 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
if !email_to_remove.is_empty() if !email_to_remove.is_empty()
&& !addr_cmp(email_to_remove, self_addr) && !addr_cmp(email_to_remove, self_addr)
&& !vec_contains_lowercase(&recipients_addr, &email_to_remove) && !recipients_contain_addr(&recipients, &email_to_remove)
{ {
recipients_names.push("".to_string()); recipients.push(("".to_string(), email_to_remove.to_string()));
recipients_addr.push(email_to_remove.to_string());
} }
} }
if command != SystemMessage::AutocryptSetupMessage if command != SystemMessage::AutocryptSetupMessage
@@ -150,8 +148,7 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
selfstatus: context selfstatus: context
.get_config(Config::Selfstatus) .get_config(Config::Selfstatus)
.unwrap_or_else(|| context.stock_str(StockMessage::StatusLine).to_string()), .unwrap_or_else(|| context.stock_str(StockMessage::StatusLine).to_string()),
recipients_names, recipients,
recipients_addr,
timestamp: msg.timestamp_sort, timestamp: msg.timestamp_sort,
loaded: Loaded::Message { chat }, loaded: Loaded::Message { chat },
msg, msg,
@@ -190,8 +187,10 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
selfstatus: context selfstatus: context
.get_config(Config::Selfstatus) .get_config(Config::Selfstatus)
.unwrap_or_else(|| context.stock_str(StockMessage::StatusLine).to_string()), .unwrap_or_else(|| context.stock_str(StockMessage::StatusLine).to_string()),
recipients_names: vec![contact.get_authname().to_string()], recipients: vec![(
recipients_addr: vec![contact.get_addr().to_string()], contact.get_authname().to_string(),
contact.get_addr().to_string(),
)],
timestamp: dc_create_smeared_timestamp(context), timestamp: dc_create_smeared_timestamp(context),
loaded: Loaded::MDN, loaded: Loaded::MDN,
msg, msg,
@@ -211,10 +210,10 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
.ok_or_else(|| format_err!("Not configured"))?; .ok_or_else(|| format_err!("Not configured"))?;
Ok(self Ok(self
.recipients_addr .recipients
.iter() .iter()
.filter(|addr| *addr != &self_addr) .filter(|(_, addr)| addr != &self_addr)
.map(|addr| { .map(|(_, addr)| {
( (
Peerstate::from_addr(self.context, &self.context.sql, addr), Peerstate::from_addr(self.context, &self.context.sql, addr),
addr.as_str(), addr.as_str(),
@@ -378,10 +377,8 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
self.from_addr.clone(), self.from_addr.clone(),
); );
let mut to = Vec::with_capacity(self.recipients_names.len()); let mut to = Vec::with_capacity(self.recipients.len());
let name_iter = self.recipients_names.iter(); for (name, addr) in self.recipients.iter() {
let addr_iter = self.recipients_addr.iter();
for (name, addr) in name_iter.zip(addr_iter) {
if name.is_empty() { if name.is_empty() {
to.push(Address::new_mailbox(addr.clone())); to.push(Address::new_mailbox(addr.clone()));
} else { } else {
@@ -574,7 +571,7 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
}; };
let MimeFactory { let MimeFactory {
recipients_addr, recipients,
from_addr, from_addr,
last_added_location_id, last_added_location_id,
msg, msg,
@@ -592,7 +589,7 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
Loaded::Message { .. } => Some(msg.id), Loaded::Message { .. } => Some(msg.id),
Loaded::MDN => None, Loaded::MDN => None,
}, },
recipients: recipients_addr, recipients: recipients.into_iter().map(|(_, addr)| addr).collect(),
from: from_addr, from: from_addr,
rfc724_mid, rfc724_mid,
}) })
@@ -1088,14 +1085,11 @@ fn build_selfavatar_file(context: &Context, path: String) -> Result<(PartBuilder
Ok((part, filename_to_send)) Ok((part, filename_to_send))
} }
pub(crate) fn vec_contains_lowercase(vec: &[String], part: &str) -> bool { fn recipients_contain_addr(recipients: &[(String, String)], addr: &str) -> bool {
let partlc = part.to_lowercase(); let addr_lc = addr.to_lowercase();
for cur in vec.iter() { recipients
if cur.to_lowercase() == partlc { .iter()
return true; .any(|(_, cur)| cur.to_lowercase() == addr_lc)
}
}
false
} }
fn is_file_size_okay(context: &Context, msg: &Message) -> bool { fn is_file_size_okay(context: &Context, msg: &Message) -> bool {