Skip to content

Commit 0e47e89

Browse files
committed
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.
1 parent 2d7dc7a commit 0e47e89

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/contact.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,8 @@ impl Contact {
15821582
pub fn get_color(&self) -> u32 {
15831583
if let Some(fingerprint) = self.fingerprint() {
15841584
str_to_color(&fingerprint.hex())
1585+
} else if self.id == ContactId::SELF {
1586+
0x808080
15851587
} else {
15861588
str_to_color(&self.addr.to_lowercase())
15871589
}

src/contact/contact_tests.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::*;
44
use crate::chat::{Chat, ProtectionStatus, get_chat_contacts, send_text_msg};
55
use crate::chatlist::Chatlist;
66
use crate::receive_imf::receive_imf;
7+
use crate::securejoin::get_securejoin_qr;
78
use crate::test_utils::{self, TestContext, TestContextManager, TimeShiftFalsePositiveNote};
89

910
#[test]
@@ -773,6 +774,20 @@ async fn test_contact_get_color() -> Result<()> {
773774
Ok(())
774775
}
775776

777+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
778+
async fn test_self_color_vs_key() -> Result<()> {
779+
let mut tcm = TestContextManager::new();
780+
let t = &tcm.unconfigured().await;
781+
t.configure_addr("alice@example.org").await;
782+
assert!(t.is_configured().await?);
783+
let color = Contact::get_by_id(t, ContactId::SELF).await?.get_color();
784+
assert_eq!(color, 0x808080);
785+
get_securejoin_qr(t, None).await?;
786+
let color1 = Contact::get_by_id(t, ContactId::SELF).await?.get_color();
787+
assert_ne!(color1, color);
788+
Ok(())
789+
}
790+
776791
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
777792
async fn test_contact_get_encrinfo() -> Result<()> {
778793
let mut tcm = TestContextManager::new();

src/key.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rand::thread_rng;
1515
use tokio::runtime::Handle;
1616

1717
use crate::context::Context;
18+
use crate::events::EventType;
1819
use crate::log::{LogExt, info};
1920
use crate::pgp::KeyPair;
2021
use crate::tools::{self, time_elapsed};
@@ -414,15 +415,11 @@ pub(crate) async fn store_self_keypair(context: &Context, keypair: &KeyPair) ->
414415
"INSERT INTO config (keyname, value) VALUES ('key_id', ?)",
415416
(new_key_id,),
416417
)?;
417-
Ok(Some(new_key_id))
418+
Ok(new_key_id)
418419
})
419420
.await?;
420-
421-
if let Some(new_key_id) = new_key_id {
422-
// Update config cache if transaction succeeded and changed current default key.
423-
config_cache_lock.insert("key_id".to_string(), Some(new_key_id.to_string()));
424-
}
425-
421+
context.emit_event(EventType::AccountsItemChanged);
422+
config_cache_lock.insert("key_id".to_string(), Some(new_key_id.to_string()));
426423
Ok(())
427424
}
428425

0 commit comments

Comments
 (0)