Flesh out channel member management
Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
parent
a2486de045
commit
87b2d599c1
13 changed files with 728 additions and 196 deletions
|
@ -3243,8 +3243,9 @@ impl Database {
|
|||
}
|
||||
}
|
||||
|
||||
let channel_ancestors = self.get_channel_ancestors(channel_id, &*tx).await?;
|
||||
let members_to_notify: Vec<UserId> = channel_member::Entity::find()
|
||||
.filter(channel_member::Column::ChannelId.is_in(channels_to_remove.keys().copied()))
|
||||
.filter(channel_member::Column::ChannelId.is_in(channel_ancestors))
|
||||
.select_only()
|
||||
.column(channel_member::Column::UserId)
|
||||
.distinct()
|
||||
|
@ -3472,6 +3473,39 @@ impl Database {
|
|||
.await
|
||||
}
|
||||
|
||||
pub async fn set_channel_member_admin(
|
||||
&self,
|
||||
channel_id: ChannelId,
|
||||
from: UserId,
|
||||
for_user: UserId,
|
||||
admin: bool,
|
||||
) -> Result<()> {
|
||||
self.transaction(|tx| async move {
|
||||
self.check_user_is_channel_admin(channel_id, from, &*tx)
|
||||
.await?;
|
||||
|
||||
let result = channel_member::Entity::update_many()
|
||||
.filter(
|
||||
channel_member::Column::ChannelId
|
||||
.eq(channel_id)
|
||||
.and(channel_member::Column::UserId.eq(for_user)),
|
||||
)
|
||||
.set(channel_member::ActiveModel {
|
||||
admin: ActiveValue::set(admin),
|
||||
..Default::default()
|
||||
})
|
||||
.exec(&*tx)
|
||||
.await?;
|
||||
|
||||
if result.rows_affected == 0 {
|
||||
Err(anyhow!("no such member"))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_channel_member_details(
|
||||
&self,
|
||||
channel_id: ChannelId,
|
||||
|
@ -3484,6 +3518,7 @@ impl Database {
|
|||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||
enum QueryMemberDetails {
|
||||
UserId,
|
||||
Admin,
|
||||
IsDirectMember,
|
||||
Accepted,
|
||||
}
|
||||
|
@ -3495,6 +3530,7 @@ impl Database {
|
|||
.filter(channel_member::Column::ChannelId.is_in(ancestor_ids.iter().copied()))
|
||||
.select_only()
|
||||
.column(channel_member::Column::UserId)
|
||||
.column(channel_member::Column::Admin)
|
||||
.column_as(
|
||||
channel_member::Column::ChannelId.eq(channel_id),
|
||||
QueryMemberDetails::IsDirectMember,
|
||||
|
@ -3507,7 +3543,12 @@ impl Database {
|
|||
|
||||
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 (user_id, is_admin, is_direct_member, is_invite_accepted): (
|
||||
UserId,
|
||||
bool,
|
||||
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,
|
||||
|
@ -3518,11 +3559,18 @@ impl Database {
|
|||
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);
|
||||
if is_direct_member {
|
||||
last_row.kind = kind;
|
||||
last_row.admin = is_admin;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
rows.push(proto::ChannelMember { user_id, kind });
|
||||
rows.push(proto::ChannelMember {
|
||||
user_id,
|
||||
kind,
|
||||
admin: is_admin,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(rows)
|
||||
|
|
|
@ -915,7 +915,7 @@ test_both_dbs!(test_channels_postgres, test_channels_sqlite, db, {
|
|||
|
||||
let zed_id = db.create_root_channel("zed", "1", a_id).await.unwrap();
|
||||
|
||||
db.invite_channel_member(zed_id, b_id, a_id, true)
|
||||
db.invite_channel_member(zed_id, b_id, a_id, false)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
@ -1000,6 +1000,43 @@ test_both_dbs!(test_channels_postgres, test_channels_sqlite, db, {
|
|||
]
|
||||
);
|
||||
|
||||
let (channels, _) = db.get_channels_for_user(b_id).await.unwrap();
|
||||
assert_eq!(
|
||||
channels,
|
||||
vec![
|
||||
Channel {
|
||||
id: zed_id,
|
||||
name: "zed".to_string(),
|
||||
parent_id: None,
|
||||
user_is_admin: false,
|
||||
},
|
||||
Channel {
|
||||
id: crdb_id,
|
||||
name: "crdb".to_string(),
|
||||
parent_id: Some(zed_id),
|
||||
user_is_admin: false,
|
||||
},
|
||||
Channel {
|
||||
id: livestreaming_id,
|
||||
name: "livestreaming".to_string(),
|
||||
parent_id: Some(zed_id),
|
||||
user_is_admin: false,
|
||||
},
|
||||
Channel {
|
||||
id: replace_id,
|
||||
name: "replace".to_string(),
|
||||
parent_id: Some(zed_id),
|
||||
user_is_admin: false,
|
||||
},
|
||||
]
|
||||
);
|
||||
|
||||
// Update member permissions
|
||||
let set_subchannel_admin = db.set_channel_member_admin(crdb_id, a_id, b_id, true).await;
|
||||
assert!(set_subchannel_admin.is_err());
|
||||
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();
|
||||
assert_eq!(
|
||||
channels,
|
||||
|
@ -1176,7 +1213,7 @@ test_both_dbs!(
|
|||
db.invite_channel_member(channel_1_2, user_2, user_1, false)
|
||||
.await
|
||||
.unwrap();
|
||||
db.invite_channel_member(channel_1_1, user_3, user_1, false)
|
||||
db.invite_channel_member(channel_1_1, user_3, user_1, true)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
@ -1210,14 +1247,17 @@ test_both_dbs!(
|
|||
proto::ChannelMember {
|
||||
user_id: user_1.to_proto(),
|
||||
kind: proto::channel_member::Kind::Member.into(),
|
||||
admin: true,
|
||||
},
|
||||
proto::ChannelMember {
|
||||
user_id: user_2.to_proto(),
|
||||
kind: proto::channel_member::Kind::Invitee.into(),
|
||||
admin: false,
|
||||
},
|
||||
proto::ChannelMember {
|
||||
user_id: user_3.to_proto(),
|
||||
kind: proto::channel_member::Kind::Invitee.into(),
|
||||
admin: true,
|
||||
},
|
||||
]
|
||||
);
|
||||
|
@ -1241,10 +1281,12 @@ test_both_dbs!(
|
|||
proto::ChannelMember {
|
||||
user_id: user_1.to_proto(),
|
||||
kind: proto::channel_member::Kind::Member.into(),
|
||||
admin: true,
|
||||
},
|
||||
proto::ChannelMember {
|
||||
user_id: user_2.to_proto(),
|
||||
kind: proto::channel_member::Kind::AncestorMember.into(),
|
||||
admin: false,
|
||||
},
|
||||
]
|
||||
);
|
||||
|
|
|
@ -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(set_channel_member_admin)
|
||||
.add_request_handler(get_channel_members)
|
||||
.add_request_handler(respond_to_channel_invite)
|
||||
.add_request_handler(join_channel)
|
||||
|
@ -2150,19 +2151,24 @@ async fn create_channel(
|
|||
id: id.to_proto(),
|
||||
name: request.name,
|
||||
parent_id: request.parent_id,
|
||||
user_is_admin: true,
|
||||
user_is_admin: false,
|
||||
});
|
||||
|
||||
if let Some(parent_id) = parent_id {
|
||||
let member_ids = db.get_channel_members(parent_id).await?;
|
||||
let connection_pool = session.connection_pool().await;
|
||||
for member_id in member_ids {
|
||||
for connection_id in connection_pool.user_connection_ids(member_id) {
|
||||
session.peer.send(connection_id, update.clone())?;
|
||||
}
|
||||
}
|
||||
let user_ids_to_notify = if let Some(parent_id) = parent_id {
|
||||
db.get_channel_members(parent_id).await?
|
||||
} else {
|
||||
session.peer.send(session.connection_id, update)?;
|
||||
vec![session.user_id]
|
||||
};
|
||||
|
||||
let connection_pool = session.connection_pool().await;
|
||||
for user_id in user_ids_to_notify {
|
||||
for connection_id in connection_pool.user_connection_ids(user_id) {
|
||||
let mut update = update.clone();
|
||||
if user_id == session.user_id {
|
||||
update.channels[0].user_is_admin = true;
|
||||
}
|
||||
session.peer.send(connection_id, update)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -2239,8 +2245,57 @@ async fn remove_channel_member(
|
|||
let db = session.db().await;
|
||||
let channel_id = ChannelId::from_proto(request.channel_id);
|
||||
let member_id = UserId::from_proto(request.user_id);
|
||||
|
||||
db.remove_channel_member(channel_id, member_id, session.user_id)
|
||||
.await?;
|
||||
|
||||
let mut update = proto::UpdateChannels::default();
|
||||
update.remove_channels.push(channel_id.to_proto());
|
||||
|
||||
for connection_id in session
|
||||
.connection_pool()
|
||||
.await
|
||||
.user_connection_ids(member_id)
|
||||
{
|
||||
session.peer.send(connection_id, update.clone())?;
|
||||
}
|
||||
|
||||
response.send(proto::Ack {})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_channel_member_admin(
|
||||
request: proto::SetChannelMemberAdmin,
|
||||
response: Response<proto::SetChannelMemberAdmin>,
|
||||
session: Session,
|
||||
) -> Result<()> {
|
||||
let db = session.db().await;
|
||||
let channel_id = ChannelId::from_proto(request.channel_id);
|
||||
let member_id = UserId::from_proto(request.user_id);
|
||||
db.set_channel_member_admin(channel_id, session.user_id, member_id, request.admin)
|
||||
.await?;
|
||||
|
||||
let channel = db
|
||||
.get_channel(channel_id, member_id)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow!("channel not found"))?;
|
||||
|
||||
let mut update = proto::UpdateChannels::default();
|
||||
update.channels.push(proto::Channel {
|
||||
id: channel.id.to_proto(),
|
||||
name: channel.name,
|
||||
parent_id: None,
|
||||
user_is_admin: request.admin,
|
||||
});
|
||||
|
||||
for connection_id in session
|
||||
.connection_pool()
|
||||
.await
|
||||
.user_connection_ids(member_id)
|
||||
{
|
||||
session.peer.send(connection_id, update.clone())?;
|
||||
}
|
||||
|
||||
response.send(proto::Ack {})?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use crate::tests::{room_participants, RoomParticipants, TestServer};
|
||||
use call::ActiveCall;
|
||||
use client::{Channel, User};
|
||||
use client::{Channel, ChannelMembership, User};
|
||||
use gpui::{executor::Deterministic, TestAppContext};
|
||||
use rpc::proto;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_basic_channels(
|
||||
async fn test_core_channels(
|
||||
deterministic: Arc<Deterministic>,
|
||||
cx_a: &mut TestAppContext,
|
||||
cx_b: &mut TestAppContext,
|
||||
|
@ -64,7 +64,7 @@ async fn test_basic_channels(
|
|||
.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(), true, cx);
|
||||
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()));
|
||||
|
@ -84,7 +84,7 @@ async fn test_basic_channels(
|
|||
parent_id: None,
|
||||
user_is_admin: false,
|
||||
depth: 0,
|
||||
}),]
|
||||
})]
|
||||
)
|
||||
});
|
||||
let members = client_a
|
||||
|
@ -100,10 +100,12 @@ async fn test_basic_channels(
|
|||
&[
|
||||
(
|
||||
client_a.user_id().unwrap(),
|
||||
true,
|
||||
proto::channel_member::Kind::Member,
|
||||
),
|
||||
(
|
||||
client_b.user_id().unwrap(),
|
||||
false,
|
||||
proto::channel_member::Kind::Invitee,
|
||||
),
|
||||
],
|
||||
|
@ -117,10 +119,82 @@ async fn test_basic_channels(
|
|||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Client B now sees that they are a member of channel A and its existing
|
||||
// subchannels. Their admin priveleges extend to subchannels of channel A.
|
||||
deterministic.run_until_parked();
|
||||
|
||||
// Client B now sees that they are a member of channel A and its existing subchannels.
|
||||
client_b.channel_store().read_with(cx_b, |channels, _| {
|
||||
assert_eq!(channels.channel_invitations(), &[]);
|
||||
assert_eq!(
|
||||
channels.channels(),
|
||||
&[
|
||||
Arc::new(Channel {
|
||||
id: channel_a_id,
|
||||
name: "channel-a".to_string(),
|
||||
parent_id: None,
|
||||
user_is_admin: false,
|
||||
depth: 0,
|
||||
}),
|
||||
Arc::new(Channel {
|
||||
id: channel_b_id,
|
||||
name: "channel-b".to_string(),
|
||||
parent_id: Some(channel_a_id),
|
||||
user_is_admin: false,
|
||||
depth: 1,
|
||||
})
|
||||
]
|
||||
)
|
||||
});
|
||||
|
||||
let channel_c_id = client_a
|
||||
.channel_store()
|
||||
.update(cx_a, |channel_store, _| {
|
||||
channel_store.create_channel("channel-c", Some(channel_b_id))
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
deterministic.run_until_parked();
|
||||
client_b.channel_store().read_with(cx_b, |channels, _| {
|
||||
assert_eq!(
|
||||
channels.channels(),
|
||||
&[
|
||||
Arc::new(Channel {
|
||||
id: channel_a_id,
|
||||
name: "channel-a".to_string(),
|
||||
parent_id: None,
|
||||
user_is_admin: false,
|
||||
depth: 0,
|
||||
}),
|
||||
Arc::new(Channel {
|
||||
id: channel_b_id,
|
||||
name: "channel-b".to_string(),
|
||||
parent_id: Some(channel_a_id),
|
||||
user_is_admin: false,
|
||||
depth: 1,
|
||||
}),
|
||||
Arc::new(Channel {
|
||||
id: channel_c_id,
|
||||
name: "channel-c".to_string(),
|
||||
parent_id: Some(channel_b_id),
|
||||
user_is_admin: false,
|
||||
depth: 2,
|
||||
}),
|
||||
]
|
||||
)
|
||||
});
|
||||
|
||||
// Update client B's membership to channel A to be an admin.
|
||||
client_a
|
||||
.channel_store()
|
||||
.update(cx_a, |store, cx| {
|
||||
store.set_member_admin(channel_a_id, client_b.user_id().unwrap(), true, cx)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
deterministic.run_until_parked();
|
||||
|
||||
// Observe that client B is now an admin of channel A, and that
|
||||
// their admin priveleges extend to subchannels of channel A.
|
||||
client_b.channel_store().read_with(cx_b, |channels, _| {
|
||||
assert_eq!(channels.channel_invitations(), &[]);
|
||||
assert_eq!(
|
||||
|
@ -137,65 +211,83 @@ async fn test_basic_channels(
|
|||
id: channel_b_id,
|
||||
name: "channel-b".to_string(),
|
||||
parent_id: Some(channel_a_id),
|
||||
user_is_admin: true,
|
||||
user_is_admin: false,
|
||||
depth: 1,
|
||||
})
|
||||
]
|
||||
)
|
||||
});
|
||||
|
||||
let channel_c_id = client_a
|
||||
.channel_store()
|
||||
.update(cx_a, |channel_store, _| {
|
||||
channel_store.create_channel("channel-c", Some(channel_a_id))
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// TODO - ensure sibling channels are sorted in a stable way
|
||||
deterministic.run_until_parked();
|
||||
client_b.channel_store().read_with(cx_b, |channels, _| {
|
||||
assert_eq!(
|
||||
channels.channels(),
|
||||
&[
|
||||
Arc::new(Channel {
|
||||
id: channel_a_id,
|
||||
name: "channel-a".to_string(),
|
||||
parent_id: None,
|
||||
user_is_admin: true,
|
||||
depth: 0,
|
||||
}),
|
||||
Arc::new(Channel {
|
||||
id: channel_c_id,
|
||||
name: "channel-c".to_string(),
|
||||
parent_id: Some(channel_a_id),
|
||||
user_is_admin: true,
|
||||
depth: 1,
|
||||
}),
|
||||
Arc::new(Channel {
|
||||
id: channel_b_id,
|
||||
name: "channel-b".to_string(),
|
||||
parent_id: Some(channel_a_id),
|
||||
user_is_admin: true,
|
||||
depth: 1,
|
||||
parent_id: Some(channel_b_id),
|
||||
user_is_admin: false,
|
||||
depth: 2,
|
||||
}),
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
assert!(channels.is_user_admin(channel_c_id))
|
||||
});
|
||||
|
||||
// Client A deletes the channel
|
||||
// Client A deletes the channel, deletion also deletes subchannels.
|
||||
client_a
|
||||
.channel_store()
|
||||
.update(cx_a, |channel_store, _| {
|
||||
channel_store.remove_channel(channel_a_id)
|
||||
channel_store.remove_channel(channel_b_id)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
deterministic.run_until_parked();
|
||||
client_a.channel_store().read_with(cx_a, |channels, _| {
|
||||
assert_eq!(
|
||||
channels.channels(),
|
||||
&[Arc::new(Channel {
|
||||
id: channel_a_id,
|
||||
name: "channel-a".to_string(),
|
||||
parent_id: None,
|
||||
user_is_admin: true,
|
||||
depth: 0,
|
||||
})]
|
||||
)
|
||||
});
|
||||
client_b.channel_store().read_with(cx_b, |channels, _| {
|
||||
assert_eq!(
|
||||
channels.channels(),
|
||||
&[Arc::new(Channel {
|
||||
id: channel_a_id,
|
||||
name: "channel-a".to_string(),
|
||||
parent_id: None,
|
||||
user_is_admin: true,
|
||||
depth: 0,
|
||||
})]
|
||||
)
|
||||
});
|
||||
|
||||
// Remove client B
|
||||
client_a
|
||||
.channel_store()
|
||||
.read_with(cx_a, |channels, _| assert_eq!(channels.channels(), &[]));
|
||||
.update(cx_a, |channel_store, cx| {
|
||||
channel_store.remove_member(channel_a_id, client_b.user_id().unwrap(), cx)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
deterministic.run_until_parked();
|
||||
|
||||
// Client A still has their channel
|
||||
client_a.channel_store().read_with(cx_a, |channels, _| {
|
||||
assert_eq!(
|
||||
channels.channels(),
|
||||
&[Arc::new(Channel {
|
||||
id: channel_a_id,
|
||||
name: "channel-a".to_string(),
|
||||
parent_id: None,
|
||||
user_is_admin: true,
|
||||
depth: 0,
|
||||
})]
|
||||
)
|
||||
});
|
||||
|
||||
// Client B is gone
|
||||
client_b
|
||||
.channel_store()
|
||||
.read_with(cx_b, |channels, _| assert_eq!(channels.channels(), &[]));
|
||||
|
@ -209,13 +301,13 @@ 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)],
|
||||
members: &[ChannelMembership],
|
||||
expected_members: &[(u64, bool, proto::channel_member::Kind)],
|
||||
) {
|
||||
assert_eq!(
|
||||
members
|
||||
.iter()
|
||||
.map(|(user, status)| (user.id, *status))
|
||||
.map(|member| (member.user.id, member.admin, member.kind))
|
||||
.collect::<Vec<_>>(),
|
||||
expected_members
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue