Rename room
crate to call
Also, rename `client::Call` to `client::IncomingCall`. Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
1158911560
commit
e0db62173a
13 changed files with 49 additions and 43 deletions
85
crates/call/src/call.rs
Normal file
85
crates/call/src/call.rs
Normal file
|
@ -0,0 +1,85 @@
|
|||
mod participant;
|
||||
mod room;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use client::{incoming_call::IncomingCall, Client, UserStore};
|
||||
use gpui::{Entity, ModelContext, ModelHandle, MutableAppContext, Task};
|
||||
pub use room::Room;
|
||||
use std::sync::Arc;
|
||||
use util::ResultExt;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ActiveCall {
|
||||
room: Option<ModelHandle<Room>>,
|
||||
}
|
||||
|
||||
impl Entity for ActiveCall {
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
impl ActiveCall {
|
||||
pub fn global(cx: &mut MutableAppContext) -> ModelHandle<Self> {
|
||||
if cx.has_global::<ModelHandle<Self>>() {
|
||||
cx.global::<ModelHandle<Self>>().clone()
|
||||
} else {
|
||||
let active_call = cx.add_model(|_| ActiveCall::default());
|
||||
cx.set_global(active_call.clone());
|
||||
active_call
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_or_create(
|
||||
&mut self,
|
||||
client: &Arc<Client>,
|
||||
user_store: &ModelHandle<UserStore>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<ModelHandle<Room>>> {
|
||||
if let Some(room) = self.room.clone() {
|
||||
Task::ready(Ok(room))
|
||||
} else {
|
||||
let client = client.clone();
|
||||
let user_store = user_store.clone();
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
let room = cx.update(|cx| Room::create(client, user_store, cx)).await?;
|
||||
this.update(&mut cx, |this, cx| {
|
||||
this.room = Some(room.clone());
|
||||
cx.notify();
|
||||
});
|
||||
Ok(room)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn join(
|
||||
&mut self,
|
||||
call: &IncomingCall,
|
||||
client: &Arc<Client>,
|
||||
user_store: &ModelHandle<UserStore>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<ModelHandle<Room>>> {
|
||||
if self.room.is_some() {
|
||||
return Task::ready(Err(anyhow!("cannot join while on another call")));
|
||||
}
|
||||
|
||||
let join = Room::join(call, client.clone(), user_store.clone(), cx);
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
let room = join.await?;
|
||||
this.update(&mut cx, |this, cx| {
|
||||
this.room = Some(room.clone());
|
||||
cx.notify();
|
||||
});
|
||||
Ok(room)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn room(&self) -> Option<&ModelHandle<Room>> {
|
||||
self.room.as_ref()
|
||||
}
|
||||
|
||||
pub fn clear(&mut self, cx: &mut ModelContext<Self>) {
|
||||
if let Some(room) = self.room.take() {
|
||||
room.update(cx, |room, cx| room.leave(cx)).log_err();
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
}
|
31
crates/call/src/participant.rs
Normal file
31
crates/call/src/participant.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use client::proto;
|
||||
use gpui::ModelHandle;
|
||||
use project::Project;
|
||||
|
||||
pub enum ParticipantLocation {
|
||||
Project { project_id: u64 },
|
||||
External,
|
||||
}
|
||||
|
||||
impl ParticipantLocation {
|
||||
pub fn from_proto(location: Option<proto::ParticipantLocation>) -> Result<Self> {
|
||||
match location.and_then(|l| l.variant) {
|
||||
Some(proto::participant_location::Variant::Project(project)) => Ok(Self::Project {
|
||||
project_id: project.id,
|
||||
}),
|
||||
Some(proto::participant_location::Variant::External(_)) => Ok(Self::External),
|
||||
None => Err(anyhow!("participant location was not provided")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LocalParticipant {
|
||||
pub projects: Vec<ModelHandle<Project>>,
|
||||
}
|
||||
|
||||
pub struct RemoteParticipant {
|
||||
pub user_id: u64,
|
||||
pub projects: Vec<ModelHandle<Project>>,
|
||||
pub location: ParticipantLocation,
|
||||
}
|
221
crates/call/src/room.rs
Normal file
221
crates/call/src/room.rs
Normal file
|
@ -0,0 +1,221 @@
|
|||
use crate::participant::{LocalParticipant, ParticipantLocation, RemoteParticipant};
|
||||
use anyhow::{anyhow, Result};
|
||||
use client::{incoming_call::IncomingCall, proto, Client, PeerId, TypedEnvelope, User, UserStore};
|
||||
use collections::HashMap;
|
||||
use futures::StreamExt;
|
||||
use gpui::{AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext, Task};
|
||||
use project::Project;
|
||||
use std::sync::Arc;
|
||||
use util::ResultExt;
|
||||
|
||||
pub enum Event {
|
||||
PeerChangedActiveProject,
|
||||
}
|
||||
|
||||
pub struct Room {
|
||||
id: u64,
|
||||
status: RoomStatus,
|
||||
local_participant: LocalParticipant,
|
||||
remote_participants: HashMap<PeerId, RemoteParticipant>,
|
||||
pending_users: Vec<Arc<User>>,
|
||||
client: Arc<Client>,
|
||||
user_store: ModelHandle<UserStore>,
|
||||
_subscriptions: Vec<client::Subscription>,
|
||||
_load_pending_users: Option<Task<()>>,
|
||||
}
|
||||
|
||||
impl Entity for Room {
|
||||
type Event = Event;
|
||||
}
|
||||
|
||||
impl Room {
|
||||
fn new(
|
||||
id: u64,
|
||||
client: Arc<Client>,
|
||||
user_store: ModelHandle<UserStore>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Self {
|
||||
let mut client_status = client.status();
|
||||
cx.spawn_weak(|this, mut cx| async move {
|
||||
let is_connected = client_status
|
||||
.next()
|
||||
.await
|
||||
.map_or(false, |s| s.is_connected());
|
||||
// Even if we're initially connected, any future change of the status means we momentarily disconnected.
|
||||
if !is_connected || client_status.next().await.is_some() {
|
||||
if let Some(this) = this.upgrade(&cx) {
|
||||
let _ = this.update(&mut cx, |this, cx| this.leave(cx));
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
Self {
|
||||
id,
|
||||
status: RoomStatus::Online,
|
||||
local_participant: LocalParticipant {
|
||||
projects: Default::default(),
|
||||
},
|
||||
remote_participants: Default::default(),
|
||||
pending_users: Default::default(),
|
||||
_subscriptions: vec![client.add_message_handler(cx.handle(), Self::handle_room_updated)],
|
||||
_load_pending_users: None,
|
||||
client,
|
||||
user_store,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create(
|
||||
client: Arc<Client>,
|
||||
user_store: ModelHandle<UserStore>,
|
||||
cx: &mut MutableAppContext,
|
||||
) -> Task<Result<ModelHandle<Self>>> {
|
||||
cx.spawn(|mut cx| async move {
|
||||
let room = client.request(proto::CreateRoom {}).await?;
|
||||
Ok(cx.add_model(|cx| Self::new(room.id, client, user_store, cx)))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn join(
|
||||
call: &IncomingCall,
|
||||
client: Arc<Client>,
|
||||
user_store: ModelHandle<UserStore>,
|
||||
cx: &mut MutableAppContext,
|
||||
) -> Task<Result<ModelHandle<Self>>> {
|
||||
let room_id = call.room_id;
|
||||
cx.spawn(|mut cx| async move {
|
||||
let response = client.request(proto::JoinRoom { id: room_id }).await?;
|
||||
let room_proto = response.room.ok_or_else(|| anyhow!("invalid room"))?;
|
||||
let room = cx.add_model(|cx| Self::new(room_id, client, user_store, cx));
|
||||
room.update(&mut cx, |room, cx| room.apply_room_update(room_proto, cx))?;
|
||||
Ok(room)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn leave(&mut self, cx: &mut ModelContext<Self>) -> Result<()> {
|
||||
if self.status.is_offline() {
|
||||
return Err(anyhow!("room is offline"));
|
||||
}
|
||||
|
||||
cx.notify();
|
||||
self.status = RoomStatus::Offline;
|
||||
self.remote_participants.clear();
|
||||
self.client.send(proto::LeaveRoom { id: self.id })?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remote_participants(&self) -> &HashMap<PeerId, RemoteParticipant> {
|
||||
&self.remote_participants
|
||||
}
|
||||
|
||||
pub fn pending_users(&self) -> &[Arc<User>] {
|
||||
&self.pending_users
|
||||
}
|
||||
|
||||
async fn handle_room_updated(
|
||||
this: ModelHandle<Self>,
|
||||
envelope: TypedEnvelope<proto::RoomUpdated>,
|
||||
_: Arc<Client>,
|
||||
mut cx: AsyncAppContext,
|
||||
) -> Result<()> {
|
||||
let room = envelope
|
||||
.payload
|
||||
.room
|
||||
.ok_or_else(|| anyhow!("invalid room"))?;
|
||||
this.update(&mut cx, |this, cx| this.apply_room_update(room, cx))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_room_update(&mut self, room: proto::Room, cx: &mut ModelContext<Self>) -> Result<()> {
|
||||
// TODO: compute diff instead of clearing participants
|
||||
self.remote_participants.clear();
|
||||
for participant in room.participants {
|
||||
if Some(participant.user_id) != self.client.user_id() {
|
||||
self.remote_participants.insert(
|
||||
PeerId(participant.peer_id),
|
||||
RemoteParticipant {
|
||||
user_id: participant.user_id,
|
||||
projects: Default::default(), // TODO: populate projects
|
||||
location: ParticipantLocation::from_proto(participant.location)?,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let pending_users = self.user_store.update(cx, move |user_store, cx| {
|
||||
user_store.get_users(room.pending_user_ids, cx)
|
||||
});
|
||||
self._load_pending_users = Some(cx.spawn(|this, mut cx| async move {
|
||||
if let Some(pending_users) = pending_users.await.log_err() {
|
||||
this.update(&mut cx, |this, cx| {
|
||||
this.pending_users = pending_users;
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
cx.notify();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn call(
|
||||
&mut self,
|
||||
recipient_user_id: u64,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<()>> {
|
||||
if self.status.is_offline() {
|
||||
return Task::ready(Err(anyhow!("room is offline")));
|
||||
}
|
||||
|
||||
let client = self.client.clone();
|
||||
let room_id = self.id;
|
||||
cx.foreground().spawn(async move {
|
||||
client
|
||||
.request(proto::Call {
|
||||
room_id,
|
||||
recipient_user_id,
|
||||
})
|
||||
.await?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn publish_project(&mut self, project: ModelHandle<Project>) -> Task<Result<()>> {
|
||||
if self.status.is_offline() {
|
||||
return Task::ready(Err(anyhow!("room is offline")));
|
||||
}
|
||||
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn unpublish_project(&mut self, project: ModelHandle<Project>) -> Task<Result<()>> {
|
||||
if self.status.is_offline() {
|
||||
return Task::ready(Err(anyhow!("room is offline")));
|
||||
}
|
||||
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn set_active_project(
|
||||
&mut self,
|
||||
project: Option<&ModelHandle<Project>>,
|
||||
) -> Task<Result<()>> {
|
||||
if self.status.is_offline() {
|
||||
return Task::ready(Err(anyhow!("room is offline")));
|
||||
}
|
||||
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
pub enum RoomStatus {
|
||||
Online,
|
||||
Offline,
|
||||
}
|
||||
|
||||
impl RoomStatus {
|
||||
fn is_offline(&self) -> bool {
|
||||
matches!(self, RoomStatus::Offline)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue