| 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 | pub async fn server_suggestions( |
| 19 | &self, |
| 20 | search_session: &SearchSession, |
| 21 | text: &str, |
| 22 | ) -> Result<Suggestions> { |
| 23 | let identity = self.search_identity().await?; |
| 24 | let mut suggestions = self |
| 25 | .remote |
| 26 | .server_suggestions(&identity, search_session, text) |
| 27 | .await?; |
| 28 | if crate::native::is_email_address(text) && !has_person_with_email(&suggestions, text) { |
| 29 | let external = self.search_people(text).await.unwrap_or_default(); |
| 30 | suggestions.people.extend( |
| 31 | external |
| 32 | .into_iter() |
| 33 | .filter(|user| { |
| 34 | user.mail |
| 35 | .as_deref() |
| 36 | .is_some_and(|mail| mail.eq_ignore_ascii_case(text.trim())) |
| 37 | }) |
| 38 | .map(|user| SearchPerson { |
| 39 | company_name: crate::native::known_profile(&user.id) |
| 40 | .and_then(|profile| profile.tenant_name), |
| 41 | mri: Some(format!("8:orgid:{}", user.id)), |
| 42 | email_addresses: user.mail.into_iter().collect(), |
| 43 | display_name: user.display_name.unwrap_or_default(), |
| 44 | user_principal_name: user.user_principal_name, |
| 45 | user_id: Some(user.id), |
| 46 | ..SearchPerson::default() |
| 47 | }), |
| 48 | ); |
| 49 | } |
| 50 | Ok(suggestions) |
| 51 | } |
| 52 | |
| 53 | pub async fn server_query( |
| 54 | &self, |
| 55 | search_session: &SearchSession, |
| 56 | request: &QueryRequest, |
| 57 | ) -> Result<QueryResults> { |
| 58 | let identity = self.search_identity().await?; |
| 59 | self.remote |
| 60 | .server_query(&identity, search_session, request) |
| 61 | .await |
| 62 | } |
| 63 | |
| 64 | async fn search_identity(&self) -> Result<SearchIdentity> { |
| 65 | Ok(SearchIdentity { |
| 66 | user_id: self.my_user_id().await?, |
| 67 | tenant_id: self.my_tenant_id_from_any_chat().await?, |
| 68 | }) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn has_person_with_email(suggestions: &Suggestions, email: &str) -> bool { |
| 73 | suggestions.people.iter().any(|person| { |
| 74 | person |
| 75 | .email_addresses |
| 76 | .iter() |
| 77 | .any(|address| address.eq_ignore_ascii_case(email.trim())) |
| 78 | }) |
| 79 | } |