Resultify get_chat_id_by_grpid and create_or_lookup_mailinglist

Use `Option` instead of `Error` to indicate that no chat ID is found.
This commit is contained in:
link2xt
2021-08-07 09:20:30 +00:00
parent ac245a6cb2
commit 5a5b80c960
7 changed files with 42 additions and 58 deletions

View File

@@ -532,7 +532,7 @@ async fn add_parts(
list_id,
mime_parser,
)
.await
.await?
{
chat_id = Some(new_chat_id);
chat_id_blocked = new_chat_id_blocked;
@@ -548,7 +548,7 @@ async fn add_parts(
sender,
mime_parser,
)
.await
.await?
{
chat_id = Some(new_chat_id);
chat_id_blocked = new_chat_id_blocked;
@@ -1347,13 +1347,9 @@ async fn create_or_lookup_group(
};
// check, if we have a chat with this group ID
let mut chat_id = if let Ok((chat_id, _protected, _blocked)) =
chat::get_chat_id_by_grpid(context, &grpid).await
{
Some(chat_id)
} else {
None
};
let mut chat_id = chat::get_chat_id_by_grpid(context, &grpid)
.await?
.map(|(chat_id, _protected, _blocked)| chat_id);
// For chat messages, we don't have to guess (is_*probably*_private_reply()) but we know for sure that
// they belong to the group because of the Chat-Group-Id or Message-Id header
@@ -1616,7 +1612,7 @@ async fn create_or_lookup_mailinglist(
allow_creation: bool,
list_id_header: &str,
mime_parser: &MimeMessage,
) -> Option<(ChatId, Blocked)> {
) -> Result<Option<(ChatId, Blocked)>> {
static LIST_ID: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(.+)<(.+)>$").unwrap());
let (mut name, listid) = match LIST_ID.captures(list_id_header) {
Some(cap) => (cap[1].trim().to_string(), cap[2].trim().to_string()),
@@ -1630,8 +1626,8 @@ async fn create_or_lookup_mailinglist(
),
};
if let Ok((chat_id, _, blocked)) = chat::get_chat_id_by_grpid(context, &listid).await {
return Some((chat_id, blocked));
if let Some((chat_id, _, blocked)) = chat::get_chat_id_by_grpid(context, &listid).await? {
return Ok(Some((chat_id, blocked)));
}
// for mailchimp lists, the name in `ListId` is just a long number.
@@ -1678,7 +1674,7 @@ async fn create_or_lookup_mailinglist(
if allow_creation {
// list does not exist but should be created
match create_multiuser_record(
let chat_id = create_multiuser_record(
context,
Chattype::Mailinglist,
&listid,
@@ -1687,25 +1683,18 @@ async fn create_or_lookup_mailinglist(
ProtectionStatus::Unprotected,
)
.await
{
Ok(chat_id) => {
chat::add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF).await;
Some((chat_id, Blocked::Request))
}
Err(e) => {
warn!(
context,
"Failed to create mailinglist '{}' for grpid={}: {}",
&name,
&listid,
e.to_string()
);
None
}
}
.map_err(|err| {
err.context(format!(
"Failed to create mailinglist '{}' for grpid={}",
&name, &listid
))
})?;
chat::add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF).await;
Ok(Some((chat_id, Blocked::Request)))
} else {
info!(context, "creating list forbidden by caller");
None
Ok(None)
}
}