mirror of
https://github.com/chatmail/core.git
synced 2026-04-27 02:16:29 +03:00
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:
@@ -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(())
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user