From ee715da078eccba725038b35b756d26e7ebcb1d9 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 8 Jul 2023 22:53:55 +0000 Subject: [PATCH] fix: do not ignore chat loading errors in forward_msgs() --- src/chat.rs | 143 ++++++++++++++++++++++++++-------------------------- 1 file changed, 71 insertions(+), 72 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index ff35944d8..3eb1759d8 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -3493,89 +3493,88 @@ pub async fn forward_msgs(context: &Context, msg_ids: &[MsgId], chat_id: ChatId) chat_id .unarchive_if_not_muted(context, MessageState::Undefined) .await?; - if let Ok(mut chat) = Chat::load_from_db(context, chat_id).await { - if let Some(reason) = chat.why_cant_send(context).await? { - bail!("cannot send to {}: {}", chat_id, reason); + let mut chat = Chat::load_from_db(context, chat_id).await?; + if let Some(reason) = chat.why_cant_send(context).await? { + bail!("cannot send to {}: {}", chat_id, reason); + } + curr_timestamp = create_smeared_timestamps(context, msg_ids.len()); + let ids = context + .sql + .query_map( + &format!( + "SELECT id FROM msgs WHERE id IN({}) ORDER BY timestamp,id", + sql::repeat_vars(msg_ids.len()) + ), + rusqlite::params_from_iter(msg_ids), + |row| row.get::<_, MsgId>(0), + |ids| ids.collect::, _>>().map_err(Into::into), + ) + .await?; + + for id in ids { + let src_msg_id: MsgId = id; + let mut msg = Message::load_from_db(context, src_msg_id).await?; + if msg.state == MessageState::OutDraft { + bail!("cannot forward drafts."); } - curr_timestamp = create_smeared_timestamps(context, msg_ids.len()); - let ids = context - .sql - .query_map( - &format!( - "SELECT id FROM msgs WHERE id IN({}) ORDER BY timestamp,id", - sql::repeat_vars(msg_ids.len()) - ), - rusqlite::params_from_iter(msg_ids), - |row| row.get::<_, MsgId>(0), - |ids| ids.collect::, _>>().map_err(Into::into), - ) - .await?; - for id in ids { - let src_msg_id: MsgId = id; - let mut msg = Message::load_from_db(context, src_msg_id).await?; - if msg.state == MessageState::OutDraft { - bail!("cannot forward drafts."); - } + let original_param = msg.param.clone(); - let original_param = msg.param.clone(); + // we tested a sort of broadcast + // by not marking own forwarded messages as such, + // however, this turned out to be to confusing and unclear. - // we tested a sort of broadcast - // by not marking own forwarded messages as such, - // however, this turned out to be to confusing and unclear. + if msg.get_viewtype() != Viewtype::Sticker { + msg.param + .set_int(Param::Forwarded, src_msg_id.to_u32() as i32); + } - if msg.get_viewtype() != Viewtype::Sticker { - msg.param - .set_int(Param::Forwarded, src_msg_id.to_u32() as i32); - } + msg.param.remove(Param::GuaranteeE2ee); + msg.param.remove(Param::ForcePlaintext); + msg.param.remove(Param::Cmd); + msg.param.remove(Param::OverrideSenderDisplayname); + msg.param.remove(Param::WebxdcSummary); + msg.param.remove(Param::WebxdcSummaryTimestamp); + msg.in_reply_to = None; - msg.param.remove(Param::GuaranteeE2ee); - msg.param.remove(Param::ForcePlaintext); - msg.param.remove(Param::Cmd); - msg.param.remove(Param::OverrideSenderDisplayname); - msg.param.remove(Param::WebxdcSummary); - msg.param.remove(Param::WebxdcSummaryTimestamp); - msg.in_reply_to = None; + // do not leak data as group names; a default subject is generated by mimefactory + msg.subject = "".to_string(); - // do not leak data as group names; a default subject is generated by mimefactory - msg.subject = "".to_string(); + let new_msg_id: MsgId; + if msg.state == MessageState::OutPreparing { + new_msg_id = chat + .prepare_msg_raw(context, &mut msg, None, curr_timestamp) + .await?; + curr_timestamp += 1; + let save_param = msg.param.clone(); + msg.param = original_param; + msg.id = src_msg_id; - let new_msg_id: MsgId; - if msg.state == MessageState::OutPreparing { - new_msg_id = chat - .prepare_msg_raw(context, &mut msg, None, curr_timestamp) - .await?; - curr_timestamp += 1; - let save_param = msg.param.clone(); - msg.param = original_param; - msg.id = src_msg_id; - - if let Some(old_fwd) = msg.param.get(Param::PrepForwards) { - let new_fwd = format!("{} {}", old_fwd, new_msg_id.to_u32()); - msg.param.set(Param::PrepForwards, new_fwd); - } else { - msg.param - .set(Param::PrepForwards, new_msg_id.to_u32().to_string()); - } - - msg.update_param(context).await?; - msg.param = save_param; + if let Some(old_fwd) = msg.param.get(Param::PrepForwards) { + let new_fwd = format!("{} {}", old_fwd, new_msg_id.to_u32()); + msg.param.set(Param::PrepForwards, new_fwd); } else { - msg.state = MessageState::OutPending; - new_msg_id = chat - .prepare_msg_raw(context, &mut msg, None, curr_timestamp) - .await?; - curr_timestamp += 1; - if create_send_msg_job(context, new_msg_id).await?.is_some() { - context - .scheduler - .interrupt_smtp(InterruptInfo::new(false)) - .await; - } + msg.param + .set(Param::PrepForwards, new_msg_id.to_u32().to_string()); + } + + msg.update_param(context).await?; + msg.param = save_param; + } else { + msg.state = MessageState::OutPending; + new_msg_id = chat + .prepare_msg_raw(context, &mut msg, None, curr_timestamp) + .await?; + curr_timestamp += 1; + if create_send_msg_job(context, new_msg_id).await?.is_some() { + context + .scheduler + .interrupt_smtp(InterruptInfo::new(false)) + .await; } - created_chats.push(chat_id); - created_msgs.push(new_msg_id); } + created_chats.push(chat_id); + created_msgs.push(new_msg_id); } for (chat_id, msg_id) in created_chats.iter().zip(created_msgs.iter()) { context.emit_msgs_changed(*chat_id, *msg_id);