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.
This commit is contained in:
link2xt
2025-03-19 18:22:42 +00:00
committed by l
parent 5280448cd3
commit b5fa6553af
2 changed files with 26 additions and 4 deletions

View File

@@ -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(())
}

View File

@@ -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;
}