Update contacts list when a project is shared

This commit is contained in:
Antonio Scandurra 2022-04-12 11:41:20 +02:00
parent d17e9c071b
commit 4e057da69b
2 changed files with 42 additions and 13 deletions

View file

@ -372,9 +372,9 @@ impl Server {
self: Arc<Server>, self: Arc<Server>,
request: TypedEnvelope<proto::ShareProject>, request: TypedEnvelope<proto::ShareProject>,
) -> tide::Result<proto::Ack> { ) -> tide::Result<proto::Ack> {
self.state_mut() let mut state = self.state_mut().await;
.await let project = state.share_project(request.payload.project_id, request.sender_id)?;
.share_project(request.payload.project_id, request.sender_id); self.update_contacts_for_users(&mut *state, &project.authorized_user_ids);
Ok(proto::Ack {}) Ok(proto::Ack {})
} }
@ -4465,19 +4465,19 @@ mod tests {
client_a client_a
.user_store .user_store
.condition(&cx_a, |user_store, _| { .condition(&cx_a, |user_store, _| {
contacts(user_store) == vec![("user_a", vec![("a", vec![])])] contacts(user_store) == vec![("user_a", vec![("a", false, vec![])])]
}) })
.await; .await;
client_b client_b
.user_store .user_store
.condition(&cx_b, |user_store, _| { .condition(&cx_b, |user_store, _| {
contacts(user_store) == vec![("user_a", vec![("a", vec![])])] contacts(user_store) == vec![("user_a", vec![("a", false, vec![])])]
}) })
.await; .await;
client_c client_c
.user_store .user_store
.condition(&cx_c, |user_store, _| { .condition(&cx_c, |user_store, _| {
contacts(user_store) == vec![("user_a", vec![("a", vec![])])] contacts(user_store) == vec![("user_a", vec![("a", false, vec![])])]
}) })
.await; .await;
@ -4488,6 +4488,24 @@ mod tests {
.update(cx_a, |project, cx| project.share(cx)) .update(cx_a, |project, cx| project.share(cx))
.await .await
.unwrap(); .unwrap();
client_a
.user_store
.condition(&cx_a, |user_store, _| {
contacts(user_store) == vec![("user_a", vec![("a", true, vec![])])]
})
.await;
client_b
.user_store
.condition(&cx_b, |user_store, _| {
contacts(user_store) == vec![("user_a", vec![("a", true, vec![])])]
})
.await;
client_c
.user_store
.condition(&cx_c, |user_store, _| {
contacts(user_store) == vec![("user_a", vec![("a", true, vec![])])]
})
.await;
let _project_b = Project::remote( let _project_b = Project::remote(
project_id, project_id,
@ -4503,19 +4521,19 @@ mod tests {
client_a client_a
.user_store .user_store
.condition(&cx_a, |user_store, _| { .condition(&cx_a, |user_store, _| {
contacts(user_store) == vec![("user_a", vec![("a", vec!["user_b"])])] contacts(user_store) == vec![("user_a", vec![("a", true, vec!["user_b"])])]
}) })
.await; .await;
client_b client_b
.user_store .user_store
.condition(&cx_b, |user_store, _| { .condition(&cx_b, |user_store, _| {
contacts(user_store) == vec![("user_a", vec![("a", vec!["user_b"])])] contacts(user_store) == vec![("user_a", vec![("a", true, vec!["user_b"])])]
}) })
.await; .await;
client_c client_c
.user_store .user_store
.condition(&cx_c, |user_store, _| { .condition(&cx_c, |user_store, _| {
contacts(user_store) == vec![("user_a", vec![("a", vec!["user_b"])])] contacts(user_store) == vec![("user_a", vec![("a", true, vec!["user_b"])])]
}) })
.await; .await;
@ -4539,7 +4557,7 @@ mod tests {
.condition(&cx_c, |user_store, _| contacts(user_store) == vec![]) .condition(&cx_c, |user_store, _| contacts(user_store) == vec![])
.await; .await;
fn contacts(user_store: &UserStore) -> Vec<(&str, Vec<(&str, Vec<&str>)>)> { fn contacts(user_store: &UserStore) -> Vec<(&str, Vec<(&str, bool, Vec<&str>)>)> {
user_store user_store
.contacts() .contacts()
.iter() .iter()
@ -4550,6 +4568,7 @@ mod tests {
.map(|p| { .map(|p| {
( (
p.worktree_root_names[0].as_str(), p.worktree_root_names[0].as_str(),
p.is_shared,
p.guests.iter().map(|p| p.github_login.as_str()).collect(), p.guests.iter().map(|p| p.github_login.as_str()).collect(),
) )
}) })

View file

@ -66,6 +66,10 @@ pub struct JoinedProject<'a> {
pub project: &'a Project, pub project: &'a Project,
} }
pub struct SharedProject {
pub authorized_user_ids: Vec<UserId>,
}
pub struct UnsharedProject { pub struct UnsharedProject {
pub connection_ids: Vec<ConnectionId>, pub connection_ids: Vec<ConnectionId>,
pub authorized_user_ids: Vec<UserId>, pub authorized_user_ids: Vec<UserId>,
@ -355,7 +359,11 @@ impl Store {
Ok((worktree, guest_connection_ids)) Ok((worktree, guest_connection_ids))
} }
pub fn share_project(&mut self, project_id: u64, connection_id: ConnectionId) -> bool { pub fn share_project(
&mut self,
project_id: u64,
connection_id: ConnectionId,
) -> tide::Result<SharedProject> {
if let Some(project) = self.projects.get_mut(&project_id) { if let Some(project) = self.projects.get_mut(&project_id) {
if project.host_connection_id == connection_id { if project.host_connection_id == connection_id {
let mut share = ProjectShare::default(); let mut share = ProjectShare::default();
@ -363,10 +371,12 @@ impl Store {
share.worktrees.insert(*worktree_id, Default::default()); share.worktrees.insert(*worktree_id, Default::default());
} }
project.share = Some(share); project.share = Some(share);
return true; return Ok(SharedProject {
authorized_user_ids: project.authorized_user_ids(),
});
} }
} }
false Err(anyhow!("no such project"))?
} }
pub fn unshare_project( pub fn unshare_project(