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

@ -3442,10 +3442,7 @@ impl Database {
.await
}
pub async fn get_channels_for_user(
&self,
user_id: UserId,
) -> Result<(Vec<Channel>, HashMap<ChannelId, Vec<UserId>>)> {
pub async fn get_channels_for_user(&self, user_id: UserId) -> Result<ChannelsForUser> {
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<ChannelId, Vec<UserId>> = HashMap::default();
let mut channel_participants: HashMap<ChannelId, Vec<UserId>> = 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<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 {
nanoid::nanoid!(16)
}