Skip to content

Commit d0cb211

Browse files
committed
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.
1 parent 11e3480 commit d0cb211

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/chat.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ impl Chat {
17981798
/// Returns chat avatar color.
17991799
///
18001800
/// For 1:1 chats, the color is calculated from the contact's address.
1801-
/// For group chats the color is calculated from the chat name.
1801+
/// For group chats the color is calculated from the grpid, if present, or the chat name.
18021802
pub async fn get_color(&self, context: &Context) -> Result<u32> {
18031803
let mut color = 0;
18041804

@@ -1809,6 +1809,8 @@ impl Chat {
18091809
color = contact.get_color();
18101810
}
18111811
}
1812+
} else if !self.grpid.is_empty() {
1813+
color = str_to_color(&self.grpid);
18121814
} else {
18131815
color = str_to_color(&self.name);
18141816
}

src/chat/chat_tests.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,19 +1929,31 @@ async fn test_classic_email_chat() -> Result<()> {
19291929
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
19301930
async fn test_chat_get_color() -> Result<()> {
19311931
let t = TestContext::new().await;
1932-
let chat_id = create_group_chat(&t, ProtectionStatus::Unprotected, "a chat").await?;
1932+
let chat_id = create_group_ex(&t, None, "a chat").await?;
19331933
let color1 = Chat::load_from_db(&t, chat_id).await?.get_color(&t).await?;
19341934
assert_eq!(color1, 0x008772);
19351935

19361936
// upper-/lowercase makes a difference for the colors, these are different groups
19371937
// (in contrast to email addresses, where upper-/lowercase is ignored in practise)
19381938
let t = TestContext::new().await;
1939-
let chat_id = create_group_chat(&t, ProtectionStatus::Unprotected, "A CHAT").await?;
1939+
let chat_id = create_group_ex(&t, None, "A CHAT").await?;
19401940
let color2 = Chat::load_from_db(&t, chat_id).await?.get_color(&t).await?;
19411941
assert_ne!(color2, color1);
19421942
Ok(())
19431943
}
19441944

1945+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1946+
async fn test_chat_get_color_encrypted() -> Result<()> {
1947+
let mut tcm = TestContextManager::new();
1948+
let t = &tcm.alice().await;
1949+
let chat_id = create_group_ex(t, Some(ProtectionStatus::Unprotected), "a chat").await?;
1950+
let color1 = Chat::load_from_db(t, chat_id).await?.get_color(t).await?;
1951+
set_chat_name(t, chat_id, "A CHAT").await?;
1952+
let color2 = Chat::load_from_db(t, chat_id).await?.get_color(t).await?;
1953+
assert_eq!(color2, color1);
1954+
Ok(())
1955+
}
1956+
19451957
async fn test_sticker(
19461958
filename: &str,
19471959
bytes: &[u8],

0 commit comments

Comments
 (0)