| 1 | use chatsvc::substrate_search::{ |
| 2 | QueryRequest, QueryResults, SearchIdentity, SearchPerson, SearchSession, Suggestions, |
| 3 | }; |
| 4 | |
| 5 | use crate::engine::SyncEngine; |
| 6 | use crate::error::Result; |
| 7 | use crate::remote::Remote; |
| 8 | |
| 9 | impl<R: Remote> SyncEngine<R> { |
| 10 | pub async fn server_search_init(&self, search_session: &SearchSession) -> Result<()> { |
| 11 | let identity = self.search_identity().await?; |
| 12 | self.remote |
| 13 | .server_search_init(&identity, search_session) |
| 14 | .await |
| 15 | } |
| 16 | |
| 17 | /// An empty text returns the zero-query state (top people, recent searches). |
| 18 | /// A complete email that the people index misses is resolved through `search_people`. |
| 19 | pub async fn server_suggestions( |
| 20 | &self, |
| 21 | search_session: &SearchSession, |
| 22 | text: &str, |
| 23 | ) -> Result<Suggestions> { |
| 24 | self.collect_suggestions(search_session, text, true).await |
| 25 | } |
| 26 | |
| 27 | /// People-index suggestions without the complete-email directory lookup. |
| 28 | /// The new-chat field calls `search_people` itself. |
| 29 | pub async fn server_suggestions_without_email_lookup( |
| 30 | &self, |
| 31 | search_session: &SearchSession, |
| 32 | text: &str, |
| 33 | ) -> Result<Suggestions> { |
| 34 | self.collect_suggestions(search_session, text, false).await |
| 35 | } |
| 36 | |
| 37 | async fn collect_suggestions( |
| 38 | &self, |
| 39 | search_session: &SearchSession, |
| 40 | text: &str, |
| 41 | resolve_email: bool, |
| 42 | ) -> Result<Suggestions> { |
| 43 | let identity = self.search_identity().await?; |
| 44 | let mut suggestions = self |
| 45 | .remote |
| 46 | .server_suggestions(&identity, search_session, text) |
| 47 | .await?; |
| 48 | if resolve_email |
| 49 | && crate::native::is_email_address(text) |
| 50 | && !has_person_with_email(&suggestions, text) |
| 51 | { |
| 52 | let external = self.search_people(text).await.unwrap_or_default(); |
| 53 | suggestions.people.extend( |
| 54 | external |
| 55 | .into_iter() |
| 56 | .filter_map(|user| search_person(user, text)), |
| 57 | ); |
| 58 | } |
| 59 | Ok(suggestions) |
| 60 | } |
| 61 | |
| 62 | pub async fn server_query( |
| 63 | &self, |
| 64 | search_session: &SearchSession, |
| 65 | request: &QueryRequest, |
| 66 | ) -> Result<QueryResults> { |
| 67 | let identity = self.search_identity().await?; |
| 68 | self.remote |
| 69 | .server_query(&identity, search_session, request) |
| 70 | .await |
| 71 | } |
| 72 | |
| 73 | async fn search_identity(&self) -> Result<SearchIdentity> { |
| 74 | Ok(SearchIdentity { |
| 75 | user_id: self.my_user_id().await?, |
| 76 | tenant_id: self.my_tenant_id_from_any_chat().await?, |
| 77 | }) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | fn has_person_with_email(suggestions: &Suggestions, email: &str) -> bool { |
| 82 | let email = email.trim(); |
| 83 | suggestions.people.iter().any(|person| { |
| 84 | person |
| 85 | .email_addresses |
| 86 | .iter() |
| 87 | .any(|address| address.eq_ignore_ascii_case(email)) |
| 88 | || person |
| 89 | .user_principal_name |
| 90 | .as_deref() |
| 91 | .is_some_and(|address| address.eq_ignore_ascii_case(email)) |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | fn search_person(user: graph::User, query: &str) -> Option<SearchPerson> { |
| 96 | if !crate::native::user_has_address( |
| 97 | user.mail.as_deref(), |
| 98 | user.user_principal_name.as_deref(), |
| 99 | query, |
| 100 | ) { |
| 101 | return None; |
| 102 | } |
| 103 | let email = user |
| 104 | .mail |
| 105 | .clone() |
| 106 | .or_else(|| user.user_principal_name.clone()); |
| 107 | Some(SearchPerson { |
| 108 | company_name: crate::native::known_profile(&user.id) |
| 109 | .and_then(|profile| profile.tenant_name), |
| 110 | mri: Some(chatsvc::members::user_mri(&user.id)), |
| 111 | email_addresses: email.into_iter().collect(), |
| 112 | display_name: user.display_name.unwrap_or_default(), |
| 113 | user_principal_name: user.user_principal_name, |
| 114 | user_id: Some(user.id), |
| 115 | ..SearchPerson::default() |
| 116 | }) |
| 117 | } |
| 118 | |
| 119 | #[cfg(test)] |
| 120 | mod tests { |
| 121 | use super::*; |
| 122 | |
| 123 | #[test] |
| 124 | fn a_principal_name_still_becomes_a_person_row() { |
| 125 | let user = graph::User { |
| 126 | id: "ada".into(), |
| 127 | display_name: Some("Ada".into()), |
| 128 | mail: None, |
| 129 | user_principal_name: Some("ada@other.example".into()), |
| 130 | job_title: None, |
| 131 | department: None, |
| 132 | }; |
| 133 | let person = search_person(user, "Ada@Other.Example").unwrap(); |
| 134 | assert_eq!(person.user_id.as_deref(), Some("ada")); |
| 135 | assert_eq!(person.email_addresses, ["ada@other.example"]); |
| 136 | } |
| 137 | } |