Implement basic channel member management UI

Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
Max Brunsfeld 2023-08-03 14:49:01 -07:00
parent 129f2890c5
commit a7e883d956
9 changed files with 368 additions and 100 deletions

View file

@ -213,20 +213,21 @@ impl Database {
);
let (channel_id, room) = self.get_channel_room(room_id, &tx).await?;
let channel_members = if let Some(channel_id) = channel_id {
self.get_channel_members_internal(channel_id, &tx).await?
let channel_members;
if let Some(channel_id) = channel_id {
channel_members = self.get_channel_members_internal(channel_id, &tx).await?;
} else {
Vec::new()
};
channel_members = Vec::new();
// Delete the room if it becomes empty.
if room.participants.is_empty() {
project::Entity::delete_many()
.filter(project::Column::RoomId.eq(room_id))
.exec(&*tx)
.await?;
room::Entity::delete_by_id(room_id).exec(&*tx).await?;
}
// Delete the room if it becomes empty.
if room.participants.is_empty() {
project::Entity::delete_many()
.filter(project::Column::RoomId.eq(room_id))
.exec(&*tx)
.await?;
room::Entity::delete_by_id(room_id).exec(&*tx).await?;
}
};
Ok(RefreshedRoom {
room,
@ -3475,10 +3476,61 @@ impl Database {
}
pub async fn get_channel_members(&self, id: ChannelId) -> Result<Vec<UserId>> {
self.transaction(|tx| async move { self.get_channel_members_internal(id, &*tx).await })
.await
}
// TODO: Add a chekc whether this user is allowed to read this channel
pub async fn get_channel_member_details(
&self,
id: ChannelId,
) -> Result<Vec<proto::ChannelMember>> {
self.transaction(|tx| async move {
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
enum QueryMemberDetails {
UserId,
IsDirectMember,
Accepted,
}
let tx = tx;
let user_ids = self.get_channel_members_internal(id, &*tx).await?;
Ok(user_ids)
let ancestor_ids = self.get_channel_ancestors(id, &*tx).await?;
let mut stream = channel_member::Entity::find()
.distinct()
.filter(channel_member::Column::ChannelId.is_in(ancestor_ids.iter().copied()))
.select_only()
.column(channel_member::Column::UserId)
.column_as(
channel_member::Column::ChannelId.eq(id),
QueryMemberDetails::IsDirectMember,
)
.column(channel_member::Column::Accepted)
.order_by_asc(channel_member::Column::UserId)
.into_values::<_, QueryMemberDetails>()
.stream(&*tx)
.await?;
let mut rows = Vec::<proto::ChannelMember>::new();
while let Some(row) = stream.next().await {
let (user_id, is_direct_member, is_invite_accepted): (UserId, bool, bool) = row?;
let kind = match (is_direct_member, is_invite_accepted) {
(true, true) => proto::channel_member::Kind::Member,
(true, false) => proto::channel_member::Kind::Invitee,
(false, true) => proto::channel_member::Kind::AncestorMember,
(false, false) => continue,
};
let user_id = user_id.to_proto();
let kind = kind.into();
if let Some(last_row) = rows.last_mut() {
if last_row.user_id == user_id {
last_row.kind = last_row.kind.min(kind);
continue;
}
}
rows.push(proto::ChannelMember { user_id, kind });
}
Ok(rows)
})
.await
}

View file

@ -1161,7 +1161,50 @@ test_both_dbs!(
.map(|channel| channel.id)
.collect::<Vec<_>>();
assert_eq!(user_3_invites, &[channel_1_1])
assert_eq!(user_3_invites, &[channel_1_1]);
let members = db.get_channel_member_details(channel_1_1).await.unwrap();
assert_eq!(
members,
&[
proto::ChannelMember {
user_id: user_1.to_proto(),
kind: proto::channel_member::Kind::Member.into(),
},
proto::ChannelMember {
user_id: user_2.to_proto(),
kind: proto::channel_member::Kind::Invitee.into(),
},
proto::ChannelMember {
user_id: user_3.to_proto(),
kind: proto::channel_member::Kind::Invitee.into(),
},
]
);
db.respond_to_channel_invite(channel_1_1, user_2, true)
.await
.unwrap();
let channel_1_3 = db
.create_channel("channel_3", Some(channel_1_1), "1", user_1)
.await
.unwrap();
let members = db.get_channel_member_details(channel_1_3).await.unwrap();
assert_eq!(
members,
&[
proto::ChannelMember {
user_id: user_1.to_proto(),
kind: proto::channel_member::Kind::Member.into(),
},
proto::ChannelMember {
user_id: user_2.to_proto(),
kind: proto::channel_member::Kind::AncestorMember.into(),
},
]
);
}
);

View file

@ -246,6 +246,7 @@ impl Server {
.add_request_handler(remove_channel)
.add_request_handler(invite_channel_member)
.add_request_handler(remove_channel_member)
.add_request_handler(get_channel_members)
.add_request_handler(respond_to_channel_invite)
.add_request_handler(join_channel)
.add_request_handler(follow)
@ -2236,6 +2237,18 @@ async fn remove_channel_member(
Ok(())
}
async fn get_channel_members(
request: proto::GetChannelMembers,
response: Response<proto::GetChannelMembers>,
session: Session,
) -> Result<()> {
let db = session.db().await;
let channel_id = ChannelId::from_proto(request.channel_id);
let members = db.get_channel_member_details(channel_id).await?;
response.send(proto::GetChannelMembersResponse { members })?;
Ok(())
}
async fn respond_to_channel_invite(
request: proto::RespondToChannelInvite,
response: Response<proto::RespondToChannelInvite>,

View file

@ -291,8 +291,13 @@ impl TestServer {
admin_client
.app_state
.channel_store
.update(admin_cx, |channel_store, _| {
channel_store.invite_member(channel_id, member_client.user_id().unwrap(), false)
.update(admin_cx, |channel_store, cx| {
channel_store.invite_member(
channel_id,
member_client.user_id().unwrap(),
false,
cx,
)
})
.await
.unwrap();

View file

@ -1,6 +1,7 @@
use call::ActiveCall;
use client::{Channel, User};
use gpui::{executor::Deterministic, TestAppContext};
use rpc::proto;
use std::sync::Arc;
use crate::tests::{room_participants, RoomParticipants};
@ -46,8 +47,14 @@ async fn test_basic_channels(
// Invite client B to channel A as client A.
client_a
.channel_store()
.update(cx_a, |channel_store, _| {
channel_store.invite_member(channel_a_id, client_b.user_id().unwrap(), false)
.update(cx_a, |store, cx| {
assert!(!store.has_pending_channel_invite(channel_a_id, client_b.user_id().unwrap()));
let invite = store.invite_member(channel_a_id, client_b.user_id().unwrap(), false, cx);
// Make sure we're synchronously storing the pending invite
assert!(store.has_pending_channel_invite(channel_a_id, client_b.user_id().unwrap()));
invite
})
.await
.unwrap();
@ -66,6 +73,27 @@ async fn test_basic_channels(
})]
)
});
let members = client_a
.channel_store()
.update(cx_a, |store, cx| {
assert!(!store.has_pending_channel_invite(channel_a_id, client_b.user_id().unwrap()));
store.get_channel_member_details(channel_a_id, cx)
})
.await
.unwrap();
assert_members_eq(
&members,
&[
(
client_a.user_id().unwrap(),
proto::channel_member::Kind::Member,
),
(
client_b.user_id().unwrap(),
proto::channel_member::Kind::Invitee,
),
],
);
// Client B now sees that they are a member channel A.
client_b
@ -113,6 +141,19 @@ fn assert_participants_eq(participants: &[Arc<User>], expected_partitipants: &[u
);
}
fn assert_members_eq(
members: &[(Arc<User>, proto::channel_member::Kind)],
expected_members: &[(u64, proto::channel_member::Kind)],
) {
assert_eq!(
members
.iter()
.map(|(user, status)| (user.id, *status))
.collect::<Vec<_>>(),
expected_members
);
}
#[gpui::test]
async fn test_channel_room(
deterministic: Arc<Deterministic>,