Skip to content

Commit 6fa0342

Browse files
committed
refactor: receive_imf: Don't abort processing message on non-critical errors
Follow-up to f27d54f. Non-critical errors shouldn't lead to missing messages.
1 parent d6af8d2 commit 6fa0342

File tree

1 file changed

+87
-54
lines changed

1 file changed

+87
-54
lines changed

src/receive_imf.rs

Lines changed: 87 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,9 @@ async fn add_parts(
19521952
None,
19531953
added_removed_id,
19541954
)
1955-
.await?;
1955+
.await
1956+
.log_err(context)
1957+
.ok();
19561958
}
19571959

19581960
if let Some(node_addr) = mime_parser.get_header(HeaderDef::IrohNodeAddr) {
@@ -1990,7 +1992,7 @@ async fn add_parts(
19901992
if part.is_reaction {
19911993
let reaction_str = simplify::remove_footers(part.msg.as_str());
19921994
let is_incoming_fresh = mime_parser.incoming && !seen;
1993-
set_msg_reaction(
1995+
if set_msg_reaction(
19941996
context,
19951997
mime_in_reply_to,
19961998
chat_id,
@@ -1999,16 +2001,23 @@ async fn add_parts(
19992001
Reaction::from(reaction_str.as_str()),
20002002
is_incoming_fresh,
20012003
)
2002-
.await?;
2004+
.await
2005+
.log_err(context)
2006+
.is_err()
2007+
{
2008+
continue;
2009+
}
20032010
}
20042011

20052012
let mut param = part.param.clone();
20062013
if is_system_message != SystemMessage::Unknown {
20072014
param.set_int(Param::Cmd, is_system_message as i32);
20082015
}
20092016

2010-
if let Some(replace_msg_id) = replace_msg_id {
2011-
let placeholder = Message::load_from_db(context, replace_msg_id).await?;
2017+
if let Some(placeholder) = match replace_msg_id {
2018+
None => None,
2019+
Some(replace_msg_id) => Message::load_from_db_optional(context, replace_msg_id).await?,
2020+
} {
20122021
for key in [
20132022
Param::WebxdcSummary,
20142023
Param::WebxdcSummaryTimestamp,
@@ -2019,6 +2028,8 @@ async fn add_parts(
20192028
param.set(key, value);
20202029
}
20212030
}
2031+
} else {
2032+
replace_msg_id = None;
20222033
}
20232034

20242035
let (msg, typ): (&str, Viewtype) = if let Some(better_msg) = &better_msg {
@@ -2156,13 +2167,19 @@ RETURNING id
21562167
if let Some(topic) = mime_parser.get_header(HeaderDef::IrohGossipTopic) {
21572168
// default encoding of topic ids is `hex`.
21582169
let mut topic_raw = [0u8; 32];
2159-
BASE32_NOPAD
2170+
if BASE32_NOPAD
21602171
.decode_mut(topic.to_ascii_uppercase().as_bytes(), &mut topic_raw)
21612172
.map_err(|e| e.error)
2162-
.context("Wrong gossip topic header")?;
2163-
2164-
let topic = TopicId::from_bytes(topic_raw);
2165-
insert_topic_stub(context, *msg_id, topic).await?;
2173+
.context("Wrong gossip topic header")
2174+
.log_err(context)
2175+
.is_ok()
2176+
{
2177+
let topic = TopicId::from_bytes(topic_raw);
2178+
insert_topic_stub(context, *msg_id, topic)
2179+
.await
2180+
.log_err(context)
2181+
.ok();
2182+
}
21662183
} else {
21672184
warn!(context, "webxdc doesn't have a gossip topic")
21682185
}
@@ -2175,7 +2192,9 @@ RETURNING id
21752192
part.param.get(Param::Filename),
21762193
*msg_id,
21772194
)
2178-
.await?;
2195+
.await
2196+
.log_err(context)
2197+
.ok();
21792198
}
21802199

21812200
if let Some(replace_msg_id) = replace_msg_id {
@@ -3647,7 +3666,10 @@ async fn mark_recipients_as_verified(
36473666
}
36483667

36493668
mark_contact_id_as_verified(context, to_id, from_id).await?;
3650-
ChatId::set_protection_for_contact(context, to_id, mimeparser.timestamp_sent).await?;
3669+
ChatId::set_protection_for_contact(context, to_id, mimeparser.timestamp_sent)
3670+
.await
3671+
.log_err(context)
3672+
.ok();
36513673
}
36523674

36533675
Ok(())
@@ -3709,21 +3731,24 @@ async fn add_or_lookup_contacts_by_address_list(
37093731
) -> Result<Vec<Option<ContactId>>> {
37103732
let mut contact_ids = Vec::new();
37113733
for info in address_list {
3734+
contact_ids.push(None);
37123735
let addr = &info.addr;
37133736
if !may_be_valid_addr(addr) {
3714-
contact_ids.push(None);
37153737
continue;
37163738
}
37173739
let display_name = info.display_name.as_deref();
3718-
if let Ok(addr) = ContactAddress::new(addr) {
3719-
let (contact_id, _) =
3720-
Contact::add_or_lookup(context, display_name.unwrap_or_default(), &addr, origin)
3721-
.await?;
3722-
contact_ids.push(Some(contact_id));
3723-
} else {
3740+
let Ok(addr) = ContactAddress::new(addr) else {
37243741
warn!(context, "Contact with address {:?} cannot exist.", addr);
3725-
contact_ids.push(None);
3726-
}
3742+
continue;
3743+
};
3744+
let contact_id =
3745+
Contact::add_or_lookup(context, display_name.unwrap_or_default(), &addr, origin)
3746+
.await
3747+
.log_err(context)
3748+
.ok()
3749+
.map(|(id, _)| id);
3750+
contact_ids.pop();
3751+
contact_ids.push(contact_id);
37273752
}
37283753

37293754
Ok(contact_ids)
@@ -3740,9 +3765,9 @@ async fn add_or_lookup_key_contacts_by_address_list(
37403765
let mut contact_ids = Vec::new();
37413766
let mut fingerprint_iter = fingerprints.iter();
37423767
for info in address_list {
3768+
contact_ids.push(None);
37433769
let addr = &info.addr;
37443770
if !may_be_valid_addr(addr) {
3745-
contact_ids.push(None);
37463771
continue;
37473772
}
37483773
let fingerprint: String = if let Some(fp) = fingerprint_iter.next() {
@@ -3751,24 +3776,26 @@ async fn add_or_lookup_key_contacts_by_address_list(
37513776
} else if let Some(key) = gossiped_keys.get(addr) {
37523777
key.dc_fingerprint().hex()
37533778
} else {
3754-
contact_ids.push(None);
37553779
continue;
37563780
};
37573781
let display_name = info.display_name.as_deref();
3758-
if let Ok(addr) = ContactAddress::new(addr) {
3759-
let (contact_id, _) = Contact::add_or_lookup_ex(
3760-
context,
3761-
display_name.unwrap_or_default(),
3762-
&addr,
3763-
&fingerprint,
3764-
origin,
3765-
)
3766-
.await?;
3767-
contact_ids.push(Some(contact_id));
3768-
} else {
3782+
let Ok(addr) = ContactAddress::new(addr) else {
37693783
warn!(context, "Contact with address {:?} cannot exist.", addr);
3770-
contact_ids.push(None);
3771-
}
3784+
continue;
3785+
};
3786+
let contact_id = Contact::add_or_lookup_ex(
3787+
context,
3788+
display_name.unwrap_or_default(),
3789+
&addr,
3790+
&fingerprint,
3791+
origin,
3792+
)
3793+
.await
3794+
.log_err(context)
3795+
.ok()
3796+
.map(|(id, _)| id);
3797+
contact_ids.pop();
3798+
contact_ids.push(contact_id);
37723799
}
37733800

37743801
ensure_and_debug_assert_eq!(contact_ids.len(), address_list.len(),);
@@ -3899,35 +3926,41 @@ async fn lookup_key_contacts_by_address_list(
38993926
let mut contact_ids = Vec::new();
39003927
let mut fingerprint_iter = fingerprints.iter();
39013928
for info in address_list {
3929+
contact_ids.push(None);
39023930
let addr = &info.addr;
39033931
if !may_be_valid_addr(addr) {
3904-
contact_ids.push(None);
39053932
continue;
39063933
}
39073934

3908-
if let Some(fp) = fingerprint_iter.next() {
3935+
let contact_id = if let Some(fp) = fingerprint_iter.next() {
39093936
// Iterator has not ran out of fingerprints yet.
39103937
let display_name = info.display_name.as_deref();
39113938
let fingerprint: String = fp.hex();
39123939

3913-
if let Ok(addr) = ContactAddress::new(addr) {
3914-
let (contact_id, _) = Contact::add_or_lookup_ex(
3915-
context,
3916-
display_name.unwrap_or_default(),
3917-
&addr,
3918-
&fingerprint,
3919-
Origin::Hidden,
3920-
)
3921-
.await?;
3922-
contact_ids.push(Some(contact_id));
3923-
} else {
3940+
let Ok(addr) = ContactAddress::new(addr) else {
39243941
warn!(context, "Contact with address {:?} cannot exist.", addr);
3925-
contact_ids.push(None);
3926-
}
3942+
continue;
3943+
};
3944+
Contact::add_or_lookup_ex(
3945+
context,
3946+
display_name.unwrap_or_default(),
3947+
&addr,
3948+
&fingerprint,
3949+
Origin::Hidden,
3950+
)
3951+
.await
3952+
.log_err(context)
3953+
.ok()
3954+
.map(|(id, _)| id)
39273955
} else {
3928-
let contact_id = lookup_key_contact_by_address(context, addr, chat_id).await?;
3929-
contact_ids.push(contact_id);
3930-
}
3956+
lookup_key_contact_by_address(context, addr, chat_id)
3957+
.await
3958+
.log_err(context)
3959+
.ok()
3960+
.flatten()
3961+
};
3962+
contact_ids.pop();
3963+
contact_ids.push(contact_id);
39313964
}
39323965
ensure_and_debug_assert_eq!(address_list.len(), contact_ids.len(),);
39333966
Ok(contact_ids)

0 commit comments

Comments
 (0)