Correct failed recipient

This commit is contained in:
Hocuri
2020-06-09 19:28:49 +02:00
parent 2e59d5674e
commit bd2a7a3d40
4 changed files with 63 additions and 46 deletions

View File

@@ -2784,7 +2784,7 @@ pub(crate) async fn delete_and_reset_all_device_msgs(context: &Context) -> Resul
pub(crate) async fn add_info_msg(context: &Context, chat_id: ChatId, text: impl AsRef<str>) { pub(crate) async fn add_info_msg(context: &Context, chat_id: ChatId, text: impl AsRef<str>) {
let rfc724_mid = dc_create_outgoing_rfc724_mid(None, "@device"); let rfc724_mid = dc_create_outgoing_rfc724_mid(None, "@device");
if context.sql.execute( if let Err(e) = context.sql.execute(
"INSERT INTO msgs (chat_id,from_id,to_id, timestamp,type,state, txt,rfc724_mid) VALUES (?,?,?, ?,?,?, ?,?);", "INSERT INTO msgs (chat_id,from_id,to_id, timestamp,type,state, txt,rfc724_mid) VALUES (?,?,?, ?,?,?, ?,?);",
paramsv![ paramsv![
chat_id, chat_id,
@@ -2796,7 +2796,8 @@ pub(crate) async fn add_info_msg(context: &Context, chat_id: ChatId, text: impl
text.as_ref().to_string(), text.as_ref().to_string(),
rfc724_mid, rfc724_mid,
] ]
).await.is_err() { ).await {
error!(context, "Could not add info msg: {}", e);
return; return;
} }

View File

@@ -2441,12 +2441,15 @@ mod tests {
assert_eq!(msg.state, MessageState::OutFailed); assert_eq!(msg.state, MessageState::OutFailed);
let msgs = chat::get_chat_msgs(&t.ctx, msg.chat_id, 0, None).await; let msgs = chat::get_chat_msgs(&t.ctx, msg.chat_id, 0, None).await;
let mut found = false; println!("Loading {}", msg.chat_id);
for id in msgs.iter() { let last_msg = Message::load_from_db(&t.ctx, *msgs.last().unwrap())
let m = Message::load_from_db(&t.ctx, *id).await.unwrap(); .await
if m.from_id == DC_CONTACT_ID_INFO .unwrap();
&& m.text
== Some( assert_eq!(last_msg.from_id, DC_CONTACT_ID_INFO);
assert_eq!(
last_msg.text,
Some(
t.ctx t.ctx
.stock_string_repl_str( .stock_string_repl_str(
StockMessage::FailedSendingTo, StockMessage::FailedSendingTo,
@@ -2454,10 +2457,6 @@ mod tests {
) )
.await, .await,
) )
{ );
found = true;
}
}
assert!(found);
} }
} }

View File

@@ -14,7 +14,7 @@ use crate::error::{ensure, Error};
use crate::events::Event; use crate::events::Event;
use crate::job::{self, Action}; use crate::job::{self, Action};
use crate::lot::{Lot, LotState, Meaning}; use crate::lot::{Lot, LotState, Meaning};
use crate::mimeparser::SystemMessage; use crate::mimeparser::{FailedMsg, SystemMessage};
use crate::param::*; use crate::param::*;
use crate::pgp::*; use crate::pgp::*;
use crate::stock::StockMessage; use crate::stock::StockMessage;
@@ -1392,13 +1392,12 @@ pub async fn mdn_from_ext(
None None
} }
pub async fn ndn_from_ext( pub(crate) async fn ndn_from_ext(
context: &Context, context: &Context,
from_id: u32, failed: &FailedMsg,
rfc724_mid: &str,
error: Option<impl AsRef<str>>, error: Option<impl AsRef<str>>,
) { ) {
if from_id <= DC_MSG_ID_LAST_SPECIAL || rfc724_mid.is_empty() { if failed.rfc724_mid.is_empty() {
return; return;
} }
@@ -1410,17 +1409,15 @@ pub async fn ndn_from_ext(
" m.id AS msg_id,", " m.id AS msg_id,",
" c.id AS chat_id,", " c.id AS chat_id,",
" c.type AS type,", " c.type AS type,",
" m.to_id AS to_id",
" FROM msgs m LEFT JOIN chats c ON m.chat_id=c.id", " FROM msgs m LEFT JOIN chats c ON m.chat_id=c.id",
" WHERE rfc724_mid=? AND from_id=1", " WHERE rfc724_mid=? AND from_id=1",
), ),
paramsv![rfc724_mid], paramsv![failed.rfc724_mid],
|row| { |row| {
Ok(( Ok((
row.get::<_, MsgId>("msg_id")?, row.get::<_, MsgId>("msg_id")?,
row.get::<_, ChatId>("chat_id")?, row.get::<_, ChatId>("chat_id")?,
row.get::<_, Chattype>("type")?, row.get::<_, Chattype>("type")?,
row.get::<_, u32>("to_id")?,
)) ))
}, },
) )
@@ -1429,13 +1426,15 @@ pub async fn ndn_from_ext(
info!(context, "Failed to select NDN {:?}", err); info!(context, "Failed to select NDN {:?}", err);
} }
if let Ok((msg_id, chat_id, chat_type, contact_id)) = res { if let Ok((msg_id, chat_id, chat_type)) = res {
set_msg_failed(context, msg_id, error).await; set_msg_failed(context, msg_id, error).await;
info!(context, "cht {} {} {}", chat_id, chat_type, contact_id);
if chat_type == Chattype::Group || chat_type == Chattype::VerifiedGroup { if chat_type == Chattype::Group || chat_type == Chattype::VerifiedGroup {
info!(context, "Adding info msg to chat {}", chat_id); if let Some(failed_recipient) = &failed.failed_recipient {
let contact = Contact::load_from_db(context, contact_id).await.unwrap(); let contact_id =
Contact::lookup_id_by_addr(context, failed_recipient, Origin::Unknown).await;
if let Ok(contact) = Contact::load_from_db(context, contact_id).await {
// Tell the user which of the recipients failed if we know that (because in a group, this might otherwise be unclear)
chat::add_info_msg( chat::add_info_msg(
context, context,
chat_id, chat_id,
@@ -1450,6 +1449,8 @@ pub async fn ndn_from_ext(
context.emit_event(Event::ChatModified(chat_id)); context.emit_event(Event::ChatModified(chat_id));
} }
} }
}
}
} }
/// The number of messages assigned to real chat (!=deaddrop, !=trash) /// The number of messages assigned to real chat (!=deaddrop, !=trash)

View File

@@ -54,7 +54,7 @@ pub struct MimeMessage {
pub(crate) user_avatar: Option<AvatarAction>, pub(crate) user_avatar: Option<AvatarAction>,
pub(crate) group_avatar: Option<AvatarAction>, pub(crate) group_avatar: Option<AvatarAction>,
pub(crate) reports: Vec<Report>, pub(crate) reports: Vec<Report>,
pub(crate) failed_msg: Option<String>, pub(crate) failed_msg: Option<FailedMsg>,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@@ -881,7 +881,7 @@ impl MimeMessage {
.iter() .iter()
.find(|p| p.typ == Viewtype::Text) .find(|p| p.typ == Viewtype::Text)
.map(|p| &p.msg); .map(|p| &p.msg);
message::ndn_from_ext(context, from_id, original_message_id, error).await message::ndn_from_ext(context, original_message_id, error).await
} }
} }
@@ -889,7 +889,7 @@ impl MimeMessage {
&self, &self,
context: &Context, context: &Context,
report: &mailparse::ParsedMail<'_>, report: &mailparse::ParsedMail<'_>,
) -> Result<Option<String>> { ) -> Result<Option<FailedMsg>> {
// parse as mailheaders // parse as mailheaders
if let Some(original_msg) = report if let Some(original_msg) = report
.subparts .subparts
@@ -903,7 +903,17 @@ impl MimeMessage {
.get_header_value(HeaderDef::MessageId) .get_header_value(HeaderDef::MessageId)
.and_then(|v| parse_message_id(&v).ok()) .and_then(|v| parse_message_id(&v).ok())
{ {
return Ok(Some(original_message_id)); let mut to_list = get_recipients(&report_fields);
let to = if to_list.len() == 1 {
Some(to_list.pop().unwrap())
} else {
None // We do not know which recipient failed
};
return Ok(Some(FailedMsg {
rfc724_mid: original_message_id,
failed_recipient: to.map(|s| s.addr),
}));
} }
warn!( warn!(
@@ -970,6 +980,12 @@ pub(crate) struct Report {
additional_message_ids: Vec<String>, additional_message_ids: Vec<String>,
} }
#[derive(Debug)]
pub(crate) struct FailedMsg {
pub rfc724_mid: String,
pub failed_recipient: Option<String>,
}
pub(crate) fn parse_message_ids(ids: &str) -> Result<Vec<String>> { pub(crate) fn parse_message_ids(ids: &str) -> Result<Vec<String>> {
// take care with mailparse::msgidparse() that is pretty untolerant eg. wrt missing `<` or `>` // take care with mailparse::msgidparse() that is pretty untolerant eg. wrt missing `<` or `>`
let mut msgids = Vec::new(); let mut msgids = Vec::new();