diff --git a/deltachat-jsonrpc/src/api.rs b/deltachat-jsonrpc/src/api.rs index db36bdb87..87fbb65fa 100644 --- a/deltachat-jsonrpc/src/api.rs +++ b/deltachat-jsonrpc/src/api.rs @@ -1537,6 +1537,7 @@ impl CommandApi { Ok(()) } + /// Sets display name for existing contact. async fn change_contact_name( &self, account_id: u32, @@ -1545,9 +1546,7 @@ impl CommandApi { ) -> Result<()> { let ctx = self.get_context(account_id).await?; let contact_id = ContactId::new(contact_id); - let contact = Contact::get_by_id(&ctx, contact_id).await?; - let addr = contact.get_addr(); - Contact::create(&ctx, &name, addr).await?; + contact_id.set_name(&ctx, &name).await?; Ok(()) } diff --git a/src/contact.rs b/src/contact.rs index 072798216..9a60071c1 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -95,6 +95,29 @@ impl ContactId { self.0 } + /// Sets display name for existing contact. + /// + /// Display name may be an empty string, + /// in which case the name displayed in the UI + /// for this contact will switch to the + /// contact's authorized name. + pub async fn set_name(self, context: &Context, name: &str) -> Result<()> { + context + .sql + .transaction(|transaction| { + let is_changed = transaction.execute( + "UPDATE contacts SET name=?1 WHERE id=?2 AND name!=?1", + (name, self), + )? > 0; + if is_changed { + update_chat_names(context, transaction, self)?; + } + Ok(()) + }) + .await?; + Ok(()) + } + /// Mark contact as bot. pub(crate) async fn mark_bot(&self, context: &Context, is_bot: bool) -> Result<()> { context @@ -909,7 +932,7 @@ impl Contact { if update_name || update_authname { let contact_id = ContactId::new(row_id); - update_chat_names(context, &transaction, contact_id)?; + update_chat_names(context, transaction, contact_id)?; } sth_modified = Modifier::Modified; }