Skip to content

Commit b5fa655

Browse files
committed
api: add ContactId.set_name()
This API allows to explicitly set a name of the contact instead of trying to create a new contact with the same address. Not all contacts are identified by the email address and we are going to introduce contacts identified by their keys.
1 parent 5280448 commit b5fa655

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

Diff for: deltachat-jsonrpc/src/api.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,7 @@ impl CommandApi {
15371537
Ok(())
15381538
}
15391539

1540+
/// Sets display name for existing contact.
15401541
async fn change_contact_name(
15411542
&self,
15421543
account_id: u32,
@@ -1545,9 +1546,7 @@ impl CommandApi {
15451546
) -> Result<()> {
15461547
let ctx = self.get_context(account_id).await?;
15471548
let contact_id = ContactId::new(contact_id);
1548-
let contact = Contact::get_by_id(&ctx, contact_id).await?;
1549-
let addr = contact.get_addr();
1550-
Contact::create(&ctx, &name, addr).await?;
1549+
contact_id.set_name(&ctx, &name).await?;
15511550
Ok(())
15521551
}
15531552

Diff for: src/contact.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ impl ContactId {
9595
self.0
9696
}
9797

98+
/// Sets display name for existing contact.
99+
///
100+
/// Display name may be an empty string,
101+
/// in which case the name displayed in the UI
102+
/// for this contact will switch to the
103+
/// contact's authorized name.
104+
pub async fn set_name(self, context: &Context, name: &str) -> Result<()> {
105+
context
106+
.sql
107+
.transaction(|transaction| {
108+
let is_changed = transaction.execute(
109+
"UPDATE contacts SET name=?1 WHERE id=?2 AND name!=?1",
110+
(name, self),
111+
)? > 0;
112+
if is_changed {
113+
update_chat_names(context, transaction, self)?;
114+
}
115+
Ok(())
116+
})
117+
.await?;
118+
Ok(())
119+
}
120+
98121
/// Mark contact as bot.
99122
pub(crate) async fn mark_bot(&self, context: &Context, is_bot: bool) -> Result<()> {
100123
context
@@ -909,7 +932,7 @@ impl Contact {
909932

910933
if update_name || update_authname {
911934
let contact_id = ContactId::new(row_id);
912-
update_chat_names(context, &transaction, contact_id)?;
935+
update_chat_names(context, transaction, contact_id)?;
913936
}
914937
sth_modified = Modifier::Modified;
915938
}

0 commit comments

Comments
 (0)