Move ContactId constants to struct.

This makes the APIs much more Rust-like and keep contact IDs clearer
and in one place.
This commit is contained in:
Floris Bruynooghe
2022-04-03 14:08:27 +02:00
parent 918ee47c79
commit 35c0434dc7
20 changed files with 243 additions and 267 deletions

View File

@@ -13,9 +13,7 @@ use sha2::{Digest, Sha256};
use crate::chat::{self, Chat, ChatId, ChatIdBlocked, ProtectionStatus};
use crate::config::Config;
use crate::constants::{
Blocked, Chattype, ShowEmails, DC_CHAT_ID_TRASH, DC_CONTACT_ID_LAST_SPECIAL, DC_CONTACT_ID_SELF,
};
use crate::constants::{Blocked, Chattype, ShowEmails, DC_CHAT_ID_TRASH};
use crate::contact;
use crate::contact::{
addr_cmp, may_be_valid_addr, normalize_name, Contact, ContactId, Origin, VerifiedStatus,
@@ -180,7 +178,7 @@ pub(crate) async fn dc_receive_imf_inner(
let (from_id, _from_id_blocked, incoming_origin) =
from_field_to_contact_id(context, &mime_parser.from, prevent_rename).await?;
let incoming = from_id != DC_CONTACT_ID_SELF;
let incoming = from_id != ContactId::SELF;
let to_ids = dc_add_or_lookup_contacts_by_address_list(
context,
@@ -222,7 +220,7 @@ pub(crate) async fn dc_receive_imf_inner(
.await
.context("add_parts error")?;
if from_id > DC_CONTACT_ID_LAST_SPECIAL {
if from_id > ContactId::LAST_SPECIAL {
contact::update_last_seen(context, from_id, sent_timestamp).await?;
}
@@ -256,7 +254,7 @@ pub(crate) async fn dc_receive_imf_inner(
save_locations(context, &mime_parser, chat_id, from_id, insert_msg_id).await?;
if let Some(ref sync_items) = mime_parser.sync_items {
if from_id == DC_CONTACT_ID_SELF {
if from_id == ContactId::SELF {
if mime_parser.was_encrypted() {
if let Err(err) = context.execute_sync_items(sync_items).await {
warn!(context, "receive_imf cannot execute sync items: {}", err);
@@ -395,8 +393,8 @@ pub async fn from_field_to_contact_id(
)
.await?;
if from_ids.contains(&DC_CONTACT_ID_SELF) {
Ok((DC_CONTACT_ID_SELF, false, Origin::OutgoingBcc))
if from_ids.contains(&ContactId::SELF) {
Ok((ContactId::SELF, false, Origin::OutgoingBcc))
} else if !from_ids.is_empty() {
if from_ids.len() > 1 {
warn!(
@@ -494,7 +492,7 @@ async fn add_parts(
let state: MessageState;
let mut needs_delete_job = false;
if incoming {
to_id = DC_CONTACT_ID_SELF;
to_id = ContactId::SELF;
// Whether the message is a part of securejoin handshake that should be marked as seen
// automatically.
@@ -668,7 +666,7 @@ async fn add_parts(
if chat_id.is_none() {
// try to create a normal chat
let create_blocked = if from_id == DC_CONTACT_ID_SELF {
let create_blocked = if from_id == ContactId::SELF {
Blocked::Not
} else {
Blocked::Request
@@ -720,9 +718,8 @@ async fn add_parts(
state = MessageState::OutDelivered;
to_id = to_ids.get(0).cloned().unwrap_or_default();
let self_sent = from_id == DC_CONTACT_ID_SELF
&& to_ids.len() == 1
&& to_ids.contains(&DC_CONTACT_ID_SELF);
let self_sent =
from_id == ContactId::SELF && to_ids.len() == 1 && to_ids.contains(&ContactId::SELF);
// handshake may mark contacts as verified and must be processed before chats are created
if mime_parser.get_header(HeaderDef::SecureJoin).is_some() {
@@ -836,12 +833,11 @@ async fn add_parts(
}
if chat_id.is_none() && self_sent {
// from_id==to_id==DC_CONTACT_ID_SELF - this is a self-sent messages,
// from_id==to_id==ContactId::SELF - this is a self-sent messages,
// maybe an Autocrypt Setup Message
if let Ok(chat) =
ChatIdBlocked::get_for_contact(context, DC_CONTACT_ID_SELF, Blocked::Not)
.await
.log_err(context, "Failed to get (new) chat for contact")
if let Ok(chat) = ChatIdBlocked::get_for_contact(context, ContactId::SELF, Blocked::Not)
.await
.log_err(context, "Failed to get (new) chat for contact")
{
chat_id = Some(chat.id);
chat_id_blocked = chat.blocked;
@@ -1375,14 +1371,14 @@ async fn is_probably_private_reply(
// contain a Chat-Group-Id header and can be sorted into the correct chat this way.
let private_message =
(to_ids == [DC_CONTACT_ID_SELF]) || (from_id == DC_CONTACT_ID_SELF && to_ids.len() == 1);
(to_ids == [ContactId::SELF]) || (from_id == ContactId::SELF && to_ids.len() == 1);
if !private_message {
return Ok(false);
}
if !mime_parser.has_chat_version() {
let chat_contacts = chat::get_chat_contacts(context, parent_chat_id).await?;
if chat_contacts.len() == 2 && chat_contacts.contains(&DC_CONTACT_ID_SELF) {
if chat_contacts.len() == 2 && chat_contacts.contains(&ContactId::SELF) {
return Ok(false);
}
}
@@ -1410,8 +1406,8 @@ async fn create_or_lookup_group(
if !member_ids.contains(&(from_id)) {
member_ids.push(from_id);
}
if !member_ids.contains(&(DC_CONTACT_ID_SELF)) {
member_ids.push(DC_CONTACT_ID_SELF);
if !member_ids.contains(&(ContactId::SELF)) {
member_ids.push(ContactId::SELF);
}
let res = create_adhoc_group(context, mime_parser, create_blocked, &member_ids)
@@ -1492,8 +1488,8 @@ async fn create_or_lookup_group(
chat_id_blocked = create_blocked;
// Create initial member list.
chat::add_to_chat_contacts_table(context, new_chat_id, DC_CONTACT_ID_SELF).await?;
if from_id > DC_CONTACT_ID_LAST_SPECIAL
chat::add_to_chat_contacts_table(context, new_chat_id, ContactId::SELF).await?;
if from_id > ContactId::LAST_SPECIAL
&& !chat::is_contact_in_chat(context, new_chat_id, from_id).await?
{
chat::add_to_chat_contacts_table(context, new_chat_id, from_id).await?;
@@ -1647,7 +1643,7 @@ async fn apply_group_changes(
// add members to group/check members
if recreate_member_list {
if chat::is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await?
if chat::is_contact_in_chat(context, chat_id, ContactId::SELF).await?
&& !chat::is_contact_in_chat(context, chat_id, from_id).await?
{
warn!(
@@ -1661,7 +1657,7 @@ async fn apply_group_changes(
.await?
{
if removed_id.is_some()
|| !chat::is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await?
|| !chat::is_contact_in_chat(context, chat_id, ContactId::SELF).await?
{
// Members could have been removed while we were
// absent. We can't use existing member list and need to
@@ -1674,11 +1670,11 @@ async fn apply_group_changes(
)
.await?;
if removed_id != Some(DC_CONTACT_ID_SELF) {
chat::add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF).await?;
if removed_id != Some(ContactId::SELF) {
chat::add_to_chat_contacts_table(context, chat_id, ContactId::SELF).await?;
}
}
if from_id > DC_CONTACT_ID_LAST_SPECIAL
if from_id > ContactId::LAST_SPECIAL
&& !Contact::addr_equals_contact(context, &self_addr, from_id).await?
&& !chat::is_contact_in_chat(context, chat_id, from_id).await?
&& removed_id != Some(from_id)
@@ -1699,7 +1695,7 @@ async fn apply_group_changes(
}
if let Some(avatar_action) = &mime_parser.group_avatar {
if !chat::is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await? {
if !chat::is_contact_in_chat(context, chat_id, ContactId::SELF).await? {
warn!(
context,
"Received group avatar update for group chat {} we are not a member of.", chat_id
@@ -1849,7 +1845,7 @@ async fn create_or_lookup_mailinglist(
)
})?;
chat::add_to_chat_contacts_table(context, chat_id, DC_CONTACT_ID_SELF).await?;
chat::add_to_chat_contacts_table(context, chat_id, ContactId::SELF).await?;
Ok(Some((chat_id, Blocked::Request)))
} else {
info!(context, "creating list forbidden by caller");
@@ -2015,7 +2011,7 @@ async fn create_adhoc_grp_id(context: &Context, member_ids: &[ContactId]) -> Res
);
let mut params = Vec::new();
params.extend_from_slice(member_ids);
params.push(DC_CONTACT_ID_SELF);
params.push(ContactId::SELF);
let members = context
.sql
@@ -2071,7 +2067,7 @@ async fn check_verified_properties(
// and the message is signed with a verified key of the sender.
// this check is skipped for SELF as there is no proper SELF-peerstate
// and results in group-splits otherwise.
if from_id != DC_CONTACT_ID_SELF {
if from_id != ContactId::SELF {
let peerstate = Peerstate::from_addr(context, contact.get_addr()).await?;
if peerstate.is_none()
@@ -2096,7 +2092,7 @@ async fn check_verified_properties(
let to_ids = to_ids
.iter()
.copied()
.filter(|id| *id != DC_CONTACT_ID_SELF)
.filter(|id| *id != ContactId::SELF)
.collect::<Vec<ContactId>>();
if to_ids.is_empty() {
@@ -2291,7 +2287,7 @@ async fn add_or_lookup_contact_by_addr(
origin: Origin,
) -> Result<ContactId> {
if context.is_self_addr(addr).await? {
return Ok(DC_CONTACT_ID_SELF);
return Ok(ContactId::SELF);
}
let display_name_normalized = display_name.map(normalize_name).unwrap_or_default();
@@ -2311,7 +2307,7 @@ mod tests {
use crate::chat::get_chat_contacts;
use crate::chat::{get_chat_msgs, ChatItem, ChatVisibility};
use crate::chatlist::Chatlist;
use crate::constants::{DC_CONTACT_ID_INFO, DC_GCL_NO_SPECIALS};
use crate::constants::DC_GCL_NO_SPECIALS;
use crate::message::Message;
use crate::test_utils::{get_chat_msg, TestContext, TestContextManager};
@@ -2959,7 +2955,7 @@ mod tests {
last_msg.text,
Some(stock_str::failed_sending_to(&t, "assidhfaaspocwaeofi@gmail.com").await,)
);
assert_eq!(last_msg.from_id, DC_CONTACT_ID_INFO);
assert_eq!(last_msg.from_id, ContactId::INFO);
Ok(())
}
@@ -4901,7 +4897,7 @@ Hi, I created a group"#,
)
.await?;
let msg_out = t.get_last_msg().await;
assert_eq!(msg_out.from_id, DC_CONTACT_ID_SELF);
assert_eq!(msg_out.from_id, ContactId::SELF);
assert_eq!(msg_out.text.unwrap(), "Hi, I created a group");
assert_eq!(msg_out.in_reply_to, None);
@@ -4926,7 +4922,7 @@ Reply from different address
)
.await?;
let msg_in = t.get_last_msg().await;
assert_eq!(msg_in.to_id, DC_CONTACT_ID_SELF);
assert_eq!(msg_in.to_id, ContactId::SELF);
assert_eq!(msg_in.text.unwrap(), "Reply from different address");
assert_eq!(
msg_in.in_reply_to.unwrap(),
@@ -5077,7 +5073,7 @@ Reply from different address
let received = alice1.get_last_msg().await;
let alice1_bob_contact = alice1.add_or_lookup_contact(&bob).await;
assert_eq!(received.from_id, alice1_bob_contact.id);
assert_eq!(received.to_id, DC_CONTACT_ID_SELF);
assert_eq!(received.to_id, ContactId::SELF);
assert!(!received.hidden);
assert_eq!(received.text, Some("Hello all!".to_string()));
assert_eq!(received.in_reply_to, None);
@@ -5104,7 +5100,7 @@ Reply from different address
assert_eq!(received.chat_id, alice2.get_chat(&bob).await.unwrap().id);
let alice2_bob_contact = alice2.add_or_lookup_contact(&bob).await;
assert_eq!(received.from_id, DC_CONTACT_ID_SELF);
assert_eq!(received.from_id, ContactId::SELF);
assert_eq!(received.to_id, alice2_bob_contact.id);
assert!(!received.hidden);
assert_eq!(received.text, Some("Private reply".to_string()));