| 1 | use serde_json::{Value, json}; |
| 2 | |
| 3 | use super::kql::{MessageFilter, message_query_string}; |
| 4 | |
| 5 | const APP_NAME: &str = "Microsoft Teams"; |
| 6 | const SCENARIO: &str = "powerbar"; |
| 7 | const TIME_ZONE: &str = "Europe/Berlin"; |
| 8 | const FORM_FACTOR: &str = "general.web.reactSearch"; |
| 9 | const TAB_PAGE_SIZE: usize = 25; |
| 10 | const TAB_TOP_RESULTS: usize = 5; |
| 11 | const MESSAGE_FIELDS: [&str; 8] = [ |
| 12 | "Extension_SkypeSpaces_ConversationPost_Extension_FromSkypeInternalId_String", |
| 13 | "Extension_SkypeSpaces_ConversationPost_Extension_FileData_String", |
| 14 | "Extension_SkypeSpaces_ConversationPost_Extension_ThreadType_String", |
| 15 | "Extension_SkypeSpaces_ConversationPost_Extension_SkypeGroupId_String", |
| 16 | "Extension_SkypeSpaces_ConversationPost_Extension_SenderTenantId_String", |
| 17 | "Extension_SkypeSpaces_ConversationPost_Extension_ParentMessageId_String", |
| 18 | "Extension_SkypeSpaces_ConversationPost_Extension_ImageSrc_String", |
| 19 | "Extension_SkypeSpaces_ConversationPost_Extension_AmsReferences_StringArray", |
| 20 | ]; |
| 21 | const PEOPLE_FIELDS: [&str; 18] = [ |
| 22 | "Id", |
| 23 | "MRI", |
| 24 | "DisplayName", |
| 25 | "EmailAddresses", |
| 26 | "PeopleType", |
| 27 | "PeopleSubtype", |
| 28 | "Phones", |
| 29 | "UserPrincipalName", |
| 30 | "GivenName", |
| 31 | "Surname", |
| 32 | "CompanyName", |
| 33 | "JobTitle", |
| 34 | "ImAddress", |
| 35 | "ExternalDirectoryObjectId", |
| 36 | "Department", |
| 37 | "OfficeLocation", |
| 38 | "HomeTenantName", |
| 39 | "MTOLabel", |
| 40 | ]; |
| 41 | |
| 42 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 43 | pub enum QueryScope { |
| 44 | All, |
| 45 | Messages, |
| 46 | People, |
| 47 | Chats, |
| 48 | Channels, |
| 49 | Files, |
| 50 | } |
| 51 | |
| 52 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 53 | pub struct QueryRequest { |
| 54 | pub text: String, |
| 55 | pub scope: QueryScope, |
| 56 | pub filters: Vec<MessageFilter>, |
| 57 | pub from: usize, |
| 58 | pub size: usize, |
| 59 | } |
| 60 | |
| 61 | impl QueryRequest { |
| 62 | pub fn new(text: impl Into<String>, scope: QueryScope) -> Self { |
| 63 | QueryRequest { |
| 64 | text: text.into(), |
| 65 | scope, |
| 66 | filters: Vec::new(), |
| 67 | from: 0, |
| 68 | size: TAB_PAGE_SIZE, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | pub fn with_filters(mut self, filters: Vec<MessageFilter>) -> Self { |
| 73 | self.filters = filters; |
| 74 | self |
| 75 | } |
| 76 | |
| 77 | pub fn page(mut self, from: usize) -> Self { |
| 78 | self.from = from; |
| 79 | self |
| 80 | } |
| 81 | |
| 82 | fn is_contextual(&self) -> bool { |
| 83 | self.filters |
| 84 | .iter() |
| 85 | .any(|filter| matches!(filter, MessageFilter::InChat { .. })) |
| 86 | } |
| 87 | |
| 88 | fn query_type(&self) -> &'static str { |
| 89 | if self.is_contextual() { |
| 90 | return "ContextualTypeahead"; |
| 91 | } |
| 92 | match self.scope { |
| 93 | QueryScope::All => "All", |
| 94 | QueryScope::Messages => "Messages", |
| 95 | QueryScope::People => "People", |
| 96 | QueryScope::Chats => "Chats", |
| 97 | QueryScope::Channels => "Channels", |
| 98 | QueryScope::Files => "Files", |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | pub(super) fn suggestion_body(text: &str, correlation_id: &str, logical_id: &str) -> Value { |
| 104 | let query = json!({ |
| 105 | "QueryString": text, |
| 106 | "DisplayQueryString": text, |
| 107 | "NormalizedQueryString": text, |
| 108 | }); |
| 109 | let entity = |entity_type: &str, size: usize| json!({"EntityType": entity_type, "Size": size, "Query": query}); |
| 110 | let mut people = entity("People", 5); |
| 111 | people["Fields"] = json!(&PEOPLE_FIELDS[..15]); |
| 112 | people["Filter"] = people_filter(); |
| 113 | people["Provenances"] = json!(["Mailbox", "Directory"]); |
| 114 | people["From"] = json!(0); |
| 115 | let mut channel = entity("Channel", 4); |
| 116 | channel["Filter"] = json!({"Or": [ |
| 117 | {"Term": {"ChannelType": "Standard"}}, |
| 118 | {"Term": {"ChannelType": "Shared"}}, |
| 119 | ]}); |
| 120 | json!({ |
| 121 | "EntityRequests": [ |
| 122 | people, |
| 123 | entity("File", 3), |
| 124 | entity("Chat", 5), |
| 125 | entity("Team", 3), |
| 126 | channel, |
| 127 | entity("Text", 2), |
| 128 | entity("Message", 11), |
| 129 | entity("Event", 3), |
| 130 | ], |
| 131 | "Scenario": { |
| 132 | "Name": SCENARIO, |
| 133 | "Dimensions": [{"DimensionName": "QueryType", "DimensionValue": "PeopleCentricSearch"}], |
| 134 | }, |
| 135 | "Cvid": correlation_id, |
| 136 | "LogicalId": logical_id, |
| 137 | "AppName": APP_NAME, |
| 138 | "dataSource": "personScoped", |
| 139 | }) |
| 140 | } |
| 141 | |
| 142 | pub(super) fn query_body( |
| 143 | request: &QueryRequest, |
| 144 | user_id: &str, |
| 145 | correlation_id: &str, |
| 146 | logical_id: &str, |
| 147 | ) -> Value { |
| 148 | let plain_query = json!({"queryString": request.text, "displayQueryString": request.text}); |
| 149 | let entity_requests: Vec<Value> = match request.scope { |
| 150 | QueryScope::All => vec![ |
| 151 | message_entity(request, user_id, 3, 3), |
| 152 | file_entity(&plain_query, 0, 3), |
| 153 | people_entity(&plain_query, 0, 8), |
| 154 | plain_entity("Chat", &plain_query, 0, 3), |
| 155 | plain_entity("TeamsChannel", &plain_query, 0, 3), |
| 156 | ], |
| 157 | QueryScope::Messages => vec![message_entity( |
| 158 | request, |
| 159 | user_id, |
| 160 | request.size, |
| 161 | if request.from == 0 { |
| 162 | TAB_TOP_RESULTS |
| 163 | } else { |
| 164 | 0 |
| 165 | }, |
| 166 | )], |
| 167 | QueryScope::People => vec![people_entity(&plain_query, request.from, request.size)], |
| 168 | QueryScope::Chats => vec![plain_entity( |
| 169 | "Chat", |
| 170 | &plain_query, |
| 171 | request.from, |
| 172 | request.size, |
| 173 | )], |
| 174 | QueryScope::Channels => vec![plain_entity( |
| 175 | "TeamsChannel", |
| 176 | &plain_query, |
| 177 | request.from, |
| 178 | request.size, |
| 179 | )], |
| 180 | QueryScope::Files => vec![file_entity(&plain_query, request.from, request.size)], |
| 181 | }; |
| 182 | json!({ |
| 183 | "EntityRequests": entity_requests, |
| 184 | "QueryAlterationOptions": { |
| 185 | "EnableAlteration": true, |
| 186 | "EnableSuggestion": true, |
| 187 | "SupportedRecourseDisplayTypes": ["Suggestion"], |
| 188 | }, |
| 189 | "scenario": { |
| 190 | "Name": SCENARIO, |
| 191 | "Dimensions": [ |
| 192 | {"DimensionName": "QueryType", "DimensionValue": request.query_type()}, |
| 193 | {"DimensionName": "FormFactor", "DimensionValue": FORM_FACTOR}, |
| 194 | ], |
| 195 | }, |
| 196 | "cvid": correlation_id, |
| 197 | "logicalId": logical_id, |
| 198 | "timezone": TIME_ZONE, |
| 199 | }) |
| 200 | } |
| 201 | |
| 202 | fn message_entity( |
| 203 | request: &QueryRequest, |
| 204 | user_id: &str, |
| 205 | size: usize, |
| 206 | top_results_count: usize, |
| 207 | ) -> Value { |
| 208 | let query_string = message_query_string(&request.text, &request.filters, user_id); |
| 209 | let mut entity = json!({ |
| 210 | "entityType": "Message", |
| 211 | "contentSources": ["Teams"], |
| 212 | "propertySet": "Optimized", |
| 213 | "fields": MESSAGE_FIELDS, |
| 214 | "query": {"queryString": query_string, "displayQueryString": request.text}, |
| 215 | "from": request.from, |
| 216 | "size": size, |
| 217 | }); |
| 218 | if top_results_count > 0 { |
| 219 | entity["topResultsCount"] = json!(top_results_count); |
| 220 | } |
| 221 | entity |
| 222 | } |
| 223 | |
| 224 | fn people_entity(query: &Value, from: usize, size: usize) -> Value { |
| 225 | json!({ |
| 226 | "entityType": "People", |
| 227 | "contentSources": ["Exchange"], |
| 228 | "Fields": PEOPLE_FIELDS, |
| 229 | "Filter": people_filter(), |
| 230 | "query": query, |
| 231 | "from": from, |
| 232 | "size": size, |
| 233 | }) |
| 234 | } |
| 235 | |
| 236 | fn file_entity(query: &Value, from: usize, size: usize) -> Value { |
| 237 | json!({ |
| 238 | "entityType": "File", |
| 239 | "contentSources": ["OneDriveBusiness", "Exchange"], |
| 240 | "query": query, |
| 241 | "from": from, |
| 242 | "size": size, |
| 243 | }) |
| 244 | } |
| 245 | |
| 246 | fn plain_entity(entity_type: &str, query: &Value, from: usize, size: usize) -> Value { |
| 247 | json!({ |
| 248 | "entityType": entity_type, |
| 249 | "contentSources": ["Teams"], |
| 250 | "query": query, |
| 251 | "from": from, |
| 252 | "size": size, |
| 253 | }) |
| 254 | } |
| 255 | |
| 256 | fn people_filter() -> Value { |
| 257 | let any_term = |field: &str, values: &[&str]| { |
| 258 | let terms: Vec<Value> = values |
| 259 | .iter() |
| 260 | .map(|value| json!({"Term": {field: value}})) |
| 261 | .collect(); |
| 262 | json!({"Or": terms}) |
| 263 | }; |
| 264 | json!({"And": [ |
| 265 | any_term("PeopleType", &["Person", "Other"]), |
| 266 | any_term( |
| 267 | "PeopleSubtype", |
| 268 | &[ |
| 269 | "OrganizationUser", |
| 270 | "MTOUser", |
| 271 | "PersonalContact", |
| 272 | "Guest", |
| 273 | "ImplicitContact", |
| 274 | "ChatImplicitContact", |
| 275 | "OrganizationContact", |
| 276 | "EDUser", |
| 277 | "Unknown", |
| 278 | ], |
| 279 | ), |
| 280 | any_term("Flags", &["NonHidden"]), |
| 281 | ]}) |
| 282 | } |
| 283 | |
| 284 | #[cfg(test)] |
| 285 | mod tests { |
| 286 | use super::*; |
| 287 | |
| 288 | #[test] |
| 289 | fn suggestions_ask_for_every_domain_in_teams_order() { |
| 290 | let body = suggestion_body("chr", "session", "request"); |
| 291 | let types: Vec<&str> = body["EntityRequests"] |
| 292 | .as_array() |
| 293 | .unwrap() |
| 294 | .iter() |
| 295 | .map(|entity| entity["EntityType"].as_str().unwrap()) |
| 296 | .collect(); |
| 297 | assert_eq!( |
| 298 | types, |
| 299 | [ |
| 300 | "People", "File", "Chat", "Team", "Channel", "Text", "Message", "Event" |
| 301 | ] |
| 302 | ); |
| 303 | assert_eq!(body["Cvid"], "session"); |
| 304 | assert_eq!(body["LogicalId"], "request"); |
| 305 | assert_eq!(body["EntityRequests"][0]["Query"]["QueryString"], "chr"); |
| 306 | assert_eq!( |
| 307 | body["EntityRequests"][0]["Provenances"], |
| 308 | json!(["Mailbox", "Directory"]) |
| 309 | ); |
| 310 | let subtypes = body["EntityRequests"][0]["Filter"]["And"][1]["Or"] |
| 311 | .as_array() |
| 312 | .unwrap(); |
| 313 | let names: Vec<&str> = subtypes |
| 314 | .iter() |
| 315 | .map(|term| term["Term"]["PeopleSubtype"].as_str().unwrap()) |
| 316 | .collect(); |
| 317 | assert!(names.contains(&"EDUser")); |
| 318 | assert!(names.contains(&"ChatImplicitContact")); |
| 319 | assert!(names.contains(&"ImplicitContact")); |
| 320 | assert!(names.contains(&"OrganizationContact")); |
| 321 | assert!(names.contains(&"Unknown")); |
| 322 | } |
| 323 | |
| 324 | #[test] |
| 325 | fn all_scope_uses_the_teams_section_sizes() { |
| 326 | let request = QueryRequest::new("release", QueryScope::All); |
| 327 | let body = query_body(&request, "user", "session", "request"); |
| 328 | let sizes: Vec<(String, u64)> = body["EntityRequests"] |
| 329 | .as_array() |
| 330 | .unwrap() |
| 331 | .iter() |
| 332 | .map(|entity| { |
| 333 | ( |
| 334 | entity["entityType"].as_str().unwrap().to_owned(), |
| 335 | entity["size"].as_u64().unwrap(), |
| 336 | ) |
| 337 | }) |
| 338 | .collect(); |
| 339 | assert_eq!( |
| 340 | sizes, |
| 341 | [ |
| 342 | ("Message".to_owned(), 3), |
| 343 | ("File".to_owned(), 3), |
| 344 | ("People".to_owned(), 8), |
| 345 | ("Chat".to_owned(), 3), |
| 346 | ("TeamsChannel".to_owned(), 3), |
| 347 | ] |
| 348 | ); |
| 349 | assert_eq!( |
| 350 | body["EntityRequests"][0]["query"]["queryString"], |
| 351 | "NOT (isClientSoftDeleted:TRUE) AND release" |
| 352 | ); |
| 353 | assert_eq!( |
| 354 | body["EntityRequests"][0]["query"]["displayQueryString"], |
| 355 | "release" |
| 356 | ); |
| 357 | assert_eq!(body["scenario"]["Dimensions"][0]["DimensionValue"], "All"); |
| 358 | } |
| 359 | |
| 360 | #[test] |
| 361 | fn messages_tab_pages_by_25_with_top_results_on_the_first_page_only() { |
| 362 | let first = query_body( |
| 363 | &QueryRequest::new("x", QueryScope::Messages), |
| 364 | "user", |
| 365 | "session", |
| 366 | "request", |
| 367 | ); |
| 368 | assert_eq!(first["EntityRequests"][0]["size"], 25); |
| 369 | assert_eq!(first["EntityRequests"][0]["topResultsCount"], 5); |
| 370 | let second = query_body( |
| 371 | &QueryRequest::new("x", QueryScope::Messages).page(25), |
| 372 | "user", |
| 373 | "session", |
| 374 | "request", |
| 375 | ); |
| 376 | assert_eq!(second["EntityRequests"][0]["from"], 25); |
| 377 | assert!(second["EntityRequests"][0].get("topResultsCount").is_none()); |
| 378 | } |
| 379 | |
| 380 | #[test] |
| 381 | fn find_in_chat_is_a_contextual_query() { |
| 382 | let request = QueryRequest::new("x", QueryScope::Messages).with_filters(vec![ |
| 383 | MessageFilter::InChat { |
| 384 | thread_id: "19:abc@thread.v2".to_owned(), |
| 385 | }, |
| 386 | ]); |
| 387 | let body = query_body(&request, "user", "session", "request"); |
| 388 | assert_eq!( |
| 389 | body["scenario"]["Dimensions"][0]["DimensionValue"], |
| 390 | "ContextualTypeahead" |
| 391 | ); |
| 392 | } |
| 393 | } |