BlueGit

ict rusty-teams

feat(chatsvc): list the account's home and guest tenants

Same middle-tier call the Teams org switcher makes. The home tenant
comes first; userType tells guest tenants apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Michael Wallner · pushed by metis

Authored 2026-11-15 · 2 weeks ago

Hash
7c44058e66e0615085e8df54e00ccae23b44cc8e
Parent
3707776f
3 files changed +76 −0
crates/chatsvc/src/lib.rs +2 −0
@@ -44,6 +44,7 @@ pub mod sounds;
44 44 pub mod substrate_search;
45 45 pub mod team_admin;
46 46 pub mod teams_cdn;
47 pub mod tenants;
47 48 pub mod thread_activity;
48 49 pub mod transcripts;
49 50 pub mod translate;
@@ -81,6 +82,7 @@ pub use emotions::{EmotionReaction, MessageEmotions, parse_message_emotions, use
81 82 pub use error::{Error, Result};
82 83 pub use forward::{ForwardResult, MAX_FORWARD_MESSAGES};
83 84 pub use gifs::{Gif, Gifs};
85 pub use tenants::{Tenant, list_tenants};
84 86 pub use google_calendar::{
85 87 AuthPending, GoogleCalendar, GoogleEventDraft, GoogleOAuth, TokenBundle, allocate_redirect_uri,
86 88 client_id_from_env, exchange_code, google_event_body, google_rsvp_query, google_rsvp_status,
@@ -44,6 +44,7 @@ pub mod sounds;
44pub mod substrate_search;
44pub mod substrate_search;
45pub mod team_admin;
45pub mod team_admin;
46pub mod teams_cdn;
46pub mod teams_cdn;
47pub mod tenants;
47pub mod thread_activity;
48pub mod thread_activity;
48pub mod transcripts;
49pub mod transcripts;
49pub mod translate;
50pub mod translate;
@@ -81,6 +82,7 @@ pub use emotions::{EmotionReaction, MessageEmotions, parse_message_emotions, use
81pub use error::{Error, Result};
82pub use error::{Error, Result};
82pub use forward::{ForwardResult, MAX_FORWARD_MESSAGES};
83pub use forward::{ForwardResult, MAX_FORWARD_MESSAGES};
83pub use gifs::{Gif, Gifs};
84pub use gifs::{Gif, Gifs};
85pub use tenants::{Tenant, list_tenants};
84pub use google_calendar::{
86pub use google_calendar::{
85 AuthPending, GoogleCalendar, GoogleEventDraft, GoogleOAuth, TokenBundle, allocate_redirect_uri,
87 AuthPending, GoogleCalendar, GoogleEventDraft, GoogleOAuth, TokenBundle, allocate_redirect_uri,
86 client_id_from_env, exchange_code, google_event_body, google_rsvp_query, google_rsvp_status,
88 client_id_from_env, exchange_code, google_event_body, google_rsvp_query, google_rsvp_status,
crates/chatsvc/src/tenants.rs added +73 −0
@@ -0,0 +1,73 @@
1 //! The organizations the signed-in account belongs to, as the Teams org switcher lists them.
2 use serde_json::Value;
3 use session::{Method, Session};
4
5 use crate::cards::{DEFAULT_MT_REGION, spaces_scope};
6 use crate::error::{Error, Result};
7
8 #[derive(Debug, Clone, PartialEq, Eq)]
9 pub struct Tenant {
10 pub id: String,
11 pub name: String,
12 /// The account is a guest here; its home tenant is the one with `guest == false`.
13 pub guest: bool,
14 }
15
16 /// Every tenant of the account, home tenant first.
17 pub async fn list_tenants(session: &Session) -> Result<Vec<Tenant>> {
18 let url = format!("https://teams.cloud.microsoft/api/mt/{DEFAULT_MT_REGION}/beta/users/tenants");
19 let answer = session
20 .request(Method::Get, &url, &spaces_scope(), None)
21 .await?;
22 parse_tenants(&answer.body)
23 .ok_or_else(|| Error::UnexpectedAnswer(format!("{url}: no tenant list")))
24 }
25
26 fn parse_tenants(body: &Value) -> Option<Vec<Tenant>> {
27 let mut tenants: Vec<Tenant> = body
28 .as_array()?
29 .iter()
30 .filter_map(|entry| {
31 Some(Tenant {
32 id: entry["tenantId"].as_str()?.to_ascii_lowercase(),
33 name: entry["tenantName"].as_str()?.to_owned(),
34 guest: entry["userType"]
35 .as_str()
36 .is_some_and(|kind| kind.eq_ignore_ascii_case("guest")),
37 })
38 })
39 .collect();
40 tenants.sort_by_key(|tenant| tenant.guest);
41 Some(tenants)
42 }
43
44 #[cfg(test)]
45 mod tests {
46 use super::*;
47 use serde_json::json;
48
49 #[test]
50 fn lists_the_home_tenant_first_and_marks_guest_tenants() {
51 let body = json!([
52 {"tenantId": "BBBB-GUEST", "tenantName": "Guest Org", "userType": "guest"},
53 {"tenantId": "aaaa-home", "tenantName": "Home Org", "userType": "member"},
54 {"tenantName": "no id"},
55 ]);
56 assert_eq!(
57 parse_tenants(&body).unwrap(),
58 vec![
59 Tenant {
60 id: "aaaa-home".into(),
61 name: "Home Org".into(),
62 guest: false
63 },
64 Tenant {
65 id: "bbbb-guest".into(),
66 name: "Guest Org".into(),
67 guest: true
68 },
69 ]
70 );
71 assert_eq!(parse_tenants(&json!({"error": 1})), None);
72 }
73 }
@@ -0,0 +1,73 @@
1//! The organizations the signed-in account belongs to, as the Teams org switcher lists them.
2use serde_json::Value;
3use session::{Method, Session};
4
5use crate::cards::{DEFAULT_MT_REGION, spaces_scope};
6use crate::error::{Error, Result};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct Tenant {
10 pub id: String,
11 pub name: String,
12 /// The account is a guest here; its home tenant is the one with `guest == false`.
13 pub guest: bool,
14}
15
16/// Every tenant of the account, home tenant first.
17pub async fn list_tenants(session: &Session) -> Result<Vec<Tenant>> {
18 let url = format!("https://teams.cloud.microsoft/api/mt/{DEFAULT_MT_REGION}/beta/users/tenants");
19 let answer = session
20 .request(Method::Get, &url, &spaces_scope(), None)
21 .await?;
22 parse_tenants(&answer.body)
23 .ok_or_else(|| Error::UnexpectedAnswer(format!("{url}: no tenant list")))
24}
25
26fn parse_tenants(body: &Value) -> Option<Vec<Tenant>> {
27 let mut tenants: Vec<Tenant> = body
28 .as_array()?
29 .iter()
30 .filter_map(|entry| {
31 Some(Tenant {
32 id: entry["tenantId"].as_str()?.to_ascii_lowercase(),
33 name: entry["tenantName"].as_str()?.to_owned(),
34 guest: entry["userType"]
35 .as_str()
36 .is_some_and(|kind| kind.eq_ignore_ascii_case("guest")),
37 })
38 })
39 .collect();
40 tenants.sort_by_key(|tenant| tenant.guest);
41 Some(tenants)
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use serde_json::json;
48
49 #[test]
50 fn lists_the_home_tenant_first_and_marks_guest_tenants() {
51 let body = json!([
52 {"tenantId": "BBBB-GUEST", "tenantName": "Guest Org", "userType": "guest"},
53 {"tenantId": "aaaa-home", "tenantName": "Home Org", "userType": "member"},
54 {"tenantName": "no id"},
55 ]);
56 assert_eq!(
57 parse_tenants(&body).unwrap(),
58 vec![
59 Tenant {
60 id: "aaaa-home".into(),
61 name: "Home Org".into(),
62 guest: false
63 },
64 Tenant {
65 id: "bbbb-guest".into(),
66 name: "Guest Org".into(),
67 guest: true
68 },
69 ]
70 );
71 assert_eq!(parse_tenants(&json!({"error": 1})), None);
72 }
73}
docs/research/internal-endpoints.md +1 −0
@@ -65,6 +65,7 @@ Token audiences (not requests): `crates/session/src/scope.rs` - `ic3.teams.offic
65 65 | `/beta/me/calendarEvents/privateMeeting/schedulingService/create` | POST | `crates/chatsvc/src/calendar.rs` | Meet now | W | partly - `/me/onlineMeetings`; no ad-hoc chat unhide |
66 66 | `/beta/chats/{id}/apps/chatentitlements` | GET | `crates/chatsvc/src/cards.rs` | apps entitled in a chat | R | partly - `/chats/{id}/installedApps` |
67 67 | `/beta/commands/com.microsoft.teamspace.inputextension.giphy/execute?version=1.0` | POST | `crates/chatsvc/src/gifs.rs` | GIF search and trending | R | no |
68 | `/beta/users/tenants` | GET | `crates/chatsvc/src/tenants.rs` | the account's home and guest tenants for the org switcher (`userType` member or guest) | R | no - Graph lists only the tenant of the token |
68 69 | `/beta/users/fetchShortProfile?...&includeBots=true` | POST | `crates/chatsvc/src/bot_profiles.rs` | bot names and images | R | no |
69 70 | `/beta/users/useraggregatesettings` | POST | `crates/chatsvc/src/language_settings.rs`, `crates/chatsvc/src/sound_settings.rs` | translation, voice and recording policy settings | R | no |
70 71 | `/beta/users/languageAccountSettings` | PATCH | `crates/chatsvc/src/language_settings.rs` | translation preferences | W | no |
@@ -65,6 +65,7 @@ Token audiences (not requests): `crates/session/src/scope.rs` - `ic3.teams.offic
65| `/beta/me/calendarEvents/privateMeeting/schedulingService/create` | POST | `crates/chatsvc/src/calendar.rs` | Meet now | W | partly - `/me/onlineMeetings`; no ad-hoc chat unhide |
65| `/beta/me/calendarEvents/privateMeeting/schedulingService/create` | POST | `crates/chatsvc/src/calendar.rs` | Meet now | W | partly - `/me/onlineMeetings`; no ad-hoc chat unhide |
66| `/beta/chats/{id}/apps/chatentitlements` | GET | `crates/chatsvc/src/cards.rs` | apps entitled in a chat | R | partly - `/chats/{id}/installedApps` |
66| `/beta/chats/{id}/apps/chatentitlements` | GET | `crates/chatsvc/src/cards.rs` | apps entitled in a chat | R | partly - `/chats/{id}/installedApps` |
67| `/beta/commands/com.microsoft.teamspace.inputextension.giphy/execute?version=1.0` | POST | `crates/chatsvc/src/gifs.rs` | GIF search and trending | R | no |
67| `/beta/commands/com.microsoft.teamspace.inputextension.giphy/execute?version=1.0` | POST | `crates/chatsvc/src/gifs.rs` | GIF search and trending | R | no |
68| `/beta/users/tenants` | GET | `crates/chatsvc/src/tenants.rs` | the account's home and guest tenants for the org switcher (`userType` member or guest) | R | no - Graph lists only the tenant of the token |
68| `/beta/users/fetchShortProfile?...&includeBots=true` | POST | `crates/chatsvc/src/bot_profiles.rs` | bot names and images | R | no |
69| `/beta/users/fetchShortProfile?...&includeBots=true` | POST | `crates/chatsvc/src/bot_profiles.rs` | bot names and images | R | no |
69| `/beta/users/useraggregatesettings` | POST | `crates/chatsvc/src/language_settings.rs`, `crates/chatsvc/src/sound_settings.rs` | translation, voice and recording policy settings | R | no |
70| `/beta/users/useraggregatesettings` | POST | `crates/chatsvc/src/language_settings.rs`, `crates/chatsvc/src/sound_settings.rs` | translation, voice and recording policy settings | R | no |
70| `/beta/users/languageAccountSettings` | PATCH | `crates/chatsvc/src/language_settings.rs` | translation preferences | W | no |
71| `/beta/users/languageAccountSettings` | PATCH | `crates/chatsvc/src/language_settings.rs` | translation preferences | W | no |