mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-26 10:40:54 +00:00
WIP - start restructuring collaboration around entire projects
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
88d663a253
commit
c41b958829
8 changed files with 771 additions and 561 deletions
|
@ -22,14 +22,14 @@ pub struct User {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Contact {
|
pub struct Contact {
|
||||||
pub user: Arc<User>,
|
pub user: Arc<User>,
|
||||||
pub worktrees: Vec<WorktreeMetadata>,
|
pub projects: Vec<ProjectMetadata>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct WorktreeMetadata {
|
pub struct ProjectMetadata {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub root_name: String,
|
|
||||||
pub is_shared: bool,
|
pub is_shared: bool,
|
||||||
|
pub worktree_root_names: Vec<String>,
|
||||||
pub guests: Vec<Arc<User>>,
|
pub guests: Vec<Arc<User>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ impl UserStore {
|
||||||
let mut user_ids = HashSet::new();
|
let mut user_ids = HashSet::new();
|
||||||
for contact in &message.contacts {
|
for contact in &message.contacts {
|
||||||
user_ids.insert(contact.user_id);
|
user_ids.insert(contact.user_id);
|
||||||
user_ids.extend(contact.worktrees.iter().flat_map(|w| &w.guests).copied());
|
user_ids.extend(contact.projects.iter().flat_map(|w| &w.guests).copied());
|
||||||
}
|
}
|
||||||
|
|
||||||
let load_users = self.load_users(user_ids.into_iter().collect(), cx);
|
let load_users = self.load_users(user_ids.into_iter().collect(), cx);
|
||||||
|
@ -221,10 +221,10 @@ impl Contact {
|
||||||
user_store.fetch_user(contact.user_id, cx)
|
user_store.fetch_user(contact.user_id, cx)
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
let mut worktrees = Vec::new();
|
let mut projects = Vec::new();
|
||||||
for worktree in contact.worktrees {
|
for project in contact.projects {
|
||||||
let mut guests = Vec::new();
|
let mut guests = Vec::new();
|
||||||
for participant_id in worktree.guests {
|
for participant_id in project.guests {
|
||||||
guests.push(
|
guests.push(
|
||||||
user_store
|
user_store
|
||||||
.update(cx, |user_store, cx| {
|
.update(cx, |user_store, cx| {
|
||||||
|
@ -233,14 +233,14 @@ impl Contact {
|
||||||
.await?,
|
.await?,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
worktrees.push(WorktreeMetadata {
|
projects.push(ProjectMetadata {
|
||||||
id: worktree.id,
|
id: project.id,
|
||||||
root_name: worktree.root_name,
|
worktree_root_names: project.worktree_root_names.clone(),
|
||||||
is_shared: worktree.is_shared,
|
is_shared: project.is_shared,
|
||||||
guests,
|
guests,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Ok(Self { user, worktrees })
|
Ok(Self { user, projects })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,18 +3,20 @@ mod ignore;
|
||||||
mod worktree;
|
mod worktree;
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use client::{Client, UserStore};
|
use client::{proto, Client, PeerId, TypedEnvelope, User, UserStore};
|
||||||
use clock::ReplicaId;
|
use clock::ReplicaId;
|
||||||
|
use collections::HashMap;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use fuzzy::{PathMatch, PathMatchCandidate, PathMatchCandidateSet};
|
use fuzzy::{PathMatch, PathMatchCandidate, PathMatchCandidateSet};
|
||||||
use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task};
|
use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task};
|
||||||
use language::{Buffer, DiagnosticEntry, LanguageRegistry};
|
use language::{Buffer, DiagnosticEntry, LanguageRegistry};
|
||||||
use lsp::DiagnosticSeverity;
|
use lsp::DiagnosticSeverity;
|
||||||
|
use postage::{prelude::Stream, sink::Sink, watch};
|
||||||
use std::{
|
use std::{
|
||||||
path::Path,
|
path::Path,
|
||||||
sync::{atomic::AtomicBool, Arc},
|
sync::{atomic::AtomicBool, Arc},
|
||||||
};
|
};
|
||||||
use util::TryFutureExt as _;
|
use util::{ResultExt, TryFutureExt as _};
|
||||||
|
|
||||||
pub use fs::*;
|
pub use fs::*;
|
||||||
pub use worktree::*;
|
pub use worktree::*;
|
||||||
|
@ -27,6 +29,29 @@ pub struct Project {
|
||||||
client: Arc<client::Client>,
|
client: Arc<client::Client>,
|
||||||
user_store: ModelHandle<UserStore>,
|
user_store: ModelHandle<UserStore>,
|
||||||
fs: Arc<dyn Fs>,
|
fs: Arc<dyn Fs>,
|
||||||
|
client_state: ProjectClientState,
|
||||||
|
collaborators: HashMap<PeerId, Collaborator>,
|
||||||
|
subscriptions: Vec<client::Subscription>,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ProjectClientState {
|
||||||
|
Local {
|
||||||
|
is_shared: bool,
|
||||||
|
remote_id_tx: watch::Sender<Option<u64>>,
|
||||||
|
remote_id_rx: watch::Receiver<Option<u64>>,
|
||||||
|
_maintain_remote_id_task: Task<Option<()>>,
|
||||||
|
},
|
||||||
|
Remote {
|
||||||
|
remote_id: u64,
|
||||||
|
replica_id: ReplicaId,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Collaborator {
|
||||||
|
pub user: Arc<User>,
|
||||||
|
pub peer_id: PeerId,
|
||||||
|
pub replica_id: ReplicaId,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
|
@ -80,14 +105,46 @@ pub struct ProjectEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Project {
|
impl Project {
|
||||||
pub fn new(
|
pub fn local(
|
||||||
languages: Arc<LanguageRegistry>,
|
languages: Arc<LanguageRegistry>,
|
||||||
client: Arc<Client>,
|
client: Arc<Client>,
|
||||||
user_store: ModelHandle<UserStore>,
|
user_store: ModelHandle<UserStore>,
|
||||||
fs: Arc<dyn Fs>,
|
fs: Arc<dyn Fs>,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
let (remote_id_tx, remote_id_rx) = watch::channel();
|
||||||
|
let _maintain_remote_id_task = cx.spawn_weak({
|
||||||
|
let rpc = client.clone();
|
||||||
|
move |this, cx| {
|
||||||
|
async move {
|
||||||
|
let mut status = rpc.status();
|
||||||
|
while let Some(status) = status.recv().await {
|
||||||
|
if let Some(this) = this.upgrade(&cx) {
|
||||||
|
let remote_id = if let client::Status::Connected { .. } = status {
|
||||||
|
let response = rpc.request(proto::RegisterProject {}).await?;
|
||||||
|
Some(response.project_id)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
this.update(&mut cx, |this, cx| this.set_remote_id(remote_id, cx));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
.log_err()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
worktrees: Default::default(),
|
worktrees: Default::default(),
|
||||||
|
collaborators: Default::default(),
|
||||||
|
client_state: ProjectClientState::Local {
|
||||||
|
is_shared: false,
|
||||||
|
remote_id_tx,
|
||||||
|
remote_id_rx,
|
||||||
|
_maintain_remote_id_task,
|
||||||
|
},
|
||||||
|
subscriptions: Vec::new(),
|
||||||
active_worktree: None,
|
active_worktree: None,
|
||||||
active_entry: None,
|
active_entry: None,
|
||||||
languages,
|
languages,
|
||||||
|
@ -97,9 +154,120 @@ impl Project {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn open_remote(
|
||||||
|
remote_id: u64,
|
||||||
|
languages: Arc<LanguageRegistry>,
|
||||||
|
client: Arc<Client>,
|
||||||
|
user_store: ModelHandle<UserStore>,
|
||||||
|
fs: Arc<dyn Fs>,
|
||||||
|
cx: &mut AsyncAppContext,
|
||||||
|
) -> Result<ModelHandle<Self>> {
|
||||||
|
client.authenticate_and_connect(&cx).await?;
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.request(proto::JoinProject {
|
||||||
|
project_id: remote_id,
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let replica_id = response.replica_id as ReplicaId;
|
||||||
|
|
||||||
|
let mut worktrees = Vec::new();
|
||||||
|
for worktree in response.worktrees {
|
||||||
|
worktrees.push(
|
||||||
|
Worktree::remote(
|
||||||
|
remote_id,
|
||||||
|
replica_id,
|
||||||
|
worktree,
|
||||||
|
client.clone(),
|
||||||
|
user_store.clone(),
|
||||||
|
languages.clone(),
|
||||||
|
cx,
|
||||||
|
)
|
||||||
|
.await?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let user_ids = response
|
||||||
|
.collaborators
|
||||||
|
.iter()
|
||||||
|
.map(|peer| peer.user_id)
|
||||||
|
.collect();
|
||||||
|
user_store
|
||||||
|
.update(cx, |user_store, cx| user_store.load_users(user_ids, cx))
|
||||||
|
.await?;
|
||||||
|
let mut collaborators = HashMap::default();
|
||||||
|
for message in response.collaborators {
|
||||||
|
let collaborator = Collaborator::from_proto(message, &user_store, cx).await?;
|
||||||
|
collaborators.insert(collaborator.peer_id, collaborator);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(cx.add_model(|cx| Self {
|
||||||
|
worktrees,
|
||||||
|
active_worktree: None,
|
||||||
|
active_entry: None,
|
||||||
|
collaborators,
|
||||||
|
languages,
|
||||||
|
user_store,
|
||||||
|
fs,
|
||||||
|
subscriptions: vec![
|
||||||
|
client.subscribe_to_entity(remote_id, cx, Self::handle_add_collaborator),
|
||||||
|
client.subscribe_to_entity(remote_id, cx, Self::handle_remove_collaborator),
|
||||||
|
client.subscribe_to_entity(remote_id, cx, Self::handle_register_worktree),
|
||||||
|
client.subscribe_to_entity(remote_id, cx, Self::handle_update_worktree),
|
||||||
|
client.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer),
|
||||||
|
client.subscribe_to_entity(remote_id, cx, Self::handle_buffer_saved),
|
||||||
|
],
|
||||||
|
client,
|
||||||
|
client_state: ProjectClientState::Remote {
|
||||||
|
remote_id,
|
||||||
|
replica_id,
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_remote_id(&mut self, remote_id: Option<u64>, cx: &mut ModelContext<Self>) {
|
||||||
|
if let ProjectClientState::Local { remote_id_tx, .. } = &mut self.client_state {
|
||||||
|
cx.foreground().spawn(remote_id_tx.send(remote_id)).detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
for worktree in &self.worktrees {
|
||||||
|
worktree.update(cx, |worktree, cx| {
|
||||||
|
if let Some(worktree) = worktree.as_local_mut() {
|
||||||
|
worktree.set_project_remote_id(remote_id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.subscriptions.clear();
|
||||||
|
if let Some(remote_id) = remote_id {
|
||||||
|
self.subscriptions.extend([
|
||||||
|
self.client
|
||||||
|
.subscribe_to_entity(remote_id, cx, Self::handle_update_worktree),
|
||||||
|
self.client
|
||||||
|
.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer),
|
||||||
|
self.client
|
||||||
|
.subscribe_to_entity(remote_id, cx, Self::handle_buffer_saved),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remote_id(&self) -> Option<u64> {
|
||||||
|
match &self.client_state {
|
||||||
|
ProjectClientState::Local { remote_id_rx, .. } => *remote_id_rx.borrow(),
|
||||||
|
ProjectClientState::Remote { remote_id, .. } => Some(*remote_id),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn replica_id(&self, cx: &AppContext) -> ReplicaId {
|
pub fn replica_id(&self, cx: &AppContext) -> ReplicaId {
|
||||||
// TODO
|
match &self.client_state {
|
||||||
self.worktrees.first().unwrap().read(cx).replica_id()
|
ProjectClientState::Local { .. } => 0,
|
||||||
|
ProjectClientState::Remote { replica_id, .. } => *replica_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn collaborators(&self) -> &HashMap<PeerId, Collaborator> {
|
||||||
|
&self.collaborators
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn worktrees(&self) -> &[ModelHandle<Worktree>] {
|
pub fn worktrees(&self) -> &[ModelHandle<Worktree>] {
|
||||||
|
@ -113,6 +281,40 @@ impl Project {
|
||||||
.cloned()
|
.cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn share(&self, cx: &mut ModelContext<Self>) -> Task<anyhow::Result<()>> {
|
||||||
|
let rpc = self.client.clone();
|
||||||
|
cx.spawn(|this, mut cx| async move {
|
||||||
|
let remote_id = this.update(&mut cx, |this, cx| {
|
||||||
|
if let ProjectClientState::Local {
|
||||||
|
is_shared,
|
||||||
|
remote_id_rx,
|
||||||
|
..
|
||||||
|
} = &mut this.client_state
|
||||||
|
{
|
||||||
|
*is_shared = true;
|
||||||
|
Ok(*remote_id_rx.borrow())
|
||||||
|
} else {
|
||||||
|
Err(anyhow!("can't share a remote project"))
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let remote_id = remote_id.ok_or_else(|| anyhow!("no project id"))?;
|
||||||
|
rpc.send(proto::ShareProject {
|
||||||
|
project_id: remote_id,
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
this.update(&mut cx, |this, cx| {
|
||||||
|
for worktree in &this.worktrees {
|
||||||
|
worktree.update(cx, |worktree, cx| {
|
||||||
|
worktree.as_local_mut().unwrap().share(cx).detach();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn open_buffer(
|
pub fn open_buffer(
|
||||||
&self,
|
&self,
|
||||||
path: ProjectPath,
|
path: ProjectPath,
|
||||||
|
@ -139,6 +341,24 @@ impl Project {
|
||||||
let worktree =
|
let worktree =
|
||||||
Worktree::open_local(client, user_store, path, fs, languages, &mut cx).await?;
|
Worktree::open_local(client, user_store, path, fs, languages, &mut cx).await?;
|
||||||
this.update(&mut cx, |this, cx| {
|
this.update(&mut cx, |this, cx| {
|
||||||
|
if let Some(project_id) = this.remote_id() {
|
||||||
|
worktree.update(cx, |worktree, cx| {
|
||||||
|
worktree
|
||||||
|
.as_local_mut()
|
||||||
|
.unwrap()
|
||||||
|
.set_project_remote_id(Some(project_id));
|
||||||
|
cx.foreground().spawn(
|
||||||
|
client
|
||||||
|
.request(proto::RegisterWorktree {
|
||||||
|
project_id,
|
||||||
|
root_name: worktree.root_name().to_string(),
|
||||||
|
authorized_logins: worktree.authorized_logins(),
|
||||||
|
worktree_id: worktree.id() as u64,
|
||||||
|
})
|
||||||
|
.log_err(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
this.add_worktree(worktree.clone(), cx);
|
this.add_worktree(worktree.clone(), cx);
|
||||||
});
|
});
|
||||||
Ok(worktree)
|
Ok(worktree)
|
||||||
|
@ -154,10 +374,8 @@ impl Project {
|
||||||
let languages = self.languages.clone();
|
let languages = self.languages.clone();
|
||||||
let user_store = self.user_store.clone();
|
let user_store = self.user_store.clone();
|
||||||
cx.spawn(|this, mut cx| async move {
|
cx.spawn(|this, mut cx| async move {
|
||||||
rpc.authenticate_and_connect(&cx).await?;
|
|
||||||
let worktree =
|
let worktree =
|
||||||
Worktree::open_remote(rpc.clone(), remote_id, languages, user_store, &mut cx)
|
Worktree::remote(rpc.clone(), remote_id, languages, user_store, &mut cx).await?;
|
||||||
.await?;
|
|
||||||
this.update(&mut cx, |this, cx| {
|
this.update(&mut cx, |this, cx| {
|
||||||
cx.subscribe(&worktree, move |this, _, event, cx| match event {
|
cx.subscribe(&worktree, move |this, _, event, cx| match event {
|
||||||
worktree::Event::Closed => {
|
worktree::Event::Closed => {
|
||||||
|
@ -304,6 +522,116 @@ impl Project {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RPC message handlers
|
||||||
|
|
||||||
|
fn handle_add_collaborator(
|
||||||
|
&mut self,
|
||||||
|
mut envelope: TypedEnvelope<proto::AddProjectCollaborator>,
|
||||||
|
_: Arc<Client>,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let user_store = self.user_store.clone();
|
||||||
|
let collaborator = envelope
|
||||||
|
.payload
|
||||||
|
.collaborator
|
||||||
|
.take()
|
||||||
|
.ok_or_else(|| anyhow!("empty collaborator"))?;
|
||||||
|
|
||||||
|
cx.spawn(|this, mut cx| {
|
||||||
|
async move {
|
||||||
|
let collaborator =
|
||||||
|
Collaborator::from_proto(collaborator, &user_store, &mut cx).await?;
|
||||||
|
this.collaborators
|
||||||
|
.insert(collaborator.peer_id, collaborator);
|
||||||
|
cx.notify();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
.log_err()
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_remove_collaborator(
|
||||||
|
&mut self,
|
||||||
|
envelope: TypedEnvelope<proto::RemoveProjectCollaborator>,
|
||||||
|
_: Arc<Client>,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let peer_id = PeerId(envelope.payload.peer_id);
|
||||||
|
let replica_id = self
|
||||||
|
.collaborators
|
||||||
|
.remove(&peer_id)
|
||||||
|
.ok_or_else(|| anyhow!("unknown peer {:?}", peer_id))?
|
||||||
|
.replica_id;
|
||||||
|
for worktree in &self.worktrees {
|
||||||
|
worktree.update(cx, |worktree, cx| {
|
||||||
|
worktree.remove_collaborator(peer_id, replica_id, cx);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_register_worktree(
|
||||||
|
&mut self,
|
||||||
|
envelope: TypedEnvelope<proto::RegisterWorktree>,
|
||||||
|
_: Arc<Client>,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let peer_id = PeerId(envelope.payload.peer_id);
|
||||||
|
let replica_id = self
|
||||||
|
.collaborators
|
||||||
|
.remove(&peer_id)
|
||||||
|
.ok_or_else(|| anyhow!("unknown peer {:?}", peer_id))?
|
||||||
|
.replica_id;
|
||||||
|
for worktree in &self.worktrees {
|
||||||
|
worktree.update(cx, |worktree, cx| {
|
||||||
|
worktree.remove_collaborator(peer_id, replica_id, cx);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_update_worktree(
|
||||||
|
&mut self,
|
||||||
|
mut envelope: TypedEnvelope<proto::UpdateWorktree>,
|
||||||
|
_: Arc<Client>,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) -> Result<()> {
|
||||||
|
if let Some(worktree) = self.worktree_for_id(envelope.payload.worktree_id as usize) {
|
||||||
|
worktree
|
||||||
|
.as_remote_mut()
|
||||||
|
.unwrap()
|
||||||
|
.update_from_remote(envelope, cx);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_update_buffer(
|
||||||
|
&mut self,
|
||||||
|
envelope: TypedEnvelope<proto::UpdateBuffer>,
|
||||||
|
_: Arc<Client>,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) -> Result<()> {
|
||||||
|
if let Some(worktree) = self.worktree_for_id(envelope.payload.worktree_id as usize) {
|
||||||
|
worktree.handle_update_buffer(envelope, cx)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_buffer_saved(
|
||||||
|
&mut self,
|
||||||
|
envelope: TypedEnvelope<proto::BufferSaved>,
|
||||||
|
_: Arc<Client>,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) -> Result<()> {
|
||||||
|
if let Some(worktree) = self.worktree_for_id(envelope.payload.worktree_id as usize) {
|
||||||
|
worktree.handle_buffer_saved(envelope, cx);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn match_paths<'a>(
|
pub fn match_paths<'a>(
|
||||||
&self,
|
&self,
|
||||||
query: &'a str,
|
query: &'a str,
|
||||||
|
@ -400,6 +728,38 @@ impl<'a> Iterator for CandidateSetIter<'a> {
|
||||||
|
|
||||||
impl Entity for Project {
|
impl Entity for Project {
|
||||||
type Event = Event;
|
type Event = Event;
|
||||||
|
|
||||||
|
fn release(&mut self, cx: &mut gpui::MutableAppContext) {
|
||||||
|
if let Some(project_id) = *self.remote_id.borrow() {
|
||||||
|
let rpc = self.client.clone();
|
||||||
|
cx.spawn(|_| async move {
|
||||||
|
if let Err(err) = rpc.send(proto::UnregisterProject { project_id }).await {
|
||||||
|
log::error!("error unregistering project: {}", err);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Collaborator {
|
||||||
|
fn from_proto(
|
||||||
|
message: proto::Collaborator,
|
||||||
|
user_store: &ModelHandle<UserStore>,
|
||||||
|
cx: &mut AsyncAppContext,
|
||||||
|
) -> impl Future<Output = Result<Self>> {
|
||||||
|
let user = user_store.update(cx, |user_store, cx| {
|
||||||
|
user_store.fetch_user(message.user_id, cx)
|
||||||
|
});
|
||||||
|
|
||||||
|
async move {
|
||||||
|
Ok(Self {
|
||||||
|
peer_id: PeerId(message.peer_id),
|
||||||
|
user: user.await?,
|
||||||
|
replica_id: message.replica_id as ReplicaId,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -514,6 +874,6 @@ mod tests {
|
||||||
let client = client::Client::new();
|
let client = client::Client::new();
|
||||||
let http_client = FakeHttpClient::new(|_| async move { Ok(ServerResponse::new(404)) });
|
let http_client = FakeHttpClient::new(|_| async move { Ok(ServerResponse::new(404)) });
|
||||||
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
|
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
|
||||||
cx.add_model(|_| Project::new(languages, client, user_store, fs))
|
cx.add_model(|cx| Project::new(languages, client, user_store, fs, cx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use super::{
|
||||||
};
|
};
|
||||||
use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
|
use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use client::{proto, Client, PeerId, TypedEnvelope, User, UserStore};
|
use client::{proto, Client, PeerId, TypedEnvelope, UserStore};
|
||||||
use clock::ReplicaId;
|
use clock::ReplicaId;
|
||||||
use collections::{hash_map, HashMap};
|
use collections::{hash_map, HashMap};
|
||||||
use futures::{Stream, StreamExt};
|
use futures::{Stream, StreamExt};
|
||||||
|
@ -66,62 +66,9 @@ pub enum Event {
|
||||||
Closed,
|
Closed,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct Collaborator {
|
|
||||||
pub user: Arc<User>,
|
|
||||||
pub peer_id: PeerId,
|
|
||||||
pub replica_id: ReplicaId,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Collaborator {
|
|
||||||
fn from_proto(
|
|
||||||
message: proto::Collaborator,
|
|
||||||
user_store: &ModelHandle<UserStore>,
|
|
||||||
cx: &mut AsyncAppContext,
|
|
||||||
) -> impl Future<Output = Result<Self>> {
|
|
||||||
let user = user_store.update(cx, |user_store, cx| {
|
|
||||||
user_store.fetch_user(message.user_id, cx)
|
|
||||||
});
|
|
||||||
|
|
||||||
async move {
|
|
||||||
Ok(Self {
|
|
||||||
peer_id: PeerId(message.peer_id),
|
|
||||||
user: user.await?,
|
|
||||||
replica_id: message.replica_id as ReplicaId,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Entity for Worktree {
|
impl Entity for Worktree {
|
||||||
type Event = Event;
|
type Event = Event;
|
||||||
|
|
||||||
fn release(&mut self, cx: &mut MutableAppContext) {
|
|
||||||
match self {
|
|
||||||
Self::Local(tree) => {
|
|
||||||
if let Some(worktree_id) = *tree.remote_id.borrow() {
|
|
||||||
let rpc = tree.client.clone();
|
|
||||||
cx.spawn(|_| async move {
|
|
||||||
if let Err(err) = rpc.send(proto::CloseWorktree { worktree_id }).await {
|
|
||||||
log::error!("error closing worktree: {}", err);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Self::Remote(tree) => {
|
|
||||||
let rpc = tree.client.clone();
|
|
||||||
let worktree_id = tree.remote_id;
|
|
||||||
cx.spawn(|_| async move {
|
|
||||||
if let Err(err) = rpc.send(proto::LeaveWorktree { worktree_id }).await {
|
|
||||||
log::error!("error closing worktree: {}", err);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn app_will_quit(
|
fn app_will_quit(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: &mut MutableAppContext,
|
_: &mut MutableAppContext,
|
||||||
|
@ -172,32 +119,16 @@ impl Worktree {
|
||||||
Ok(tree)
|
Ok(tree)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn open_remote(
|
pub async fn remote(
|
||||||
client: Arc<Client>,
|
project_remote_id: u64,
|
||||||
id: u64,
|
replica_id: ReplicaId,
|
||||||
languages: Arc<LanguageRegistry>,
|
worktree: proto::Worktree,
|
||||||
user_store: ModelHandle<UserStore>,
|
|
||||||
cx: &mut AsyncAppContext,
|
|
||||||
) -> Result<ModelHandle<Self>> {
|
|
||||||
let response = client
|
|
||||||
.request(proto::JoinWorktree { worktree_id: id })
|
|
||||||
.await?;
|
|
||||||
Worktree::remote(response, client, user_store, languages, cx).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn remote(
|
|
||||||
join_response: proto::JoinWorktreeResponse,
|
|
||||||
client: Arc<Client>,
|
client: Arc<Client>,
|
||||||
user_store: ModelHandle<UserStore>,
|
user_store: ModelHandle<UserStore>,
|
||||||
languages: Arc<LanguageRegistry>,
|
languages: Arc<LanguageRegistry>,
|
||||||
cx: &mut AsyncAppContext,
|
cx: &mut AsyncAppContext,
|
||||||
) -> Result<ModelHandle<Self>> {
|
) -> Result<ModelHandle<Self>> {
|
||||||
let worktree = join_response
|
|
||||||
.worktree
|
|
||||||
.ok_or_else(|| anyhow!("empty worktree"))?;
|
|
||||||
|
|
||||||
let remote_id = worktree.id;
|
let remote_id = worktree.id;
|
||||||
let replica_id = join_response.replica_id as ReplicaId;
|
|
||||||
let root_char_bag: CharBag = worktree
|
let root_char_bag: CharBag = worktree
|
||||||
.root_name
|
.root_name
|
||||||
.chars()
|
.chars()
|
||||||
|
@ -232,20 +163,6 @@ impl Worktree {
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
let user_ids = join_response
|
|
||||||
.collaborators
|
|
||||||
.iter()
|
|
||||||
.map(|peer| peer.user_id)
|
|
||||||
.collect();
|
|
||||||
user_store
|
|
||||||
.update(cx, |user_store, cx| user_store.load_users(user_ids, cx))
|
|
||||||
.await?;
|
|
||||||
let mut collaborators = HashMap::default();
|
|
||||||
for message in join_response.collaborators {
|
|
||||||
let collaborator = Collaborator::from_proto(message, &user_store, cx).await?;
|
|
||||||
collaborators.insert(collaborator.peer_id, collaborator);
|
|
||||||
}
|
|
||||||
|
|
||||||
let worktree = cx.update(|cx| {
|
let worktree = cx.update(|cx| {
|
||||||
cx.add_model(|cx: &mut ModelContext<Worktree>| {
|
cx.add_model(|cx: &mut ModelContext<Worktree>| {
|
||||||
let snapshot = Snapshot {
|
let snapshot = Snapshot {
|
||||||
|
@ -290,16 +207,8 @@ impl Worktree {
|
||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
let _subscriptions = vec![
|
|
||||||
client.subscribe_to_entity(remote_id, cx, Self::handle_add_collaborator),
|
|
||||||
client.subscribe_to_entity(remote_id, cx, Self::handle_remove_collaborator),
|
|
||||||
client.subscribe_to_entity(remote_id, cx, Self::handle_update),
|
|
||||||
client.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer),
|
|
||||||
client.subscribe_to_entity(remote_id, cx, Self::handle_buffer_saved),
|
|
||||||
client.subscribe_to_entity(remote_id, cx, Self::handle_unshare),
|
|
||||||
];
|
|
||||||
|
|
||||||
Worktree::Remote(RemoteWorktree {
|
Worktree::Remote(RemoteWorktree {
|
||||||
|
project_remote_id,
|
||||||
remote_id,
|
remote_id,
|
||||||
replica_id,
|
replica_id,
|
||||||
snapshot,
|
snapshot,
|
||||||
|
@ -309,11 +218,9 @@ impl Worktree {
|
||||||
loading_buffers: Default::default(),
|
loading_buffers: Default::default(),
|
||||||
open_buffers: Default::default(),
|
open_buffers: Default::default(),
|
||||||
diagnostic_summaries: HashMap::default(),
|
diagnostic_summaries: HashMap::default(),
|
||||||
collaborators,
|
|
||||||
queued_operations: Default::default(),
|
queued_operations: Default::default(),
|
||||||
languages,
|
languages,
|
||||||
user_store,
|
user_store,
|
||||||
_subscriptions,
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
@ -359,6 +266,25 @@ impl Worktree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn authorized_logins(&self) -> Vec<String> {
|
||||||
|
match self {
|
||||||
|
Worktree::Local(worktree) => worktree.config.collaborators.clone(),
|
||||||
|
Worktree::Remote(worktree) => Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_collaborator(
|
||||||
|
&mut self,
|
||||||
|
peer_id: PeerId,
|
||||||
|
replica_id: ReplicaId,
|
||||||
|
cx: &mut ModelContext<Self>,
|
||||||
|
) {
|
||||||
|
match self {
|
||||||
|
Worktree::Local(worktree) => worktree.remove_collaborator(peer_id, replica_id, cx),
|
||||||
|
Worktree::Remote(worktree) => worktree.remove_collaborator(peer_id, replica_id, cx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn languages(&self) -> &Arc<LanguageRegistry> {
|
pub fn languages(&self) -> &Arc<LanguageRegistry> {
|
||||||
match self {
|
match self {
|
||||||
Worktree::Local(worktree) => &worktree.languages,
|
Worktree::Local(worktree) => &worktree.languages,
|
||||||
|
@ -373,59 +299,6 @@ impl Worktree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_add_collaborator(
|
|
||||||
&mut self,
|
|
||||||
mut envelope: TypedEnvelope<proto::AddCollaborator>,
|
|
||||||
_: Arc<Client>,
|
|
||||||
cx: &mut ModelContext<Self>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let user_store = self.user_store().clone();
|
|
||||||
let collaborator = envelope
|
|
||||||
.payload
|
|
||||||
.collaborator
|
|
||||||
.take()
|
|
||||||
.ok_or_else(|| anyhow!("empty collaborator"))?;
|
|
||||||
|
|
||||||
cx.spawn(|this, mut cx| {
|
|
||||||
async move {
|
|
||||||
let collaborator =
|
|
||||||
Collaborator::from_proto(collaborator, &user_store, &mut cx).await?;
|
|
||||||
this.update(&mut cx, |this, cx| match this {
|
|
||||||
Worktree::Local(worktree) => worktree.add_collaborator(collaborator, cx),
|
|
||||||
Worktree::Remote(worktree) => worktree.add_collaborator(collaborator, cx),
|
|
||||||
});
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
.log_err()
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn handle_remove_collaborator(
|
|
||||||
&mut self,
|
|
||||||
envelope: TypedEnvelope<proto::RemoveCollaborator>,
|
|
||||||
_: Arc<Client>,
|
|
||||||
cx: &mut ModelContext<Self>,
|
|
||||||
) -> Result<()> {
|
|
||||||
match self {
|
|
||||||
Worktree::Local(worktree) => worktree.remove_collaborator(envelope, cx),
|
|
||||||
Worktree::Remote(worktree) => worktree.remove_collaborator(envelope, cx),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn handle_update(
|
|
||||||
&mut self,
|
|
||||||
envelope: TypedEnvelope<proto::UpdateWorktree>,
|
|
||||||
_: Arc<Client>,
|
|
||||||
cx: &mut ModelContext<Self>,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
self.as_remote_mut()
|
|
||||||
.unwrap()
|
|
||||||
.update_from_remote(envelope, cx)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn handle_open_buffer(
|
pub fn handle_open_buffer(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::OpenBuffer>,
|
envelope: TypedEnvelope<proto::OpenBuffer>,
|
||||||
|
@ -463,13 +336,6 @@ impl Worktree {
|
||||||
.close_remote_buffer(envelope, cx)
|
.close_remote_buffer(envelope, cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn collaborators(&self) -> &HashMap<PeerId, Collaborator> {
|
|
||||||
match self {
|
|
||||||
Worktree::Local(worktree) => &worktree.collaborators,
|
|
||||||
Worktree::Remote(worktree) => &worktree.collaborators,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn diagnostic_summaries<'a>(
|
pub fn diagnostic_summaries<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
) -> impl Iterator<Item = (Arc<Path>, DiagnosticSummary)> + 'a {
|
) -> impl Iterator<Item = (Arc<Path>, DiagnosticSummary)> + 'a {
|
||||||
|
@ -623,9 +489,12 @@ impl Worktree {
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let sender_id = envelope.original_sender_id()?;
|
let sender_id = envelope.original_sender_id()?;
|
||||||
let buffer = self
|
let this = self.as_local().unwrap();
|
||||||
.as_local()
|
let project_id = this
|
||||||
.unwrap()
|
.project_remote_id
|
||||||
|
.ok_or_else(|| anyhow!("can't save buffer while disconnected"))?;
|
||||||
|
|
||||||
|
let buffer = this
|
||||||
.shared_buffers
|
.shared_buffers
|
||||||
.get(&sender_id)
|
.get(&sender_id)
|
||||||
.and_then(|shared_buffers| shared_buffers.get(&envelope.payload.buffer_id).cloned())
|
.and_then(|shared_buffers| shared_buffers.get(&envelope.payload.buffer_id).cloned())
|
||||||
|
@ -646,6 +515,7 @@ impl Worktree {
|
||||||
rpc.respond(
|
rpc.respond(
|
||||||
receipt,
|
receipt,
|
||||||
proto::BufferSaved {
|
proto::BufferSaved {
|
||||||
|
project_id,
|
||||||
worktree_id,
|
worktree_id,
|
||||||
buffer_id,
|
buffer_id,
|
||||||
version: (&version).into(),
|
version: (&version).into(),
|
||||||
|
@ -689,16 +559,6 @@ impl Worktree {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_unshare(
|
|
||||||
&mut self,
|
|
||||||
_: TypedEnvelope<proto::UnshareWorktree>,
|
|
||||||
_: Arc<Client>,
|
|
||||||
cx: &mut ModelContext<Self>,
|
|
||||||
) -> Result<()> {
|
|
||||||
cx.emit(Event::Closed);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll_snapshot(&mut self, cx: &mut ModelContext<Self>) {
|
fn poll_snapshot(&mut self, cx: &mut ModelContext<Self>) {
|
||||||
match self {
|
match self {
|
||||||
Self::Local(worktree) => {
|
Self::Local(worktree) => {
|
||||||
|
@ -905,17 +765,19 @@ impl Worktree {
|
||||||
operation: Operation,
|
operation: Operation,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) {
|
) {
|
||||||
if let Some((rpc, remote_id)) = match self {
|
if let Some((rpc, project_id)) = match self {
|
||||||
Worktree::Local(worktree) => worktree
|
Worktree::Local(worktree) => worktree
|
||||||
.remote_id
|
.project_remote_id
|
||||||
.borrow()
|
|
||||||
.map(|id| (worktree.client.clone(), id)),
|
.map(|id| (worktree.client.clone(), id)),
|
||||||
Worktree::Remote(worktree) => Some((worktree.client.clone(), worktree.remote_id)),
|
Worktree::Remote(worktree) => {
|
||||||
|
Some((worktree.client.clone(), worktree.project_remote_id))
|
||||||
|
}
|
||||||
} {
|
} {
|
||||||
cx.spawn(|worktree, mut cx| async move {
|
cx.spawn(|worktree, mut cx| async move {
|
||||||
if let Err(error) = rpc
|
if let Err(error) = rpc
|
||||||
.request(proto::UpdateBuffer {
|
.request(proto::UpdateBuffer {
|
||||||
worktree_id: remote_id,
|
project_id,
|
||||||
|
worktree_id: worktree.id() as u64,
|
||||||
buffer_id,
|
buffer_id,
|
||||||
operations: vec![language::proto::serialize_operation(&operation)],
|
operations: vec![language::proto::serialize_operation(&operation)],
|
||||||
})
|
})
|
||||||
|
@ -956,16 +818,14 @@ pub struct LocalWorktree {
|
||||||
background_snapshot: Arc<Mutex<Snapshot>>,
|
background_snapshot: Arc<Mutex<Snapshot>>,
|
||||||
last_scan_state_rx: watch::Receiver<ScanState>,
|
last_scan_state_rx: watch::Receiver<ScanState>,
|
||||||
_background_scanner_task: Option<Task<()>>,
|
_background_scanner_task: Option<Task<()>>,
|
||||||
_maintain_remote_id_task: Task<Option<()>>,
|
project_remote_id: Option<u64>,
|
||||||
poll_task: Option<Task<()>>,
|
poll_task: Option<Task<()>>,
|
||||||
remote_id: watch::Receiver<Option<u64>>,
|
|
||||||
share: Option<ShareState>,
|
share: Option<ShareState>,
|
||||||
loading_buffers: LoadingBuffers,
|
loading_buffers: LoadingBuffers,
|
||||||
open_buffers: HashMap<usize, WeakModelHandle<Buffer>>,
|
open_buffers: HashMap<usize, WeakModelHandle<Buffer>>,
|
||||||
shared_buffers: HashMap<PeerId, HashMap<u64, ModelHandle<Buffer>>>,
|
shared_buffers: HashMap<PeerId, HashMap<u64, ModelHandle<Buffer>>>,
|
||||||
diagnostics: HashMap<Arc<Path>, Vec<DiagnosticEntry<PointUtf16>>>,
|
diagnostics: HashMap<Arc<Path>, Vec<DiagnosticEntry<PointUtf16>>>,
|
||||||
diagnostic_summaries: HashMap<Arc<Path>, DiagnosticSummary>,
|
diagnostic_summaries: HashMap<Arc<Path>, DiagnosticSummary>,
|
||||||
collaborators: HashMap<PeerId, Collaborator>,
|
|
||||||
queued_operations: Vec<(u64, Operation)>,
|
queued_operations: Vec<(u64, Operation)>,
|
||||||
languages: Arc<LanguageRegistry>,
|
languages: Arc<LanguageRegistry>,
|
||||||
client: Arc<Client>,
|
client: Arc<Client>,
|
||||||
|
@ -976,10 +836,10 @@ pub struct LocalWorktree {
|
||||||
|
|
||||||
struct ShareState {
|
struct ShareState {
|
||||||
snapshots_tx: Sender<Snapshot>,
|
snapshots_tx: Sender<Snapshot>,
|
||||||
_subscriptions: Vec<client::Subscription>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RemoteWorktree {
|
pub struct RemoteWorktree {
|
||||||
|
project_remote_id: u64,
|
||||||
remote_id: u64,
|
remote_id: u64,
|
||||||
snapshot: Snapshot,
|
snapshot: Snapshot,
|
||||||
snapshot_rx: watch::Receiver<Snapshot>,
|
snapshot_rx: watch::Receiver<Snapshot>,
|
||||||
|
@ -988,12 +848,10 @@ pub struct RemoteWorktree {
|
||||||
replica_id: ReplicaId,
|
replica_id: ReplicaId,
|
||||||
loading_buffers: LoadingBuffers,
|
loading_buffers: LoadingBuffers,
|
||||||
open_buffers: HashMap<usize, RemoteBuffer>,
|
open_buffers: HashMap<usize, RemoteBuffer>,
|
||||||
collaborators: HashMap<PeerId, Collaborator>,
|
|
||||||
diagnostic_summaries: HashMap<Arc<Path>, DiagnosticSummary>,
|
diagnostic_summaries: HashMap<Arc<Path>, DiagnosticSummary>,
|
||||||
languages: Arc<LanguageRegistry>,
|
languages: Arc<LanguageRegistry>,
|
||||||
user_store: ModelHandle<UserStore>,
|
user_store: ModelHandle<UserStore>,
|
||||||
queued_operations: Vec<(u64, Operation)>,
|
queued_operations: Vec<(u64, Operation)>,
|
||||||
_subscriptions: Vec<client::Subscription>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type LoadingBuffers = HashMap<
|
type LoadingBuffers = HashMap<
|
||||||
|
@ -1061,48 +919,13 @@ impl LocalWorktree {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (mut remote_id_tx, remote_id_rx) = watch::channel();
|
|
||||||
let _maintain_remote_id_task = cx.spawn_weak({
|
|
||||||
let rpc = client.clone();
|
|
||||||
move |this, cx| {
|
|
||||||
async move {
|
|
||||||
let mut status = rpc.status();
|
|
||||||
while let Some(status) = status.recv().await {
|
|
||||||
if let Some(this) = this.upgrade(&cx) {
|
|
||||||
let remote_id = if let client::Status::Connected { .. } = status {
|
|
||||||
let authorized_logins = this.read_with(&cx, |this, _| {
|
|
||||||
this.as_local().unwrap().config.collaborators.clone()
|
|
||||||
});
|
|
||||||
let response = rpc
|
|
||||||
.request(proto::OpenWorktree {
|
|
||||||
root_name: root_name.clone(),
|
|
||||||
authorized_logins,
|
|
||||||
})
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Some(response.worktree_id)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
if remote_id_tx.send(remote_id).await.is_err() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
.log_err()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let tree = Self {
|
let tree = Self {
|
||||||
snapshot: snapshot.clone(),
|
snapshot: snapshot.clone(),
|
||||||
config,
|
config,
|
||||||
remote_id: remote_id_rx,
|
project_remote_id: None,
|
||||||
background_snapshot: Arc::new(Mutex::new(snapshot)),
|
background_snapshot: Arc::new(Mutex::new(snapshot)),
|
||||||
last_scan_state_rx,
|
last_scan_state_rx,
|
||||||
_background_scanner_task: None,
|
_background_scanner_task: None,
|
||||||
_maintain_remote_id_task,
|
|
||||||
share: None,
|
share: None,
|
||||||
poll_task: None,
|
poll_task: None,
|
||||||
loading_buffers: Default::default(),
|
loading_buffers: Default::default(),
|
||||||
|
@ -1111,7 +934,6 @@ impl LocalWorktree {
|
||||||
diagnostics: Default::default(),
|
diagnostics: Default::default(),
|
||||||
diagnostic_summaries: Default::default(),
|
diagnostic_summaries: Default::default(),
|
||||||
queued_operations: Default::default(),
|
queued_operations: Default::default(),
|
||||||
collaborators: Default::default(),
|
|
||||||
languages,
|
languages,
|
||||||
client,
|
client,
|
||||||
user_store,
|
user_store,
|
||||||
|
@ -1152,6 +974,10 @@ impl LocalWorktree {
|
||||||
Ok((tree, scan_states_tx))
|
Ok((tree, scan_states_tx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_project_remote_id(&mut self, id: Option<u64>) {
|
||||||
|
self.project_remote_id = id;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn languages(&self) -> &LanguageRegistry {
|
pub fn languages(&self) -> &LanguageRegistry {
|
||||||
&self.languages
|
&self.languages
|
||||||
}
|
}
|
||||||
|
@ -1297,27 +1123,12 @@ impl LocalWorktree {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_collaborator(
|
|
||||||
&mut self,
|
|
||||||
collaborator: Collaborator,
|
|
||||||
cx: &mut ModelContext<Worktree>,
|
|
||||||
) {
|
|
||||||
self.collaborators
|
|
||||||
.insert(collaborator.peer_id, collaborator);
|
|
||||||
cx.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove_collaborator(
|
pub fn remove_collaborator(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::RemoveCollaborator>,
|
peer_id: PeerId,
|
||||||
|
replica_id: ReplicaId,
|
||||||
cx: &mut ModelContext<Worktree>,
|
cx: &mut ModelContext<Worktree>,
|
||||||
) -> Result<()> {
|
) {
|
||||||
let peer_id = PeerId(envelope.payload.peer_id);
|
|
||||||
let replica_id = self
|
|
||||||
.collaborators
|
|
||||||
.remove(&peer_id)
|
|
||||||
.ok_or_else(|| anyhow!("unknown peer {:?}", peer_id))?
|
|
||||||
.replica_id;
|
|
||||||
self.shared_buffers.remove(&peer_id);
|
self.shared_buffers.remove(&peer_id);
|
||||||
for (_, buffer) in &self.open_buffers {
|
for (_, buffer) in &self.open_buffers {
|
||||||
if let Some(buffer) = buffer.upgrade(cx) {
|
if let Some(buffer) = buffer.upgrade(cx) {
|
||||||
|
@ -1325,8 +1136,6 @@ impl LocalWorktree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cx.notify();
|
cx.notify();
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scan_complete(&self) -> impl Future<Output = ()> {
|
pub fn scan_complete(&self) -> impl Future<Output = ()> {
|
||||||
|
@ -1339,22 +1148,6 @@ impl LocalWorktree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remote_id(&self) -> Option<u64> {
|
|
||||||
*self.remote_id.borrow()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn next_remote_id(&self) -> impl Future<Output = Option<u64>> {
|
|
||||||
let mut remote_id = self.remote_id.clone();
|
|
||||||
async move {
|
|
||||||
while let Some(remote_id) = remote_id.recv().await {
|
|
||||||
if remote_id.is_some() {
|
|
||||||
return remote_id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_scanning(&self) -> bool {
|
fn is_scanning(&self) -> bool {
|
||||||
if let ScanState::Scanning = *self.last_scan_state_rx.borrow() {
|
if let ScanState::Scanning = *self.last_scan_state_rx.borrow() {
|
||||||
true
|
true
|
||||||
|
@ -1456,31 +1249,28 @@ impl LocalWorktree {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn share(&mut self, cx: &mut ModelContext<Worktree>) -> Task<anyhow::Result<u64>> {
|
pub fn share(&mut self, cx: &mut ModelContext<Worktree>) -> Task<anyhow::Result<()>> {
|
||||||
let snapshot = self.snapshot();
|
let snapshot = self.snapshot();
|
||||||
let share_request = self.share_request(cx);
|
|
||||||
let rpc = self.client.clone();
|
let rpc = self.client.clone();
|
||||||
|
let project_id = self.project_remote_id;
|
||||||
|
let worktree_id = cx.model_id() as u64;
|
||||||
cx.spawn(|this, mut cx| async move {
|
cx.spawn(|this, mut cx| async move {
|
||||||
let share_request = if let Some(request) = share_request.await {
|
let project_id = project_id.ok_or_else(|| anyhow!("no project id"))?;
|
||||||
request
|
|
||||||
} else {
|
|
||||||
return Err(anyhow!("failed to open worktree on the server"));
|
|
||||||
};
|
|
||||||
|
|
||||||
let remote_id = share_request.worktree.as_ref().unwrap().id;
|
|
||||||
let share_response = rpc.request(share_request).await?;
|
|
||||||
|
|
||||||
log::info!("sharing worktree {:?}", share_response);
|
|
||||||
let (snapshots_to_send_tx, snapshots_to_send_rx) =
|
let (snapshots_to_send_tx, snapshots_to_send_rx) =
|
||||||
smol::channel::unbounded::<Snapshot>();
|
smol::channel::unbounded::<Snapshot>();
|
||||||
|
|
||||||
cx.background()
|
cx.background()
|
||||||
.spawn({
|
.spawn({
|
||||||
let rpc = rpc.clone();
|
let rpc = rpc.clone();
|
||||||
async move {
|
async move {
|
||||||
let mut prev_snapshot = snapshot;
|
let mut prev_snapshot = snapshot;
|
||||||
while let Ok(snapshot) = snapshots_to_send_rx.recv().await {
|
while let Ok(snapshot) = snapshots_to_send_rx.recv().await {
|
||||||
let message = snapshot.build_update(&prev_snapshot, remote_id, false);
|
let message = snapshot.build_update(
|
||||||
|
&prev_snapshot,
|
||||||
|
project_id,
|
||||||
|
worktree_id,
|
||||||
|
false,
|
||||||
|
);
|
||||||
match rpc.send(message).await {
|
match rpc.send(message).await {
|
||||||
Ok(()) => prev_snapshot = snapshot,
|
Ok(()) => prev_snapshot = snapshot,
|
||||||
Err(err) => log::error!("error sending snapshot diff {}", err),
|
Err(err) => log::error!("error sending snapshot diff {}", err),
|
||||||
|
@ -1491,64 +1281,32 @@ impl LocalWorktree {
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
this.update(&mut cx, |worktree, cx| {
|
this.update(&mut cx, |worktree, cx| {
|
||||||
let _subscriptions = vec![
|
|
||||||
rpc.subscribe_to_entity(remote_id, cx, Worktree::handle_add_collaborator),
|
|
||||||
rpc.subscribe_to_entity(remote_id, cx, Worktree::handle_remove_collaborator),
|
|
||||||
rpc.subscribe_to_entity(remote_id, cx, Worktree::handle_open_buffer),
|
|
||||||
rpc.subscribe_to_entity(remote_id, cx, Worktree::handle_close_buffer),
|
|
||||||
rpc.subscribe_to_entity(remote_id, cx, Worktree::handle_update_buffer),
|
|
||||||
rpc.subscribe_to_entity(remote_id, cx, Worktree::handle_save_buffer),
|
|
||||||
];
|
|
||||||
|
|
||||||
let worktree = worktree.as_local_mut().unwrap();
|
let worktree = worktree.as_local_mut().unwrap();
|
||||||
worktree.share = Some(ShareState {
|
worktree.share = Some(ShareState {
|
||||||
snapshots_tx: snapshots_to_send_tx,
|
snapshots_tx: snapshots_to_send_tx,
|
||||||
_subscriptions,
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(remote_id)
|
Ok(())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unshare(&mut self, cx: &mut ModelContext<Worktree>) {
|
fn to_proto(&self, cx: &mut ModelContext<Worktree>) -> impl Future<Output = proto::Worktree> {
|
||||||
self.share.take();
|
let id = cx.model_id() as u64;
|
||||||
let rpc = self.client.clone();
|
|
||||||
let remote_id = self.remote_id();
|
|
||||||
cx.foreground()
|
|
||||||
.spawn(
|
|
||||||
async move {
|
|
||||||
if let Some(worktree_id) = remote_id {
|
|
||||||
rpc.send(proto::UnshareWorktree { worktree_id }).await?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
.log_err(),
|
|
||||||
)
|
|
||||||
.detach()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn share_request(&self, cx: &mut ModelContext<Worktree>) -> Task<Option<proto::ShareWorktree>> {
|
|
||||||
let remote_id = self.next_remote_id();
|
|
||||||
let snapshot = self.snapshot();
|
let snapshot = self.snapshot();
|
||||||
let root_name = self.root_name.clone();
|
let root_name = self.root_name.clone();
|
||||||
cx.background().spawn(async move {
|
async move {
|
||||||
remote_id.await.map(|id| {
|
proto::Worktree {
|
||||||
let entries = snapshot
|
id,
|
||||||
|
root_name,
|
||||||
|
entries: snapshot
|
||||||
.entries_by_path
|
.entries_by_path
|
||||||
.cursor::<()>()
|
.cursor::<()>()
|
||||||
.filter(|e| !e.is_ignored)
|
.filter(|e| !e.is_ignored)
|
||||||
.map(Into::into)
|
.map(Into::into)
|
||||||
.collect();
|
.collect(),
|
||||||
proto::ShareWorktree {
|
}
|
||||||
worktree: Some(proto::Worktree {
|
}
|
||||||
id,
|
|
||||||
root_name,
|
|
||||||
entries,
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1617,6 +1375,7 @@ impl RemoteWorktree {
|
||||||
) -> Task<Result<ModelHandle<Buffer>>> {
|
) -> Task<Result<ModelHandle<Buffer>>> {
|
||||||
let rpc = self.client.clone();
|
let rpc = self.client.clone();
|
||||||
let replica_id = self.replica_id;
|
let replica_id = self.replica_id;
|
||||||
|
let project_id = self.project_remote_id;
|
||||||
let remote_worktree_id = self.remote_id;
|
let remote_worktree_id = self.remote_id;
|
||||||
let root_path = self.snapshot.abs_path.clone();
|
let root_path = self.snapshot.abs_path.clone();
|
||||||
let path: Arc<Path> = Arc::from(path);
|
let path: Arc<Path> = Arc::from(path);
|
||||||
|
@ -1629,6 +1388,7 @@ impl RemoteWorktree {
|
||||||
.ok_or_else(|| anyhow!("file does not exist"))?;
|
.ok_or_else(|| anyhow!("file does not exist"))?;
|
||||||
let response = rpc
|
let response = rpc
|
||||||
.request(proto::OpenBuffer {
|
.request(proto::OpenBuffer {
|
||||||
|
project_id,
|
||||||
worktree_id: remote_worktree_id as u64,
|
worktree_id: remote_worktree_id as u64,
|
||||||
path: path_string,
|
path: path_string,
|
||||||
})
|
})
|
||||||
|
@ -1669,10 +1429,6 @@ impl RemoteWorktree {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remote_id(&self) -> u64 {
|
|
||||||
self.remote_id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn close_all_buffers(&mut self, cx: &mut MutableAppContext) {
|
pub fn close_all_buffers(&mut self, cx: &mut MutableAppContext) {
|
||||||
for (_, buffer) in self.open_buffers.drain() {
|
for (_, buffer) in self.open_buffers.drain() {
|
||||||
if let RemoteBuffer::Loaded(buffer) = buffer {
|
if let RemoteBuffer::Loaded(buffer) = buffer {
|
||||||
|
@ -1703,34 +1459,18 @@ impl RemoteWorktree {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_collaborator(
|
|
||||||
&mut self,
|
|
||||||
collaborator: Collaborator,
|
|
||||||
cx: &mut ModelContext<Worktree>,
|
|
||||||
) {
|
|
||||||
self.collaborators
|
|
||||||
.insert(collaborator.peer_id, collaborator);
|
|
||||||
cx.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove_collaborator(
|
pub fn remove_collaborator(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::RemoveCollaborator>,
|
peer_id: PeerId,
|
||||||
|
replica_id: ReplicaId,
|
||||||
cx: &mut ModelContext<Worktree>,
|
cx: &mut ModelContext<Worktree>,
|
||||||
) -> Result<()> {
|
) {
|
||||||
let peer_id = PeerId(envelope.payload.peer_id);
|
|
||||||
let replica_id = self
|
|
||||||
.collaborators
|
|
||||||
.remove(&peer_id)
|
|
||||||
.ok_or_else(|| anyhow!("unknown peer {:?}", peer_id))?
|
|
||||||
.replica_id;
|
|
||||||
for (_, buffer) in &self.open_buffers {
|
for (_, buffer) in &self.open_buffers {
|
||||||
if let Some(buffer) = buffer.upgrade(cx) {
|
if let Some(buffer) = buffer.upgrade(cx) {
|
||||||
buffer.update(cx, |buffer, cx| buffer.remove_peer(replica_id, cx));
|
buffer.update(cx, |buffer, cx| buffer.remove_peer(replica_id, cx));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cx.notify();
|
cx.notify();
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1756,6 +1496,7 @@ impl Snapshot {
|
||||||
pub fn build_update(
|
pub fn build_update(
|
||||||
&self,
|
&self,
|
||||||
other: &Self,
|
other: &Self,
|
||||||
|
project_id: u64,
|
||||||
worktree_id: u64,
|
worktree_id: u64,
|
||||||
include_ignored: bool,
|
include_ignored: bool,
|
||||||
) -> proto::UpdateWorktree {
|
) -> proto::UpdateWorktree {
|
||||||
|
@ -1809,9 +1550,10 @@ impl Snapshot {
|
||||||
}
|
}
|
||||||
|
|
||||||
proto::UpdateWorktree {
|
proto::UpdateWorktree {
|
||||||
|
project_id,
|
||||||
|
worktree_id,
|
||||||
updated_entries,
|
updated_entries,
|
||||||
removed_entries,
|
removed_entries,
|
||||||
worktree_id,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2168,15 +1910,17 @@ impl language::File for File {
|
||||||
version: clock::Global,
|
version: clock::Global,
|
||||||
cx: &mut MutableAppContext,
|
cx: &mut MutableAppContext,
|
||||||
) -> Task<Result<(clock::Global, SystemTime)>> {
|
) -> Task<Result<(clock::Global, SystemTime)>> {
|
||||||
|
let worktree_id = self.worktree.id() as u64;
|
||||||
self.worktree.update(cx, |worktree, cx| match worktree {
|
self.worktree.update(cx, |worktree, cx| match worktree {
|
||||||
Worktree::Local(worktree) => {
|
Worktree::Local(worktree) => {
|
||||||
let rpc = worktree.client.clone();
|
let rpc = worktree.client.clone();
|
||||||
let worktree_id = *worktree.remote_id.borrow();
|
let project_id = worktree.project_remote_id;
|
||||||
let save = worktree.save(self.path.clone(), text, cx);
|
let save = worktree.save(self.path.clone(), text, cx);
|
||||||
cx.background().spawn(async move {
|
cx.background().spawn(async move {
|
||||||
let entry = save.await?;
|
let entry = save.await?;
|
||||||
if let Some(worktree_id) = worktree_id {
|
if let Some(project_id) = project_id {
|
||||||
rpc.send(proto::BufferSaved {
|
rpc.send(proto::BufferSaved {
|
||||||
|
project_id,
|
||||||
worktree_id,
|
worktree_id,
|
||||||
buffer_id,
|
buffer_id,
|
||||||
version: (&version).into(),
|
version: (&version).into(),
|
||||||
|
@ -2189,10 +1933,11 @@ impl language::File for File {
|
||||||
}
|
}
|
||||||
Worktree::Remote(worktree) => {
|
Worktree::Remote(worktree) => {
|
||||||
let rpc = worktree.client.clone();
|
let rpc = worktree.client.clone();
|
||||||
let worktree_id = worktree.remote_id;
|
let project_id = worktree.project_remote_id;
|
||||||
cx.foreground().spawn(async move {
|
cx.foreground().spawn(async move {
|
||||||
let response = rpc
|
let response = rpc
|
||||||
.request(proto::SaveBuffer {
|
.request(proto::SaveBuffer {
|
||||||
|
project_id,
|
||||||
worktree_id,
|
worktree_id,
|
||||||
buffer_id,
|
buffer_id,
|
||||||
})
|
})
|
||||||
|
@ -2225,14 +1970,16 @@ impl language::File for File {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext) {
|
fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext) {
|
||||||
|
let worktree_id = self.worktree.id() as u64;
|
||||||
self.worktree.update(cx, |worktree, cx| {
|
self.worktree.update(cx, |worktree, cx| {
|
||||||
if let Worktree::Remote(worktree) = worktree {
|
if let Worktree::Remote(worktree) = worktree {
|
||||||
let worktree_id = worktree.remote_id;
|
let project_id = worktree.project_remote_id;
|
||||||
let rpc = worktree.client.clone();
|
let rpc = worktree.client.clone();
|
||||||
cx.background()
|
cx.background()
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
if let Err(error) = rpc
|
if let Err(error) = rpc
|
||||||
.send(proto::CloseBuffer {
|
.send(proto::CloseBuffer {
|
||||||
|
project_id,
|
||||||
worktree_id,
|
worktree_id,
|
||||||
buffer_id,
|
buffer_id,
|
||||||
})
|
})
|
||||||
|
@ -3370,9 +3117,7 @@ mod tests {
|
||||||
// Create a remote copy of this worktree.
|
// Create a remote copy of this worktree.
|
||||||
let initial_snapshot = tree.read_with(&cx, |tree, _| tree.snapshot());
|
let initial_snapshot = tree.read_with(&cx, |tree, _| tree.snapshot());
|
||||||
let worktree_id = 1;
|
let worktree_id = 1;
|
||||||
let share_request = tree.update(&mut cx, |tree, cx| {
|
let proto_message = tree.update(&mut cx, |tree, cx| tree.as_local().unwrap().to_proto(cx));
|
||||||
tree.as_local().unwrap().share_request(cx)
|
|
||||||
});
|
|
||||||
let open_worktree = server.receive::<proto::OpenWorktree>().await.unwrap();
|
let open_worktree = server.receive::<proto::OpenWorktree>().await.unwrap();
|
||||||
server
|
server
|
||||||
.respond(
|
.respond(
|
||||||
|
@ -3383,7 +3128,7 @@ mod tests {
|
||||||
|
|
||||||
let remote = Worktree::remote(
|
let remote = Worktree::remote(
|
||||||
proto::JoinWorktreeResponse {
|
proto::JoinWorktreeResponse {
|
||||||
worktree: share_request.await.unwrap().worktree,
|
worktree: Some(proto_message.await),
|
||||||
replica_id: 1,
|
replica_id: 1,
|
||||||
collaborators: Vec::new(),
|
collaborators: Vec::new(),
|
||||||
},
|
},
|
||||||
|
|
|
@ -9,38 +9,46 @@ message Envelope {
|
||||||
Ack ack = 4;
|
Ack ack = 4;
|
||||||
Error error = 5;
|
Error error = 5;
|
||||||
Ping ping = 6;
|
Ping ping = 6;
|
||||||
ShareWorktree share_worktree = 7;
|
|
||||||
ShareWorktreeResponse share_worktree_response = 8;
|
RegisterProject register_project = 7;
|
||||||
JoinWorktree join_worktree = 9;
|
RegisterProjectResponse register_project_response = 8;
|
||||||
JoinWorktreeResponse join_worktree_response = 10;
|
UnregisterProject unregister_project = 9;
|
||||||
UpdateWorktree update_worktree = 11;
|
ShareProject share_project = 10;
|
||||||
CloseWorktree close_worktree = 12;
|
UnshareProject unshare_project = 11;
|
||||||
OpenBuffer open_buffer = 13;
|
JoinProject join_project = 12;
|
||||||
OpenBufferResponse open_buffer_response = 14;
|
JoinProjectResponse join_project_response = 13;
|
||||||
CloseBuffer close_buffer = 15;
|
LeaveProject leave_project = 14;
|
||||||
UpdateBuffer update_buffer = 16;
|
AddProjectCollaborator add_project_collaborator = 15;
|
||||||
SaveBuffer save_buffer = 17;
|
RemoveProjectCollaborator remove_project_collaborator = 16;
|
||||||
BufferSaved buffer_saved = 18;
|
|
||||||
AddCollaborator add_collaborator = 19;
|
RegisterWorktree register_worktree = 17;
|
||||||
RemoveCollaborator remove_collaborator = 20;
|
UnregisterWorktree unregister_worktree = 18;
|
||||||
GetChannels get_channels = 21;
|
ShareWorktree share_worktree = 100;
|
||||||
GetChannelsResponse get_channels_response = 22;
|
UpdateWorktree update_worktree = 19;
|
||||||
GetUsers get_users = 23;
|
UpdateDiagnosticSummary update_diagnostic_summary = 20;
|
||||||
GetUsersResponse get_users_response = 24;
|
|
||||||
JoinChannel join_channel = 25;
|
OpenBuffer open_buffer = 22;
|
||||||
JoinChannelResponse join_channel_response = 26;
|
OpenBufferResponse open_buffer_response = 23;
|
||||||
LeaveChannel leave_channel = 27;
|
CloseBuffer close_buffer = 24;
|
||||||
SendChannelMessage send_channel_message = 28;
|
UpdateBuffer update_buffer = 25;
|
||||||
SendChannelMessageResponse send_channel_message_response = 29;
|
SaveBuffer save_buffer = 26;
|
||||||
ChannelMessageSent channel_message_sent = 30;
|
BufferSaved buffer_saved = 27;
|
||||||
GetChannelMessages get_channel_messages = 31;
|
|
||||||
GetChannelMessagesResponse get_channel_messages_response = 32;
|
GetChannels get_channels = 28;
|
||||||
OpenWorktree open_worktree = 33;
|
GetChannelsResponse get_channels_response = 29;
|
||||||
OpenWorktreeResponse open_worktree_response = 34;
|
JoinChannel join_channel = 30;
|
||||||
UnshareWorktree unshare_worktree = 35;
|
JoinChannelResponse join_channel_response = 31;
|
||||||
UpdateContacts update_contacts = 36;
|
LeaveChannel leave_channel = 32;
|
||||||
LeaveWorktree leave_worktree = 37;
|
SendChannelMessage send_channel_message = 33;
|
||||||
UpdateDiagnosticSummary update_diagnostic_summary = 38;
|
SendChannelMessageResponse send_channel_message_response = 34;
|
||||||
|
ChannelMessageSent channel_message_sent = 35;
|
||||||
|
GetChannelMessages get_channel_messages = 36;
|
||||||
|
GetChannelMessagesResponse get_channel_messages_response = 37;
|
||||||
|
|
||||||
|
UpdateContacts update_contacts = 38;
|
||||||
|
|
||||||
|
GetUsers get_users = 39;
|
||||||
|
GetUsersResponse get_users_response = 40;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,62 +62,76 @@ message Error {
|
||||||
string message = 1;
|
string message = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message OpenWorktree {
|
message RegisterProject {}
|
||||||
string root_name = 1;
|
|
||||||
repeated string authorized_logins = 2;
|
message RegisterProjectResponse {
|
||||||
|
uint64 project_id = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message OpenWorktreeResponse {
|
message UnregisterProject {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message ShareWorktree {
|
message ShareProject {
|
||||||
Worktree worktree = 1;
|
uint64 project_id = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message ShareWorktreeResponse {}
|
message UnshareProject {
|
||||||
|
uint64 project_id = 1;
|
||||||
message UnshareWorktree {
|
|
||||||
uint64 worktree_id = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message JoinWorktree {
|
message JoinProject {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message LeaveWorktree {
|
message JoinProjectResponse {
|
||||||
uint64 worktree_id = 1;
|
uint32 replica_id = 2;
|
||||||
}
|
repeated Worktree worktrees = 3;
|
||||||
|
|
||||||
message JoinWorktreeResponse {
|
|
||||||
Worktree worktree = 2;
|
|
||||||
uint32 replica_id = 3;
|
|
||||||
repeated Collaborator collaborators = 4;
|
repeated Collaborator collaborators = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message LeaveProject {
|
||||||
|
uint64 project_id = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message RegisterWorktree {
|
||||||
|
uint64 project_id = 1;
|
||||||
|
uint64 worktree_id = 2;
|
||||||
|
string root_name = 3;
|
||||||
|
repeated string authorized_logins = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
message UnregisterWorktree {
|
||||||
|
uint64 project_id = 1;
|
||||||
|
uint64 worktree_id = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ShareWorktree {
|
||||||
|
uint64 project_id = 1;
|
||||||
|
Worktree worktree = 2;
|
||||||
|
}
|
||||||
|
|
||||||
message UpdateWorktree {
|
message UpdateWorktree {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
repeated Entry updated_entries = 2;
|
uint64 worktree_id = 2;
|
||||||
repeated uint64 removed_entries = 3;
|
repeated Entry updated_entries = 3;
|
||||||
|
repeated uint64 removed_entries = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
message CloseWorktree {
|
message AddProjectCollaborator {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
}
|
|
||||||
|
|
||||||
message AddCollaborator {
|
|
||||||
uint64 worktree_id = 1;
|
|
||||||
Collaborator collaborator = 2;
|
Collaborator collaborator = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
message RemoveCollaborator {
|
message RemoveProjectCollaborator {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
uint32 peer_id = 2;
|
uint32 peer_id = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
message OpenBuffer {
|
message OpenBuffer {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
string path = 2;
|
uint64 worktree_id = 2;
|
||||||
|
string path = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
message OpenBufferResponse {
|
message OpenBufferResponse {
|
||||||
|
@ -117,33 +139,38 @@ message OpenBufferResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
message CloseBuffer {
|
message CloseBuffer {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
uint64 buffer_id = 2;
|
uint64 worktree_id = 2;
|
||||||
|
uint64 buffer_id = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
message UpdateBuffer {
|
message UpdateBuffer {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
uint64 buffer_id = 2;
|
uint64 worktree_id = 2;
|
||||||
repeated Operation operations = 3;
|
uint64 buffer_id = 3;
|
||||||
|
repeated Operation operations = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
message SaveBuffer {
|
message SaveBuffer {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
uint64 buffer_id = 2;
|
uint64 worktree_id = 2;
|
||||||
|
uint64 buffer_id = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
message BufferSaved {
|
message BufferSaved {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
uint64 buffer_id = 2;
|
uint64 worktree_id = 2;
|
||||||
repeated VectorClockEntry version = 3;
|
uint64 buffer_id = 3;
|
||||||
Timestamp mtime = 4;
|
repeated VectorClockEntry version = 4;
|
||||||
|
Timestamp mtime = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message UpdateDiagnosticSummary {
|
message UpdateDiagnosticSummary {
|
||||||
uint64 worktree_id = 1;
|
uint64 project_id = 1;
|
||||||
string path = 2;
|
uint64 worktree_id = 2;
|
||||||
uint32 error_count = 3;
|
string path = 3;
|
||||||
uint32 warning_count = 4;
|
uint32 error_count = 4;
|
||||||
|
uint32 warning_count = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message GetChannels {}
|
message GetChannels {}
|
||||||
|
@ -368,12 +395,12 @@ message ChannelMessage {
|
||||||
|
|
||||||
message Contact {
|
message Contact {
|
||||||
uint64 user_id = 1;
|
uint64 user_id = 1;
|
||||||
repeated WorktreeMetadata worktrees = 2;
|
repeated ProjectMetadata projects = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
message WorktreeMetadata {
|
message ProjectMetadata {
|
||||||
uint64 id = 1;
|
uint64 id = 1;
|
||||||
string root_name = 2;
|
bool is_shared = 2;
|
||||||
bool is_shared = 3;
|
repeated string worktree_root_names = 3;
|
||||||
repeated uint64 guests = 4;
|
repeated uint64 guests = 4;
|
||||||
}
|
}
|
||||||
|
|
|
@ -388,6 +388,7 @@ mod tests {
|
||||||
.request(
|
.request(
|
||||||
client1_conn_id,
|
client1_conn_id,
|
||||||
proto::OpenBuffer {
|
proto::OpenBuffer {
|
||||||
|
project_id: 0,
|
||||||
worktree_id: 1,
|
worktree_id: 1,
|
||||||
path: "path/one".to_string(),
|
path: "path/one".to_string(),
|
||||||
},
|
},
|
||||||
|
@ -410,6 +411,7 @@ mod tests {
|
||||||
.request(
|
.request(
|
||||||
client2_conn_id,
|
client2_conn_id,
|
||||||
proto::OpenBuffer {
|
proto::OpenBuffer {
|
||||||
|
project_id: 0,
|
||||||
worktree_id: 2,
|
worktree_id: 2,
|
||||||
path: "path/two".to_string(),
|
path: "path/two".to_string(),
|
||||||
},
|
},
|
||||||
|
|
|
@ -121,68 +121,70 @@ macro_rules! entity_messages {
|
||||||
|
|
||||||
messages!(
|
messages!(
|
||||||
Ack,
|
Ack,
|
||||||
AddCollaborator,
|
AddProjectCollaborator,
|
||||||
BufferSaved,
|
BufferSaved,
|
||||||
ChannelMessageSent,
|
ChannelMessageSent,
|
||||||
CloseBuffer,
|
CloseBuffer,
|
||||||
CloseWorktree,
|
|
||||||
Error,
|
Error,
|
||||||
GetChannelMessages,
|
GetChannelMessages,
|
||||||
GetChannelMessagesResponse,
|
GetChannelMessagesResponse,
|
||||||
GetChannels,
|
GetChannels,
|
||||||
GetChannelsResponse,
|
GetChannelsResponse,
|
||||||
UpdateContacts,
|
|
||||||
GetUsers,
|
GetUsers,
|
||||||
GetUsersResponse,
|
GetUsersResponse,
|
||||||
JoinChannel,
|
JoinChannel,
|
||||||
JoinChannelResponse,
|
JoinChannelResponse,
|
||||||
JoinWorktree,
|
JoinProject,
|
||||||
JoinWorktreeResponse,
|
JoinProjectResponse,
|
||||||
LeaveChannel,
|
LeaveChannel,
|
||||||
LeaveWorktree,
|
LeaveProject,
|
||||||
OpenBuffer,
|
OpenBuffer,
|
||||||
OpenBufferResponse,
|
OpenBufferResponse,
|
||||||
OpenWorktree,
|
RegisterProjectResponse,
|
||||||
OpenWorktreeResponse,
|
|
||||||
Ping,
|
Ping,
|
||||||
RemoveCollaborator,
|
RegisterProject,
|
||||||
|
RegisterWorktree,
|
||||||
|
RemoveProjectCollaborator,
|
||||||
SaveBuffer,
|
SaveBuffer,
|
||||||
SendChannelMessage,
|
SendChannelMessage,
|
||||||
SendChannelMessageResponse,
|
SendChannelMessageResponse,
|
||||||
|
ShareProject,
|
||||||
ShareWorktree,
|
ShareWorktree,
|
||||||
ShareWorktreeResponse,
|
UnregisterProject,
|
||||||
UnshareWorktree,
|
UnregisterWorktree,
|
||||||
UpdateBuffer,
|
UpdateBuffer,
|
||||||
|
UpdateContacts,
|
||||||
UpdateWorktree,
|
UpdateWorktree,
|
||||||
);
|
);
|
||||||
|
|
||||||
request_messages!(
|
request_messages!(
|
||||||
|
(GetChannelMessages, GetChannelMessagesResponse),
|
||||||
(GetChannels, GetChannelsResponse),
|
(GetChannels, GetChannelsResponse),
|
||||||
(GetUsers, GetUsersResponse),
|
(GetUsers, GetUsersResponse),
|
||||||
(JoinChannel, JoinChannelResponse),
|
(JoinChannel, JoinChannelResponse),
|
||||||
|
(JoinProject, JoinProjectResponse),
|
||||||
(OpenBuffer, OpenBufferResponse),
|
(OpenBuffer, OpenBufferResponse),
|
||||||
(JoinWorktree, JoinWorktreeResponse),
|
|
||||||
(OpenWorktree, OpenWorktreeResponse),
|
|
||||||
(Ping, Ack),
|
(Ping, Ack),
|
||||||
|
(RegisterProject, RegisterProjectResponse),
|
||||||
|
(RegisterWorktree, Ack),
|
||||||
(SaveBuffer, BufferSaved),
|
(SaveBuffer, BufferSaved),
|
||||||
(UpdateBuffer, Ack),
|
|
||||||
(ShareWorktree, ShareWorktreeResponse),
|
|
||||||
(UnshareWorktree, Ack),
|
|
||||||
(SendChannelMessage, SendChannelMessageResponse),
|
(SendChannelMessage, SendChannelMessageResponse),
|
||||||
(GetChannelMessages, GetChannelMessagesResponse),
|
(ShareWorktree, Ack),
|
||||||
|
(UpdateBuffer, Ack),
|
||||||
);
|
);
|
||||||
|
|
||||||
entity_messages!(
|
entity_messages!(
|
||||||
worktree_id,
|
project_id,
|
||||||
AddCollaborator,
|
AddProjectCollaborator,
|
||||||
|
RemoveProjectCollaborator,
|
||||||
|
JoinProject,
|
||||||
|
LeaveProject,
|
||||||
BufferSaved,
|
BufferSaved,
|
||||||
CloseBuffer,
|
|
||||||
CloseWorktree,
|
|
||||||
OpenBuffer,
|
OpenBuffer,
|
||||||
JoinWorktree,
|
CloseBuffer,
|
||||||
RemoveCollaborator,
|
|
||||||
SaveBuffer,
|
SaveBuffer,
|
||||||
UnshareWorktree,
|
RegisterWorktree,
|
||||||
|
UnregisterWorktree,
|
||||||
UpdateBuffer,
|
UpdateBuffer,
|
||||||
UpdateWorktree,
|
UpdateWorktree,
|
||||||
);
|
);
|
||||||
|
|
|
@ -60,8 +60,8 @@ impl Server {
|
||||||
|
|
||||||
server
|
server
|
||||||
.add_handler(Server::ping)
|
.add_handler(Server::ping)
|
||||||
.add_handler(Server::open_worktree)
|
.add_handler(Server::register_worktree)
|
||||||
.add_handler(Server::close_worktree)
|
.add_handler(Server::unregister_worktree)
|
||||||
.add_handler(Server::share_worktree)
|
.add_handler(Server::share_worktree)
|
||||||
.add_handler(Server::unshare_worktree)
|
.add_handler(Server::unshare_worktree)
|
||||||
.add_handler(Server::join_worktree)
|
.add_handler(Server::join_worktree)
|
||||||
|
@ -169,26 +169,26 @@ impl Server {
|
||||||
self.peer.disconnect(connection_id).await;
|
self.peer.disconnect(connection_id).await;
|
||||||
let removed_connection = self.state_mut().remove_connection(connection_id)?;
|
let removed_connection = self.state_mut().remove_connection(connection_id)?;
|
||||||
|
|
||||||
for (worktree_id, worktree) in removed_connection.hosted_worktrees {
|
for (project_id, project) in removed_connection.hosted_projects {
|
||||||
if let Some(share) = worktree.share {
|
if let Some(share) = project.share {
|
||||||
broadcast(
|
broadcast(
|
||||||
connection_id,
|
connection_id,
|
||||||
share.guests.keys().copied().collect(),
|
share.guests.keys().copied().collect(),
|
||||||
|conn_id| {
|
|conn_id| {
|
||||||
self.peer
|
self.peer
|
||||||
.send(conn_id, proto::UnshareWorktree { worktree_id })
|
.send(conn_id, proto::UnshareProject { project_id })
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await?;
|
.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| {
|
broadcast(connection_id, peer_ids, |conn_id| {
|
||||||
self.peer.send(
|
self.peer.send(
|
||||||
conn_id,
|
conn_id,
|
||||||
proto::RemoveCollaborator {
|
proto::RemoveProjectCollaborator {
|
||||||
worktree_id,
|
project_id,
|
||||||
peer_id: connection_id.0,
|
peer_id: connection_id.0,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -207,9 +207,9 @@ impl Server {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn open_worktree(
|
async fn register_worktree(
|
||||||
mut self: Arc<Server>,
|
mut self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::OpenWorktree>,
|
request: TypedEnvelope<proto::RegisterWorktree>,
|
||||||
) -> tide::Result<()> {
|
) -> tide::Result<()> {
|
||||||
let receipt = request.receipt();
|
let receipt = request.receipt();
|
||||||
let host_user_id = self.state().user_id_for_connection(request.sender_id)?;
|
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 contact_user_ids = contact_user_ids.into_iter().collect::<Vec<_>>();
|
||||||
let worktree_id = self.state_mut().add_worktree(Worktree {
|
let ok = self.state_mut().register_worktree(
|
||||||
host_connection_id: request.sender_id,
|
request.project_id,
|
||||||
host_user_id,
|
request.worktree_id,
|
||||||
authorized_user_ids: contact_user_ids.clone(),
|
Worktree {
|
||||||
root_name: request.payload.root_name,
|
authorized_user_ids: contact_user_ids.clone(),
|
||||||
share: None,
|
root_name: request.payload.root_name,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
self.peer
|
if ok {
|
||||||
.respond(receipt, proto::OpenWorktreeResponse { worktree_id })
|
self.peer.respond(receipt, proto::Ack {}).await?;
|
||||||
.await?;
|
self.update_contacts_for_users(&contact_user_ids).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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn close_worktree(
|
async fn unregister_worktree(
|
||||||
mut self: Arc<Server>,
|
mut self: Arc<Server>,
|
||||||
request: TypedEnvelope<proto::CloseWorktree>,
|
request: TypedEnvelope<proto::UnregisterWorktree>,
|
||||||
) -> tide::Result<()> {
|
) -> tide::Result<()> {
|
||||||
|
let project_id = request.payload.project_id;
|
||||||
let worktree_id = request.payload.worktree_id;
|
let worktree_id = request.payload.worktree_id;
|
||||||
let worktree = self
|
let worktree =
|
||||||
.state_mut()
|
self.state_mut()
|
||||||
.remove_worktree(worktree_id, request.sender_id)?;
|
.unregister_worktree(project_id, worktree_id, request.sender_id)?;
|
||||||
|
|
||||||
if let Some(share) = worktree.share {
|
if let Some(share) = worktree.share {
|
||||||
broadcast(
|
broadcast(
|
||||||
request.sender_id,
|
request.sender_id,
|
||||||
share.guests.keys().copied().collect(),
|
share.guests.keys().copied().collect(),
|
||||||
|conn_id| {
|
|conn_id| {
|
||||||
self.peer
|
self.peer.send(
|
||||||
.send(conn_id, proto::UnshareWorktree { worktree_id })
|
conn_id,
|
||||||
|
proto::UnregisterWorktree {
|
||||||
|
project_id,
|
||||||
|
worktree_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
|
@ -8,29 +8,38 @@ use std::collections::hash_map;
|
||||||
pub struct Store {
|
pub struct Store {
|
||||||
connections: HashMap<ConnectionId, ConnectionState>,
|
connections: HashMap<ConnectionId, ConnectionState>,
|
||||||
connections_by_user_id: HashMap<UserId, HashSet<ConnectionId>>,
|
connections_by_user_id: HashMap<UserId, HashSet<ConnectionId>>,
|
||||||
worktrees: HashMap<u64, Worktree>,
|
projects: HashMap<u64, Project>,
|
||||||
visible_worktrees_by_user_id: HashMap<UserId, HashSet<u64>>,
|
visible_projects_by_user_id: HashMap<UserId, HashSet<u64>>,
|
||||||
channels: HashMap<ChannelId, Channel>,
|
channels: HashMap<ChannelId, Channel>,
|
||||||
next_worktree_id: u64,
|
next_worktree_id: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ConnectionState {
|
struct ConnectionState {
|
||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
worktrees: HashSet<u64>,
|
projects: HashSet<u64>,
|
||||||
channels: HashSet<ChannelId>,
|
channels: HashSet<ChannelId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Worktree {
|
pub struct Project {
|
||||||
pub host_connection_id: ConnectionId,
|
pub host_connection_id: ConnectionId,
|
||||||
pub host_user_id: UserId,
|
pub host_user_id: UserId,
|
||||||
|
pub share: Option<ProjectShare>,
|
||||||
|
worktrees: HashMap<u64, Worktree>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Worktree {
|
||||||
pub authorized_user_ids: Vec<UserId>,
|
pub authorized_user_ids: Vec<UserId>,
|
||||||
pub root_name: String,
|
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 struct WorktreeShare {
|
||||||
pub guests: HashMap<ConnectionId, (ReplicaId, UserId)>,
|
|
||||||
pub active_replica_ids: HashSet<ReplicaId>,
|
|
||||||
pub entries: HashMap<u64, proto::Entry>,
|
pub entries: HashMap<u64, proto::Entry>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,8 +52,8 @@ pub type ReplicaId = u16;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct RemovedConnectionState {
|
pub struct RemovedConnectionState {
|
||||||
pub hosted_worktrees: HashMap<u64, Worktree>,
|
pub hosted_projects: HashMap<u64, Project>,
|
||||||
pub guest_worktree_ids: HashMap<u64, Vec<ConnectionId>>,
|
pub guest_project_ids: HashMap<u64, Vec<ConnectionId>>,
|
||||||
pub contact_ids: HashSet<UserId>,
|
pub contact_ids: HashSet<UserId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +78,7 @@ impl Store {
|
||||||
connection_id,
|
connection_id,
|
||||||
ConnectionState {
|
ConnectionState {
|
||||||
user_id,
|
user_id,
|
||||||
worktrees: Default::default(),
|
projects: Default::default(),
|
||||||
channels: Default::default(),
|
channels: Default::default(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -106,7 +115,7 @@ impl Store {
|
||||||
|
|
||||||
let mut result = RemovedConnectionState::default();
|
let mut result = RemovedConnectionState::default();
|
||||||
for worktree_id in connection.worktrees.clone() {
|
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
|
result
|
||||||
.contact_ids
|
.contact_ids
|
||||||
.extend(worktree.authorized_user_ids.iter().copied());
|
.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> {
|
pub fn contacts_for_user(&self, user_id: UserId) -> Vec<proto::Contact> {
|
||||||
let mut contacts = HashMap::default();
|
let mut contacts = HashMap::default();
|
||||||
for worktree_id in self
|
for project_id in self
|
||||||
.visible_worktrees_by_user_id
|
.visible_projects_by_user_id
|
||||||
.get(&user_id)
|
.get(&user_id)
|
||||||
.unwrap_or(&HashSet::default())
|
.unwrap_or(&HashSet::default())
|
||||||
{
|
{
|
||||||
let worktree = &self.worktrees[worktree_id];
|
let project = &self.projects[project_id];
|
||||||
|
|
||||||
let mut guests = HashSet::default();
|
let mut guests = HashSet::default();
|
||||||
if let Ok(share) = worktree.share() {
|
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
|
contacts
|
||||||
.entry(host_user_id)
|
.entry(host_user_id)
|
||||||
.or_insert_with(|| proto::Contact {
|
.or_insert_with(|| proto::Contact {
|
||||||
user_id: host_user_id.to_proto(),
|
user_id: host_user_id.to_proto(),
|
||||||
worktrees: Vec::new(),
|
projects: Vec::new(),
|
||||||
})
|
})
|
||||||
.worktrees
|
.projects
|
||||||
.push(proto::WorktreeMetadata {
|
.push(proto::ProjectMetadata {
|
||||||
id: *worktree_id,
|
id: *project_id,
|
||||||
root_name: worktree.root_name.clone(),
|
worktree_root_names: project
|
||||||
is_shared: worktree.share.is_some(),
|
.worktrees
|
||||||
|
.iter()
|
||||||
|
.map(|worktree| worktree.root_name.clone())
|
||||||
|
.collect(),
|
||||||
|
is_shared: project.share.is_some(),
|
||||||
guests: guests.into_iter().collect(),
|
guests: guests.into_iter().collect(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -210,41 +223,75 @@ impl Store {
|
||||||
contacts.into_values().collect()
|
contacts.into_values().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_worktree(&mut self, worktree: Worktree) -> u64 {
|
pub fn register_project(
|
||||||
let worktree_id = self.next_worktree_id;
|
&mut self,
|
||||||
for authorized_user_id in &worktree.authorized_user_ids {
|
host_connection_id: ConnectionId,
|
||||||
self.visible_worktrees_by_user_id
|
host_user_id: UserId,
|
||||||
.entry(*authorized_user_id)
|
) -> u64 {
|
||||||
.or_default()
|
let project_id = self.next_project_id;
|
||||||
.insert(worktree_id);
|
self.projects.insert(
|
||||||
}
|
project_id,
|
||||||
self.next_worktree_id += 1;
|
Project {
|
||||||
if let Some(connection) = self.connections.get_mut(&worktree.host_connection_id) {
|
host_connection_id,
|
||||||
connection.worktrees.insert(worktree_id);
|
host_user_id,
|
||||||
}
|
share: None,
|
||||||
self.worktrees.insert(worktree_id, worktree);
|
worktrees: Default::default(),
|
||||||
|
},
|
||||||
#[cfg(test)]
|
);
|
||||||
self.check_invariants();
|
self.next_project_id += 1;
|
||||||
|
project_id
|
||||||
worktree_id
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove_worktree(
|
pub fn register_worktree(
|
||||||
&mut self,
|
&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,
|
worktree_id: u64,
|
||||||
acting_connection_id: ConnectionId,
|
acting_connection_id: ConnectionId,
|
||||||
) -> tide::Result<Worktree> {
|
) -> tide::Result<Worktree> {
|
||||||
let worktree = if let hash_map::Entry::Occupied(e) = self.worktrees.entry(worktree_id) {
|
let project = self
|
||||||
if e.get().host_connection_id != acting_connection_id {
|
.projects
|
||||||
Err(anyhow!("not your worktree"))?;
|
.get_mut(&project_id)
|
||||||
}
|
.ok_or_else(|| anyhow!("no such project"))?;
|
||||||
e.remove()
|
if project.host_connection_id != acting_connection_id {
|
||||||
} else {
|
Err(anyhow!("not your worktree"))?;
|
||||||
return Err(anyhow!("no such 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);
|
connection.worktrees.remove(&worktree_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,20 +318,31 @@ impl Store {
|
||||||
Ok(worktree)
|
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(
|
pub fn share_worktree(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
project_id: u64,
|
||||||
worktree_id: u64,
|
worktree_id: u64,
|
||||||
connection_id: ConnectionId,
|
connection_id: ConnectionId,
|
||||||
entries: HashMap<u64, proto::Entry>,
|
entries: HashMap<u64, proto::Entry>,
|
||||||
) -> Option<Vec<UserId>> {
|
) -> Option<Vec<UserId>> {
|
||||||
if let Some(worktree) = self.worktrees.get_mut(&worktree_id) {
|
if let Some(project) = self.projects.get_mut(&project_id) {
|
||||||
if worktree.host_connection_id == connection_id {
|
if project.host_connection_id == connection_id {
|
||||||
worktree.share = Some(WorktreeShare {
|
if let Some(share) = project.share.as_mut() {
|
||||||
guests: Default::default(),
|
share
|
||||||
active_replica_ids: Default::default(),
|
.worktrees
|
||||||
entries,
|
.insert(worktree_id, WorktreeShare { entries });
|
||||||
});
|
return Some(project.authorized_user_ids());
|
||||||
return Some(worktree.authorized_user_ids.clone());
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
@ -586,14 +644,14 @@ impl Worktree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn share(&self) -> tide::Result<&WorktreeShare> {
|
pub fn share(&self) -> tide::Result<&ProjectShare> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.share
|
.share
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or_else(|| anyhow!("worktree is not shared"))?)
|
.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
|
Ok(self
|
||||||
.share
|
.share
|
||||||
.as_mut()
|
.as_mut()
|
||||||
|
|
Loading…
Reference in a new issue