diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 259a64ca1..7b4fbc8b7 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -166,6 +166,16 @@ class TestOfflineContact: with pytest.raises(ValueError): ac1.create_chat(ac3) + def test_contact_rename(self, acfactory): + ac1 = acfactory.get_configured_offline_account() + contact = ac1.create_contact("some1@example.com", name="some1") + chat = ac1.create_chat(contact) + assert chat.get_name() == "some1" + ac1.create_contact("some1@example.com", name="renamed") + ev = ac1._evtracker.get_matching("DC_EVENT_CHAT_MODIFIED") + assert ev.data1 == chat.id + assert chat.get_name() == "renamed" + class TestOfflineChat: @pytest.fixture diff --git a/src/contact.rs b/src/contact.rs index 8ee851d35..32059c2d0 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -456,10 +456,20 @@ impl Contact { if update_name { // Update the contact name also if it is used as a group name. // This is one of the few duplicated data, however, getting the chat list is easier this way. - context.sql.execute( - "UPDATE chats SET name=? WHERE type=? AND id IN(SELECT chat_id FROM chats_contacts WHERE contact_id=?);", - paramsv![new_name, Chattype::Single, row_id] - ).await.ok(); + let chat_id = context.sql.query_get_value::( + context, + "SELECT id FROM chats WHERE type=? AND id IN(SELECT chat_id FROM chats_contacts WHERE contact_id=?)", + paramsv![Chattype::Single, row_id] + ).await; + if let Some(chat_id) = chat_id { + match context.sql.execute("UPDATE chats SET name=? WHERE id=? AND name!=?1", paramsv![new_name, chat_id]).await { + Err(err) => warn!(context, "Can't update chat name: {}", err), + Ok(count) => if count > 0 { + // Chat name updated + context.emit_event(EventType::ChatModified(ChatId::new(chat_id as u32))); + } + } + } } sth_modified = Modifier::Modified; }