diff --git a/src/chat.rs b/src/chat.rs index 832d4b369..88f3b4b57 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -4398,6 +4398,10 @@ impl Context { pub(crate) async fn sync_alter_chat(&self, id: &SyncId, action: &SyncAction) -> Result<()> { let chat_id = match id { SyncId::ContactAddr(addr) => { + if let SyncAction::Rename(to) = action { + Contact::create_ex(self, Nosync, to, addr).await?; + return Ok(()); + } let contact_id = Contact::lookup_id_by_addr_ex(self, addr, Origin::Unknown, None) .await? .with_context(|| format!("No contact for addr '{addr}'"))?; diff --git a/src/contact.rs b/src/contact.rs index 6e8dfface..836a46003 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -477,6 +477,15 @@ impl Contact { /// /// May result in a `#DC_EVENT_CONTACTS_CHANGED` event. pub async fn create(context: &Context, name: &str, addr: &str) -> Result { + Self::create_ex(context, Sync, name, addr).await + } + + pub(crate) async fn create_ex( + context: &Context, + sync: sync::Sync, + name: &str, + addr: &str, + ) -> Result { let name = improve_single_line_input(name); let (name, addr) = sanitize_name_and_addr(&name, addr); @@ -497,6 +506,16 @@ impl Contact { set_blocked(context, Nosync, contact_id, false).await?; } + if sync.into() { + chat::sync( + context, + chat::SyncId::ContactAddr(addr.to_string()), + chat::SyncAction::Rename(name.to_string()), + ) + .await + .log_err(context) + .ok(); + } Ok(contact_id) } @@ -2808,4 +2827,33 @@ Hi."#; Ok(()) } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn test_sync_create() -> Result<()> { + let alice0 = &TestContext::new_alice().await; + let alice1 = &TestContext::new_alice().await; + for a in [alice0, alice1] { + a.set_config_bool(Config::SyncMsgs, true).await?; + } + + Contact::create(alice0, "Bob", "bob@example.net").await?; + test_utils::sync(alice0, alice1).await; + let a1b_contact_id = + Contact::lookup_id_by_addr(alice1, "bob@example.net", Origin::ManuallyCreated) + .await? + .unwrap(); + let a1b_contact = Contact::get_by_id(alice1, a1b_contact_id).await?; + assert_eq!(a1b_contact.name, "Bob"); + + Contact::create(alice0, "Bob Renamed", "bob@example.net").await?; + test_utils::sync(alice0, alice1).await; + let id = Contact::lookup_id_by_addr(alice1, "bob@example.net", Origin::ManuallyCreated) + .await? + .unwrap(); + assert_eq!(id, a1b_contact_id); + let a1b_contact = Contact::get_by_id(alice1, a1b_contact_id).await?; + assert_eq!(a1b_contact.name, "Bob Renamed"); + + Ok(()) + } }