From 60e25d780a7c54ebbf353044634f71e9f73db63f Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 9 Aug 2023 13:43:16 -0700 Subject: [PATCH] Send channel permissions to clients when they fetch their channels --- crates/client/src/channel_store_tests.rs | 9 ++++-- crates/collab/src/db.rs | 30 ++++++++++++------- crates/collab/src/db/tests.rs | 12 ++++---- crates/collab/src/rpc.rs | 38 +++++++++++++++++------- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/crates/client/src/channel_store_tests.rs b/crates/client/src/channel_store_tests.rs index 4ee54d3eca5a828ca2e82cae4317fbd38de61964..f74169eb2a57eec59ffe34e58371f8d952875334 100644 --- a/crates/client/src/channel_store_tests.rs +++ b/crates/client/src/channel_store_tests.rs @@ -1,6 +1,5 @@ -use util::http::FakeHttpClient; - use super::*; +use util::http::FakeHttpClient; #[gpui::test] fn test_update_channels(cx: &mut AppContext) { @@ -25,6 +24,10 @@ fn test_update_channels(cx: &mut AppContext) { parent_id: None, }, ], + channel_permissions: vec![proto::ChannelPermission { + channel_id: 1, + is_admin: true, + }], ..Default::default() }, cx, @@ -64,7 +67,7 @@ fn test_update_channels(cx: &mut AppContext) { (0, "a", false), (1, "y", false), (0, "b", true), - (1, "x", false), + (1, "x", true), ], cx, ); diff --git a/crates/collab/src/db.rs b/crates/collab/src/db.rs index 8faea0e40265ef2b63a5b3bb3deaacb292a591d8..b7718be1187e7e07308a520bdea381776a2d7fd7 100644 --- a/crates/collab/src/db.rs +++ b/crates/collab/src/db.rs @@ -3442,10 +3442,7 @@ impl Database { .await } - pub async fn get_channels_for_user( - &self, - user_id: UserId, - ) -> Result<(Vec, HashMap>)> { + pub async fn get_channels_for_user(&self, user_id: UserId) -> Result { self.transaction(|tx| async move { let tx = tx; @@ -3462,6 +3459,11 @@ impl Database { .get_channel_descendants(channel_memberships.iter().map(|m| m.channel_id), &*tx) .await?; + let channels_with_admin_privileges = channel_memberships + .iter() + .filter_map(|membership| membership.admin.then_some(membership.channel_id)) + .collect(); + let mut channels = Vec::with_capacity(parents_by_child_id.len()); { let mut rows = channel::Entity::find() @@ -3484,7 +3486,7 @@ impl Database { UserId, } - let mut participants_by_channel: HashMap> = HashMap::default(); + let mut channel_participants: HashMap> = HashMap::default(); { let mut rows = room_participant::Entity::find() .inner_join(room::Entity) @@ -3497,14 +3499,15 @@ impl Database { .await?; while let Some(row) = rows.next().await { let row: (ChannelId, UserId) = row?; - participants_by_channel - .entry(row.0) - .or_default() - .push(row.1) + channel_participants.entry(row.0).or_default().push(row.1) } } - Ok((channels, participants_by_channel)) + Ok(ChannelsForUser { + channels, + channel_participants, + channels_with_admin_privileges, + }) }) .await } @@ -4072,6 +4075,13 @@ pub struct Channel { pub parent_id: Option, } +#[derive(Debug, PartialEq)] +pub struct ChannelsForUser { + pub channels: Vec, + pub channel_participants: HashMap>, + pub channels_with_admin_privileges: HashSet, +} + fn random_invite_code() -> String { nanoid::nanoid!(16) } diff --git a/crates/collab/src/db/tests.rs b/crates/collab/src/db/tests.rs index a659f3d16439b2bc82966598375e4d5c56156d08..2680d81aac8c9f3e4dce686b7274680556f4e388 100644 --- a/crates/collab/src/db/tests.rs +++ b/crates/collab/src/db/tests.rs @@ -954,9 +954,9 @@ test_both_dbs!(test_channels_postgres, test_channels_sqlite, db, { .await .unwrap(); - let (channels, _) = db.get_channels_for_user(a_id).await.unwrap(); + let result = db.get_channels_for_user(a_id).await.unwrap(); assert_eq!( - channels, + result.channels, vec![ Channel { id: zed_id, @@ -996,9 +996,9 @@ test_both_dbs!(test_channels_postgres, test_channels_sqlite, db, { ] ); - let (channels, _) = db.get_channels_for_user(b_id).await.unwrap(); + let result = db.get_channels_for_user(b_id).await.unwrap(); assert_eq!( - channels, + result.channels, vec![ Channel { id: zed_id, @@ -1029,9 +1029,9 @@ test_both_dbs!(test_channels_postgres, test_channels_sqlite, db, { let set_channel_admin = db.set_channel_member_admin(zed_id, a_id, b_id, true).await; assert!(set_channel_admin.is_ok()); - let (channels, _) = db.get_channels_for_user(b_id).await.unwrap(); + let result = db.get_channels_for_user(b_id).await.unwrap(); assert_eq!( - channels, + result.channels, vec![ Channel { id: zed_id, diff --git a/crates/collab/src/rpc.rs b/crates/collab/src/rpc.rs index 0f52c8c03a24e0b98b21c29a0677bbe684b69223..07d343959fa45d04d9adbd01af7d5f05d6341830 100644 --- a/crates/collab/src/rpc.rs +++ b/crates/collab/src/rpc.rs @@ -529,7 +529,7 @@ impl Server { this.app_state.db.set_user_connected_once(user_id, true).await?; } - let (contacts, invite_code, (channels, channel_participants), channel_invites) = future::try_join4( + let (contacts, invite_code, channels_for_user, channel_invites) = future::try_join4( this.app_state.db.get_contacts(user_id), this.app_state.db.get_invite_code_for_user(user_id), this.app_state.db.get_channels_for_user(user_id), @@ -540,7 +540,11 @@ impl Server { let mut pool = this.connection_pool.lock(); pool.add_connection(connection_id, user_id, user.admin); this.peer.send(connection_id, build_initial_contacts_update(contacts, &pool))?; - this.peer.send(connection_id, build_initial_channels_update(channels, channel_participants, channel_invites))?; + this.peer.send(connection_id, build_initial_channels_update( + channels_for_user.channels, + channels_for_user.channel_participants, + channel_invites + ))?; if let Some((code, count)) = invite_code { this.peer.send(connection_id, proto::UpdateInviteInfo { @@ -2364,22 +2368,36 @@ async fn respond_to_channel_invite( .remove_channel_invitations .push(channel_id.to_proto()); if request.accept { - let (channels, participants) = db.get_channels_for_user(session.user_id).await?; + let result = db.get_channels_for_user(session.user_id).await?; update .channels - .extend(channels.into_iter().map(|channel| proto::Channel { + .extend(result.channels.into_iter().map(|channel| proto::Channel { id: channel.id.to_proto(), name: channel.name, parent_id: channel.parent_id.map(ChannelId::to_proto), })); update .channel_participants - .extend(participants.into_iter().map(|(channel_id, user_ids)| { - proto::ChannelParticipants { - channel_id: channel_id.to_proto(), - participant_user_ids: user_ids.into_iter().map(UserId::to_proto).collect(), - } - })); + .extend( + result + .channel_participants + .into_iter() + .map(|(channel_id, user_ids)| proto::ChannelParticipants { + channel_id: channel_id.to_proto(), + participant_user_ids: user_ids.into_iter().map(UserId::to_proto).collect(), + }), + ); + update + .channel_permissions + .extend( + result + .channels_with_admin_privileges + .into_iter() + .map(|channel_id| proto::ChannelPermission { + channel_id: channel_id.to_proto(), + is_admin: true, + }), + ); } session.peer.send(session.connection_id, update)?; response.send(proto::Ack {})?;