Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Add organization management permission and follow organization changes after account reloads (#66494)
morgankreydd3437011469
diff --git a/crates/client/src/user.rs b/crates/client/src/user.rsindex feaeeaf9446..3c661b6727d 100644--- a/crates/client/src/user.rs+++ b/crates/client/src/user.rs@@ -891,6 +891,10 @@ impl UserStore {.set_authenticated_user_info(Some(response.user.metrics_id.clone()), staff);}+ let previous_organization_id = self+ .current_organization+ .as_ref()+ .map(|organization| organization.id.clone());self.organizations = response.organizations.into_iter().map(Arc::new).collect();self.current_organization = response@@ -926,6 +930,16 @@ impl UserStore {}));self.plan_info = Some(response.plan);cx.emit(Event::PrivateUserInfoUpdated);+ // A reload replaces the selection when the user loses access to it, for example after+ // being removed from that organization. Listeners such as the LLM token must follow it.+ // The first load after sign-in is not a change, so it does not announce one.+ if previous_organization_id.is_some_and(|previous_organization_id| {+ self.current_organization+ .as_ref()+ .is_none_or(|organization| organization.id != previous_organization_id)+ }) {+ cx.emit(Event::OrganizationChanged);+ }}fn handle_message_to_client(this: WeakEntity<Self>, message: &MessageToClient, cx: &App) {@@ -1119,6 +1133,8 @@ mod tests {use gpui::TestAppContext;use http_client::FakeHttpClient;use settings::SettingsStore;+ use std::cell::Cell;+ use std::rc::Rc;#[gpui::test]async fn test_current_organization_has_no_active_subscription(cx: &mut TestAppContext) {@@ -1180,4 +1196,61 @@ mod tests {}));}}++ #[gpui::test]+ async fn test_reload_announces_a_changed_current_organization(cx: &mut TestAppContext) {+ cx.update(|cx| {+ let settings_store = SettingsStore::test(cx);+ cx.set_global(settings_store);+ });+ let client = cx.update(|cx| {+ Client::new(+ Arc::new(FakeSystemClock::new()),+ FakeHttpClient::with_404_response(),+ cx,+ )+ });+ let user_store = cx.new(|cx| UserStore::new(client, cx));+ let organization_changes = Rc::new(Cell::new(0));+ let _subscription = cx.update(|cx| {+ let organization_changes = organization_changes.clone();+ cx.subscribe(&user_store, move |_, event, _| {+ if matches!(event, Event::OrganizationChanged) {+ organization_changes.set(organization_changes.get() + 1);+ }+ })+ });+ let reload = |organization_ids: &[&str], default_id: &str, cx: &mut TestAppContext| {+ let mut response = make_get_authenticated_user_response(1, "user".into());+ response.organizations = organization_ids+ .iter()+ .map(|id| Organization {+ id: OrganizationId((*id).into()),+ name: (*id).into(),+ is_personal: *id == "personal",+ ui_permissions: Default::default(),+ })+ .collect();+ response.default_organization_id = Some(OrganizationId(default_id.into()));+ user_store.update(cx, |store, cx| {+ store.update_authenticated_user(response, cx)+ });+ };++ reload(&["personal", "team"], "team", cx);+ organization_changes.set(0);++ reload(&["personal", "team"], "team", cx);+ assert_eq!(organization_changes.get(), 0);++ // Removed from the selected organization.+ reload(&["personal"], "personal", cx);+ assert_eq!(organization_changes.get(), 1);+ assert_eq!(+ user_store.read_with(cx, |store, _| store+ .current_organization()+ .map(|organization| organization.id.clone())),+ Some(OrganizationId("personal".into()))+ );+ }}diff --git a/crates/cloud_api_types/src/cloud_api_types.rs b/crates/cloud_api_types/src/cloud_api_types.rsindex d13de05adc5..8c00b45abd2 100644--- a/crates/cloud_api_types/src/cloud_api_types.rs+++ b/crates/cloud_api_types/src/cloud_api_types.rs@@ -64,6 +64,11 @@ pub struct Organization {pub struct OrganizationUiPermissions {#[serde(default)]pub can_install_github_app: bool,+ /// Whether the user is an owner or admin, who may update the organization.+ ///+ /// This is also true for the owner of a personal organization.+ #[serde(default)]+ pub can_manage_organization: bool,}#[derive(Debug, PartialEq, Serialize, Deserialize)]