Automatically share project when creating the room

Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-10-04 19:25:48 +02:00
parent 678b013da6
commit fceba6814f
12 changed files with 137 additions and 35 deletions

View file

@ -5,6 +5,7 @@ use anyhow::{anyhow, Result};
use client::{incoming_call::IncomingCall, Client, UserStore}; use client::{incoming_call::IncomingCall, Client, UserStore};
use gpui::{Entity, ModelContext, ModelHandle, MutableAppContext, Subscription, Task}; use gpui::{Entity, ModelContext, ModelHandle, MutableAppContext, Subscription, Task};
pub use participant::ParticipantLocation; pub use participant::ParticipantLocation;
use project::Project;
pub use room::Room; pub use room::Room;
use std::sync::Arc; use std::sync::Arc;
@ -39,6 +40,7 @@ impl ActiveCall {
pub fn invite( pub fn invite(
&mut self, &mut self,
recipient_user_id: u64, recipient_user_id: u64,
initial_project: Option<ModelHandle<Project>>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Task<Result<()>> { ) -> Task<Result<()>> {
let room = self.room.as_ref().map(|(room, _)| room.clone()); let room = self.room.as_ref().map(|(room, _)| room.clone());
@ -52,7 +54,20 @@ impl ActiveCall {
this.update(&mut cx, |this, cx| this.set_room(Some(room.clone()), cx)); this.update(&mut cx, |this, cx| this.set_room(Some(room.clone()), cx));
room room
}; };
room.update(&mut cx, |room, cx| room.call(recipient_user_id, cx))
let initial_project_id = if let Some(initial_project) = initial_project {
let room_id = room.read_with(&cx, |room, _| room.id());
Some(
initial_project
.update(&mut cx, |project, cx| project.share(room_id, cx))
.await?,
)
} else {
None
};
room.update(&mut cx, |room, cx| {
room.call(recipient_user_id, initial_project_id, cx)
})
.await?; .await?;
Ok(()) Ok(())

View file

@ -216,6 +216,7 @@ impl Room {
pub fn call( pub fn call(
&mut self, &mut self,
recipient_user_id: u64, recipient_user_id: u64,
initial_project_id: Option<u64>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Task<Result<()>> { ) -> Task<Result<()>> {
if self.status.is_offline() { if self.status.is_offline() {
@ -229,6 +230,7 @@ impl Room {
.request(proto::Call { .request(proto::Call {
room_id, room_id,
recipient_user_id, recipient_user_id,
initial_project_id,
}) })
.await?; .await?;
Ok(()) Ok(())

View file

@ -6,4 +6,5 @@ pub struct IncomingCall {
pub room_id: u64, pub room_id: u64,
pub caller: Arc<User>, pub caller: Arc<User>,
pub participants: Vec<Arc<User>>, pub participants: Vec<Arc<User>>,
pub initial_project_id: Option<u64>,
} }

View file

@ -212,6 +212,7 @@ impl UserStore {
this.get_user(envelope.payload.caller_user_id, cx) this.get_user(envelope.payload.caller_user_id, cx)
}) })
.await?, .await?,
initial_project_id: envelope.payload.initial_project_id,
}; };
this.update(&mut cx, |this, _| { this.update(&mut cx, |this, _| {
*this.incoming_call.0.borrow_mut() = Some(call); *this.incoming_call.0.borrow_mut() = Some(call);

View file

@ -95,7 +95,9 @@ async fn test_basic_calls(
.user_store .user_store
.update(cx_b, |user, _| user.incoming_call()); .update(cx_b, |user, _| user.incoming_call());
room_a room_a
.update(cx_a, |room, cx| room.call(client_b.user_id().unwrap(), cx)) .update(cx_a, |room, cx| {
room.call(client_b.user_id().unwrap(), None, cx)
})
.await .await
.unwrap(); .unwrap();
@ -147,7 +149,9 @@ async fn test_basic_calls(
.user_store .user_store
.update(cx_c, |user, _| user.incoming_call()); .update(cx_c, |user, _| user.incoming_call());
room_b room_b
.update(cx_b, |room, cx| room.call(client_c.user_id().unwrap(), cx)) .update(cx_b, |room, cx| {
room.call(client_c.user_id().unwrap(), None, cx)
})
.await .await
.unwrap(); .unwrap();
@ -234,7 +238,9 @@ async fn test_leaving_room_on_disconnection(
.user_store .user_store
.update(cx_b, |user, _| user.incoming_call()); .update(cx_b, |user, _| user.incoming_call());
room_a room_a
.update(cx_a, |room, cx| room.call(client_b.user_id().unwrap(), cx)) .update(cx_a, |room, cx| {
room.call(client_b.user_id().unwrap(), None, cx)
})
.await .await
.unwrap(); .unwrap();
@ -4849,7 +4855,7 @@ async fn test_random_collaboration(
host_language_registry.add(Arc::new(language)); host_language_registry.add(Arc::new(language));
let host_user_id = host.current_user_id(&host_cx); let host_user_id = host.current_user_id(&host_cx);
room.update(cx, |room, cx| room.call(host_user_id.to_proto(), cx)) room.update(cx, |room, cx| room.call(host_user_id.to_proto(), None, cx))
.await .await
.unwrap(); .unwrap();
deterministic.run_until_parked(); deterministic.run_until_parked();
@ -4941,7 +4947,7 @@ async fn test_random_collaboration(
let guest = server.create_client(&mut guest_cx, &guest_username).await; let guest = server.create_client(&mut guest_cx, &guest_username).await;
let guest_user_id = guest.current_user_id(&guest_cx); let guest_user_id = guest.current_user_id(&guest_cx);
room.update(cx, |room, cx| room.call(guest_user_id.to_proto(), cx)) room.update(cx, |room, cx| room.call(guest_user_id.to_proto(), None, cx))
.await .await
.unwrap(); .unwrap();
deterministic.run_until_parked(); deterministic.run_until_parked();
@ -5353,7 +5359,7 @@ impl TestServer {
for (client_b, cx_b) in right { for (client_b, cx_b) in right {
let user_id_b = client_b.current_user_id(*cx_b).to_proto(); let user_id_b = client_b.current_user_id(*cx_b).to_proto();
room_a room_a
.update(*cx_a, |room, cx| room.call(user_id_b, cx)) .update(*cx_a, |room, cx| room.call(user_id_b, None, cx))
.await .await
.unwrap(); .unwrap();

View file

@ -660,6 +660,10 @@ impl Server {
.await .await
.user_id_for_connection(request.sender_id)?; .user_id_for_connection(request.sender_id)?;
let recipient_user_id = UserId::from_proto(request.payload.recipient_user_id); let recipient_user_id = UserId::from_proto(request.payload.recipient_user_id);
let initial_project_id = request
.payload
.initial_project_id
.map(ProjectId::from_proto);
if !self if !self
.app_state .app_state
.db .db
@ -672,8 +676,12 @@ impl Server {
let room_id = request.payload.room_id; let room_id = request.payload.room_id;
let mut calls = { let mut calls = {
let mut store = self.store().await; let mut store = self.store().await;
let (room, recipient_connection_ids, incoming_call) = let (room, recipient_connection_ids, incoming_call) = store.call(
store.call(room_id, request.sender_id, recipient_user_id)?; room_id,
recipient_user_id,
initial_project_id,
request.sender_id,
)?;
self.room_updated(room); self.room_updated(room);
recipient_connection_ids recipient_connection_ids
.into_iter() .into_iter()

View file

@ -40,6 +40,7 @@ pub struct Call {
pub caller_user_id: UserId, pub caller_user_id: UserId,
pub room_id: RoomId, pub room_id: RoomId,
pub connection_id: Option<ConnectionId>, pub connection_id: Option<ConnectionId>,
pub initial_project_id: Option<ProjectId>,
} }
#[derive(Serialize)] #[derive(Serialize)]
@ -175,6 +176,9 @@ impl Store {
.iter() .iter()
.map(|participant| participant.user_id) .map(|participant| participant.user_id)
.collect(), .collect(),
initial_project_id: active_call
.initial_project_id
.map(|project_id| project_id.to_proto()),
}) })
} }
} else { } else {
@ -379,6 +383,7 @@ impl Store {
caller_user_id: connection.user_id, caller_user_id: connection.user_id,
room_id, room_id,
connection_id: Some(creator_connection_id), connection_id: Some(creator_connection_id),
initial_project_id: None,
}); });
Ok(room_id) Ok(room_id)
} }
@ -486,17 +491,18 @@ impl Store {
pub fn call( pub fn call(
&mut self, &mut self,
room_id: RoomId, room_id: RoomId,
recipient_user_id: UserId,
initial_project_id: Option<ProjectId>,
from_connection_id: ConnectionId, from_connection_id: ConnectionId,
recipient_id: UserId,
) -> Result<(&proto::Room, Vec<ConnectionId>, proto::IncomingCall)> { ) -> Result<(&proto::Room, Vec<ConnectionId>, proto::IncomingCall)> {
let caller_user_id = self.user_id_for_connection(from_connection_id)?; let caller_user_id = self.user_id_for_connection(from_connection_id)?;
let recipient_connection_ids = self let recipient_connection_ids = self
.connection_ids_for_user(recipient_id) .connection_ids_for_user(recipient_user_id)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut recipient = self let mut recipient = self
.connected_users .connected_users
.get_mut(&recipient_id) .get_mut(&recipient_user_id)
.ok_or_else(|| anyhow!("no such connection"))?; .ok_or_else(|| anyhow!("no such connection"))?;
anyhow::ensure!( anyhow::ensure!(
recipient.active_call.is_none(), recipient.active_call.is_none(),
@ -516,14 +522,24 @@ impl Store {
anyhow::ensure!( anyhow::ensure!(
room.pending_user_ids room.pending_user_ids
.iter() .iter()
.all(|user_id| UserId::from_proto(*user_id) != recipient_id), .all(|user_id| UserId::from_proto(*user_id) != recipient_user_id),
"cannot call the same user more than once" "cannot call the same user more than once"
); );
room.pending_user_ids.push(recipient_id.to_proto()); room.pending_user_ids.push(recipient_user_id.to_proto());
if let Some(initial_project_id) = initial_project_id {
let project = self
.projects
.get(&initial_project_id)
.ok_or_else(|| anyhow!("no such project"))?;
anyhow::ensure!(project.room_id == room_id, "no such project");
}
recipient.active_call = Some(Call { recipient.active_call = Some(Call {
caller_user_id, caller_user_id,
room_id, room_id,
connection_id: None, connection_id: None,
initial_project_id,
}); });
Ok(( Ok((
@ -537,6 +553,7 @@ impl Store {
.iter() .iter()
.map(|participant| participant.user_id) .map(|participant| participant.user_id)
.collect(), .collect(),
initial_project_id: initial_project_id.map(|project_id| project_id.to_proto()),
}, },
)) ))
} }

View file

@ -100,8 +100,9 @@ impl CollabTitlebarItem {
Some(_) => {} Some(_) => {}
None => { None => {
if let Some(workspace) = self.workspace.upgrade(cx) { if let Some(workspace) = self.workspace.upgrade(cx) {
let project = workspace.read(cx).project().clone();
let user_store = workspace.read(cx).user_store().clone(); let user_store = workspace.read(cx).user_store().clone();
let view = cx.add_view(|cx| ContactsPopover::new(user_store, cx)); let view = cx.add_view(|cx| ContactsPopover::new(project, user_store, cx));
cx.focus(&view); cx.focus(&view);
cx.subscribe(&view, |this, _, event, cx| { cx.subscribe(&view, |this, _, event, cx| {
match event { match event {

View file

@ -11,6 +11,6 @@ use workspace::AppState;
pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) { pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
contacts_popover::init(cx); contacts_popover::init(cx);
collab_titlebar_item::init(cx); collab_titlebar_item::init(cx);
incoming_call_notification::init(app_state.user_store.clone(), cx); incoming_call_notification::init(app_state.clone(), cx);
project_shared_notification::init(app_state, cx); project_shared_notification::init(app_state, cx);
} }

View file

@ -10,6 +10,7 @@ use gpui::{
ViewHandle, ViewHandle,
}; };
use menu::{Confirm, SelectNext, SelectPrev}; use menu::{Confirm, SelectNext, SelectPrev};
use project::Project;
use settings::Settings; use settings::Settings;
use theme::IconButton; use theme::IconButton;
@ -30,6 +31,7 @@ struct ToggleExpanded(Section);
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
struct Call { struct Call {
recipient_user_id: u64, recipient_user_id: u64,
initial_project: Option<ModelHandle<Project>>,
} }
#[derive(Clone, Copy, PartialEq, Eq, Debug, PartialOrd, Ord)] #[derive(Clone, Copy, PartialEq, Eq, Debug, PartialOrd, Ord)]
@ -83,6 +85,7 @@ pub struct ContactsPopover {
entries: Vec<ContactEntry>, entries: Vec<ContactEntry>,
match_candidates: Vec<StringMatchCandidate>, match_candidates: Vec<StringMatchCandidate>,
list_state: ListState, list_state: ListState,
project: ModelHandle<Project>,
user_store: ModelHandle<UserStore>, user_store: ModelHandle<UserStore>,
filter_editor: ViewHandle<Editor>, filter_editor: ViewHandle<Editor>,
collapsed_sections: Vec<Section>, collapsed_sections: Vec<Section>,
@ -91,7 +94,11 @@ pub struct ContactsPopover {
} }
impl ContactsPopover { impl ContactsPopover {
pub fn new(user_store: ModelHandle<UserStore>, cx: &mut ViewContext<Self>) -> Self { pub fn new(
project: ModelHandle<Project>,
user_store: ModelHandle<UserStore>,
cx: &mut ViewContext<Self>,
) -> Self {
let filter_editor = cx.add_view(|cx| { let filter_editor = cx.add_view(|cx| {
let mut editor = Editor::single_line( let mut editor = Editor::single_line(
Some(|theme| theme.contacts_panel.user_query_editor.clone()), Some(|theme| theme.contacts_panel.user_query_editor.clone()),
@ -149,9 +156,13 @@ impl ContactsPopover {
is_selected, is_selected,
cx, cx,
), ),
ContactEntry::Contact(contact) => { ContactEntry::Contact(contact) => Self::render_contact(
Self::render_contact(contact, &theme.contacts_panel, is_selected, cx) contact,
} &this.project,
&theme.contacts_panel,
is_selected,
cx,
),
} }
}); });
@ -168,6 +179,7 @@ impl ContactsPopover {
match_candidates: Default::default(), match_candidates: Default::default(),
filter_editor, filter_editor,
_subscriptions: subscriptions, _subscriptions: subscriptions,
project,
user_store, user_store,
}; };
this.update_entries(cx); this.update_entries(cx);
@ -463,12 +475,18 @@ impl ContactsPopover {
fn render_contact( fn render_contact(
contact: &Contact, contact: &Contact,
project: &ModelHandle<Project>,
theme: &theme::ContactsPanel, theme: &theme::ContactsPanel,
is_selected: bool, is_selected: bool,
cx: &mut RenderContext<Self>, cx: &mut RenderContext<Self>,
) -> ElementBox { ) -> ElementBox {
let online = contact.online; let online = contact.online;
let user_id = contact.user.id; let user_id = contact.user.id;
let initial_project = if ActiveCall::global(cx).read(cx).room().is_none() {
Some(project.clone())
} else {
None
};
let mut element = let mut element =
MouseEventHandler::<Contact>::new(contact.user.id as usize, cx, |_, _| { MouseEventHandler::<Contact>::new(contact.user.id as usize, cx, |_, _| {
Flex::row() Flex::row()
@ -501,6 +519,7 @@ impl ContactsPopover {
if online { if online {
cx.dispatch_action(Call { cx.dispatch_action(Call {
recipient_user_id: user_id, recipient_user_id: user_id,
initial_project: initial_project.clone(),
}); });
} }
}); });
@ -629,7 +648,7 @@ impl ContactsPopover {
fn call(&mut self, action: &Call, cx: &mut ViewContext<Self>) { fn call(&mut self, action: &Call, cx: &mut ViewContext<Self>) {
ActiveCall::global(cx) ActiveCall::global(cx)
.update(cx, |active_call, cx| { .update(cx, |active_call, cx| {
active_call.invite(action.recipient_user_id, cx) active_call.invite(action.recipient_user_id, action.initial_project.clone(), cx)
}) })
.detach_and_log_err(cx); .detach_and_log_err(cx);
} }

View file

@ -1,21 +1,25 @@
use std::sync::Arc;
use call::ActiveCall; use call::ActiveCall;
use client::{incoming_call::IncomingCall, UserStore}; use client::incoming_call::IncomingCall;
use futures::StreamExt; use futures::StreamExt;
use gpui::{ use gpui::{
elements::*, elements::*,
geometry::{rect::RectF, vector::vec2f}, geometry::{rect::RectF, vector::vec2f},
impl_internal_actions, Entity, ModelHandle, MouseButton, MutableAppContext, RenderContext, impl_internal_actions, Entity, MouseButton, MutableAppContext, RenderContext, View,
View, ViewContext, WindowBounds, WindowKind, WindowOptions, ViewContext, WindowBounds, WindowKind, WindowOptions,
}; };
use project::Project;
use settings::Settings; use settings::Settings;
use util::ResultExt; use util::ResultExt;
use workspace::{AppState, Workspace};
impl_internal_actions!(incoming_call_notification, [RespondToCall]); impl_internal_actions!(incoming_call_notification, [RespondToCall]);
pub fn init(user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) { pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
cx.add_action(IncomingCallNotification::respond_to_call); cx.add_action(IncomingCallNotification::respond_to_call);
let mut incoming_call = user_store.read(cx).incoming_call(); let mut incoming_call = app_state.user_store.read(cx).incoming_call();
cx.spawn(|mut cx| async move { cx.spawn(|mut cx| async move {
let mut notification_window = None; let mut notification_window = None;
while let Some(incoming_call) = incoming_call.next().await { while let Some(incoming_call) = incoming_call.next().await {
@ -32,7 +36,7 @@ pub fn init(user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) {
kind: WindowKind::PopUp, kind: WindowKind::PopUp,
is_movable: false, is_movable: false,
}, },
|_| IncomingCallNotification::new(incoming_call, user_store.clone()), |_| IncomingCallNotification::new(incoming_call, app_state.clone()),
); );
notification_window = Some(window_id); notification_window = Some(window_id);
} }
@ -48,21 +52,47 @@ struct RespondToCall {
pub struct IncomingCallNotification { pub struct IncomingCallNotification {
call: IncomingCall, call: IncomingCall,
user_store: ModelHandle<UserStore>, app_state: Arc<AppState>,
} }
impl IncomingCallNotification { impl IncomingCallNotification {
pub fn new(call: IncomingCall, user_store: ModelHandle<UserStore>) -> Self { pub fn new(call: IncomingCall, app_state: Arc<AppState>) -> Self {
Self { call, user_store } Self { call, app_state }
} }
fn respond_to_call(&mut self, action: &RespondToCall, cx: &mut ViewContext<Self>) { fn respond_to_call(&mut self, action: &RespondToCall, cx: &mut ViewContext<Self>) {
if action.accept { if action.accept {
ActiveCall::global(cx) let app_state = self.app_state.clone();
.update(cx, |active_call, cx| active_call.join(&self.call, cx)) let join = ActiveCall::global(cx)
.update(cx, |active_call, cx| active_call.join(&self.call, cx));
let initial_project_id = self.call.initial_project_id;
cx.spawn_weak(|_, mut cx| async move {
join.await?;
if let Some(initial_project_id) = initial_project_id {
let project = Project::remote(
initial_project_id,
app_state.client.clone(),
app_state.user_store.clone(),
app_state.project_store.clone(),
app_state.languages.clone(),
app_state.fs.clone(),
cx.clone(),
)
.await?;
cx.add_window((app_state.build_window_options)(), |cx| {
let mut workspace =
Workspace::new(project, app_state.default_item_factory, cx);
(app_state.initialize_workspace)(&mut workspace, &app_state, cx);
workspace
});
}
anyhow::Ok(())
})
.detach_and_log_err(cx); .detach_and_log_err(cx);
} else { } else {
self.user_store self.app_state
.user_store
.update(cx, |user_store, _| user_store.decline_call().log_err()); .update(cx, |user_store, _| user_store.decline_call().log_err());
} }

View file

@ -179,12 +179,14 @@ message ParticipantLocation {
message Call { message Call {
uint64 room_id = 1; uint64 room_id = 1;
uint64 recipient_user_id = 2; uint64 recipient_user_id = 2;
optional uint64 initial_project_id = 3;
} }
message IncomingCall { message IncomingCall {
uint64 room_id = 1; uint64 room_id = 1;
uint64 caller_user_id = 2; uint64 caller_user_id = 2;
repeated uint64 participant_user_ids = 3; repeated uint64 participant_user_ids = 3;
optional uint64 initial_project_id = 4;
} }
message CancelCall {} message CancelCall {}