Add room creation from channel join

co-authored-by: max <max@zed.dev>
This commit is contained in:
Mikayla Maki 2023-07-31 16:48:24 -07:00
parent 92fa879b0c
commit 003a711dea
No known key found for this signature in database
7 changed files with 229 additions and 181 deletions

View file

@ -209,80 +209,6 @@ impl ActiveCall {
})
}
pub fn join_channel(
&mut self,
channel_id: u64,
cx: &mut ModelContext<Self>,
) -> Task<Result<()>> {
let room = if let Some(room) = self.room().cloned() {
Some(Task::ready(Ok(room)).shared())
} else {
self.pending_room_creation.clone()
};
todo!()
// let invite = if let Some(room) = room {
// cx.spawn_weak(|_, mut cx| async move {
// let room = room.await.map_err(|err| anyhow!("{:?}", err))?;
// // TODO join_channel:
// // let initial_project_id = if let Some(initial_project) = initial_project {
// // Some(
// // room.update(&mut cx, |room, cx| room.share_project(initial_project, cx))
// // .await?,
// // )
// // } else {
// // None
// // };
// // room.update(&mut cx, |room, cx| {
// // room.call(called_user_id, initial_project_id, cx)
// // })
// // .await?;
// anyhow::Ok(())
// })
// } else {
// let client = self.client.clone();
// let user_store = self.user_store.clone();
// let room = cx
// .spawn(|this, mut cx| async move {
// let create_room = async {
// let room = cx
// .update(|cx| {
// Room::create_from_channel(channel_id, client, user_store, cx)
// })
// .await?;
// this.update(&mut cx, |this, cx| this.set_room(Some(room.clone()), cx))
// .await?;
// anyhow::Ok(room)
// };
// let room = create_room.await;
// this.update(&mut cx, |this, _| this.pending_room_creation = None);
// room.map_err(Arc::new)
// })
// .shared();
// self.pending_room_creation = Some(room.clone());
// cx.foreground().spawn(async move {
// room.await.map_err(|err| anyhow!("{:?}", err))?;
// anyhow::Ok(())
// })
// };
// cx.spawn(|this, mut cx| async move {
// let result = invite.await;
// this.update(&mut cx, |this, cx| {
// this.pending_invites.remove(&called_user_id);
// this.report_call_event("invite", cx);
// cx.notify();
// });
// result
// })
}
pub fn cancel_invite(
&mut self,
called_user_id: u64,
@ -348,6 +274,30 @@ impl ActiveCall {
Ok(())
}
pub fn join_channel(
&mut self,
channel_id: u64,
cx: &mut ModelContext<Self>,
) -> Task<Result<()>> {
if let Some(room) = self.room().cloned() {
if room.read(cx).channel_id() == Some(channel_id) {
return Task::ready(Ok(()));
}
}
let join = Room::join_channel(channel_id, self.client.clone(), self.user_store.clone(), cx);
cx.spawn(|this, mut cx| async move {
let room = join.await?;
this.update(&mut cx, |this, cx| this.set_room(Some(room.clone()), cx))
.await?;
this.update(&mut cx, |this, cx| {
this.report_call_event("join channel", cx)
});
Ok(())
})
}
pub fn hang_up(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
cx.notify();
self.report_call_event("hang up", cx);

View file

@ -49,6 +49,7 @@ pub enum Event {
pub struct Room {
id: u64,
channel_id: Option<u64>,
live_kit: Option<LiveKitRoom>,
status: RoomStatus,
shared_projects: HashSet<WeakModelHandle<Project>>,
@ -93,8 +94,25 @@ impl Entity for Room {
}
impl Room {
pub fn channel_id(&self) -> Option<u64> {
self.channel_id
}
#[cfg(any(test, feature = "test-support"))]
pub fn is_connected(&self) -> bool {
if let Some(live_kit) = self.live_kit.as_ref() {
matches!(
*live_kit.room.status().borrow(),
live_kit_client::ConnectionState::Connected { .. }
)
} else {
false
}
}
fn new(
id: u64,
channel_id: Option<u64>,
live_kit_connection_info: Option<proto::LiveKitConnectionInfo>,
client: Arc<Client>,
user_store: ModelHandle<UserStore>,
@ -185,6 +203,7 @@ impl Room {
Self {
id,
channel_id,
live_kit: live_kit_room,
status: RoomStatus::Online,
shared_projects: Default::default(),
@ -204,15 +223,6 @@ impl Room {
}
}
pub(crate) fn create_from_channel(
channel_id: u64,
client: Arc<Client>,
user_store: ModelHandle<UserStore>,
cx: &mut AppContext,
) -> Task<Result<ModelHandle<Self>>> {
todo!()
}
pub(crate) fn create(
called_user_id: u64,
initial_project: Option<ModelHandle<Project>>,
@ -226,6 +236,7 @@ impl Room {
let room = cx.add_model(|cx| {
Self::new(
room_proto.id,
None,
response.live_kit_connection_info,
client,
user_store,
@ -257,6 +268,35 @@ impl Room {
})
}
pub(crate) fn join_channel(
channel_id: u64,
client: Arc<Client>,
user_store: ModelHandle<UserStore>,
cx: &mut AppContext,
) -> Task<Result<ModelHandle<Self>>> {
cx.spawn(|mut cx| async move {
let response = client.request(proto::JoinChannel { channel_id }).await?;
let room_proto = response.room.ok_or_else(|| anyhow!("invalid room"))?;
let room = cx.add_model(|cx| {
Self::new(
room_proto.id,
Some(channel_id),
response.live_kit_connection_info,
client,
user_store,
cx,
)
});
room.update(&mut cx, |room, cx| {
room.apply_room_update(room_proto, cx)?;
anyhow::Ok(())
})?;
Ok(room)
})
}
pub(crate) fn join(
call: &IncomingCall,
client: Arc<Client>,
@ -270,6 +310,7 @@ impl Room {
let room = cx.add_model(|cx| {
Self::new(
room_id,
None,
response.live_kit_connection_info,
client,
user_store,