Push role refactoring through RPC/client
This commit is contained in:
parent
690d9fb971
commit
540436a1f9
12 changed files with 178 additions and 89 deletions
|
@ -1,4 +1,5 @@
|
|||
use crate::Result;
|
||||
use rpc::proto;
|
||||
use sea_orm::{entity::prelude::*, DbErr};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
@ -91,3 +92,30 @@ pub enum ChannelRole {
|
|||
#[sea_orm(string_value = "guest")]
|
||||
Guest,
|
||||
}
|
||||
|
||||
impl From<proto::ChannelRole> for ChannelRole {
|
||||
fn from(value: proto::ChannelRole) -> Self {
|
||||
match value {
|
||||
proto::ChannelRole::Admin => ChannelRole::Admin,
|
||||
proto::ChannelRole::Member => ChannelRole::Member,
|
||||
proto::ChannelRole::Guest => ChannelRole::Guest,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<proto::ChannelRole> for ChannelRole {
|
||||
fn into(self) -> proto::ChannelRole {
|
||||
match self {
|
||||
ChannelRole::Admin => proto::ChannelRole::Admin,
|
||||
ChannelRole::Member => proto::ChannelRole::Member,
|
||||
ChannelRole::Guest => proto::ChannelRole::Guest,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<i32> for ChannelRole {
|
||||
fn into(self) -> i32 {
|
||||
let proto: proto::ChannelRole = self.into();
|
||||
proto.into()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -564,13 +564,18 @@ impl Database {
|
|||
(false, true) => proto::channel_member::Kind::AncestorMember,
|
||||
(false, false) => continue,
|
||||
};
|
||||
let channel_role = channel_role.unwrap_or(if is_admin {
|
||||
ChannelRole::Admin
|
||||
} else {
|
||||
ChannelRole::Member
|
||||
});
|
||||
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 {
|
||||
if is_direct_member {
|
||||
last_row.kind = kind;
|
||||
last_row.admin = channel_role == Some(ChannelRole::Admin) || is_admin;
|
||||
last_row.role = channel_role.into()
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
@ -578,7 +583,7 @@ impl Database {
|
|||
rows.push(proto::ChannelMember {
|
||||
user_id,
|
||||
kind,
|
||||
admin: channel_role == Some(ChannelRole::Admin) || is_admin,
|
||||
role: channel_role.into(),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -851,10 +856,11 @@ impl Database {
|
|||
&self,
|
||||
user: UserId,
|
||||
channel: ChannelId,
|
||||
to: ChannelId,
|
||||
new_parent: ChannelId,
|
||||
tx: &DatabaseTransaction,
|
||||
) -> Result<ChannelGraph> {
|
||||
self.check_user_is_channel_admin(to, user, &*tx).await?;
|
||||
self.check_user_is_channel_admin(new_parent, user, &*tx)
|
||||
.await?;
|
||||
|
||||
let paths = channel_path::Entity::find()
|
||||
.filter(channel_path::Column::IdPath.like(&format!("%/{}/%", channel)))
|
||||
|
@ -872,7 +878,7 @@ impl Database {
|
|||
}
|
||||
|
||||
let paths_to_new_parent = channel_path::Entity::find()
|
||||
.filter(channel_path::Column::ChannelId.eq(to))
|
||||
.filter(channel_path::Column::ChannelId.eq(new_parent))
|
||||
.all(tx)
|
||||
.await?;
|
||||
|
||||
|
@ -906,7 +912,7 @@ impl Database {
|
|||
if let Some(channel) = channel_descendants.get_mut(&channel) {
|
||||
// Remove the other parents
|
||||
channel.clear();
|
||||
channel.insert(to);
|
||||
channel.insert(new_parent);
|
||||
}
|
||||
|
||||
let channels = self
|
||||
|
|
|
@ -328,17 +328,17 @@ async fn test_channel_invites(db: &Arc<Database>) {
|
|||
proto::ChannelMember {
|
||||
user_id: user_1.to_proto(),
|
||||
kind: proto::channel_member::Kind::Member.into(),
|
||||
admin: true,
|
||||
role: proto::ChannelRole::Admin.into(),
|
||||
},
|
||||
proto::ChannelMember {
|
||||
user_id: user_2.to_proto(),
|
||||
kind: proto::channel_member::Kind::Invitee.into(),
|
||||
admin: false,
|
||||
role: proto::ChannelRole::Member.into(),
|
||||
},
|
||||
proto::ChannelMember {
|
||||
user_id: user_3.to_proto(),
|
||||
kind: proto::channel_member::Kind::Invitee.into(),
|
||||
admin: true,
|
||||
role: proto::ChannelRole::Admin.into(),
|
||||
},
|
||||
]
|
||||
);
|
||||
|
@ -362,12 +362,12 @@ async fn test_channel_invites(db: &Arc<Database>) {
|
|||
proto::ChannelMember {
|
||||
user_id: user_1.to_proto(),
|
||||
kind: proto::channel_member::Kind::Member.into(),
|
||||
admin: true,
|
||||
role: proto::ChannelRole::Admin.into(),
|
||||
},
|
||||
proto::ChannelMember {
|
||||
user_id: user_2.to_proto(),
|
||||
kind: proto::channel_member::Kind::AncestorMember.into(),
|
||||
admin: false,
|
||||
role: proto::ChannelRole::Member.into(),
|
||||
},
|
||||
]
|
||||
);
|
||||
|
|
|
@ -3,8 +3,8 @@ mod connection_pool;
|
|||
use crate::{
|
||||
auth,
|
||||
db::{
|
||||
self, BufferId, ChannelId, ChannelRole, ChannelsForUser, Database, MessageId, ProjectId,
|
||||
RoomId, ServerId, User, UserId,
|
||||
self, BufferId, ChannelId, ChannelsForUser, Database, MessageId, ProjectId, RoomId,
|
||||
ServerId, User, UserId,
|
||||
},
|
||||
executor::Executor,
|
||||
AppState, Result,
|
||||
|
@ -254,7 +254,7 @@ impl Server {
|
|||
.add_request_handler(delete_channel)
|
||||
.add_request_handler(invite_channel_member)
|
||||
.add_request_handler(remove_channel_member)
|
||||
.add_request_handler(set_channel_member_admin)
|
||||
.add_request_handler(set_channel_member_role)
|
||||
.add_request_handler(rename_channel)
|
||||
.add_request_handler(join_channel_buffer)
|
||||
.add_request_handler(leave_channel_buffer)
|
||||
|
@ -2282,13 +2282,13 @@ async fn invite_channel_member(
|
|||
let db = session.db().await;
|
||||
let channel_id = ChannelId::from_proto(request.channel_id);
|
||||
let invitee_id = UserId::from_proto(request.user_id);
|
||||
let role = if request.admin {
|
||||
ChannelRole::Admin
|
||||
} else {
|
||||
ChannelRole::Member
|
||||
};
|
||||
db.invite_channel_member(channel_id, invitee_id, session.user_id, role)
|
||||
.await?;
|
||||
db.invite_channel_member(
|
||||
channel_id,
|
||||
invitee_id,
|
||||
session.user_id,
|
||||
request.role().into(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let (channel, _) = db
|
||||
.get_channel(channel_id, session.user_id)
|
||||
|
@ -2339,21 +2339,21 @@ async fn remove_channel_member(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_channel_member_admin(
|
||||
request: proto::SetChannelMemberAdmin,
|
||||
response: Response<proto::SetChannelMemberAdmin>,
|
||||
async fn set_channel_member_role(
|
||||
request: proto::SetChannelMemberRole,
|
||||
response: Response<proto::SetChannelMemberRole>,
|
||||
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);
|
||||
let role = if request.admin {
|
||||
ChannelRole::Admin
|
||||
} else {
|
||||
ChannelRole::Member
|
||||
};
|
||||
db.set_channel_member_role(channel_id, session.user_id, member_id, role)
|
||||
.await?;
|
||||
db.set_channel_member_role(
|
||||
channel_id,
|
||||
session.user_id,
|
||||
member_id,
|
||||
request.role().into(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let (channel, has_accepted) = db
|
||||
.get_channel(channel_id, member_id)
|
||||
|
@ -2364,7 +2364,7 @@ async fn set_channel_member_admin(
|
|||
if has_accepted {
|
||||
update.channel_permissions.push(proto::ChannelPermission {
|
||||
channel_id: channel.id.to_proto(),
|
||||
is_admin: request.admin,
|
||||
role: request.role,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -2603,7 +2603,7 @@ async fn respond_to_channel_invite(
|
|||
.into_iter()
|
||||
.map(|channel_id| proto::ChannelPermission {
|
||||
channel_id: channel_id.to_proto(),
|
||||
is_admin: true,
|
||||
role: proto::ChannelRole::Admin.into(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
@ -3106,7 +3106,7 @@ fn build_initial_channels_update(
|
|||
.into_iter()
|
||||
.map(|id| proto::ChannelPermission {
|
||||
channel_id: id.to_proto(),
|
||||
is_admin: true,
|
||||
role: proto::ChannelRole::Admin.into(),
|
||||
}),
|
||||
);
|
||||
|
||||
|
|
|
@ -68,7 +68,12 @@ async fn test_core_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(), false, cx);
|
||||
let invite = store.invite_member(
|
||||
channel_a_id,
|
||||
client_b.user_id().unwrap(),
|
||||
proto::ChannelRole::Member,
|
||||
cx,
|
||||
);
|
||||
|
||||
// Make sure we're synchronously storing the pending invite
|
||||
assert!(store.has_pending_channel_invite(channel_a_id, client_b.user_id().unwrap()));
|
||||
|
@ -103,12 +108,12 @@ async fn test_core_channels(
|
|||
&[
|
||||
(
|
||||
client_a.user_id().unwrap(),
|
||||
true,
|
||||
proto::ChannelRole::Admin,
|
||||
proto::channel_member::Kind::Member,
|
||||
),
|
||||
(
|
||||
client_b.user_id().unwrap(),
|
||||
false,
|
||||
proto::ChannelRole::Member,
|
||||
proto::channel_member::Kind::Invitee,
|
||||
),
|
||||
],
|
||||
|
@ -183,7 +188,12 @@ async fn test_core_channels(
|
|||
client_a
|
||||
.channel_store()
|
||||
.update(cx_a, |store, cx| {
|
||||
store.set_member_admin(channel_a_id, client_b.user_id().unwrap(), true, cx)
|
||||
store.set_member_role(
|
||||
channel_a_id,
|
||||
client_b.user_id().unwrap(),
|
||||
proto::ChannelRole::Admin,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
@ -305,12 +315,12 @@ fn assert_participants_eq(participants: &[Arc<User>], expected_partitipants: &[u
|
|||
#[track_caller]
|
||||
fn assert_members_eq(
|
||||
members: &[ChannelMembership],
|
||||
expected_members: &[(u64, bool, proto::channel_member::Kind)],
|
||||
expected_members: &[(u64, proto::ChannelRole, proto::channel_member::Kind)],
|
||||
) {
|
||||
assert_eq!(
|
||||
members
|
||||
.iter()
|
||||
.map(|member| (member.user.id, member.admin, member.kind))
|
||||
.map(|member| (member.user.id, member.role, member.kind))
|
||||
.collect::<Vec<_>>(),
|
||||
expected_members
|
||||
);
|
||||
|
@ -611,7 +621,12 @@ async fn test_permissions_update_while_invited(
|
|||
client_a
|
||||
.channel_store()
|
||||
.update(cx_a, |channel_store, cx| {
|
||||
channel_store.invite_member(rust_id, client_b.user_id().unwrap(), false, cx)
|
||||
channel_store.invite_member(
|
||||
rust_id,
|
||||
client_b.user_id().unwrap(),
|
||||
proto::ChannelRole::Member,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
@ -634,7 +649,12 @@ async fn test_permissions_update_while_invited(
|
|||
client_a
|
||||
.channel_store()
|
||||
.update(cx_a, |channel_store, cx| {
|
||||
channel_store.set_member_admin(rust_id, client_b.user_id().unwrap(), true, cx)
|
||||
channel_store.set_member_role(
|
||||
rust_id,
|
||||
client_b.user_id().unwrap(),
|
||||
proto::ChannelRole::Admin,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
@ -803,7 +823,12 @@ async fn test_lost_channel_creation(
|
|||
client_a
|
||||
.channel_store()
|
||||
.update(cx_a, |channel_store, cx| {
|
||||
channel_store.invite_member(channel_id, client_b.user_id().unwrap(), false, cx)
|
||||
channel_store.invite_member(
|
||||
channel_id,
|
||||
client_b.user_id().unwrap(),
|
||||
proto::ChannelRole::Member,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
|
|
@ -17,7 +17,7 @@ use gpui::{executor::Deterministic, ModelHandle, Task, TestAppContext, WindowHan
|
|||
use language::LanguageRegistry;
|
||||
use parking_lot::Mutex;
|
||||
use project::{Project, WorktreeId};
|
||||
use rpc::RECEIVE_TIMEOUT;
|
||||
use rpc::{proto::ChannelRole, RECEIVE_TIMEOUT};
|
||||
use settings::SettingsStore;
|
||||
use std::{
|
||||
cell::{Ref, RefCell, RefMut},
|
||||
|
@ -325,7 +325,7 @@ impl TestServer {
|
|||
channel_store.invite_member(
|
||||
channel_id,
|
||||
member_client.user_id().unwrap(),
|
||||
false,
|
||||
ChannelRole::Member,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
|
@ -613,7 +613,12 @@ impl TestClient {
|
|||
cx_self
|
||||
.read(ChannelStore::global)
|
||||
.update(cx_self, |channel_store, cx| {
|
||||
channel_store.invite_member(channel, other_client.user_id().unwrap(), true, cx)
|
||||
channel_store.invite_member(
|
||||
channel,
|
||||
other_client.user_id().unwrap(),
|
||||
ChannelRole::Admin,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue