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_id: Option<u64>,
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_id: envelope.payload.initial_project_id,
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 room = self.room.as_ref().map(|(room, _)| room.clone());
113 let client = self.client.clone();
114 let user_store = self.user_store.clone();
115 cx.spawn(|this, mut cx| async move {
116 let room = if let Some(room) = room {
117 room
118 } else {
119 cx.update(|cx| Room::create(client, user_store, cx)).await?
120 };
121
122 let initial_project_id = if let Some(initial_project) = initial_project {
123 Some(
124 room.update(&mut cx, |room, cx| room.share_project(initial_project, cx))
125 .await?,
126 )
127 } else {
128 None
129 };
130
131 this.update(&mut cx, |this, cx| this.set_room(Some(room.clone()), cx));
132 room.update(&mut cx, |room, cx| {
133 room.call(recipient_user_id, initial_project_id, cx)
134 })
135 .await?;
136
137 Ok(())
138 })
139 }
140
141 pub fn cancel_invite(
142 &mut self,
143 recipient_user_id: u64,
144 cx: &mut ModelContext<Self>,
145 ) -> Task<Result<()>> {
146 let room_id = if let Some(room) = self.room() {
147 room.read(cx).id()
148 } else {
149 return Task::ready(Err(anyhow!("no active call")));
150 };
151
152 let client = self.client.clone();
153 cx.foreground().spawn(async move {
154 client
155 .request(proto::CancelCall {
156 room_id,
157 recipient_user_id,
158 })
159 .await?;
160 anyhow::Ok(())
161 })
162 }
163
164 pub fn incoming(&self) -> watch::Receiver<Option<IncomingCall>> {
165 self.incoming_call.1.clone()
166 }
167
168 pub fn accept_incoming(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
169 if self.room.is_some() {
170 return Task::ready(Err(anyhow!("cannot join while on another call")));
171 }
172
173 let call = if let Some(call) = self.incoming_call.1.borrow().clone() {
174 call
175 } else {
176 return Task::ready(Err(anyhow!("no incoming call")));
177 };
178
179 let join = Room::join(&call, self.client.clone(), self.user_store.clone(), cx);
180 cx.spawn(|this, mut cx| async move {
181 let room = join.await?;
182 this.update(&mut cx, |this, cx| this.set_room(Some(room.clone()), cx));
183 Ok(())
184 })
185 }
186
187 pub fn decline_incoming(&mut self) -> Result<()> {
188 let call = self
189 .incoming_call
190 .0
191 .borrow_mut()
192 .take()
193 .ok_or_else(|| anyhow!("no incoming call"))?;
194 self.client.send(proto::DeclineCall {
195 room_id: call.room_id,
196 })?;
197 Ok(())
198 }
199
200 pub fn hang_up(&mut self, cx: &mut ModelContext<Self>) -> Result<()> {
201 if let Some((room, _)) = self.room.take() {
202 room.update(cx, |room, cx| room.leave(cx))?;
203 }
204 Ok(())
205 }
206
207 pub fn share_project(
208 &mut self,
209 project: ModelHandle<Project>,
210 cx: &mut ModelContext<Self>,
211 ) -> Task<Result<u64>> {
212 if let Some((room, _)) = self.room.as_ref() {
213 room.update(cx, |room, cx| room.share_project(project, cx))
214 } else {
215 Task::ready(Err(anyhow!("no active call")))
216 }
217 }
218
219 pub fn set_location(
220 &mut self,
221 project: Option<&ModelHandle<Project>>,
222 cx: &mut ModelContext<Self>,
223 ) -> Task<Result<()>> {
224 if let Some((room, _)) = self.room.as_ref() {
225 room.update(cx, |room, cx| room.set_location(project, cx))
226 } else {
227 Task::ready(Err(anyhow!("no active call")))
228 }
229 }
230
231 fn set_room(&mut self, room: Option<ModelHandle<Room>>, cx: &mut ModelContext<Self>) {
232 if room.as_ref() != self.room.as_ref().map(|room| &room.0) {
233 if let Some(room) = room {
234 let subscriptions = vec![
235 cx.observe(&room, |this, room, cx| {
236 if room.read(cx).status().is_offline() {
237 this.set_room(None, cx);
238 }
239
240 cx.notify();
241 }),
242 cx.subscribe(&room, |_, _, event, cx| cx.emit(event.clone())),
243 ];
244 self.room = Some((room, subscriptions));
245 } else {
246 self.room = None;
247 }
248 cx.notify();
249 }
250 }
251
252 pub fn room(&self) -> Option<&ModelHandle<Room>> {
253 self.room.as_ref().map(|(room, _)| room)
254 }
255}