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 gpui::{Entity, ModelContext, ModelHandle, MutableAppContext, Subscription, Task};
pub use participant::ParticipantLocation;
use project::Project;
pub use room::Room;
use std::sync::Arc;
@ -39,6 +40,7 @@ impl ActiveCall {
pub fn invite(
&mut self,
recipient_user_id: u64,
initial_project: Option<ModelHandle<Project>>,
cx: &mut ModelContext<Self>,
) -> Task<Result<()>> {
let room = self.room.as_ref().map(|(room, _)| room.clone());
@ -52,8 +54,21 @@ impl ActiveCall {
this.update(&mut cx, |this, cx| this.set_room(Some(room.clone()), cx));
room
};
room.update(&mut cx, |room, cx| room.call(recipient_user_id, cx))
.await?;
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?;
Ok(())
})

View File

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

View File

@ -6,4 +6,5 @@ pub struct IncomingCall {
pub room_id: u64,
pub caller: 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)
})
.await?,
initial_project_id: envelope.payload.initial_project_id,
};
this.update(&mut cx, |this, _| {
*this.incoming_call.0.borrow_mut() = Some(call);

View File

@ -95,7 +95,9 @@ async fn test_basic_calls(
.user_store
.update(cx_b, |user, _| user.incoming_call());
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
.unwrap();
@ -147,7 +149,9 @@ async fn test_basic_calls(
.user_store
.update(cx_c, |user, _| user.incoming_call());
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
.unwrap();
@ -234,7 +238,9 @@ async fn test_leaving_room_on_disconnection(
.user_store
.update(cx_b, |user, _| user.incoming_call());
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
.unwrap();
@ -4849,7 +4855,7 @@ async fn test_random_collaboration(
host_language_registry.add(Arc::new(language));
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
.unwrap();
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_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
.unwrap();
deterministic.run_until_parked();
@ -5353,7 +5359,7 @@ impl TestServer {
for (client_b, cx_b) in right {
let user_id_b = client_b.current_user_id(*cx_b).to_proto();
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
.unwrap();

View File

@ -660,6 +660,10 @@ impl Server {
.await
.user_id_for_connection(request.sender_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
.app_state
.db
@ -672,8 +676,12 @@ impl Server {
let room_id = request.payload.room_id;
let mut calls = {
let mut store = self.store().await;
let (room, recipient_connection_ids, incoming_call) =
store.call(room_id, request.sender_id, recipient_user_id)?;
let (room, recipient_connection_ids, incoming_call) = store.call(
room_id,
recipient_user_id,
initial_project_id,
request.sender_id,
)?;
self.room_updated(room);
recipient_connection_ids
.into_iter()

View File

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

View File

@ -100,8 +100,9 @@ impl CollabTitlebarItem {
Some(_) => {}
None => {
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 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.subscribe(&view, |this, _, event, cx| {
match event {

View File

@ -11,6 +11,6 @@ use workspace::AppState;
pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
contacts_popover::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);
}

View File

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

View File

@ -1,21 +1,25 @@
use std::sync::Arc;
use call::ActiveCall;
use client::{incoming_call::IncomingCall, UserStore};
use client::incoming_call::IncomingCall;
use futures::StreamExt;
use gpui::{
elements::*,
geometry::{rect::RectF, vector::vec2f},
impl_internal_actions, Entity, ModelHandle, MouseButton, MutableAppContext, RenderContext,
View, ViewContext, WindowBounds, WindowKind, WindowOptions,
impl_internal_actions, Entity, MouseButton, MutableAppContext, RenderContext, View,
ViewContext, WindowBounds, WindowKind, WindowOptions,
};
use project::Project;
use settings::Settings;
use util::ResultExt;
use workspace::{AppState, Workspace};
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);
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 {
let mut notification_window = None;
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,
is_movable: false,
},
|_| IncomingCallNotification::new(incoming_call, user_store.clone()),
|_| IncomingCallNotification::new(incoming_call, app_state.clone()),
);
notification_window = Some(window_id);
}
@ -48,21 +52,47 @@ struct RespondToCall {
pub struct IncomingCallNotification {
call: IncomingCall,
user_store: ModelHandle<UserStore>,
app_state: Arc<AppState>,
}
impl IncomingCallNotification {
pub fn new(call: IncomingCall, user_store: ModelHandle<UserStore>) -> Self {
Self { call, user_store }
pub fn new(call: IncomingCall, app_state: Arc<AppState>) -> Self {
Self { call, app_state }
}
fn respond_to_call(&mut self, action: &RespondToCall, cx: &mut ViewContext<Self>) {
if action.accept {
ActiveCall::global(cx)
.update(cx, |active_call, cx| active_call.join(&self.call, cx))
.detach_and_log_err(cx);
let app_state = self.app_state.clone();
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);
} else {
self.user_store
self.app_state
.user_store
.update(cx, |user_store, _| user_store.decline_call().log_err());
}

View File

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