mirror of
https://github.com/chatmail/core.git
synced 2026-05-05 06:16:30 +03:00
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.
This commit is contained in:
@@ -1582,6 +1582,8 @@ impl Contact {
|
|||||||
pub fn get_color(&self) -> u32 {
|
pub fn get_color(&self) -> u32 {
|
||||||
if let Some(fingerprint) = self.fingerprint() {
|
if let Some(fingerprint) = self.fingerprint() {
|
||||||
str_to_color(&fingerprint.hex())
|
str_to_color(&fingerprint.hex())
|
||||||
|
} else if self.id == ContactId::SELF {
|
||||||
|
0x808080
|
||||||
} else {
|
} else {
|
||||||
str_to_color(&self.addr.to_lowercase())
|
str_to_color(&self.addr.to_lowercase())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use super::*;
|
|||||||
use crate::chat::{Chat, ProtectionStatus, get_chat_contacts, send_text_msg};
|
use crate::chat::{Chat, ProtectionStatus, get_chat_contacts, send_text_msg};
|
||||||
use crate::chatlist::Chatlist;
|
use crate::chatlist::Chatlist;
|
||||||
use crate::receive_imf::receive_imf;
|
use crate::receive_imf::receive_imf;
|
||||||
|
use crate::securejoin::get_securejoin_qr;
|
||||||
use crate::test_utils::{self, TestContext, TestContextManager, TimeShiftFalsePositiveNote};
|
use crate::test_utils::{self, TestContext, TestContextManager, TimeShiftFalsePositiveNote};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -773,6 +774,20 @@ async fn test_contact_get_color() -> Result<()> {
|
|||||||
Ok(())
|
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)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn test_contact_get_encrinfo() -> Result<()> {
|
async fn test_contact_get_encrinfo() -> Result<()> {
|
||||||
let mut tcm = TestContextManager::new();
|
let mut tcm = TestContextManager::new();
|
||||||
|
|||||||
11
src/key.rs
11
src/key.rs
@@ -15,6 +15,7 @@ use rand::thread_rng;
|
|||||||
use tokio::runtime::Handle;
|
use tokio::runtime::Handle;
|
||||||
|
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
|
use crate::events::EventType;
|
||||||
use crate::log::{LogExt, info};
|
use crate::log::{LogExt, info};
|
||||||
use crate::pgp::KeyPair;
|
use crate::pgp::KeyPair;
|
||||||
use crate::tools::{self, time_elapsed};
|
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', ?)",
|
"INSERT INTO config (keyname, value) VALUES ('key_id', ?)",
|
||||||
(new_key_id,),
|
(new_key_id,),
|
||||||
)?;
|
)?;
|
||||||
Ok(Some(new_key_id))
|
Ok(new_key_id)
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
context.emit_event(EventType::AccountsItemChanged);
|
||||||
if let Some(new_key_id) = new_key_id {
|
config_cache_lock.insert("key_id".to_string(), Some(new_key_id.to_string()));
|
||||||
// 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()));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user