BlueGit

ict rusty-teams

fix(people): a chat member of another tenant counts as external

- thread members carry their tenantId; members outside the own tenant are stored as external
- before, only people the federated lookup answered were marked, so someone whose name was already known missed the label

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

metis Daniel Däschle · pushed by metis

Authored 2026-11-20 · 11 days ago

Hash
c305b8c2a093893fe21a5507a80566ce5fe50233
Parent
a5957bf4
3 files changed +55 −12
crates/core/src/native/chat.rs +1 −1
@@ -4,7 +4,7 @@ use serde_json::{Value, json};
4 4
5 5 use super::message::graph_message_json;
6 6
7 const ORGID_PREFIX: &str = "8:orgid:";
7 pub(super) const ORGID_PREFIX: &str = "8:orgid:";
8 8 const ONE_ON_ONE_SUFFIX: &str = "@unq.gbl.spaces";
9 9 const OWNER_ROLE: &str = "admin";
10 10
@@ -4,7 +4,7 @@ use serde_json::{Value, json};
4
4
5use super::message::graph_message_json;
5use super::message::graph_message_json;
6
6
7const ORGID_PREFIX: &str = "8:orgid:";
7pub(super) const ORGID_PREFIX: &str = "8:orgid:";
8const ONE_ON_ONE_SUFFIX: &str = "@unq.gbl.spaces";
8const ONE_ON_ONE_SUFFIX: &str = "@unq.gbl.spaces";
9const OWNER_ROLE: &str = "admin";
9const OWNER_ROLE: &str = "admin";
10
10
crates/core/src/native/source.rs +52 −10
@@ -9,8 +9,8 @@ use serde_json::Value;
9 9 use session::Session;
10 10
11 11 use super::chat::{
12 NativeMember, aggregator_members, chat_type, graph_chat, graph_chat_from_aggregator,
13 graph_member, one_on_one_ids, thread_members,
12 NativeMember, ORGID_PREFIX, aggregator_members, chat_type, graph_chat,
13 graph_chat_from_aggregator, graph_member, one_on_one_ids, thread_members,
14 14 };
15 15 use super::message::graph_message;
16 16 use crate::error::{Error, Result};
@@ -24,6 +24,8 @@ static PROFILES: LazyLock<Mutex<HashMap<String, Option<UserProfile>>>> =
24 24 LazyLock::new(|| Mutex::new(HashMap::new()));
25 25 static EXTERNAL: LazyLock<Mutex<HashMap<String, Option<String>>>> =
26 26 LazyLock::new(|| Mutex::new(HashMap::new()));
27 static TENANTS: LazyLock<Mutex<HashMap<String, String>>> =
28 LazyLock::new(|| Mutex::new(HashMap::new()));
27 29 /// Thread members by chat, kept while the thread's roster version stays the same.
28 30 static ROSTERS: LazyLock<Mutex<HashMap<String, (String, Vec<NativeMember>)>>> =
29 31 LazyLock::new(|| Mutex::new(HashMap::new()));
@@ -208,6 +210,7 @@ impl<'a> NativeChats<'a> {
208 210 }
209 211 match listing.thread(chat_id).await {
210 212 Ok(thread) => {
213 remember_tenants(&thread);
211 214 let members = thread_members(&thread);
212 215 ROSTERS
213 216 .lock()
@@ -319,14 +322,34 @@ pub fn is_email_address(query: &str) -> bool {
319 322 })
320 323 }
321 324
322 /// People of other organizations the lookups met, with their organization's name.
323 pub fn external_people() -> Vec<(String, Option<String>)> {
324 EXTERNAL
325 .lock()
326 .unwrap()
327 .iter()
328 .map(|(user_id, tenant)| (user_id.clone(), tenant.clone()))
329 .collect()
325 fn remember_tenants(thread: &Value) {
326 let mut tenants = TENANTS.lock().unwrap();
327 for member in thread["members"].as_array().into_iter().flatten() {
328 if let (Some(user_id), Some(tenant)) = (
329 member["id"]
330 .as_str()
331 .and_then(|id| id.strip_prefix(ORGID_PREFIX)),
332 member["tenantId"]
333 .as_str()
334 .filter(|tenant| !tenant.is_empty()),
335 ) {
336 tenants.insert(user_id.to_owned(), tenant.to_owned());
337 }
338 }
339 }
340
341 /// People of other organizations the lookups met, with their organization's name when known.
342 /// A chat member whose tenant is not `home_tenant` counts too.
343 pub fn external_people(home_tenant: Option<&str>) -> Vec<(String, Option<String>)> {
344 let mut people: HashMap<String, Option<String>> = EXTERNAL.lock().unwrap().clone();
345 if let Some(home_tenant) = home_tenant {
346 for (user_id, tenant) in TENANTS.lock().unwrap().iter() {
347 if tenant != home_tenant {
348 people.entry(user_id.clone()).or_default();
349 }
350 }
351 }
352 people.into_iter().collect()
330 353 }
331 354
332 355 /// What the directory lookups learned about a user, for callers outside the chat path.
@@ -344,6 +367,25 @@ fn profile_email(user_id: &str) -> Option<String> {
344 367
345 368 #[cfg(test)]
346 369 mod tests {
370 #[test]
371 fn a_chat_member_of_another_tenant_is_external() {
372 super::remember_tenants(&serde_json::json!({"members": [
373 {"id": "8:orgid:tenant-test-home", "tenantId": "home"},
374 {"id": "8:orgid:tenant-test-guest", "tenantId": "other"},
375 ]}));
376 let people = super::external_people(Some("home"));
377 assert!(
378 people
379 .iter()
380 .any(|(user_id, _)| user_id == "tenant-test-guest")
381 );
382 assert!(
383 !people
384 .iter()
385 .any(|(user_id, _)| user_id == "tenant-test-home")
386 );
387 }
388
347 389 #[test]
348 390 fn only_a_full_email_address_asks_for_people_of_other_organizations() {
349 391 assert!(super::is_email_address(" ann@example.com "));
@@ -9,8 +9,8 @@ use serde_json::Value;
9use session::Session;
9use session::Session;
10
10
11use super::chat::{
11use super::chat::{
12 NativeMember, aggregator_members, chat_type, graph_chat, graph_chat_from_aggregator,
12 NativeMember, ORGID_PREFIX, aggregator_members, chat_type, graph_chat,
13 graph_member, one_on_one_ids, thread_members,
13 graph_chat_from_aggregator, graph_member, one_on_one_ids, thread_members,
14};
14};
15use super::message::graph_message;
15use super::message::graph_message;
16use crate::error::{Error, Result};
16use crate::error::{Error, Result};
@@ -24,6 +24,8 @@ static PROFILES: LazyLock<Mutex<HashMap<String, Option<UserProfile>>>> =
24 LazyLock::new(|| Mutex::new(HashMap::new()));
24 LazyLock::new(|| Mutex::new(HashMap::new()));
25static EXTERNAL: LazyLock<Mutex<HashMap<String, Option<String>>>> =
25static EXTERNAL: LazyLock<Mutex<HashMap<String, Option<String>>>> =
26 LazyLock::new(|| Mutex::new(HashMap::new()));
26 LazyLock::new(|| Mutex::new(HashMap::new()));
27static TENANTS: LazyLock<Mutex<HashMap<String, String>>> =
28 LazyLock::new(|| Mutex::new(HashMap::new()));
27/// Thread members by chat, kept while the thread's roster version stays the same.
29/// Thread members by chat, kept while the thread's roster version stays the same.
28static ROSTERS: LazyLock<Mutex<HashMap<String, (String, Vec<NativeMember>)>>> =
30static ROSTERS: LazyLock<Mutex<HashMap<String, (String, Vec<NativeMember>)>>> =
29 LazyLock::new(|| Mutex::new(HashMap::new()));
31 LazyLock::new(|| Mutex::new(HashMap::new()));
@@ -208,6 +210,7 @@ impl<'a> NativeChats<'a> {
208 }
210 }
209 match listing.thread(chat_id).await {
211 match listing.thread(chat_id).await {
210 Ok(thread) => {
212 Ok(thread) => {
213 remember_tenants(&thread);
211 let members = thread_members(&thread);
214 let members = thread_members(&thread);
212 ROSTERS
215 ROSTERS
213 .lock()
216 .lock()
@@ -319,14 +322,34 @@ pub fn is_email_address(query: &str) -> bool {
319 })
322 })
320}
323}
321
324
322/// People of other organizations the lookups met, with their organization's name.
325fn remember_tenants(thread: &Value) {
323pub fn external_people() -> Vec<(String, Option<String>)> {
326 let mut tenants = TENANTS.lock().unwrap();
324 EXTERNAL
327 for member in thread["members"].as_array().into_iter().flatten() {
325 .lock()
328 if let (Some(user_id), Some(tenant)) = (
326 .unwrap()
329 member["id"]
327 .iter()
330 .as_str()
328 .map(|(user_id, tenant)| (user_id.clone(), tenant.clone()))
331 .and_then(|id| id.strip_prefix(ORGID_PREFIX)),
329 .collect()
332 member["tenantId"]
333 .as_str()
334 .filter(|tenant| !tenant.is_empty()),
335 ) {
336 tenants.insert(user_id.to_owned(), tenant.to_owned());
337 }
338 }
339}
340
341/// People of other organizations the lookups met, with their organization's name when known.
342/// A chat member whose tenant is not `home_tenant` counts too.
343pub fn external_people(home_tenant: Option<&str>) -> Vec<(String, Option<String>)> {
344 let mut people: HashMap<String, Option<String>> = EXTERNAL.lock().unwrap().clone();
345 if let Some(home_tenant) = home_tenant {
346 for (user_id, tenant) in TENANTS.lock().unwrap().iter() {
347 if tenant != home_tenant {
348 people.entry(user_id.clone()).or_default();
349 }
350 }
351 }
352 people.into_iter().collect()
330}
353}
331
354
332/// What the directory lookups learned about a user, for callers outside the chat path.
355/// What the directory lookups learned about a user, for callers outside the chat path.
@@ -344,6 +367,25 @@ fn profile_email(user_id: &str) -> Option<String> {
344
367
345#[cfg(test)]
368#[cfg(test)]
346mod tests {
369mod tests {
370 #[test]
371 fn a_chat_member_of_another_tenant_is_external() {
372 super::remember_tenants(&serde_json::json!({"members": [
373 {"id": "8:orgid:tenant-test-home", "tenantId": "home"},
374 {"id": "8:orgid:tenant-test-guest", "tenantId": "other"},
375 ]}));
376 let people = super::external_people(Some("home"));
377 assert!(
378 people
379 .iter()
380 .any(|(user_id, _)| user_id == "tenant-test-guest")
381 );
382 assert!(
383 !people
384 .iter()
385 .any(|(user_id, _)| user_id == "tenant-test-home")
386 );
387 }
388
347 #[test]
389 #[test]
348 fn only_a_full_email_address_asks_for_people_of_other_organizations() {
390 fn only_a_full_email_address_asks_for_people_of_other_organizations() {
349 assert!(super::is_email_address(" ann@example.com "));
391 assert!(super::is_email_address(" ann@example.com "));
crates/core/src/sidebar_sync.rs +2 −1
@@ -85,8 +85,9 @@ impl<R: Remote> SyncEngine<R> {
85 85 let (records, hidden) = self.split_page(&page, &my_user_id)?;
86 86 let any_changed = self.any_chat_changed(&records)?;
87 87 self.store.upsert_chats(&records)?;
88 let home_tenant = self.my_tenant_id_from_any_chat().await.ok();
88 89 self.store
89 .upsert_external_people(&crate::native::external_people())?;
90 .upsert_external_people(&crate::native::external_people(home_tenant.as_deref()))?;
90 91 self.fill_kept_previews(&records)?;
91 92 kept_ids.extend(records.iter().map(|record| record.id.clone()));
92 93 hidden_ids.extend(hidden);
@@ -85,8 +85,9 @@ impl<R: Remote> SyncEngine<R> {
85 let (records, hidden) = self.split_page(&page, &my_user_id)?;
85 let (records, hidden) = self.split_page(&page, &my_user_id)?;
86 let any_changed = self.any_chat_changed(&records)?;
86 let any_changed = self.any_chat_changed(&records)?;
87 self.store.upsert_chats(&records)?;
87 self.store.upsert_chats(&records)?;
88 let home_tenant = self.my_tenant_id_from_any_chat().await.ok();
88 self.store
89 self.store
89 .upsert_external_people(&crate::native::external_people())?;
90 .upsert_external_people(&crate::native::external_people(home_tenant.as_deref()))?;
90 self.fill_kept_previews(&records)?;
91 self.fill_kept_previews(&records)?;
91 kept_ids.extend(records.iter().map(|record| record.id.clone()));
92 kept_ids.extend(records.iter().map(|record| record.id.clone()));
92 hidden_ids.extend(hidden);
93 hidden_ids.extend(hidden);