WIP - start restructuring collaboration around entire projects

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-12-17 22:00:39 -08:00
parent 88d663a253
commit c41b958829
8 changed files with 771 additions and 561 deletions

View file

@ -60,8 +60,8 @@ impl Server {
server
.add_handler(Server::ping)
.add_handler(Server::open_worktree)
.add_handler(Server::close_worktree)
.add_handler(Server::register_worktree)
.add_handler(Server::unregister_worktree)
.add_handler(Server::share_worktree)
.add_handler(Server::unshare_worktree)
.add_handler(Server::join_worktree)
@ -169,26 +169,26 @@ impl Server {
self.peer.disconnect(connection_id).await;
let removed_connection = self.state_mut().remove_connection(connection_id)?;
for (worktree_id, worktree) in removed_connection.hosted_worktrees {
if let Some(share) = worktree.share {
for (project_id, project) in removed_connection.hosted_projects {
if let Some(share) = project.share {
broadcast(
connection_id,
share.guests.keys().copied().collect(),
|conn_id| {
self.peer
.send(conn_id, proto::UnshareWorktree { worktree_id })
.send(conn_id, proto::UnshareProject { project_id })
},
)
.await?;
}
}
for (worktree_id, peer_ids) in removed_connection.guest_worktree_ids {
for (project_id, peer_ids) in removed_connection.guest_project_ids {
broadcast(connection_id, peer_ids, |conn_id| {
self.peer.send(
conn_id,
proto::RemoveCollaborator {
worktree_id,
proto::RemoveProjectCollaborator {
project_id,
peer_id: connection_id.0,
},
)
@ -207,9 +207,9 @@ impl Server {
Ok(())
}
async fn open_worktree(
async fn register_worktree(
mut self: Arc<Server>,
request: TypedEnvelope<proto::OpenWorktree>,
request: TypedEnvelope<proto::RegisterWorktree>,
) -> tide::Result<()> {
let receipt = request.receipt();
let host_user_id = self.state().user_id_for_connection(request.sender_id)?;
@ -232,38 +232,54 @@ impl Server {
}
let contact_user_ids = contact_user_ids.into_iter().collect::<Vec<_>>();
let worktree_id = self.state_mut().add_worktree(Worktree {
host_connection_id: request.sender_id,
host_user_id,
authorized_user_ids: contact_user_ids.clone(),
root_name: request.payload.root_name,
share: None,
});
let ok = self.state_mut().register_worktree(
request.project_id,
request.worktree_id,
Worktree {
authorized_user_ids: contact_user_ids.clone(),
root_name: request.payload.root_name,
},
);
self.peer
.respond(receipt, proto::OpenWorktreeResponse { worktree_id })
.await?;
self.update_contacts_for_users(&contact_user_ids).await?;
if ok {
self.peer.respond(receipt, proto::Ack {}).await?;
self.update_contacts_for_users(&contact_user_ids).await?;
} else {
self.peer
.respond_with_error(
receipt,
proto::Error {
message: "no such project".to_string(),
},
)
.await?;
}
Ok(())
}
async fn close_worktree(
async fn unregister_worktree(
mut self: Arc<Server>,
request: TypedEnvelope<proto::CloseWorktree>,
request: TypedEnvelope<proto::UnregisterWorktree>,
) -> tide::Result<()> {
let project_id = request.payload.project_id;
let worktree_id = request.payload.worktree_id;
let worktree = self
.state_mut()
.remove_worktree(worktree_id, request.sender_id)?;
let worktree =
self.state_mut()
.unregister_worktree(project_id, worktree_id, request.sender_id)?;
if let Some(share) = worktree.share {
broadcast(
request.sender_id,
share.guests.keys().copied().collect(),
|conn_id| {
self.peer
.send(conn_id, proto::UnshareWorktree { worktree_id })
self.peer.send(
conn_id,
proto::UnregisterWorktree {
project_id,
worktree_id,
},
)
},
)
.await?;

View file

@ -8,29 +8,38 @@ use std::collections::hash_map;
pub struct Store {
connections: HashMap<ConnectionId, ConnectionState>,
connections_by_user_id: HashMap<UserId, HashSet<ConnectionId>>,
worktrees: HashMap<u64, Worktree>,
visible_worktrees_by_user_id: HashMap<UserId, HashSet<u64>>,
projects: HashMap<u64, Project>,
visible_projects_by_user_id: HashMap<UserId, HashSet<u64>>,
channels: HashMap<ChannelId, Channel>,
next_worktree_id: u64,
}
struct ConnectionState {
user_id: UserId,
worktrees: HashSet<u64>,
projects: HashSet<u64>,
channels: HashSet<ChannelId>,
}
pub struct Worktree {
pub struct Project {
pub host_connection_id: ConnectionId,
pub host_user_id: UserId,
pub share: Option<ProjectShare>,
worktrees: HashMap<u64, Worktree>,
}
pub struct Worktree {
pub authorized_user_ids: Vec<UserId>,
pub root_name: String,
pub share: Option<WorktreeShare>,
}
#[derive(Default)]
pub struct ProjectShare {
pub guests: HashMap<ConnectionId, (ReplicaId, UserId)>,
pub active_replica_ids: HashSet<ReplicaId>,
pub worktrees: HashMap<u64, WorktreeShare>,
}
pub struct WorktreeShare {
pub guests: HashMap<ConnectionId, (ReplicaId, UserId)>,
pub active_replica_ids: HashSet<ReplicaId>,
pub entries: HashMap<u64, proto::Entry>,
}
@ -43,8 +52,8 @@ pub type ReplicaId = u16;
#[derive(Default)]
pub struct RemovedConnectionState {
pub hosted_worktrees: HashMap<u64, Worktree>,
pub guest_worktree_ids: HashMap<u64, Vec<ConnectionId>>,
pub hosted_projects: HashMap<u64, Project>,
pub guest_project_ids: HashMap<u64, Vec<ConnectionId>>,
pub contact_ids: HashSet<UserId>,
}
@ -69,7 +78,7 @@ impl Store {
connection_id,
ConnectionState {
user_id,
worktrees: Default::default(),
projects: Default::default(),
channels: Default::default(),
},
);
@ -106,7 +115,7 @@ impl Store {
let mut result = RemovedConnectionState::default();
for worktree_id in connection.worktrees.clone() {
if let Ok(worktree) = self.remove_worktree(worktree_id, connection_id) {
if let Ok(worktree) = self.unregister_worktree(worktree_id, connection_id) {
result
.contact_ids
.extend(worktree.authorized_user_ids.iter().copied());
@ -174,12 +183,12 @@ impl Store {
pub fn contacts_for_user(&self, user_id: UserId) -> Vec<proto::Contact> {
let mut contacts = HashMap::default();
for worktree_id in self
.visible_worktrees_by_user_id
for project_id in self
.visible_projects_by_user_id
.get(&user_id)
.unwrap_or(&HashSet::default())
{
let worktree = &self.worktrees[worktree_id];
let project = &self.projects[project_id];
let mut guests = HashSet::default();
if let Ok(share) = worktree.share() {
@ -190,18 +199,22 @@ impl Store {
}
}
if let Ok(host_user_id) = self.user_id_for_connection(worktree.host_connection_id) {
if let Ok(host_user_id) = self.user_id_for_connection(project.host_connection_id) {
contacts
.entry(host_user_id)
.or_insert_with(|| proto::Contact {
user_id: host_user_id.to_proto(),
worktrees: Vec::new(),
projects: Vec::new(),
})
.worktrees
.push(proto::WorktreeMetadata {
id: *worktree_id,
root_name: worktree.root_name.clone(),
is_shared: worktree.share.is_some(),
.projects
.push(proto::ProjectMetadata {
id: *project_id,
worktree_root_names: project
.worktrees
.iter()
.map(|worktree| worktree.root_name.clone())
.collect(),
is_shared: project.share.is_some(),
guests: guests.into_iter().collect(),
});
}
@ -210,41 +223,75 @@ impl Store {
contacts.into_values().collect()
}
pub fn add_worktree(&mut self, worktree: Worktree) -> u64 {
let worktree_id = self.next_worktree_id;
for authorized_user_id in &worktree.authorized_user_ids {
self.visible_worktrees_by_user_id
.entry(*authorized_user_id)
.or_default()
.insert(worktree_id);
}
self.next_worktree_id += 1;
if let Some(connection) = self.connections.get_mut(&worktree.host_connection_id) {
connection.worktrees.insert(worktree_id);
}
self.worktrees.insert(worktree_id, worktree);
#[cfg(test)]
self.check_invariants();
worktree_id
pub fn register_project(
&mut self,
host_connection_id: ConnectionId,
host_user_id: UserId,
) -> u64 {
let project_id = self.next_project_id;
self.projects.insert(
project_id,
Project {
host_connection_id,
host_user_id,
share: None,
worktrees: Default::default(),
},
);
self.next_project_id += 1;
project_id
}
pub fn remove_worktree(
pub fn register_worktree(
&mut self,
project_id: u64,
worktree_id: u64,
worktree: Worktree,
) -> bool {
if let Some(project) = self.projects.get_mut(&project_id) {
for authorized_user_id in &worktree.authorized_user_ids {
self.visible_projects_by_user_id
.entry(*authorized_user_id)
.or_default()
.insert(project_id);
}
if let Some(connection) = self.connections.get_mut(&project.host_connection_id) {
connection.projects.insert(project_id);
}
project.worktrees.insert(worktree_id, worktree);
#[cfg(test)]
self.check_invariants();
true
} else {
false
}
}
pub fn unregister_project(&mut self, project_id: u64) {
todo!()
}
pub fn unregister_worktree(
&mut self,
project_id: u64,
worktree_id: u64,
acting_connection_id: ConnectionId,
) -> tide::Result<Worktree> {
let worktree = if let hash_map::Entry::Occupied(e) = self.worktrees.entry(worktree_id) {
if e.get().host_connection_id != acting_connection_id {
Err(anyhow!("not your worktree"))?;
}
e.remove()
} else {
return Err(anyhow!("no such worktree"))?;
};
let project = self
.projects
.get_mut(&project_id)
.ok_or_else(|| anyhow!("no such project"))?;
if project.host_connection_id != acting_connection_id {
Err(anyhow!("not your worktree"))?;
}
if let Some(connection) = self.connections.get_mut(&worktree.host_connection_id) {
let worktree = project
.worktrees
.remove(&worktree_id)
.ok_or_else(|| anyhow!("no such worktree"))?;
if let Some(connection) = self.connections.get_mut(&project.host_connection_id) {
connection.worktrees.remove(&worktree_id);
}
@ -271,20 +318,31 @@ impl Store {
Ok(worktree)
}
pub fn share_project(&mut self, project_id: u64, connection_id: ConnectionId) -> bool {
if let Some(project) = self.projects.get_mut(&project_id) {
if project.host_connection_id == connection_id {
project.share = Some(ProjectShare::default());
return true;
}
}
false
}
pub fn share_worktree(
&mut self,
project_id: u64,
worktree_id: u64,
connection_id: ConnectionId,
entries: HashMap<u64, proto::Entry>,
) -> Option<Vec<UserId>> {
if let Some(worktree) = self.worktrees.get_mut(&worktree_id) {
if worktree.host_connection_id == connection_id {
worktree.share = Some(WorktreeShare {
guests: Default::default(),
active_replica_ids: Default::default(),
entries,
});
return Some(worktree.authorized_user_ids.clone());
if let Some(project) = self.projects.get_mut(&project_id) {
if project.host_connection_id == connection_id {
if let Some(share) = project.share.as_mut() {
share
.worktrees
.insert(worktree_id, WorktreeShare { entries });
return Some(project.authorized_user_ids());
}
}
}
None
@ -586,14 +644,14 @@ impl Worktree {
}
}
pub fn share(&self) -> tide::Result<&WorktreeShare> {
pub fn share(&self) -> tide::Result<&ProjectShare> {
Ok(self
.share
.as_ref()
.ok_or_else(|| anyhow!("worktree is not shared"))?)
}
fn share_mut(&mut self) -> tide::Result<&mut WorktreeShare> {
fn share_mut(&mut self) -> tide::Result<&mut ProjectShare> {
Ok(self
.share
.as_mut()