From 4317354b1bc64fa17a7a1f5b3a5c9069cd30d3a5 Mon Sep 17 00:00:00 2001 From: iequidoo Date: Fri, 10 Oct 2025 14:59:22 -0300 Subject: [PATCH] fix: Emit AccountsItemChanged when own key is generated/imported, use gray self-color until that (#7296) Emitting an `AccountsItemChanged` event is needed for UIs to know when `Contact::get_color()` starts returning the true color for `SELF`. Before, an address-based color was returned for a new account which was changing to a fingerprint-based color e.g. on app restart. Now the self-color is our favorite gray until own keypair is generated or imported e.g. via ASM. --- src/contact.rs | 2 ++ src/contact/contact_tests.rs | 15 +++++++++++++++ src/key.rs | 11 ++++------- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/contact.rs b/src/contact.rs index 19fe5ddaaa..ad556bc1f5 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -1582,6 +1582,8 @@ impl Contact { pub fn get_color(&self) -> u32 { if let Some(fingerprint) = self.fingerprint() { str_to_color(&fingerprint.hex()) + } else if self.id == ContactId::SELF { + 0x808080 } else { str_to_color(&self.addr.to_lowercase()) } diff --git a/src/contact/contact_tests.rs b/src/contact/contact_tests.rs index 8bf08126e3..90e8b151b8 100644 --- a/src/contact/contact_tests.rs +++ b/src/contact/contact_tests.rs @@ -4,6 +4,7 @@ use super::*; use crate::chat::{Chat, ProtectionStatus, get_chat_contacts, send_text_msg}; use crate::chatlist::Chatlist; use crate::receive_imf::receive_imf; +use crate::securejoin::get_securejoin_qr; use crate::test_utils::{self, TestContext, TestContextManager, TimeShiftFalsePositiveNote}; #[test] @@ -773,6 +774,20 @@ async fn test_contact_get_color() -> Result<()> { Ok(()) } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_self_color_vs_key() -> Result<()> { + let mut tcm = TestContextManager::new(); + let t = &tcm.unconfigured().await; + t.configure_addr("alice@example.org").await; + assert!(t.is_configured().await?); + let color = Contact::get_by_id(t, ContactId::SELF).await?.get_color(); + assert_eq!(color, 0x808080); + get_securejoin_qr(t, None).await?; + let color1 = Contact::get_by_id(t, ContactId::SELF).await?.get_color(); + assert_ne!(color1, color); + Ok(()) +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_contact_get_encrinfo() -> Result<()> { let mut tcm = TestContextManager::new(); diff --git a/src/key.rs b/src/key.rs index be3c07c3e0..06f25b8eb1 100644 --- a/src/key.rs +++ b/src/key.rs @@ -15,6 +15,7 @@ use rand::thread_rng; use tokio::runtime::Handle; use crate::context::Context; +use crate::events::EventType; use crate::log::{LogExt, info}; use crate::pgp::KeyPair; use crate::tools::{self, time_elapsed}; @@ -414,15 +415,11 @@ pub(crate) async fn store_self_keypair(context: &Context, keypair: &KeyPair) -> "INSERT INTO config (keyname, value) VALUES ('key_id', ?)", (new_key_id,), )?; - Ok(Some(new_key_id)) + Ok(new_key_id) }) .await?; - - if let Some(new_key_id) = new_key_id { - // Update config cache if transaction succeeded and changed current default key. - config_cache_lock.insert("key_id".to_string(), Some(new_key_id.to_string())); - } - + context.emit_event(EventType::AccountsItemChanged); + config_cache_lock.insert("key_id".to_string(), Some(new_key_id.to_string())); Ok(()) }