1mod participant;
2pub mod room;
3
4use anyhow::{anyhow, Result};
5use client::{proto, Client, TypedEnvelope, User, UserStore};
6use gpui::{
7 AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
8 Subscription, Task,
9};
10pub use participant::ParticipantLocation;
11use postage::watch;
12use project::Project;
13pub use room::Room;
14use std::sync::Arc;
15
16pub fn init(client: Arc<Client>, user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) {
17 let active_call = cx.add_model(|cx| ActiveCall::new(client, user_store, cx));
18 cx.set_global(active_call);
19}
20
21#[derive(Clone)]
22pub struct IncomingCall {
23 pub room_id: u64,
24 pub caller: Arc<User>,
25 pub participants: Vec<Arc<User>>,
26 pub initial_project: Option<proto::ParticipantProject>,
27}
28
29pub struct ActiveCall {
30 room: Option<(ModelHandle<Room>, Vec<Subscription>)>,
31 incoming_call: (
32 watch::Sender<Option<IncomingCall>>,
33 watch::Receiver<Option<IncomingCall>>,
34 ),
35 client: Arc<Client>,
36 user_store: ModelHandle<UserStore>,
37 _subscriptions: Vec<client::Subscription>,
38}
39
40impl Entity for ActiveCall {
41 type Event = room::Event;
42}
43
44impl ActiveCall {
45 fn new(
46 client: Arc<Client>,
47 user_store: ModelHandle<UserStore>,
48 cx: &mut ModelContext<Self>,
49 ) -> Self {
50 Self {
51 room: None,
52 incoming_call: watch::channel(),
53 _subscriptions: vec![
54 client.add_request_handler(cx.handle(), Self::handle_incoming_call),
55 client.add_message_handler(cx.handle(), Self::handle_call_canceled),
56 ],
57 client,
58 user_store,
59 }
60 }
61
62 async fn handle_incoming_call(
63 this: ModelHandle<Self>,
64 envelope: TypedEnvelope<proto::IncomingCall>,
65 _: Arc<Client>,
66 mut cx: AsyncAppContext,
67 ) -> Result<proto::Ack> {
68 let user_store = this.read_with(&cx, |this, _| this.user_store.clone());
69 let call = IncomingCall {
70 room_id: envelope.payload.room_id,
71 participants: user_store
72 .update(&mut cx, |user_store, cx| {
73 user_store.get_users(envelope.payload.participant_user_ids, cx)
74 })
75 .await?,
76 caller: user_store
77 .update(&mut cx, |user_store, cx| {
78 user_store.get_user(envelope.payload.caller_user_id, cx)
79 })
80 .await?,
81 initial_project: envelope.payload.initial_project,
82 };
83 this.update(&mut cx, |this, _| {
84 *this.incoming_call.0.borrow_mut() = Some(call);
85 });
86
87 Ok(proto::Ack {})
88 }
89
90 async fn handle_call_canceled(
91 this: ModelHandle<Self>,
92 _: TypedEnvelope<proto::CallCanceled>,
93 _: Arc<Client>,
94 mut cx: AsyncAppContext,
95 ) -> Result<()> {
96 this.update(&mut cx, |this, _| {
97 *this.incoming_call.0.borrow_mut() = None;
98 });
99 Ok(())
100 }
101
102 pub fn global(cx: &AppContext) -> ModelHandle<Self> {
103 cx.global::<ModelHandle<Self>>().clone()
104 }
105
106 pub fn invite(
107 &mut self,
108 recipient_user_id: u64,
109 initial_project: Option<ModelHandle<Project>>,
110 cx: &mut ModelContext<Self>,
111 ) -> Task<Result<()>> {
112 let client = self.client.clone();
113 let user_store = self.user_store.clone();
114 cx.spawn(|this, mut cx| async move {
115 if let Some(room) = this.read_with(&cx, |this, _| this.room().cloned()) {
116 let initial_project_id = if let Some(initial_project) = initial_project {
117 Some(
118 room.update(&mut cx, |room, cx| room.share_project(initial_project, cx))
119 .await?,
120 )
121 } else {
122 None
123 };
124
125 room.update(&mut cx, |room, cx| {
126 room.call(recipient_user_id, initial_project_id, cx)
127 })
128 .await?;
129 } else {
130 let room = cx
131 .update(|cx| {
132 Room::create(recipient_user_id, initial_project, client, user_store, cx)
133 })
134 .await?;
135 room.update(&mut cx, |room, cx| room.share_screen(cx))
136 .await?;
137 this.update(&mut cx, |this, cx| this.set_room(Some(room), cx));
138 };
139
140 Ok(())
141 })
142 }
143
144 pub fn cancel_invite(
145 &mut self,
146 recipient_user_id: u64,
147 cx: &mut ModelContext<Self>,
148 ) -> Task<Result<()>> {
149 let room_id = if let Some(room) = self.room() {
150 room.read(cx).id()
151 } else {
152 return Task::ready(Err(anyhow!("no active call")));
153 };
154
155 let client = self.client.clone();
156 cx.foreground().spawn(async move {
157 client
158 .request(proto::CancelCall {
159 room_id,
160 recipient_user_id,
161 })
162 .await?;
163 anyhow::Ok(())
164 })
165 }
166
167 pub fn incoming(&self) -> watch::Receiver<Option<IncomingCall>> {
168 self.incoming_call.1.clone()
169 }
170
171 pub fn accept_incoming(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
172 if self.room.is_some() {
173 return Task::ready(Err(anyhow!("cannot join while on another call")));
174 }
175
176 let call = if let Some(call) = self.incoming_call.1.borrow().clone() {
177 call
178 } else {
179 return Task::ready(Err(anyhow!("no incoming call")));
180 };
181
182 let join = Room::join(&call, self.client.clone(), self.user_store.clone(), cx);
183 cx.spawn(|this, mut cx| async move {
184 let room = join.await?;
185 this.update(&mut cx, |this, cx| this.set_room(Some(room.clone()), cx));
186 Ok(())
187 })
188 }
189
190 pub fn decline_incoming(&mut self) -> Result<()> {
191 let call = self
192 .incoming_call
193 .0
194 .borrow_mut()
195 .take()
196 .ok_or_else(|| anyhow!("no incoming call"))?;
197 self.client.send(proto::DeclineCall {
198 room_id: call.room_id,
199 })?;
200 Ok(())
201 }
202
203 pub fn hang_up(&mut self, cx: &mut ModelContext<Self>) -> Result<()> {
204 if let Some((room, _)) = self.room.take() {
205 room.update(cx, |room, cx| room.leave(cx))?;
206 cx.notify();
207 }
208 Ok(())
209 }
210
211 pub fn share_project(
212 &mut self,
213 project: ModelHandle<Project>,
214 cx: &mut ModelContext<Self>,
215 ) -> Task<Result<u64>> {
216 if let Some((room, _)) = self.room.as_ref() {
217 room.update(cx, |room, cx| room.share_project(project, cx))
218 } else {
219 Task::ready(Err(anyhow!("no active call")))
220 }
221 }
222
223 pub fn set_location(
224 &mut self,
225 project: Option<&ModelHandle<Project>>,
226 cx: &mut ModelContext<Self>,
227 ) -> Task<Result<()>> {
228 if let Some((room, _)) = self.room.as_ref() {
229 room.update(cx, |room, cx| room.set_location(project, cx))
230 } else {
231 Task::ready(Err(anyhow!("no active call")))
232 }
233 }
234
235 fn set_room(&mut self, room: Option<ModelHandle<Room>>, cx: &mut ModelContext<Self>) {
236 if room.as_ref() != self.room.as_ref().map(|room| &room.0) {
237 if let Some(room) = room {
238 if room.read(cx).status().is_offline() {
239 self.room = None;
240 } else {
241 let subscriptions = vec![
242 cx.observe(&room, |this, room, cx| {
243 if room.read(cx).status().is_offline() {
244 this.set_room(None, cx);
245 }
246
247 cx.notify();
248 }),
249 cx.subscribe(&room, |_, _, event, cx| cx.emit(event.clone())),
250 ];
251 self.room = Some((room, subscriptions));
252 }
253 } else {
254 self.room = None;
255 }
256 cx.notify();
257 }
258 }
259
260 pub fn room(&self) -> Option<&ModelHandle<Room>> {
261 self.room.as_ref().map(|(room, _)| room)
262 }
263}