Skip to content

Commit

Permalink
feat: Sync Chat::name across devices (#4953)
Browse files Browse the repository at this point in the history
  • Loading branch information
iequidoo committed Nov 11, 2023
1 parent 0f3036f commit 9731d0a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3630,6 +3630,11 @@ pub(crate) async fn is_group_explicitly_left(context: &Context, grpid: &str) ->

/// Sets group or mailing list chat name.
pub async fn set_chat_name(context: &Context, chat_id: ChatId, new_name: &str) -> Result<()> {
rename_ex(context, Sync, chat_id, new_name).await
}

async fn rename_ex(context: &Context, sync: sync::Sync, chat_id: ChatId, new_name: &str) -> Result<()> {
let sync_name = new_name;
let new_name = improve_single_line_input(new_name);
/* the function only sets the names of group chats; normal chats get their names from the contacts */
let mut success = false;
Expand Down Expand Up @@ -3681,7 +3686,9 @@ pub async fn set_chat_name(context: &Context, chat_id: ChatId, new_name: &str) -
if !success {
bail!("Failed to set name");
}

if sync.into() {
chat.sync(context, SyncAction::Rename(sync_name.to_string())).await.ok();
}
Ok(())
}

Expand Down Expand Up @@ -4199,6 +4206,7 @@ pub(crate) enum SyncAction {
SetMuted(MuteDuration),
/// Create broadcast list with the given name.
CreateBroadcast(String),
Rename(String),
/// Set chat contacts by their addresses.
SetContacts(Vec<String>),
}
Expand Down Expand Up @@ -4250,6 +4258,7 @@ impl Context {
SyncAction::CreateBroadcast(_) => {
bail!("sync_alter_chat({id:?}, {action:?}): Bad request.")
}
SyncAction::Rename(to) => rename_ex(self, Nosync, chat_id, to).await,
SyncAction::SetContacts(addrs) => set_contacts_by_addrs(self, chat_id, addrs).await,
}
}
Expand Down Expand Up @@ -6864,6 +6873,7 @@ mod tests {
}

let a0_broadcast_id = create_broadcast_list(&alices[0]).await?;
sync(&alices).await?;
let a0_broadcast_chat = Chat::load_from_db(&alices[0], a0_broadcast_id).await?;
set_chat_name(&alices[0], a0_broadcast_id, "Broadcast list 42").await?;
sync(&alices).await?;
Expand All @@ -6873,8 +6883,7 @@ mod tests {
.0;
let a1_broadcast_chat = Chat::load_from_db(&alices[1], a1_broadcast_id).await?;
assert_eq!(a1_broadcast_chat.get_type(), Chattype::Broadcast);
// TODO: Implement synchronisation of `set_chat_name()`.
// assert_eq!(a1_broadcast_chat.get_name(), "Broadcast list 42");
assert_eq!(a1_broadcast_chat.get_name(), "Broadcast list 42");
assert!(get_chat_contacts(&alices[1], a1_broadcast_id)
.await?
.is_empty());
Expand Down

0 comments on commit 9731d0a

Please sign in to comment.