| 1 | mod highlight; |
| 2 | mod kql; |
| 3 | mod model; |
| 4 | mod request; |
| 5 | |
| 6 | use percent_encoding::{NON_ALPHANUMERIC, utf8_percent_encode}; |
| 7 | use session::{ApiResponse, Method, Request, Scope, Session}; |
| 8 | |
| 9 | use crate::error::{Error, Result}; |
| 10 | use crate::pins::{CsaTransport, SessionTransport}; |
| 11 | |
| 12 | pub use highlight::{HighlightedText, split_highlights}; |
| 13 | pub use kql::{MessageFilter, message_query_string}; |
| 14 | pub use model::{ |
| 15 | BestMatch, ChatMember, QueryResults, ResultPage, SearchChannel, SearchChat, SearchEvent, |
| 16 | SearchFile, SearchMessage, SearchPerson, SearchTeam, SearchText, Suggestions, |
| 17 | parse_query_results, parse_suggestions, |
| 18 | }; |
| 19 | pub use request::{QueryRequest, QueryScope}; |
| 20 | |
| 21 | pub const SUBSTRATE: &str = "https://substrate.office.com"; |
| 22 | pub const SUBSTRATE_SEARCH_SCOPE: &str = "SubstrateSearch-Internal.ReadWrite"; |
| 23 | const SCENARIO: &str = "powerbar"; |
| 24 | const SUGGESTION_CLIENT_VERSION: &str = "T2.1"; |
| 25 | const QUERY_CLIENT_VERSION: &str = "1"; |
| 26 | const SUGGESTION_FLIGHTS: &str = "IncludeHistoryInBMDynamic,PeopleKeywordAsTextSuggestion,TDPeopleSynthetic,EnableKql,EnableSelfSuggestion"; |
| 27 | const SUGGESTION_SET_FLIGHTS: &str = "EnableMessageImageSearch,EnableTeamsChannelDomainPowerbar"; |
| 28 | |
| 29 | /// Without the anchor mailbox headers Substrate answers 200 with empty result sets. |
| 30 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 31 | pub struct SearchIdentity { |
| 32 | pub user_id: String, |
| 33 | pub tenant_id: String, |
| 34 | } |
| 35 | |
| 36 | /// One search session per opened search box: Teams sends the same cvid for every keystroke. |
| 37 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 38 | pub struct SearchSession { |
| 39 | correlation_id: String, |
| 40 | } |
| 41 | |
| 42 | impl SearchSession { |
| 43 | pub fn new() -> Self { |
| 44 | SearchSession { |
| 45 | correlation_id: new_id(), |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub fn correlation_id(&self) -> &str { |
| 50 | &self.correlation_id |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl Default for SearchSession { |
| 55 | fn default() -> Self { |
| 56 | SearchSession::new() |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | pub struct SubstrateSearch<T: CsaTransport = SessionTransport> { |
| 61 | transport: T, |
| 62 | identity: SearchIdentity, |
| 63 | base_url: String, |
| 64 | } |
| 65 | |
| 66 | impl SubstrateSearch<SessionTransport> { |
| 67 | pub fn new(session: &Session, identity: SearchIdentity) -> Self { |
| 68 | let transport = |
| 69 | SessionTransport::with_scope(session, Scope::new(SUBSTRATE, SUBSTRATE_SEARCH_SCOPE)); |
| 70 | SubstrateSearch::with_transport(transport, identity, SUBSTRATE) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl<T: CsaTransport> SubstrateSearch<T> { |
| 75 | pub fn with_transport(transport: T, identity: SearchIdentity, base_url: &str) -> Self { |
| 76 | SubstrateSearch { |
| 77 | transport, |
| 78 | identity, |
| 79 | base_url: base_url.trim_end_matches('/').to_owned(), |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | pub async fn init(&self, search_session: &SearchSession) -> Result<()> { |
| 84 | let url = format!( |
| 85 | "{}/search/api/v2/init?cvid={}&scenario={SCENARIO}", |
| 86 | self.base_url, |
| 87 | search_session.correlation_id() |
| 88 | ); |
| 89 | let answer = self |
| 90 | .send(Request::get(url), search_session, SUGGESTION_CLIENT_VERSION) |
| 91 | .await?; |
| 92 | ensure_success(&answer) |
| 93 | } |
| 94 | |
| 95 | /// An empty text returns the zero-query state: top people, recent searches, chats. |
| 96 | pub async fn suggestions( |
| 97 | &self, |
| 98 | search_session: &SearchSession, |
| 99 | text: &str, |
| 100 | ) -> Result<Suggestions> { |
| 101 | let logical_id = new_id(); |
| 102 | let correlation_id = search_session.correlation_id(); |
| 103 | let mut request = if text.trim().is_empty() { |
| 104 | Request::get(format!( |
| 105 | "{}/search/api/v1/suggestions?scenario={SCENARIO}&query=&cvid={correlation_id}&logicalId={logical_id}", |
| 106 | self.base_url |
| 107 | )) |
| 108 | } else { |
| 109 | Request::with_body( |
| 110 | Method::Post, |
| 111 | format!( |
| 112 | "{}/search/api/v1/suggestions?scenario={SCENARIO}&setflight={}", |
| 113 | self.base_url, |
| 114 | utf8_percent_encode(SUGGESTION_SET_FLIGHTS, NON_ALPHANUMERIC) |
| 115 | ), |
| 116 | request::suggestion_body(text, correlation_id, &logical_id), |
| 117 | ) |
| 118 | }; |
| 119 | request |
| 120 | .headers |
| 121 | .push(("x-client-flights".to_owned(), SUGGESTION_FLIGHTS.to_owned())); |
| 122 | let answer = self |
| 123 | .send(request, search_session, SUGGESTION_CLIENT_VERSION) |
| 124 | .await?; |
| 125 | ensure_success(&answer)?; |
| 126 | Ok(parse_suggestions(&answer.body)) |
| 127 | } |
| 128 | |
| 129 | pub async fn query( |
| 130 | &self, |
| 131 | search_session: &SearchSession, |
| 132 | query: &QueryRequest, |
| 133 | ) -> Result<QueryResults> { |
| 134 | let body = request::query_body( |
| 135 | query, |
| 136 | &self.identity.user_id, |
| 137 | search_session.correlation_id(), |
| 138 | &new_id(), |
| 139 | ); |
| 140 | let request = Request::with_body( |
| 141 | Method::Post, |
| 142 | format!("{}/searchservice/api/v2/query", self.base_url), |
| 143 | body, |
| 144 | ); |
| 145 | let answer = self |
| 146 | .send(request, search_session, QUERY_CLIENT_VERSION) |
| 147 | .await?; |
| 148 | ensure_success(&answer)?; |
| 149 | Ok(parse_query_results(&answer.body)) |
| 150 | } |
| 151 | |
| 152 | async fn send( |
| 153 | &self, |
| 154 | mut request: Request, |
| 155 | search_session: &SearchSession, |
| 156 | client_version: &str, |
| 157 | ) -> Result<ApiResponse> { |
| 158 | request |
| 159 | .headers |
| 160 | .extend(self.headers(search_session, client_version)); |
| 161 | self.transport.send(request).await |
| 162 | } |
| 163 | |
| 164 | fn headers( |
| 165 | &self, |
| 166 | search_session: &SearchSession, |
| 167 | client_version: &str, |
| 168 | ) -> Vec<(String, String)> { |
| 169 | let SearchIdentity { user_id, tenant_id } = &self.identity; |
| 170 | vec![ |
| 171 | ("x-client-version".to_owned(), client_version.to_owned()), |
| 172 | ("X-ClientType".to_owned(), "MicrosoftTeams".to_owned()), |
| 173 | ("X-ClientScenario".to_owned(), "TeamsSearch".to_owned()), |
| 174 | ("client-request-id".to_owned(), new_id()), |
| 175 | ( |
| 176 | "client-session-id".to_owned(), |
| 177 | search_session.correlation_id().to_owned(), |
| 178 | ), |
| 179 | ( |
| 180 | "x-anchormailbox".to_owned(), |
| 181 | format!("Oid:{user_id}@{tenant_id}"), |
| 182 | ), |
| 183 | ( |
| 184 | "x-routingparameter-sessionkey".to_owned(), |
| 185 | format!("OID:{user_id}@{tenant_id}"), |
| 186 | ), |
| 187 | ] |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | fn ensure_success(answer: &ApiResponse) -> Result<()> { |
| 192 | if answer.is_success() { |
| 193 | Ok(()) |
| 194 | } else { |
| 195 | Err(Error::Session(session::Error::api( |
| 196 | answer.status, |
| 197 | "substrate search", |
| 198 | answer.body.clone(), |
| 199 | ))) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | fn new_id() -> String { |
| 204 | uuid::Uuid::new_v4().to_string() |
| 205 | } |
| 206 | |
| 207 | #[cfg(test)] |
| 208 | mod tests; |