fix(mentions): find people by alias like Teams
- Directory search also matches mailNickname and userPrincipalName ("bto" finds Bogdan); falls back to name and mail if Graph rejects the fields
- Directory hits the server matched on a field we do not hold stay in the list, ranked after name and mail matches
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -294,6 +294,9 @@ fn directory_person(user: User) -> PersonCandidate {
294
294
}
295
295
}
296
296
297
/// The directory matched it on a field we do not hold, like the alias "bto"; Teams lists it too.
298
const DIRECTORY_MATCH_TIER: u8 = 5;
299
297
300
/// Keeps matches only. Best match first, the incoming order breaks ties.
298
301
fn ranked(
299
302
candidates: impl Iterator<Item = MentionCandidate>,
@@ -326,11 +329,11 @@ fn match_tier(candidate: &MentionCandidate, query: &str) -> Option<u8> {
326
329
let MentionCandidate::Person(person) = candidate else {
327
330
return None;
328
331
};
329
let mail = person.mail.as_deref()?.to_lowercase();
330
if mail.starts_with(query) {
331
Some(3)
332
} else {
333
mail.contains(query).then_some(4)
332
let mail = person.mail.as_deref().map(str::to_lowercase);
333
match mail {
334
Some(mail) if mail.starts_with(query) => Some(3),
335
Some(mail) if mail.contains(query) => Some(4),
336
_ => (person.source == PersonSource::Directory).then_some(DIRECTORY_MATCH_TIER),
334
337
}
335
338
}
336
339
@@ -344,7 +347,7 @@ mod tests {
344
347
display_name: name.to_owned(),
345
348
mail: mail.map(str::to_owned),
346
349
job_title: None,
347
source: PersonSource::Directory,
350
source: PersonSource::Member,
348
351
})
349
352
}
350
353
@@ -386,6 +389,24 @@ mod tests {
386
389
assert_eq!(names(ranked), ["Ada", "Zed"]);
387
390
}
388
391
392
#[test]
393
fn a_directory_hit_on_the_alias_stays_after_the_name_matches() {
394
let MentionCandidate::Person(mut alias_hit) = person("1", "Bogdan Example", None) else {
395
unreachable!()
396
};
397
alias_hit.source = PersonSource::Directory;
398
let ranked = ranked(
399
[
400
MentionCandidate::Person(alias_hit),
401
person("2", "Bto Member", None),
402
person("3", "Nobody", None),
403
]
404
.into_iter(),
405
"bto",
406
);
407
assert_eq!(names(ranked), ["Bto Member", "Bogdan Example"]);
408
}
409
389
410
#[test]
390
411
fn empty_query_keeps_the_incoming_order() {
391
412
let ranked = ranked(
@@ -294,6 +294,9 @@ fn directory_person(user: User) -> PersonCandidate {294
}294
}295
}295
}296
296
297
/// The directory matched it on a field we do not hold, like the alias "bto"; Teams lists it too.298
const DIRECTORY_MATCH_TIER: u8 = 5;299
297
/// Keeps matches only. Best match first, the incoming order breaks ties.300
/// Keeps matches only. Best match first, the incoming order breaks ties.298
fn ranked(301
fn ranked(299
candidates: impl Iterator<Item = MentionCandidate>,302
candidates: impl Iterator<Item = MentionCandidate>,@@ -326,11 +329,11 @@ fn match_tier(candidate: &MentionCandidate, query: &str) -> Option<u8> {326
let MentionCandidate::Person(person) = candidate else {329
let MentionCandidate::Person(person) = candidate else {327
return None;330
return None;328
};331
};329
let mail = person.mail.as_deref()?.to_lowercase();332
let mail = person.mail.as_deref().map(str::to_lowercase);330
if mail.starts_with(query) {333
match mail {331
Some(3)334
Some(mail) if mail.starts_with(query) => Some(3),332
} else {335
Some(mail) if mail.contains(query) => Some(4),333
mail.contains(query).then_some(4)336
_ => (person.source == PersonSource::Directory).then_some(DIRECTORY_MATCH_TIER),334
}337
}335
}338
}336
339
@@ -344,7 +347,7 @@ mod tests {344
display_name: name.to_owned(),347
display_name: name.to_owned(),345
mail: mail.map(str::to_owned),348
mail: mail.map(str::to_owned),346
job_title: None,349
job_title: None,347
source: PersonSource::Directory,350
source: PersonSource::Member,348
})351
})349
}352
}350
353
@@ -386,6 +389,24 @@ mod tests {386
assert_eq!(names(ranked), ["Ada", "Zed"]);389
assert_eq!(names(ranked), ["Ada", "Zed"]);387
}390
}388
391
392
#[test]393
fn a_directory_hit_on_the_alias_stays_after_the_name_matches() {394
let MentionCandidate::Person(mut alias_hit) = person("1", "Bogdan Example", None) else {395
unreachable!()396
};397
alias_hit.source = PersonSource::Directory;398
let ranked = ranked(399
[400
MentionCandidate::Person(alias_hit),401
person("2", "Bto Member", None),402
person("3", "Nobody", None),403
]404
.into_iter(),405
"bto",406
);407
assert_eq!(names(ranked), ["Bto Member", "Bogdan Example"]);408
}409
389
#[test]410
#[test]390
fn empty_query_keeps_the_incoming_order() {411
fn empty_query_keeps_the_incoming_order() {391
let ranked = ranked(412
let ranked = ranked(@@ -236,16 +236,23 @@ impl Graph {
236
236
.await?
237
237
.items);
238
238
}
239
let mut request = Request::get(urls::people_search(query, PEOPLE_PAGE_SIZE));
239
match self.search_users(query, true, &scope).await {
240
Ok(users) => Ok(users),
241
Err(_) => self.search_users(query, false, &scope).await,
242
}
243
}
244
245
async fn search_users(&self, query: &str, with_alias: bool, scope: &Scope) -> Result<Vec<User>> {
246
let mut request = Request::get(urls::people_search(query, PEOPLE_PAGE_SIZE, with_alias));
240
247
request
241
248
.headers
242
249
.push(("ConsistencyLevel".to_owned(), "eventual".to_owned()));
243
250
let answer = self
244
251
.session
245
.batch(std::slice::from_ref(&request), &scope)
252
.batch(std::slice::from_ref(&request), scope)
246
253
.await?
247
254
.remove(0);
248
self.collect_pages::<User>(&request.url, answer, &scope)
255
self.collect_pages::<User>(&request.url, answer, scope)
249
256
.await
250
257
}
251
258
@@ -236,16 +236,23 @@ impl Graph {236
.await?236
.await?237
.items);237
.items);238
}238
}239
let mut request = Request::get(urls::people_search(query, PEOPLE_PAGE_SIZE));239
match self.search_users(query, true, &scope).await {240
Ok(users) => Ok(users),241
Err(_) => self.search_users(query, false, &scope).await,242
}243
}244
245
async fn search_users(&self, query: &str, with_alias: bool, scope: &Scope) -> Result<Vec<User>> {246
let mut request = Request::get(urls::people_search(query, PEOPLE_PAGE_SIZE, with_alias));240
request247
request241
.headers248
.headers242
.push(("ConsistencyLevel".to_owned(), "eventual".to_owned()));249
.push(("ConsistencyLevel".to_owned(), "eventual".to_owned()));243
let answer = self250
let answer = self244
.session251
.session245
.batch(std::slice::from_ref(&request), &scope)252
.batch(std::slice::from_ref(&request), scope)246
.await?253
.await?247
.remove(0);254
.remove(0);248
self.collect_pages::<User>(&request.url, answer, &scope)255
self.collect_pages::<User>(&request.url, answer, scope)249
.await256
.await250
}257
}251
258
@@ -381,9 +381,18 @@ pub fn channel_delta(
381
381
url
382
382
}
383
383
384
pub fn people_search(query: &str, top: usize) -> String {
384
pub fn people_search(query: &str, top: usize, with_alias: bool) -> String {
385
385
let term = query.replace('"', "");
386
let search = segment(&format!("\"displayName:{term}\" OR \"mail:{term}\""));
386
let fields: &[&str] = if with_alias {
387
&["displayName", "mail", "mailNickname", "userPrincipalName"]
388
} else {
389
&["displayName", "mail"]
390
};
391
let clauses: Vec<String> = fields
392
.iter()
393
.map(|field| format!("\"{field}:{term}\""))
394
.collect();
395
let search = segment(&clauses.join(" OR "));
387
396
format!(
388
397
"{GRAPH}/v1.0/users?$search={search}&$select=id,displayName,mail,userPrincipalName,jobTitle,department&$top={top}"
389
398
)
@@ -381,9 +381,18 @@ pub fn channel_delta(381
url381
url382
}382
}383
383
384
pub fn people_search(query: &str, top: usize) -> String {384
pub fn people_search(query: &str, top: usize, with_alias: bool) -> String {385
let term = query.replace('"', "");385
let term = query.replace('"', "");386
let search = segment(&format!("\"displayName:{term}\" OR \"mail:{term}\""));386
let fields: &[&str] = if with_alias {387
&["displayName", "mail", "mailNickname", "userPrincipalName"]388
} else {389
&["displayName", "mail"]390
};391
let clauses: Vec<String> = fields392
.iter()393
.map(|field| format!("\"{field}:{term}\""))394
.collect();395
let search = segment(&clauses.join(" OR "));387
format!(396
format!(388
"{GRAPH}/v1.0/users?$search={search}&$select=id,displayName,mail,userPrincipalName,jobTitle,department&$top={top}"397
"{GRAPH}/v1.0/users?$search={search}&$select=id,displayName,mail,userPrincipalName,jobTitle,department&$top={top}"389
)398
)