From d0cb2110e66933bbeca4fc476407b8fa489f6ec7 Mon Sep 17 00:00:00 2001 From: iequidoo Date: Thu, 21 Aug 2025 17:55:48 -0300 Subject: [PATCH] feat: Chat::get_color(): Use grpid, if present, instead of name While testing the previous commit i understood that it's better to try giving different colors to groups, particularly if their names are equal so that they visually differ, and at the same time preserve the color if the group is renamed. Using `grpid` solves this. So let groups change colors once and forever. --- src/chat.rs | 4 +++- src/chat/chat_tests.rs | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index e6c1fa5a1..445a0efe9 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1798,7 +1798,7 @@ impl Chat { /// Returns chat avatar color. /// /// For 1:1 chats, the color is calculated from the contact's address. - /// For group chats the color is calculated from the chat name. + /// For group chats the color is calculated from the grpid, if present, or the chat name. pub async fn get_color(&self, context: &Context) -> Result { let mut color = 0; @@ -1809,6 +1809,8 @@ impl Chat { color = contact.get_color(); } } + } else if !self.grpid.is_empty() { + color = str_to_color(&self.grpid); } else { color = str_to_color(&self.name); } diff --git a/src/chat/chat_tests.rs b/src/chat/chat_tests.rs index 34f1775ea..f83542766 100644 --- a/src/chat/chat_tests.rs +++ b/src/chat/chat_tests.rs @@ -1929,19 +1929,31 @@ async fn test_classic_email_chat() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_chat_get_color() -> Result<()> { let t = TestContext::new().await; - let chat_id = create_group_chat(&t, ProtectionStatus::Unprotected, "a chat").await?; + let chat_id = create_group_ex(&t, None, "a chat").await?; let color1 = Chat::load_from_db(&t, chat_id).await?.get_color(&t).await?; assert_eq!(color1, 0x008772); // upper-/lowercase makes a difference for the colors, these are different groups // (in contrast to email addresses, where upper-/lowercase is ignored in practise) let t = TestContext::new().await; - let chat_id = create_group_chat(&t, ProtectionStatus::Unprotected, "A CHAT").await?; + let chat_id = create_group_ex(&t, None, "A CHAT").await?; let color2 = Chat::load_from_db(&t, chat_id).await?.get_color(&t).await?; assert_ne!(color2, color1); Ok(()) } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_chat_get_color_encrypted() -> Result<()> { + let mut tcm = TestContextManager::new(); + let t = &tcm.alice().await; + let chat_id = create_group_ex(t, Some(ProtectionStatus::Unprotected), "a chat").await?; + let color1 = Chat::load_from_db(t, chat_id).await?.get_color(t).await?; + set_chat_name(t, chat_id, "A CHAT").await?; + let color2 = Chat::load_from_db(t, chat_id).await?.get_color(t).await?; + assert_eq!(color2, color1); + Ok(()) +} + async fn test_sticker( filename: &str, bytes: &[u8],