Send channel permissions to clients when they fetch their channels

This commit is contained in:
Max Brunsfeld 2023-08-09 13:43:16 -07:00
parent a3623ec2b8
commit 60e25d780a
4 changed files with 60 additions and 29 deletions

View file

@ -1,6 +1,5 @@
use util::http::FakeHttpClient;
use super::*; use super::*;
use util::http::FakeHttpClient;
#[gpui::test] #[gpui::test]
fn test_update_channels(cx: &mut AppContext) { fn test_update_channels(cx: &mut AppContext) {
@ -25,6 +24,10 @@ fn test_update_channels(cx: &mut AppContext) {
parent_id: None, parent_id: None,
}, },
], ],
channel_permissions: vec![proto::ChannelPermission {
channel_id: 1,
is_admin: true,
}],
..Default::default() ..Default::default()
}, },
cx, cx,
@ -64,7 +67,7 @@ fn test_update_channels(cx: &mut AppContext) {
(0, "a", false), (0, "a", false),
(1, "y", false), (1, "y", false),
(0, "b", true), (0, "b", true),
(1, "x", false), (1, "x", true),
], ],
cx, cx,
); );

View file

@ -3442,10 +3442,7 @@ impl Database {
.await .await
} }
pub async fn get_channels_for_user( pub async fn get_channels_for_user(&self, user_id: UserId) -> Result<ChannelsForUser> {
&self,
user_id: UserId,
) -> Result<(Vec<Channel>, HashMap<ChannelId, Vec<UserId>>)> {
self.transaction(|tx| async move { self.transaction(|tx| async move {
let tx = tx; let tx = tx;
@ -3462,6 +3459,11 @@ impl Database {
.get_channel_descendants(channel_memberships.iter().map(|m| m.channel_id), &*tx) .get_channel_descendants(channel_memberships.iter().map(|m| m.channel_id), &*tx)
.await?; .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 channels = Vec::with_capacity(parents_by_child_id.len());
{ {
let mut rows = channel::Entity::find() let mut rows = channel::Entity::find()
@ -3484,7 +3486,7 @@ impl Database {
UserId, UserId,
} }
let mut participants_by_channel: HashMap<ChannelId, Vec<UserId>> = HashMap::default(); let mut channel_participants: HashMap<ChannelId, Vec<UserId>> = HashMap::default();
{ {
let mut rows = room_participant::Entity::find() let mut rows = room_participant::Entity::find()
.inner_join(room::Entity) .inner_join(room::Entity)
@ -3497,14 +3499,15 @@ impl Database {
.await?; .await?;
while let Some(row) = rows.next().await { while let Some(row) = rows.next().await {
let row: (ChannelId, UserId) = row?; let row: (ChannelId, UserId) = row?;
participants_by_channel channel_participants.entry(row.0).or_default().push(row.1)
.entry(row.0)
.or_default()
.push(row.1)
} }
} }
Ok((channels, participants_by_channel)) Ok(ChannelsForUser {
channels,
channel_participants,
channels_with_admin_privileges,
})
}) })
.await .await
} }
@ -4072,6 +4075,13 @@ pub struct Channel {
pub parent_id: Option<ChannelId>, pub parent_id: Option<ChannelId>,
} }
#[derive(Debug, PartialEq)]
pub struct ChannelsForUser {
pub channels: Vec<Channel>,
pub channel_participants: HashMap<ChannelId, Vec<UserId>>,
pub channels_with_admin_privileges: HashSet<ChannelId>,
}
fn random_invite_code() -> String { fn random_invite_code() -> String {
nanoid::nanoid!(16) nanoid::nanoid!(16)
} }

View file

@ -954,9 +954,9 @@ test_both_dbs!(test_channels_postgres, test_channels_sqlite, db, {
.await .await
.unwrap(); .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!( assert_eq!(
channels, result.channels,
vec![ vec![
Channel { Channel {
id: zed_id, 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!( assert_eq!(
channels, result.channels,
vec![ vec![
Channel { Channel {
id: zed_id, 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; let set_channel_admin = db.set_channel_member_admin(zed_id, a_id, b_id, true).await;
assert!(set_channel_admin.is_ok()); 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!( assert_eq!(
channels, result.channels,
vec![ vec![
Channel { Channel {
id: zed_id, id: zed_id,

View file

@ -529,7 +529,7 @@ impl Server {
this.app_state.db.set_user_connected_once(user_id, true).await?; 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_contacts(user_id),
this.app_state.db.get_invite_code_for_user(user_id), this.app_state.db.get_invite_code_for_user(user_id),
this.app_state.db.get_channels_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(); let mut pool = this.connection_pool.lock();
pool.add_connection(connection_id, user_id, user.admin); 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_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 { if let Some((code, count)) = invite_code {
this.peer.send(connection_id, proto::UpdateInviteInfo { this.peer.send(connection_id, proto::UpdateInviteInfo {
@ -2364,22 +2368,36 @@ async fn respond_to_channel_invite(
.remove_channel_invitations .remove_channel_invitations
.push(channel_id.to_proto()); .push(channel_id.to_proto());
if request.accept { 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 update
.channels .channels
.extend(channels.into_iter().map(|channel| proto::Channel { .extend(result.channels.into_iter().map(|channel| proto::Channel {
id: channel.id.to_proto(), id: channel.id.to_proto(),
name: channel.name, name: channel.name,
parent_id: channel.parent_id.map(ChannelId::to_proto), parent_id: channel.parent_id.map(ChannelId::to_proto),
})); }));
update update
.channel_participants .channel_participants
.extend(participants.into_iter().map(|(channel_id, user_ids)| { .extend(
proto::ChannelParticipants { result
.channel_participants
.into_iter()
.map(|(channel_id, user_ids)| proto::ChannelParticipants {
channel_id: channel_id.to_proto(), channel_id: channel_id.to_proto(),
participant_user_ids: user_ids.into_iter().map(UserId::to_proto).collect(), 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)?; session.peer.send(session.connection_id, update)?;
response.send(proto::Ack {})?; response.send(proto::Ack {})?;