diff --git a/src/config.rs b/src/config.rs index 66b80ce2d..34da4aa31 100644 --- a/src/config.rs +++ b/src/config.rs @@ -336,7 +336,7 @@ impl Context { // Separate impl block for self address handling impl Context { /// determine whether the specified addr maps to the/a self addr - pub async fn is_self_addr(&self, addr: &str) -> Result { + pub(crate) async fn is_self_addr(&self, addr: &str) -> Result { let addr = addr_normalize(addr).to_lowercase(); // The addresses we get here are already normalized and lowercase TODO is this true? @@ -349,15 +349,15 @@ impl Context { /// Sets `primary_new` as the new primary self address and saves the old /// primary address (if exists) as a secondary address. pub(crate) async fn set_primary_self_addr(&self, primary_new: &str) -> Result<()> { - let primary_new = addr_normalize(primary_new).to_lowercase(); // TODO check if this might make problems + let primary_new = addr_normalize(primary_new).to_lowercase(); - // Get all self addrs, including the old primary addr: - let mut secondary_new = self.get_all_self_addrs().await?; - // Remove the new primary addr in case it was a secondary addr before: - secondary_new.retain(|a| a != &primary_new); + // add old primary address (if exists) to secondary addresses + let mut secondary_addrs = self.get_all_self_addrs().await?; + // never store a primary address also as a secondary + secondary_addrs.retain(|a| a != &primary_new); self.set_config( Config::SecondaryAddrs, - Some(secondary_new.join(" ").as_str()), + Some(secondary_addrs.join(" ").as_str()), ) .await?; @@ -368,17 +368,17 @@ impl Context { } /// Returns all primary and secondary self addresses. - pub async fn get_all_self_addrs(&self) -> Result> { + pub(crate) async fn get_all_self_addrs(&self) -> Result> { let mut ret = Vec::new(); - ret.extend(self.get_config(Config::ConfiguredAddr).await?.into_iter()); + ret.push(self.get_primary_self_addr().await.unwrap_or_default()); ret.extend(self.get_secondary_self_addrs().await?.into_iter()); Ok(ret) } /// Returns all secondary self addresses. - pub async fn get_secondary_self_addrs(&self) -> Result> { + pub(crate) async fn get_secondary_self_addrs(&self) -> Result> { let secondary_addrs = self .get_config(Config::SecondaryAddrs) .await? @@ -481,4 +481,54 @@ mod tests { assert_eq!(t.get_config_bool(c).await?, false); Ok(()) } + + #[async_std::test] + async fn test_self_addrs() -> Result<()> { + let alice = TestContext::new_alice().await; + + assert!(alice.is_self_addr("alice@example.org").await?); + assert_eq!(alice.get_all_self_addrs().await?, vec!["alice@example.org"]); + assert!(!alice.is_self_addr("alice@alice.com").await?); + + // Test adding the same primary address + alice.set_primary_self_addr("alice@example.org").await?; + alice.set_primary_self_addr("Alice@Example.Org").await?; + assert_eq!(alice.get_all_self_addrs().await?, vec!["alice@example.org"]); + + // Test adding a new (primary) self address + alice.set_primary_self_addr(" Alice@alice.com ").await?; + assert!(alice.is_self_addr("aliCe@example.org").await?); + assert!(alice.is_self_addr("alice@alice.com").await?); + assert_eq!( + alice.get_all_self_addrs().await?, + vec!["alice@alice.com", "alice@example.org"] + ); + + // Check that the entry is not duplicated + alice.set_primary_self_addr("alice@alice.com").await?; + assert_eq!( + alice.get_all_self_addrs().await?, + vec!["alice@alice.com", "alice@example.org"] + ); + + // Test switching back + alice.set_primary_self_addr("alice@example.org").await?; + assert_eq!( + alice.get_all_self_addrs().await?, + vec!["alice@example.org", "alice@alice.com"] + ); + + // Test setting a new primary self address, the previous self address + // should be kept as a secondary self address + alice.set_primary_self_addr("alice@alice.xyz").await?; + assert_eq!( + alice.get_all_self_addrs().await?, + vec!["alice@alice.xyz", "alice@example.org", "alice@alice.com"] + ); + assert!(alice.is_self_addr("alice@example.org").await?); + assert!(alice.is_self_addr("alice@alice.com").await?); + assert!(alice.is_self_addr("Alice@alice.xyz").await?); + + Ok(()) + } } diff --git a/src/contact.rs b/src/contact.rs index 97c03a9ee..c68ad259f 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -720,10 +720,7 @@ impl Contact { .await?; if let Some(query) = query { - let self_addr = context - .get_config(Config::ConfiguredAddr) - .await? - .unwrap_or_default(); + let self_addr = context.get_primary_self_addr().await.unwrap_or_default(); let self_name = context .get_config(Config::Displayname) .await? @@ -2130,49 +2127,4 @@ Hi."#; Ok(()) } - - #[async_std::test] - async fn test_self_addrs() -> Result<()> { - let alice = TestContext::new_alice().await; - - assert!(alice.is_self_addr("alice@example.org").await?); - assert_eq!(alice.get_all_self_addrs().await?, vec!["alice@example.org"]); - assert!(!alice.is_self_addr("alice@alice.com").await?); - - // Test adding a new (primary) self address - alice.set_primary_self_addr(" Alice@alice.com ").await?; - assert!(alice.is_self_addr("aliCe@example.org").await?); - assert!(alice.is_self_addr("alice@alice.com").await?); - assert_eq!( - alice.get_all_self_addrs().await?, - vec!["alice@alice.com", "alice@example.org"] - ); - - // Check that the entry is not duplicated - alice.set_primary_self_addr("alice@alice.com").await?; - assert_eq!( - alice.get_all_self_addrs().await?, - vec!["alice@alice.com", "alice@example.org"] - ); - - // Test switching back - alice.set_primary_self_addr("alice@example.org").await?; - assert_eq!( - alice.get_all_self_addrs().await?, - vec!["alice@example.org", "alice@alice.com"] - ); - - // Test setting a new primary self address, the previous self address - // should be kept as a secondary self address - alice.set_primary_self_addr("alice@alice.xyz").await?; - assert_eq!( - alice.get_all_self_addrs().await?, - vec!["alice@alice.xyz", "alice@example.org", "alice@alice.com"] - ); - assert!(alice.is_self_addr("alice@example.org").await?); - assert!(alice.is_self_addr("alice@alice.com").await?); - assert!(alice.is_self_addr("Alice@alice.xyz").await?); - - Ok(()) - } }