Start work on requesting to join projects
Co-authored-by: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
e199b7e50e
commit
be51a58311
25 changed files with 660 additions and 883 deletions
|
@ -66,6 +66,11 @@ impl<R: RequestMessage> Response<R> {
|
|||
self.server.peer.respond(self.receipt, payload)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn into_receipt(self) -> Receipt<R> {
|
||||
self.responded.store(true, SeqCst);
|
||||
self.receipt
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Server {
|
||||
|
@ -115,10 +120,9 @@ impl Server {
|
|||
.add_request_handler(Server::ping)
|
||||
.add_request_handler(Server::register_project)
|
||||
.add_message_handler(Server::unregister_project)
|
||||
.add_request_handler(Server::share_project)
|
||||
.add_message_handler(Server::unshare_project)
|
||||
.add_request_handler(Server::join_project)
|
||||
.add_message_handler(Server::leave_project)
|
||||
.add_message_handler(Server::respond_to_join_project_request)
|
||||
.add_request_handler(Server::register_worktree)
|
||||
.add_message_handler(Server::unregister_worktree)
|
||||
.add_request_handler(Server::update_worktree)
|
||||
|
@ -336,12 +340,10 @@ impl Server {
|
|||
let removed_connection = self.store_mut().await.remove_connection(connection_id)?;
|
||||
|
||||
for (project_id, project) in removed_connection.hosted_projects {
|
||||
if let Some(share) = project.share {
|
||||
broadcast(connection_id, share.guests.keys().copied(), |conn_id| {
|
||||
self.peer
|
||||
.send(conn_id, proto::UnshareProject { project_id })
|
||||
});
|
||||
}
|
||||
broadcast(connection_id, project.guests.keys().copied(), |conn_id| {
|
||||
self.peer
|
||||
.send(conn_id, proto::UnregisterProject { project_id })
|
||||
});
|
||||
}
|
||||
|
||||
for (project_id, peer_ids) in removed_connection.guest_project_ids {
|
||||
|
@ -402,20 +404,20 @@ impl Server {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn share_project(
|
||||
self: Arc<Server>,
|
||||
request: TypedEnvelope<proto::ShareProject>,
|
||||
response: Response<proto::ShareProject>,
|
||||
) -> Result<()> {
|
||||
let user_id = {
|
||||
let mut state = self.store_mut().await;
|
||||
state.share_project(request.payload.project_id, request.sender_id)?;
|
||||
state.user_id_for_connection(request.sender_id)?
|
||||
};
|
||||
self.update_user_contacts(user_id).await?;
|
||||
response.send(proto::Ack {})?;
|
||||
Ok(())
|
||||
}
|
||||
// async fn share_project(
|
||||
// self: Arc<Server>,
|
||||
// request: TypedEnvelope<proto::ShareProject>,
|
||||
// response: Response<proto::ShareProject>,
|
||||
// ) -> Result<()> {
|
||||
// let user_id = {
|
||||
// let mut state = self.store_mut().await;
|
||||
// state.share_project(request.payload.project_id, request.sender_id)?;
|
||||
// state.user_id_for_connection(request.sender_id)?
|
||||
// };
|
||||
// self.update_user_contacts(user_id).await?;
|
||||
// response.send(proto::Ack {})?;
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
async fn update_user_contacts(self: &Arc<Server>, user_id: UserId) -> Result<()> {
|
||||
let contacts = self.app_state.db.get_contacts(user_id).await?;
|
||||
|
@ -447,24 +449,6 @@ impl Server {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn unshare_project(
|
||||
self: Arc<Server>,
|
||||
request: TypedEnvelope<proto::UnshareProject>,
|
||||
) -> Result<()> {
|
||||
let project_id = request.payload.project_id;
|
||||
let project;
|
||||
{
|
||||
let mut state = self.store_mut().await;
|
||||
project = state.unshare_project(project_id, request.sender_id)?;
|
||||
broadcast(request.sender_id, project.connection_ids, |conn_id| {
|
||||
self.peer
|
||||
.send(conn_id, proto::UnshareProject { project_id })
|
||||
});
|
||||
}
|
||||
self.update_user_contacts(project.host_user_id).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn join_project(
|
||||
self: Arc<Server>,
|
||||
request: TypedEnvelope<proto::JoinProject>,
|
||||
|
@ -473,9 +457,12 @@ impl Server {
|
|||
let project_id = request.payload.project_id;
|
||||
let host_user_id;
|
||||
let guest_user_id;
|
||||
let host_connection_id;
|
||||
{
|
||||
let state = self.store().await;
|
||||
host_user_id = state.project(project_id)?.host_user_id;
|
||||
let project = state.project(project_id)?;
|
||||
host_user_id = project.host_user_id;
|
||||
host_connection_id = project.host_connection_id;
|
||||
guest_user_id = state.user_id_for_connection(request.sender_id)?;
|
||||
};
|
||||
|
||||
|
@ -488,22 +475,71 @@ impl Server {
|
|||
return Err(anyhow!("no such project"))?;
|
||||
}
|
||||
|
||||
self.store_mut().await.request_join_project(
|
||||
guest_user_id,
|
||||
project_id,
|
||||
response.into_receipt(),
|
||||
)?;
|
||||
self.peer.send(
|
||||
host_connection_id,
|
||||
proto::RequestJoinProject {
|
||||
project_id,
|
||||
requester_id: guest_user_id.to_proto(),
|
||||
},
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn respond_to_join_project_request(
|
||||
self: Arc<Server>,
|
||||
request: TypedEnvelope<proto::RespondToJoinProjectRequest>,
|
||||
) -> Result<()> {
|
||||
let host_user_id;
|
||||
|
||||
{
|
||||
let state = &mut *self.store_mut().await;
|
||||
let joined = state.join_project(request.sender_id, guest_user_id, project_id)?;
|
||||
let share = joined.project.share()?;
|
||||
let peer_count = share.guests.len();
|
||||
let mut state = self.store_mut().await;
|
||||
let project_id = request.payload.project_id;
|
||||
let project = state.project(project_id)?;
|
||||
if project.host_connection_id != request.sender_id {
|
||||
Err(anyhow!("no such connection"))?;
|
||||
}
|
||||
|
||||
host_user_id = project.host_user_id;
|
||||
let guest_user_id = UserId::from_proto(request.payload.requester_id);
|
||||
|
||||
if !request.payload.allow {
|
||||
let receipts = state
|
||||
.deny_join_project_request(request.sender_id, guest_user_id, project_id)
|
||||
.ok_or_else(|| anyhow!("no such request"))?;
|
||||
for receipt in receipts {
|
||||
self.peer.respond(
|
||||
receipt,
|
||||
proto::JoinProjectResponse {
|
||||
variant: Some(proto::join_project_response::Variant::Decline(
|
||||
proto::join_project_response::Decline {},
|
||||
)),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let (receipts_with_replica_ids, project) = state
|
||||
.accept_join_project_request(request.sender_id, guest_user_id, project_id)
|
||||
.ok_or_else(|| anyhow!("no such request"))?;
|
||||
|
||||
let peer_count = project.guests.len();
|
||||
let mut collaborators = Vec::with_capacity(peer_count);
|
||||
collaborators.push(proto::Collaborator {
|
||||
peer_id: joined.project.host_connection_id.0,
|
||||
peer_id: project.host_connection_id.0,
|
||||
replica_id: 0,
|
||||
user_id: joined.project.host_user_id.to_proto(),
|
||||
user_id: project.host_user_id.to_proto(),
|
||||
});
|
||||
let worktrees = share
|
||||
let worktrees = project
|
||||
.worktrees
|
||||
.iter()
|
||||
.filter_map(|(id, shared_worktree)| {
|
||||
let worktree = joined.project.worktrees.get(&id)?;
|
||||
let worktree = project.worktrees.get(&id)?;
|
||||
Some(proto::Worktree {
|
||||
id: *id,
|
||||
root_name: worktree.root_name.clone(),
|
||||
|
@ -517,8 +553,8 @@ impl Server {
|
|||
scan_id: shared_worktree.scan_id,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
for (peer_conn_id, (peer_replica_id, peer_user_id)) in &share.guests {
|
||||
.collect::<Vec<_>>();
|
||||
for (peer_conn_id, (peer_replica_id, peer_user_id)) in &project.guests {
|
||||
if *peer_conn_id != request.sender_id {
|
||||
collaborators.push(proto::Collaborator {
|
||||
peer_id: peer_conn_id.0,
|
||||
|
@ -527,30 +563,41 @@ impl Server {
|
|||
});
|
||||
}
|
||||
}
|
||||
broadcast(
|
||||
request.sender_id,
|
||||
joined.project.connection_ids(),
|
||||
|conn_id| {
|
||||
self.peer.send(
|
||||
conn_id,
|
||||
proto::AddProjectCollaborator {
|
||||
project_id,
|
||||
collaborator: Some(proto::Collaborator {
|
||||
peer_id: request.sender_id.0,
|
||||
replica_id: joined.replica_id as u32,
|
||||
user_id: guest_user_id.to_proto(),
|
||||
}),
|
||||
},
|
||||
)
|
||||
},
|
||||
);
|
||||
response.send(proto::JoinProjectResponse {
|
||||
worktrees,
|
||||
replica_id: joined.replica_id as u32,
|
||||
collaborators,
|
||||
language_servers: joined.project.language_servers.clone(),
|
||||
})?;
|
||||
for conn_id in project.connection_ids() {
|
||||
for (receipt, replica_id) in &receipts_with_replica_ids {
|
||||
if conn_id != receipt.sender_id {
|
||||
self.peer.send(
|
||||
conn_id,
|
||||
proto::AddProjectCollaborator {
|
||||
project_id,
|
||||
collaborator: Some(proto::Collaborator {
|
||||
peer_id: receipt.sender_id.0,
|
||||
replica_id: *replica_id as u32,
|
||||
user_id: guest_user_id.to_proto(),
|
||||
}),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (receipt, replica_id) in receipts_with_replica_ids {
|
||||
self.peer.respond(
|
||||
receipt,
|
||||
proto::JoinProjectResponse {
|
||||
variant: Some(proto::join_project_response::Variant::Accept(
|
||||
proto::join_project_response::Accept {
|
||||
worktrees: worktrees.clone(),
|
||||
replica_id: replica_id as u32,
|
||||
collaborators: collaborators.clone(),
|
||||
language_servers: project.language_servers.clone(),
|
||||
},
|
||||
)),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
self.update_user_contacts(host_user_id).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -599,6 +646,7 @@ impl Server {
|
|||
Worktree {
|
||||
root_name: request.payload.root_name.clone(),
|
||||
visible: request.payload.visible,
|
||||
..Default::default()
|
||||
},
|
||||
)?;
|
||||
|
||||
|
@ -2782,9 +2830,6 @@ mod tests {
|
|||
let worktree = store
|
||||
.project(project_id)
|
||||
.unwrap()
|
||||
.share
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.worktrees
|
||||
.get(&worktree_id.to_proto())
|
||||
.unwrap();
|
||||
|
@ -5055,7 +5100,7 @@ mod tests {
|
|||
assert_eq!(
|
||||
contacts(store),
|
||||
[
|
||||
("user_a", true, vec![("a", false, vec![])]),
|
||||
("user_a", true, vec![("a", vec![])]),
|
||||
("user_b", true, vec![]),
|
||||
("user_c", true, vec![])
|
||||
]
|
||||
|
@ -5077,7 +5122,7 @@ mod tests {
|
|||
assert_eq!(
|
||||
contacts(store),
|
||||
[
|
||||
("user_a", true, vec![("a", true, vec![])]),
|
||||
("user_a", true, vec![("a", vec![])]),
|
||||
("user_b", true, vec![]),
|
||||
("user_c", true, vec![])
|
||||
]
|
||||
|
@ -5093,7 +5138,7 @@ mod tests {
|
|||
assert_eq!(
|
||||
contacts(store),
|
||||
[
|
||||
("user_a", true, vec![("a", true, vec!["user_b"])]),
|
||||
("user_a", true, vec![("a", vec!["user_b"])]),
|
||||
("user_b", true, vec![]),
|
||||
("user_c", true, vec![])
|
||||
]
|
||||
|
@ -5112,8 +5157,8 @@ mod tests {
|
|||
assert_eq!(
|
||||
contacts(store),
|
||||
[
|
||||
("user_a", true, vec![("a", true, vec!["user_b"])]),
|
||||
("user_b", true, vec![("b", false, vec![])]),
|
||||
("user_a", true, vec![("a", vec!["user_b"])]),
|
||||
("user_b", true, vec![("b", vec![])]),
|
||||
("user_c", true, vec![])
|
||||
]
|
||||
)
|
||||
|
@ -5135,7 +5180,7 @@ mod tests {
|
|||
contacts(store),
|
||||
[
|
||||
("user_a", true, vec![]),
|
||||
("user_b", true, vec![("b", false, vec![])]),
|
||||
("user_b", true, vec![("b", vec![])]),
|
||||
("user_c", true, vec![])
|
||||
]
|
||||
)
|
||||
|
@ -5151,7 +5196,7 @@ mod tests {
|
|||
contacts(store),
|
||||
[
|
||||
("user_a", true, vec![]),
|
||||
("user_b", true, vec![("b", false, vec![])]),
|
||||
("user_b", true, vec![("b", vec![])]),
|
||||
("user_c", false, vec![])
|
||||
]
|
||||
)
|
||||
|
@ -5174,14 +5219,14 @@ mod tests {
|
|||
contacts(store),
|
||||
[
|
||||
("user_a", true, vec![]),
|
||||
("user_b", true, vec![("b", false, vec![])]),
|
||||
("user_b", true, vec![("b", vec![])]),
|
||||
("user_c", true, vec![])
|
||||
]
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
fn contacts(user_store: &UserStore) -> Vec<(&str, bool, Vec<(&str, bool, Vec<&str>)>)> {
|
||||
fn contacts(user_store: &UserStore) -> Vec<(&str, bool, Vec<(&str, Vec<&str>)>)> {
|
||||
user_store
|
||||
.contacts()
|
||||
.iter()
|
||||
|
@ -5192,7 +5237,6 @@ mod tests {
|
|||
.map(|p| {
|
||||
(
|
||||
p.worktree_root_names[0].as_str(),
|
||||
p.is_shared,
|
||||
p.guests.iter().map(|p| p.github_login.as_str()).collect(),
|
||||
)
|
||||
})
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::db::{self, ChannelId, UserId};
|
||||
use anyhow::{anyhow, Result};
|
||||
use collections::{BTreeMap, HashMap, HashSet};
|
||||
use rpc::{proto, ConnectionId};
|
||||
use rpc::{proto, ConnectionId, Receipt};
|
||||
use std::{collections::hash_map, path::PathBuf};
|
||||
use tracing::instrument;
|
||||
|
||||
|
@ -17,31 +17,24 @@ pub struct Store {
|
|||
struct ConnectionState {
|
||||
user_id: UserId,
|
||||
projects: HashSet<u64>,
|
||||
requested_projects: HashSet<u64>,
|
||||
channels: HashSet<ChannelId>,
|
||||
}
|
||||
|
||||
pub struct Project {
|
||||
pub host_connection_id: ConnectionId,
|
||||
pub host_user_id: UserId,
|
||||
pub share: Option<ProjectShare>,
|
||||
pub guests: HashMap<ConnectionId, (ReplicaId, UserId)>,
|
||||
pub join_requests: HashMap<UserId, Vec<Receipt<proto::JoinProject>>>,
|
||||
pub active_replica_ids: HashSet<ReplicaId>,
|
||||
pub worktrees: HashMap<u64, Worktree>,
|
||||
pub language_servers: Vec<proto::LanguageServer>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Worktree {
|
||||
pub root_name: String,
|
||||
pub visible: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ProjectShare {
|
||||
pub guests: HashMap<ConnectionId, (ReplicaId, UserId)>,
|
||||
pub active_replica_ids: HashSet<ReplicaId>,
|
||||
pub worktrees: HashMap<u64, WorktreeShare>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WorktreeShare {
|
||||
pub entries: HashMap<u64, proto::Entry>,
|
||||
pub diagnostic_summaries: BTreeMap<PathBuf, proto::DiagnosticSummary>,
|
||||
pub scan_id: u64,
|
||||
|
@ -62,18 +55,6 @@ pub struct RemovedConnectionState {
|
|||
pub contact_ids: HashSet<UserId>,
|
||||
}
|
||||
|
||||
pub struct JoinedProject<'a> {
|
||||
pub replica_id: ReplicaId,
|
||||
pub project: &'a Project,
|
||||
}
|
||||
|
||||
pub struct SharedProject {}
|
||||
|
||||
pub struct UnsharedProject {
|
||||
pub connection_ids: Vec<ConnectionId>,
|
||||
pub host_user_id: UserId,
|
||||
}
|
||||
|
||||
pub struct LeftProject {
|
||||
pub connection_ids: Vec<ConnectionId>,
|
||||
pub host_user_id: UserId,
|
||||
|
@ -93,7 +74,7 @@ impl Store {
|
|||
let mut shared_projects = 0;
|
||||
for project in self.projects.values() {
|
||||
registered_projects += 1;
|
||||
if project.share.is_some() {
|
||||
if !project.guests.is_empty() {
|
||||
shared_projects += 1;
|
||||
}
|
||||
}
|
||||
|
@ -112,6 +93,7 @@ impl Store {
|
|||
ConnectionState {
|
||||
user_id,
|
||||
projects: Default::default(),
|
||||
requested_projects: Default::default(),
|
||||
channels: Default::default(),
|
||||
},
|
||||
);
|
||||
|
@ -275,18 +257,15 @@ impl Store {
|
|||
if project.host_user_id == user_id {
|
||||
metadata.push(proto::ProjectMetadata {
|
||||
id: project_id,
|
||||
is_shared: project.share.is_some(),
|
||||
worktree_root_names: project
|
||||
.worktrees
|
||||
.values()
|
||||
.map(|worktree| worktree.root_name.clone())
|
||||
.collect(),
|
||||
guests: project
|
||||
.share
|
||||
.iter()
|
||||
.flat_map(|share| {
|
||||
share.guests.values().map(|(_, user_id)| user_id.to_proto())
|
||||
})
|
||||
.guests
|
||||
.values()
|
||||
.map(|(_, user_id)| user_id.to_proto())
|
||||
.collect(),
|
||||
});
|
||||
}
|
||||
|
@ -307,7 +286,9 @@ impl Store {
|
|||
Project {
|
||||
host_connection_id,
|
||||
host_user_id,
|
||||
share: None,
|
||||
guests: Default::default(),
|
||||
join_requests: Default::default(),
|
||||
active_replica_ids: Default::default(),
|
||||
worktrees: Default::default(),
|
||||
language_servers: Default::default(),
|
||||
},
|
||||
|
@ -332,10 +313,6 @@ impl Store {
|
|||
.ok_or_else(|| anyhow!("no such project"))?;
|
||||
if project.host_connection_id == connection_id {
|
||||
project.worktrees.insert(worktree_id, worktree);
|
||||
if let Ok(share) = project.share_mut() {
|
||||
share.worktrees.insert(worktree_id, Default::default());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("no such project"))?
|
||||
|
@ -356,11 +333,9 @@ impl Store {
|
|||
host_connection.projects.remove(&project_id);
|
||||
}
|
||||
|
||||
if let Some(share) = &project.share {
|
||||
for guest_connection in share.guests.keys() {
|
||||
if let Some(connection) = self.connections.get_mut(&guest_connection) {
|
||||
connection.projects.remove(&project_id);
|
||||
}
|
||||
for guest_connection in project.guests.keys() {
|
||||
if let Some(connection) = self.connections.get_mut(&guest_connection) {
|
||||
connection.projects.remove(&project_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -391,64 +366,7 @@ impl Store {
|
|||
.worktrees
|
||||
.remove(&worktree_id)
|
||||
.ok_or_else(|| anyhow!("no such worktree"))?;
|
||||
|
||||
let mut guest_connection_ids = Vec::new();
|
||||
if let Ok(share) = project.share_mut() {
|
||||
guest_connection_ids.extend(share.guests.keys());
|
||||
share.worktrees.remove(&worktree_id);
|
||||
}
|
||||
|
||||
Ok((worktree, guest_connection_ids))
|
||||
}
|
||||
|
||||
pub fn share_project(
|
||||
&mut self,
|
||||
project_id: u64,
|
||||
connection_id: ConnectionId,
|
||||
) -> Result<SharedProject> {
|
||||
if let Some(project) = self.projects.get_mut(&project_id) {
|
||||
if project.host_connection_id == connection_id {
|
||||
let mut share = ProjectShare::default();
|
||||
for worktree_id in project.worktrees.keys() {
|
||||
share.worktrees.insert(*worktree_id, Default::default());
|
||||
}
|
||||
project.share = Some(share);
|
||||
return Ok(SharedProject {});
|
||||
}
|
||||
}
|
||||
Err(anyhow!("no such project"))?
|
||||
}
|
||||
|
||||
pub fn unshare_project(
|
||||
&mut self,
|
||||
project_id: u64,
|
||||
acting_connection_id: ConnectionId,
|
||||
) -> Result<UnsharedProject> {
|
||||
let project = if let Some(project) = self.projects.get_mut(&project_id) {
|
||||
project
|
||||
} else {
|
||||
return Err(anyhow!("no such project"))?;
|
||||
};
|
||||
|
||||
if project.host_connection_id != acting_connection_id {
|
||||
return Err(anyhow!("not your project"))?;
|
||||
}
|
||||
|
||||
let connection_ids = project.connection_ids();
|
||||
if let Some(share) = project.share.take() {
|
||||
for connection_id in share.guests.into_keys() {
|
||||
if let Some(connection) = self.connections.get_mut(&connection_id) {
|
||||
connection.projects.remove(&project_id);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(UnsharedProject {
|
||||
connection_ids,
|
||||
host_user_id: project.host_user_id,
|
||||
})
|
||||
} else {
|
||||
Err(anyhow!("project is not shared"))?
|
||||
}
|
||||
Ok((worktree, project.guest_connection_ids()))
|
||||
}
|
||||
|
||||
pub fn update_diagnostic_summary(
|
||||
|
@ -464,7 +382,6 @@ impl Store {
|
|||
.ok_or_else(|| anyhow!("no such project"))?;
|
||||
if project.host_connection_id == connection_id {
|
||||
let worktree = project
|
||||
.share_mut()?
|
||||
.worktrees
|
||||
.get_mut(&worktree_id)
|
||||
.ok_or_else(|| anyhow!("no such worktree"))?;
|
||||
|
@ -495,35 +412,77 @@ impl Store {
|
|||
Err(anyhow!("no such project"))?
|
||||
}
|
||||
|
||||
pub fn join_project(
|
||||
pub fn request_join_project(
|
||||
&mut self,
|
||||
connection_id: ConnectionId,
|
||||
user_id: UserId,
|
||||
requester_id: UserId,
|
||||
project_id: u64,
|
||||
) -> Result<JoinedProject> {
|
||||
receipt: Receipt<proto::JoinProject>,
|
||||
) -> Result<()> {
|
||||
let connection = self
|
||||
.connections
|
||||
.get_mut(&connection_id)
|
||||
.get_mut(&receipt.sender_id)
|
||||
.ok_or_else(|| anyhow!("no such connection"))?;
|
||||
let project = self
|
||||
.projects
|
||||
.get_mut(&project_id)
|
||||
.ok_or_else(|| anyhow!("no such project"))?;
|
||||
connection.requested_projects.insert(project_id);
|
||||
project
|
||||
.join_requests
|
||||
.entry(requester_id)
|
||||
.or_default()
|
||||
.push(receipt);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
let share = project.share_mut()?;
|
||||
connection.projects.insert(project_id);
|
||||
|
||||
let mut replica_id = 1;
|
||||
while share.active_replica_ids.contains(&replica_id) {
|
||||
replica_id += 1;
|
||||
pub fn deny_join_project_request(
|
||||
&mut self,
|
||||
responder_connection_id: ConnectionId,
|
||||
requester_id: UserId,
|
||||
project_id: u64,
|
||||
) -> Option<Vec<Receipt<proto::JoinProject>>> {
|
||||
let project = self.projects.get_mut(&project_id)?;
|
||||
if responder_connection_id != project.host_connection_id {
|
||||
return None;
|
||||
}
|
||||
share.active_replica_ids.insert(replica_id);
|
||||
share.guests.insert(connection_id, (replica_id, user_id));
|
||||
|
||||
Ok(JoinedProject {
|
||||
replica_id,
|
||||
project: &self.projects[&project_id],
|
||||
})
|
||||
let receipts = project.join_requests.remove(&requester_id)?;
|
||||
for receipt in &receipts {
|
||||
let requester_connection = self.connections.get_mut(&receipt.sender_id)?;
|
||||
requester_connection.requested_projects.remove(&project_id);
|
||||
}
|
||||
Some(receipts)
|
||||
}
|
||||
|
||||
pub fn accept_join_project_request(
|
||||
&mut self,
|
||||
responder_connection_id: ConnectionId,
|
||||
requester_id: UserId,
|
||||
project_id: u64,
|
||||
) -> Option<(Vec<(Receipt<proto::JoinProject>, ReplicaId)>, &Project)> {
|
||||
let project = self.projects.get_mut(&project_id)?;
|
||||
if responder_connection_id != project.host_connection_id {
|
||||
return None;
|
||||
}
|
||||
|
||||
let receipts = project.join_requests.remove(&requester_id)?;
|
||||
let mut receipts_with_replica_ids = Vec::new();
|
||||
for receipt in receipts {
|
||||
let requester_connection = self.connections.get_mut(&receipt.sender_id)?;
|
||||
requester_connection.requested_projects.remove(&project_id);
|
||||
requester_connection.projects.insert(project_id);
|
||||
let mut replica_id = 1;
|
||||
while project.active_replica_ids.contains(&replica_id) {
|
||||
replica_id += 1;
|
||||
}
|
||||
project.active_replica_ids.insert(replica_id);
|
||||
project
|
||||
.guests
|
||||
.insert(receipt.sender_id, (replica_id, requester_id));
|
||||
receipts_with_replica_ids.push((receipt, replica_id));
|
||||
}
|
||||
|
||||
Some((receipts_with_replica_ids, project))
|
||||
}
|
||||
|
||||
pub fn leave_project(
|
||||
|
@ -535,15 +494,11 @@ impl Store {
|
|||
.projects
|
||||
.get_mut(&project_id)
|
||||
.ok_or_else(|| anyhow!("no such project"))?;
|
||||
let share = project
|
||||
.share
|
||||
.as_mut()
|
||||
.ok_or_else(|| anyhow!("project is not shared"))?;
|
||||
let (replica_id, _) = share
|
||||
let (replica_id, _) = project
|
||||
.guests
|
||||
.remove(&connection_id)
|
||||
.ok_or_else(|| anyhow!("cannot leave a project before joining it"))?;
|
||||
share.active_replica_ids.remove(&replica_id);
|
||||
project.active_replica_ids.remove(&replica_id);
|
||||
|
||||
if let Some(connection) = self.connections.get_mut(&connection_id) {
|
||||
connection.projects.remove(&project_id);
|
||||
|
@ -566,7 +521,6 @@ impl Store {
|
|||
) -> Result<Vec<ConnectionId>> {
|
||||
let project = self.write_project(project_id, connection_id)?;
|
||||
let worktree = project
|
||||
.share_mut()?
|
||||
.worktrees
|
||||
.get_mut(&worktree_id)
|
||||
.ok_or_else(|| anyhow!("no such worktree"))?;
|
||||
|
@ -611,12 +565,7 @@ impl Store {
|
|||
.get(&project_id)
|
||||
.ok_or_else(|| anyhow!("no such project"))?;
|
||||
if project.host_connection_id == connection_id
|
||||
|| project
|
||||
.share
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow!("project is not shared"))?
|
||||
.guests
|
||||
.contains_key(&connection_id)
|
||||
|| project.guests.contains_key(&connection_id)
|
||||
{
|
||||
Ok(project)
|
||||
} else {
|
||||
|
@ -634,12 +583,7 @@ impl Store {
|
|||
.get_mut(&project_id)
|
||||
.ok_or_else(|| anyhow!("no such project"))?;
|
||||
if project.host_connection_id == connection_id
|
||||
|| project
|
||||
.share
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow!("project is not shared"))?
|
||||
.guests
|
||||
.contains_key(&connection_id)
|
||||
|| project.guests.contains_key(&connection_id)
|
||||
{
|
||||
Ok(project)
|
||||
} else {
|
||||
|
@ -653,28 +597,21 @@ impl Store {
|
|||
for project_id in &connection.projects {
|
||||
let project = &self.projects.get(&project_id).unwrap();
|
||||
if project.host_connection_id != *connection_id {
|
||||
assert!(project
|
||||
.share
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.guests
|
||||
.contains_key(connection_id));
|
||||
assert!(project.guests.contains_key(connection_id));
|
||||
}
|
||||
|
||||
if let Some(share) = project.share.as_ref() {
|
||||
for (worktree_id, worktree) in share.worktrees.iter() {
|
||||
let mut paths = HashMap::default();
|
||||
for entry in worktree.entries.values() {
|
||||
let prev_entry = paths.insert(&entry.path, entry);
|
||||
assert_eq!(
|
||||
prev_entry,
|
||||
None,
|
||||
"worktree {:?}, duplicate path for entries {:?} and {:?}",
|
||||
worktree_id,
|
||||
prev_entry.unwrap(),
|
||||
entry
|
||||
);
|
||||
}
|
||||
for (worktree_id, worktree) in project.worktrees.iter() {
|
||||
let mut paths = HashMap::default();
|
||||
for entry in worktree.entries.values() {
|
||||
let prev_entry = paths.insert(&entry.path, entry);
|
||||
assert_eq!(
|
||||
prev_entry,
|
||||
None,
|
||||
"worktree {:?}, duplicate path for entries {:?} and {:?}",
|
||||
worktree_id,
|
||||
prev_entry.unwrap(),
|
||||
entry
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -702,21 +639,19 @@ impl Store {
|
|||
let host_connection = self.connections.get(&project.host_connection_id).unwrap();
|
||||
assert!(host_connection.projects.contains(project_id));
|
||||
|
||||
if let Some(share) = &project.share {
|
||||
for guest_connection_id in share.guests.keys() {
|
||||
let guest_connection = self.connections.get(guest_connection_id).unwrap();
|
||||
assert!(guest_connection.projects.contains(project_id));
|
||||
}
|
||||
assert_eq!(share.active_replica_ids.len(), share.guests.len(),);
|
||||
assert_eq!(
|
||||
share.active_replica_ids,
|
||||
share
|
||||
.guests
|
||||
.values()
|
||||
.map(|(replica_id, _)| *replica_id)
|
||||
.collect::<HashSet<_>>(),
|
||||
);
|
||||
for guest_connection_id in project.guests.keys() {
|
||||
let guest_connection = self.connections.get(guest_connection_id).unwrap();
|
||||
assert!(guest_connection.projects.contains(project_id));
|
||||
}
|
||||
assert_eq!(project.active_replica_ids.len(), project.guests.len(),);
|
||||
assert_eq!(
|
||||
project.active_replica_ids,
|
||||
project
|
||||
.guests
|
||||
.values()
|
||||
.map(|(replica_id, _)| *replica_id)
|
||||
.collect::<HashSet<_>>(),
|
||||
);
|
||||
}
|
||||
|
||||
for (channel_id, channel) in &self.channels {
|
||||
|
@ -730,38 +665,15 @@ impl Store {
|
|||
|
||||
impl Project {
|
||||
pub fn guest_connection_ids(&self) -> Vec<ConnectionId> {
|
||||
if let Some(share) = &self.share {
|
||||
share.guests.keys().copied().collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
self.guests.keys().copied().collect()
|
||||
}
|
||||
|
||||
pub fn connection_ids(&self) -> Vec<ConnectionId> {
|
||||
if let Some(share) = &self.share {
|
||||
share
|
||||
.guests
|
||||
.keys()
|
||||
.copied()
|
||||
.chain(Some(self.host_connection_id))
|
||||
.collect()
|
||||
} else {
|
||||
vec![self.host_connection_id]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn share(&self) -> Result<&ProjectShare> {
|
||||
Ok(self
|
||||
.share
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow!("worktree is not shared"))?)
|
||||
}
|
||||
|
||||
fn share_mut(&mut self) -> Result<&mut ProjectShare> {
|
||||
Ok(self
|
||||
.share
|
||||
.as_mut()
|
||||
.ok_or_else(|| anyhow!("worktree is not shared"))?)
|
||||
self.guests
|
||||
.keys()
|
||||
.copied()
|
||||
.chain(Some(self.host_connection_id))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue