1mod connection_pool;
2
3use crate::{
4 auth,
5 db::{self, Database, ProjectId, RoomId, ServerId, User, UserId},
6 executor::Executor,
7 AppState, Result,
8};
9use anyhow::anyhow;
10use async_tungstenite::tungstenite::{
11 protocol::CloseFrame as TungsteniteCloseFrame, Message as TungsteniteMessage,
12};
13use axum::{
14 body::Body,
15 extract::{
16 ws::{CloseFrame as AxumCloseFrame, Message as AxumMessage},
17 ConnectInfo, WebSocketUpgrade,
18 },
19 headers::{Header, HeaderName},
20 http::StatusCode,
21 middleware,
22 response::IntoResponse,
23 routing::get,
24 Extension, Router, TypedHeader,
25};
26use collections::{HashMap, HashSet};
27pub use connection_pool::ConnectionPool;
28use futures::{
29 channel::oneshot,
30 future::{self, BoxFuture},
31 stream::FuturesUnordered,
32 FutureExt, SinkExt, StreamExt, TryStreamExt,
33};
34use lazy_static::lazy_static;
35use prometheus::{register_int_gauge, IntGauge};
36use rpc::{
37 proto::{self, AnyTypedEnvelope, EntityMessage, EnvelopedMessage, RequestMessage},
38 Connection, ConnectionId, Peer, Receipt, TypedEnvelope,
39};
40use serde::{Serialize, Serializer};
41use std::{
42 any::TypeId,
43 fmt,
44 future::Future,
45 marker::PhantomData,
46 mem,
47 net::SocketAddr,
48 ops::{Deref, DerefMut},
49 rc::Rc,
50 sync::{
51 atomic::{AtomicBool, Ordering::SeqCst},
52 Arc,
53 },
54 time::Duration,
55};
56use tokio::sync::watch;
57use tower::ServiceBuilder;
58use tracing::{info_span, instrument, Instrument};
59
60pub const RECONNECT_TIMEOUT: Duration = Duration::from_secs(5);
61pub const CLEANUP_TIMEOUT: Duration = Duration::from_secs(10);
62
63lazy_static! {
64 static ref METRIC_CONNECTIONS: IntGauge =
65 register_int_gauge!("connections", "number of connections").unwrap();
66 static ref METRIC_SHARED_PROJECTS: IntGauge = register_int_gauge!(
67 "shared_projects",
68 "number of open projects with one or more guests"
69 )
70 .unwrap();
71}
72
73type MessageHandler =
74 Box<dyn Send + Sync + Fn(Box<dyn AnyTypedEnvelope>, Session) -> BoxFuture<'static, ()>>;
75
76struct Response<R> {
77 peer: Arc<Peer>,
78 receipt: Receipt<R>,
79 responded: Arc<AtomicBool>,
80}
81
82impl<R: RequestMessage> Response<R> {
83 fn send(self, payload: R::Response) -> Result<()> {
84 self.responded.store(true, SeqCst);
85 self.peer.respond(self.receipt, payload)?;
86 Ok(())
87 }
88}
89
90#[derive(Clone)]
91struct Session {
92 user_id: UserId,
93 connection_id: ConnectionId,
94 db: Arc<tokio::sync::Mutex<DbHandle>>,
95 peer: Arc<Peer>,
96 connection_pool: Arc<parking_lot::Mutex<ConnectionPool>>,
97 live_kit_client: Option<Arc<dyn live_kit_server::api::Client>>,
98 executor: Executor,
99}
100
101impl Session {
102 async fn db(&self) -> tokio::sync::MutexGuard<DbHandle> {
103 #[cfg(test)]
104 tokio::task::yield_now().await;
105 let guard = self.db.lock().await;
106 #[cfg(test)]
107 tokio::task::yield_now().await;
108 guard
109 }
110
111 async fn connection_pool(&self) -> ConnectionPoolGuard<'_> {
112 #[cfg(test)]
113 tokio::task::yield_now().await;
114 let guard = self.connection_pool.lock();
115 ConnectionPoolGuard {
116 guard,
117 _not_send: PhantomData,
118 }
119 }
120}
121
122impl fmt::Debug for Session {
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 f.debug_struct("Session")
125 .field("user_id", &self.user_id)
126 .field("connection_id", &self.connection_id)
127 .finish()
128 }
129}
130
131struct DbHandle(Arc<Database>);
132
133impl Deref for DbHandle {
134 type Target = Database;
135
136 fn deref(&self) -> &Self::Target {
137 self.0.as_ref()
138 }
139}
140
141pub struct Server {
142 id: parking_lot::Mutex<ServerId>,
143 peer: Arc<Peer>,
144 pub(crate) connection_pool: Arc<parking_lot::Mutex<ConnectionPool>>,
145 app_state: Arc<AppState>,
146 executor: Executor,
147 handlers: HashMap<TypeId, MessageHandler>,
148 teardown: watch::Sender<()>,
149}
150
151pub(crate) struct ConnectionPoolGuard<'a> {
152 guard: parking_lot::MutexGuard<'a, ConnectionPool>,
153 _not_send: PhantomData<Rc<()>>,
154}
155
156#[derive(Serialize)]
157pub struct ServerSnapshot<'a> {
158 peer: &'a Peer,
159 #[serde(serialize_with = "serialize_deref")]
160 connection_pool: ConnectionPoolGuard<'a>,
161}
162
163pub fn serialize_deref<S, T, U>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
164where
165 S: Serializer,
166 T: Deref<Target = U>,
167 U: Serialize,
168{
169 Serialize::serialize(value.deref(), serializer)
170}
171
172impl Server {
173 pub fn new(id: ServerId, app_state: Arc<AppState>, executor: Executor) -> Arc<Self> {
174 let mut server = Self {
175 id: parking_lot::Mutex::new(id),
176 peer: Peer::new(id.0 as u32),
177 app_state,
178 executor,
179 connection_pool: Default::default(),
180 handlers: Default::default(),
181 teardown: watch::channel(()).0,
182 };
183
184 server
185 .add_request_handler(ping)
186 .add_request_handler(create_room)
187 .add_request_handler(join_room)
188 .add_request_handler(rejoin_room)
189 .add_message_handler(leave_room)
190 .add_request_handler(call)
191 .add_request_handler(cancel_call)
192 .add_message_handler(decline_call)
193 .add_request_handler(update_participant_location)
194 .add_request_handler(share_project)
195 .add_message_handler(unshare_project)
196 .add_request_handler(join_project)
197 .add_message_handler(leave_project)
198 .add_request_handler(update_project)
199 .add_request_handler(update_worktree)
200 .add_message_handler(start_language_server)
201 .add_message_handler(update_language_server)
202 .add_message_handler(update_diagnostic_summary)
203 .add_request_handler(forward_project_request::<proto::GetHover>)
204 .add_request_handler(forward_project_request::<proto::GetDefinition>)
205 .add_request_handler(forward_project_request::<proto::GetTypeDefinition>)
206 .add_request_handler(forward_project_request::<proto::GetReferences>)
207 .add_request_handler(forward_project_request::<proto::SearchProject>)
208 .add_request_handler(forward_project_request::<proto::GetDocumentHighlights>)
209 .add_request_handler(forward_project_request::<proto::GetProjectSymbols>)
210 .add_request_handler(forward_project_request::<proto::OpenBufferForSymbol>)
211 .add_request_handler(forward_project_request::<proto::OpenBufferById>)
212 .add_request_handler(forward_project_request::<proto::OpenBufferByPath>)
213 .add_request_handler(forward_project_request::<proto::GetCompletions>)
214 .add_request_handler(forward_project_request::<proto::ApplyCompletionAdditionalEdits>)
215 .add_request_handler(forward_project_request::<proto::GetCodeActions>)
216 .add_request_handler(forward_project_request::<proto::ApplyCodeAction>)
217 .add_request_handler(forward_project_request::<proto::PrepareRename>)
218 .add_request_handler(forward_project_request::<proto::PerformRename>)
219 .add_request_handler(forward_project_request::<proto::ReloadBuffers>)
220 .add_request_handler(forward_project_request::<proto::SynchronizeBuffers>)
221 .add_request_handler(forward_project_request::<proto::FormatBuffers>)
222 .add_request_handler(forward_project_request::<proto::CreateProjectEntry>)
223 .add_request_handler(forward_project_request::<proto::RenameProjectEntry>)
224 .add_request_handler(forward_project_request::<proto::CopyProjectEntry>)
225 .add_request_handler(forward_project_request::<proto::DeleteProjectEntry>)
226 .add_message_handler(create_buffer_for_peer)
227 .add_request_handler(update_buffer)
228 .add_message_handler(update_buffer_file)
229 .add_message_handler(buffer_reloaded)
230 .add_message_handler(buffer_saved)
231 .add_request_handler(save_buffer)
232 .add_request_handler(get_users)
233 .add_request_handler(fuzzy_search_users)
234 .add_request_handler(request_contact)
235 .add_request_handler(remove_contact)
236 .add_request_handler(respond_to_contact_request)
237 .add_request_handler(follow)
238 .add_message_handler(unfollow)
239 .add_message_handler(update_followers)
240 .add_message_handler(update_diff_base)
241 .add_request_handler(get_private_user_info);
242
243 Arc::new(server)
244 }
245
246 pub async fn start(&self) -> Result<()> {
247 let server_id = *self.id.lock();
248 let app_state = self.app_state.clone();
249 let peer = self.peer.clone();
250 let timeout = self.executor.sleep(CLEANUP_TIMEOUT);
251 let pool = self.connection_pool.clone();
252 let live_kit_client = self.app_state.live_kit_client.clone();
253
254 let span = info_span!("start server");
255 self.executor.spawn_detached(
256 async move {
257 tracing::info!("waiting for cleanup timeout");
258 timeout.await;
259 tracing::info!("cleanup timeout expired, retrieving stale rooms");
260 if let Some(room_ids) = app_state
261 .db
262 .stale_room_ids(&app_state.config.zed_environment, server_id)
263 .await
264 .trace_err()
265 {
266 tracing::info!(stale_room_count = room_ids.len(), "retrieved stale rooms");
267 for room_id in room_ids {
268 let mut contacts_to_update = HashSet::default();
269 let mut canceled_calls_to_user_ids = Vec::new();
270 let mut live_kit_room = String::new();
271 let mut delete_live_kit_room = false;
272
273 if let Ok(mut refreshed_room) =
274 app_state.db.refresh_room(room_id, server_id).await
275 {
276 tracing::info!(
277 room_id = room_id.0,
278 new_participant_count = refreshed_room.room.participants.len(),
279 "refreshed room"
280 );
281 room_updated(&refreshed_room.room, &peer);
282 contacts_to_update
283 .extend(refreshed_room.stale_participant_user_ids.iter().copied());
284 contacts_to_update
285 .extend(refreshed_room.canceled_calls_to_user_ids.iter().copied());
286 canceled_calls_to_user_ids =
287 mem::take(&mut refreshed_room.canceled_calls_to_user_ids);
288 live_kit_room = mem::take(&mut refreshed_room.room.live_kit_room);
289 delete_live_kit_room = refreshed_room.room.participants.is_empty();
290 }
291
292 {
293 let pool = pool.lock();
294 for canceled_user_id in canceled_calls_to_user_ids {
295 for connection_id in pool.user_connection_ids(canceled_user_id) {
296 peer.send(
297 connection_id,
298 proto::CallCanceled {
299 room_id: room_id.to_proto(),
300 },
301 )
302 .trace_err();
303 }
304 }
305 }
306
307 for user_id in contacts_to_update {
308 let busy = app_state.db.is_user_busy(user_id).await.trace_err();
309 let contacts = app_state.db.get_contacts(user_id).await.trace_err();
310 if let Some((busy, contacts)) = busy.zip(contacts) {
311 let pool = pool.lock();
312 let updated_contact = contact_for_user(user_id, false, busy, &pool);
313 for contact in contacts {
314 if let db::Contact::Accepted {
315 user_id: contact_user_id,
316 ..
317 } = contact
318 {
319 for contact_conn_id in
320 pool.user_connection_ids(contact_user_id)
321 {
322 peer.send(
323 contact_conn_id,
324 proto::UpdateContacts {
325 contacts: vec![updated_contact.clone()],
326 remove_contacts: Default::default(),
327 incoming_requests: Default::default(),
328 remove_incoming_requests: Default::default(),
329 outgoing_requests: Default::default(),
330 remove_outgoing_requests: Default::default(),
331 },
332 )
333 .trace_err();
334 }
335 }
336 }
337 }
338 }
339
340 if let Some(live_kit) = live_kit_client.as_ref() {
341 if delete_live_kit_room {
342 live_kit.delete_room(live_kit_room).await.trace_err();
343 }
344 }
345 }
346 }
347
348 app_state
349 .db
350 .delete_stale_servers(&app_state.config.zed_environment, server_id)
351 .await
352 .trace_err();
353 }
354 .instrument(span),
355 );
356 Ok(())
357 }
358
359 pub fn teardown(&self) {
360 self.peer.teardown();
361 self.connection_pool.lock().reset();
362 let _ = self.teardown.send(());
363 }
364
365 #[cfg(test)]
366 pub fn reset(&self, id: ServerId) {
367 self.teardown();
368 *self.id.lock() = id;
369 self.peer.reset(id.0 as u32);
370 }
371
372 #[cfg(test)]
373 pub fn id(&self) -> ServerId {
374 *self.id.lock()
375 }
376
377 fn add_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
378 where
379 F: 'static + Send + Sync + Fn(TypedEnvelope<M>, Session) -> Fut,
380 Fut: 'static + Send + Future<Output = Result<()>>,
381 M: EnvelopedMessage,
382 {
383 let prev_handler = self.handlers.insert(
384 TypeId::of::<M>(),
385 Box::new(move |envelope, session| {
386 let envelope = envelope.into_any().downcast::<TypedEnvelope<M>>().unwrap();
387 let span = info_span!(
388 "handle message",
389 payload_type = envelope.payload_type_name()
390 );
391 span.in_scope(|| {
392 tracing::info!(
393 payload_type = envelope.payload_type_name(),
394 "message received"
395 );
396 });
397 let future = (handler)(*envelope, session);
398 async move {
399 if let Err(error) = future.await {
400 tracing::error!(%error, "error handling message");
401 }
402 }
403 .instrument(span)
404 .boxed()
405 }),
406 );
407 if prev_handler.is_some() {
408 panic!("registered a handler for the same message twice");
409 }
410 self
411 }
412
413 fn add_message_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
414 where
415 F: 'static + Send + Sync + Fn(M, Session) -> Fut,
416 Fut: 'static + Send + Future<Output = Result<()>>,
417 M: EnvelopedMessage,
418 {
419 self.add_handler(move |envelope, session| handler(envelope.payload, session));
420 self
421 }
422
423 fn add_request_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
424 where
425 F: 'static + Send + Sync + Fn(M, Response<M>, Session) -> Fut,
426 Fut: Send + Future<Output = Result<()>>,
427 M: RequestMessage,
428 {
429 let handler = Arc::new(handler);
430 self.add_handler(move |envelope, session| {
431 let receipt = envelope.receipt();
432 let handler = handler.clone();
433 async move {
434 let peer = session.peer.clone();
435 let responded = Arc::new(AtomicBool::default());
436 let response = Response {
437 peer: peer.clone(),
438 responded: responded.clone(),
439 receipt,
440 };
441 match (handler)(envelope.payload, response, session).await {
442 Ok(()) => {
443 if responded.load(std::sync::atomic::Ordering::SeqCst) {
444 Ok(())
445 } else {
446 Err(anyhow!("handler did not send a response"))?
447 }
448 }
449 Err(error) => {
450 peer.respond_with_error(
451 receipt,
452 proto::Error {
453 message: error.to_string(),
454 },
455 )?;
456 Err(error)
457 }
458 }
459 }
460 })
461 }
462
463 pub fn handle_connection(
464 self: &Arc<Self>,
465 connection: Connection,
466 address: String,
467 user: User,
468 mut send_connection_id: Option<oneshot::Sender<ConnectionId>>,
469 executor: Executor,
470 ) -> impl Future<Output = Result<()>> {
471 let this = self.clone();
472 let user_id = user.id;
473 let login = user.github_login;
474 let span = info_span!("handle connection", %user_id, %login, %address);
475 let mut teardown = self.teardown.subscribe();
476 async move {
477 let (connection_id, handle_io, mut incoming_rx) = this
478 .peer
479 .add_connection(connection, {
480 let executor = executor.clone();
481 move |duration| executor.sleep(duration)
482 });
483
484 tracing::info!(%user_id, %login, %connection_id, %address, "connection opened");
485 this.peer.send(connection_id, proto::Hello { peer_id: Some(connection_id.into()) })?;
486 tracing::info!(%user_id, %login, %connection_id, %address, "sent hello message");
487
488 if let Some(send_connection_id) = send_connection_id.take() {
489 let _ = send_connection_id.send(connection_id);
490 }
491
492 if !user.connected_once {
493 this.peer.send(connection_id, proto::ShowContacts {})?;
494 this.app_state.db.set_user_connected_once(user_id, true).await?;
495 }
496
497 let (contacts, invite_code) = future::try_join(
498 this.app_state.db.get_contacts(user_id),
499 this.app_state.db.get_invite_code_for_user(user_id)
500 ).await?;
501
502 {
503 let mut pool = this.connection_pool.lock();
504 pool.add_connection(connection_id, user_id, user.admin);
505 this.peer.send(connection_id, build_initial_contacts_update(contacts, &pool))?;
506
507 if let Some((code, count)) = invite_code {
508 this.peer.send(connection_id, proto::UpdateInviteInfo {
509 url: format!("{}{}", this.app_state.config.invite_link_prefix, code),
510 count: count as u32,
511 })?;
512 }
513 }
514
515 if let Some(incoming_call) = this.app_state.db.incoming_call_for_user(user_id).await? {
516 this.peer.send(connection_id, incoming_call)?;
517 }
518
519 let session = Session {
520 user_id,
521 connection_id,
522 db: Arc::new(tokio::sync::Mutex::new(DbHandle(this.app_state.db.clone()))),
523 peer: this.peer.clone(),
524 connection_pool: this.connection_pool.clone(),
525 live_kit_client: this.app_state.live_kit_client.clone(),
526 executor: executor.clone(),
527 };
528 update_user_contacts(user_id, &session).await?;
529
530 let handle_io = handle_io.fuse();
531 futures::pin_mut!(handle_io);
532
533 // Handlers for foreground messages are pushed into the following `FuturesUnordered`.
534 // This prevents deadlocks when e.g., client A performs a request to client B and
535 // client B performs a request to client A. If both clients stop processing further
536 // messages until their respective request completes, they won't have a chance to
537 // respond to the other client's request and cause a deadlock.
538 //
539 // This arrangement ensures we will attempt to process earlier messages first, but fall
540 // back to processing messages arrived later in the spirit of making progress.
541 let mut foreground_message_handlers = FuturesUnordered::new();
542 loop {
543 let next_message = incoming_rx.next().fuse();
544 futures::pin_mut!(next_message);
545 futures::select_biased! {
546 _ = teardown.changed().fuse() => return Ok(()),
547 result = handle_io => {
548 if let Err(error) = result {
549 tracing::error!(?error, %user_id, %login, %connection_id, %address, "error handling I/O");
550 }
551 break;
552 }
553 _ = foreground_message_handlers.next() => {}
554 message = next_message => {
555 if let Some(message) = message {
556 let type_name = message.payload_type_name();
557 let span = tracing::info_span!("receive message", %user_id, %login, %connection_id, %address, type_name);
558 let span_enter = span.enter();
559 if let Some(handler) = this.handlers.get(&message.payload_type_id()) {
560 let is_background = message.is_background();
561 let handle_message = (handler)(message, session.clone());
562 drop(span_enter);
563
564 let handle_message = handle_message.instrument(span);
565 if is_background {
566 executor.spawn_detached(handle_message);
567 } else {
568 foreground_message_handlers.push(handle_message);
569 }
570 } else {
571 tracing::error!(%user_id, %login, %connection_id, %address, "no message handler");
572 }
573 } else {
574 tracing::info!(%user_id, %login, %connection_id, %address, "connection closed");
575 break;
576 }
577 }
578 }
579 }
580
581 drop(foreground_message_handlers);
582 tracing::info!(%user_id, %login, %connection_id, %address, "signing out");
583 if let Err(error) = connection_lost(session, teardown, executor).await {
584 tracing::error!(%user_id, %login, %connection_id, %address, ?error, "error signing out");
585 }
586
587 Ok(())
588 }.instrument(span)
589 }
590
591 pub async fn invite_code_redeemed(
592 self: &Arc<Self>,
593 inviter_id: UserId,
594 invitee_id: UserId,
595 ) -> Result<()> {
596 if let Some(user) = self.app_state.db.get_user_by_id(inviter_id).await? {
597 if let Some(code) = &user.invite_code {
598 let pool = self.connection_pool.lock();
599 let invitee_contact = contact_for_user(invitee_id, true, false, &pool);
600 for connection_id in pool.user_connection_ids(inviter_id) {
601 self.peer.send(
602 connection_id,
603 proto::UpdateContacts {
604 contacts: vec![invitee_contact.clone()],
605 ..Default::default()
606 },
607 )?;
608 self.peer.send(
609 connection_id,
610 proto::UpdateInviteInfo {
611 url: format!("{}{}", self.app_state.config.invite_link_prefix, &code),
612 count: user.invite_count as u32,
613 },
614 )?;
615 }
616 }
617 }
618 Ok(())
619 }
620
621 pub async fn invite_count_updated(self: &Arc<Self>, user_id: UserId) -> Result<()> {
622 if let Some(user) = self.app_state.db.get_user_by_id(user_id).await? {
623 if let Some(invite_code) = &user.invite_code {
624 let pool = self.connection_pool.lock();
625 for connection_id in pool.user_connection_ids(user_id) {
626 self.peer.send(
627 connection_id,
628 proto::UpdateInviteInfo {
629 url: format!(
630 "{}{}",
631 self.app_state.config.invite_link_prefix, invite_code
632 ),
633 count: user.invite_count as u32,
634 },
635 )?;
636 }
637 }
638 }
639 Ok(())
640 }
641
642 pub async fn snapshot<'a>(self: &'a Arc<Self>) -> ServerSnapshot<'a> {
643 ServerSnapshot {
644 connection_pool: ConnectionPoolGuard {
645 guard: self.connection_pool.lock(),
646 _not_send: PhantomData,
647 },
648 peer: &self.peer,
649 }
650 }
651}
652
653impl<'a> Deref for ConnectionPoolGuard<'a> {
654 type Target = ConnectionPool;
655
656 fn deref(&self) -> &Self::Target {
657 &*self.guard
658 }
659}
660
661impl<'a> DerefMut for ConnectionPoolGuard<'a> {
662 fn deref_mut(&mut self) -> &mut Self::Target {
663 &mut *self.guard
664 }
665}
666
667impl<'a> Drop for ConnectionPoolGuard<'a> {
668 fn drop(&mut self) {
669 #[cfg(test)]
670 self.check_invariants();
671 }
672}
673
674fn broadcast<F>(
675 sender_id: Option<ConnectionId>,
676 receiver_ids: impl IntoIterator<Item = ConnectionId>,
677 mut f: F,
678) where
679 F: FnMut(ConnectionId) -> anyhow::Result<()>,
680{
681 for receiver_id in receiver_ids {
682 if Some(receiver_id) != sender_id {
683 if let Err(error) = f(receiver_id) {
684 tracing::error!("failed to send to {:?} {}", receiver_id, error);
685 }
686 }
687 }
688}
689
690lazy_static! {
691 static ref ZED_PROTOCOL_VERSION: HeaderName = HeaderName::from_static("x-zed-protocol-version");
692}
693
694pub struct ProtocolVersion(u32);
695
696impl Header for ProtocolVersion {
697 fn name() -> &'static HeaderName {
698 &ZED_PROTOCOL_VERSION
699 }
700
701 fn decode<'i, I>(values: &mut I) -> Result<Self, axum::headers::Error>
702 where
703 Self: Sized,
704 I: Iterator<Item = &'i axum::http::HeaderValue>,
705 {
706 let version = values
707 .next()
708 .ok_or_else(axum::headers::Error::invalid)?
709 .to_str()
710 .map_err(|_| axum::headers::Error::invalid())?
711 .parse()
712 .map_err(|_| axum::headers::Error::invalid())?;
713 Ok(Self(version))
714 }
715
716 fn encode<E: Extend<axum::http::HeaderValue>>(&self, values: &mut E) {
717 values.extend([self.0.to_string().parse().unwrap()]);
718 }
719}
720
721pub fn routes(server: Arc<Server>) -> Router<Body> {
722 Router::new()
723 .route("/rpc", get(handle_websocket_request))
724 .layer(
725 ServiceBuilder::new()
726 .layer(Extension(server.app_state.clone()))
727 .layer(middleware::from_fn(auth::validate_header)),
728 )
729 .route("/metrics", get(handle_metrics))
730 .layer(Extension(server))
731}
732
733pub async fn handle_websocket_request(
734 TypedHeader(ProtocolVersion(protocol_version)): TypedHeader<ProtocolVersion>,
735 ConnectInfo(socket_address): ConnectInfo<SocketAddr>,
736 Extension(server): Extension<Arc<Server>>,
737 Extension(user): Extension<User>,
738 ws: WebSocketUpgrade,
739) -> axum::response::Response {
740 if protocol_version != rpc::PROTOCOL_VERSION {
741 return (
742 StatusCode::UPGRADE_REQUIRED,
743 "client must be upgraded".to_string(),
744 )
745 .into_response();
746 }
747 let socket_address = socket_address.to_string();
748 ws.on_upgrade(move |socket| {
749 use util::ResultExt;
750 let socket = socket
751 .map_ok(to_tungstenite_message)
752 .err_into()
753 .with(|message| async move { Ok(to_axum_message(message)) });
754 let connection = Connection::new(Box::pin(socket));
755 async move {
756 server
757 .handle_connection(connection, socket_address, user, None, Executor::Production)
758 .await
759 .log_err();
760 }
761 })
762}
763
764pub async fn handle_metrics(Extension(server): Extension<Arc<Server>>) -> Result<String> {
765 let connections = server
766 .connection_pool
767 .lock()
768 .connections()
769 .filter(|connection| !connection.admin)
770 .count();
771
772 METRIC_CONNECTIONS.set(connections as _);
773
774 let shared_projects = server.app_state.db.project_count_excluding_admins().await?;
775 METRIC_SHARED_PROJECTS.set(shared_projects as _);
776
777 let encoder = prometheus::TextEncoder::new();
778 let metric_families = prometheus::gather();
779 let encoded_metrics = encoder
780 .encode_to_string(&metric_families)
781 .map_err(|err| anyhow!("{}", err))?;
782 Ok(encoded_metrics)
783}
784
785#[instrument(err, skip(executor))]
786async fn connection_lost(
787 session: Session,
788 mut teardown: watch::Receiver<()>,
789 executor: Executor,
790) -> Result<()> {
791 session.peer.disconnect(session.connection_id);
792 session
793 .connection_pool()
794 .await
795 .remove_connection(session.connection_id)?;
796
797 session
798 .db()
799 .await
800 .connection_lost(session.connection_id)
801 .await
802 .trace_err();
803
804 futures::select_biased! {
805 _ = executor.sleep(RECONNECT_TIMEOUT).fuse() => {
806 leave_room_for_session(&session).await.trace_err();
807
808 if !session
809 .connection_pool()
810 .await
811 .is_user_online(session.user_id)
812 {
813 let db = session.db().await;
814 if let Some(room) = db.decline_call(None, session.user_id).await.trace_err().flatten() {
815 room_updated(&room, &session.peer);
816 }
817 }
818 update_user_contacts(session.user_id, &session).await?;
819 }
820 _ = teardown.changed().fuse() => {}
821 }
822
823 Ok(())
824}
825
826async fn ping(_: proto::Ping, response: Response<proto::Ping>, _session: Session) -> Result<()> {
827 response.send(proto::Ack {})?;
828 Ok(())
829}
830
831async fn create_room(
832 _request: proto::CreateRoom,
833 response: Response<proto::CreateRoom>,
834 session: Session,
835) -> Result<()> {
836 let live_kit_room = nanoid::nanoid!(30);
837 let live_kit_connection_info = if let Some(live_kit) = session.live_kit_client.as_ref() {
838 if let Some(_) = live_kit
839 .create_room(live_kit_room.clone())
840 .await
841 .trace_err()
842 {
843 if let Some(token) = live_kit
844 .room_token(&live_kit_room, &session.user_id.to_string())
845 .trace_err()
846 {
847 Some(proto::LiveKitConnectionInfo {
848 server_url: live_kit.url().into(),
849 token,
850 })
851 } else {
852 None
853 }
854 } else {
855 None
856 }
857 } else {
858 None
859 };
860
861 {
862 let room = session
863 .db()
864 .await
865 .create_room(session.user_id, session.connection_id, &live_kit_room)
866 .await?;
867
868 response.send(proto::CreateRoomResponse {
869 room: Some(room.clone()),
870 live_kit_connection_info,
871 })?;
872 }
873
874 update_user_contacts(session.user_id, &session).await?;
875 Ok(())
876}
877
878async fn join_room(
879 request: proto::JoinRoom,
880 response: Response<proto::JoinRoom>,
881 session: Session,
882) -> Result<()> {
883 let room_id = RoomId::from_proto(request.id);
884 let room = {
885 let room = session
886 .db()
887 .await
888 .join_room(room_id, session.user_id, session.connection_id)
889 .await?;
890 room_updated(&room, &session.peer);
891 room.clone()
892 };
893
894 for connection_id in session
895 .connection_pool()
896 .await
897 .user_connection_ids(session.user_id)
898 {
899 session
900 .peer
901 .send(
902 connection_id,
903 proto::CallCanceled {
904 room_id: room_id.to_proto(),
905 },
906 )
907 .trace_err();
908 }
909
910 let live_kit_connection_info = if let Some(live_kit) = session.live_kit_client.as_ref() {
911 if let Some(token) = live_kit
912 .room_token(&room.live_kit_room, &session.user_id.to_string())
913 .trace_err()
914 {
915 Some(proto::LiveKitConnectionInfo {
916 server_url: live_kit.url().into(),
917 token,
918 })
919 } else {
920 None
921 }
922 } else {
923 None
924 };
925
926 response.send(proto::JoinRoomResponse {
927 room: Some(room),
928 live_kit_connection_info,
929 })?;
930
931 update_user_contacts(session.user_id, &session).await?;
932 Ok(())
933}
934
935async fn rejoin_room(
936 request: proto::RejoinRoom,
937 response: Response<proto::RejoinRoom>,
938 session: Session,
939) -> Result<()> {
940 {
941 let mut rejoined_room = session
942 .db()
943 .await
944 .rejoin_room(request, session.user_id, session.connection_id)
945 .await?;
946
947 response.send(proto::RejoinRoomResponse {
948 room: Some(rejoined_room.room.clone()),
949 reshared_projects: rejoined_room
950 .reshared_projects
951 .iter()
952 .map(|project| proto::ResharedProject {
953 id: project.id.to_proto(),
954 collaborators: project
955 .collaborators
956 .iter()
957 .map(|collaborator| collaborator.to_proto())
958 .collect(),
959 })
960 .collect(),
961 rejoined_projects: rejoined_room
962 .rejoined_projects
963 .iter()
964 .map(|rejoined_project| proto::RejoinedProject {
965 id: rejoined_project.id.to_proto(),
966 worktrees: rejoined_project
967 .worktrees
968 .iter()
969 .map(|worktree| proto::WorktreeMetadata {
970 id: worktree.id,
971 root_name: worktree.root_name.clone(),
972 visible: worktree.visible,
973 abs_path: worktree.abs_path.clone(),
974 })
975 .collect(),
976 collaborators: rejoined_project
977 .collaborators
978 .iter()
979 .map(|collaborator| collaborator.to_proto())
980 .collect(),
981 language_servers: rejoined_project.language_servers.clone(),
982 })
983 .collect(),
984 })?;
985 room_updated(&rejoined_room.room, &session.peer);
986
987 for project in &rejoined_room.reshared_projects {
988 for collaborator in &project.collaborators {
989 session
990 .peer
991 .send(
992 collaborator.connection_id,
993 proto::UpdateProjectCollaborator {
994 project_id: project.id.to_proto(),
995 old_peer_id: Some(project.old_connection_id.into()),
996 new_peer_id: Some(session.connection_id.into()),
997 },
998 )
999 .trace_err();
1000 }
1001
1002 broadcast(
1003 Some(session.connection_id),
1004 project
1005 .collaborators
1006 .iter()
1007 .map(|collaborator| collaborator.connection_id),
1008 |connection_id| {
1009 session.peer.forward_send(
1010 session.connection_id,
1011 connection_id,
1012 proto::UpdateProject {
1013 project_id: project.id.to_proto(),
1014 worktrees: project.worktrees.clone(),
1015 },
1016 )
1017 },
1018 );
1019 }
1020
1021 for project in &rejoined_room.rejoined_projects {
1022 for collaborator in &project.collaborators {
1023 session
1024 .peer
1025 .send(
1026 collaborator.connection_id,
1027 proto::UpdateProjectCollaborator {
1028 project_id: project.id.to_proto(),
1029 old_peer_id: Some(project.old_connection_id.into()),
1030 new_peer_id: Some(session.connection_id.into()),
1031 },
1032 )
1033 .trace_err();
1034 }
1035 }
1036
1037 for project in &mut rejoined_room.rejoined_projects {
1038 for worktree in mem::take(&mut project.worktrees) {
1039 #[cfg(any(test, feature = "test-support"))]
1040 const MAX_CHUNK_SIZE: usize = 2;
1041 #[cfg(not(any(test, feature = "test-support")))]
1042 const MAX_CHUNK_SIZE: usize = 256;
1043
1044 // Stream this worktree's entries.
1045 let message = proto::UpdateWorktree {
1046 project_id: project.id.to_proto(),
1047 worktree_id: worktree.id,
1048 abs_path: worktree.abs_path.clone(),
1049 root_name: worktree.root_name,
1050 updated_entries: worktree.updated_entries,
1051 removed_entries: worktree.removed_entries,
1052 scan_id: worktree.scan_id,
1053 is_last_update: worktree.completed_scan_id == worktree.scan_id,
1054 };
1055 for update in proto::split_worktree_update(message, MAX_CHUNK_SIZE) {
1056 session.peer.send(session.connection_id, update.clone())?;
1057 }
1058
1059 // Stream this worktree's diagnostics.
1060 for summary in worktree.diagnostic_summaries {
1061 session.peer.send(
1062 session.connection_id,
1063 proto::UpdateDiagnosticSummary {
1064 project_id: project.id.to_proto(),
1065 worktree_id: worktree.id,
1066 summary: Some(summary),
1067 },
1068 )?;
1069 }
1070 }
1071
1072 for language_server in &project.language_servers {
1073 session.peer.send(
1074 session.connection_id,
1075 proto::UpdateLanguageServer {
1076 project_id: project.id.to_proto(),
1077 language_server_id: language_server.id,
1078 variant: Some(
1079 proto::update_language_server::Variant::DiskBasedDiagnosticsUpdated(
1080 proto::LspDiskBasedDiagnosticsUpdated {},
1081 ),
1082 ),
1083 },
1084 )?;
1085 }
1086 }
1087 }
1088
1089 update_user_contacts(session.user_id, &session).await?;
1090 Ok(())
1091}
1092
1093async fn leave_room(_message: proto::LeaveRoom, session: Session) -> Result<()> {
1094 leave_room_for_session(&session).await
1095}
1096
1097async fn call(
1098 request: proto::Call,
1099 response: Response<proto::Call>,
1100 session: Session,
1101) -> Result<()> {
1102 let room_id = RoomId::from_proto(request.room_id);
1103 let calling_user_id = session.user_id;
1104 let calling_connection_id = session.connection_id;
1105 let called_user_id = UserId::from_proto(request.called_user_id);
1106 let initial_project_id = request.initial_project_id.map(ProjectId::from_proto);
1107 if !session
1108 .db()
1109 .await
1110 .has_contact(calling_user_id, called_user_id)
1111 .await?
1112 {
1113 return Err(anyhow!("cannot call a user who isn't a contact"))?;
1114 }
1115
1116 let incoming_call = {
1117 let (room, incoming_call) = &mut *session
1118 .db()
1119 .await
1120 .call(
1121 room_id,
1122 calling_user_id,
1123 calling_connection_id,
1124 called_user_id,
1125 initial_project_id,
1126 )
1127 .await?;
1128 room_updated(&room, &session.peer);
1129 mem::take(incoming_call)
1130 };
1131 update_user_contacts(called_user_id, &session).await?;
1132
1133 let mut calls = session
1134 .connection_pool()
1135 .await
1136 .user_connection_ids(called_user_id)
1137 .map(|connection_id| session.peer.request(connection_id, incoming_call.clone()))
1138 .collect::<FuturesUnordered<_>>();
1139
1140 while let Some(call_response) = calls.next().await {
1141 match call_response.as_ref() {
1142 Ok(_) => {
1143 response.send(proto::Ack {})?;
1144 return Ok(());
1145 }
1146 Err(_) => {
1147 call_response.trace_err();
1148 }
1149 }
1150 }
1151
1152 {
1153 let room = session
1154 .db()
1155 .await
1156 .call_failed(room_id, called_user_id)
1157 .await?;
1158 room_updated(&room, &session.peer);
1159 }
1160 update_user_contacts(called_user_id, &session).await?;
1161
1162 Err(anyhow!("failed to ring user"))?
1163}
1164
1165async fn cancel_call(
1166 request: proto::CancelCall,
1167 response: Response<proto::CancelCall>,
1168 session: Session,
1169) -> Result<()> {
1170 let called_user_id = UserId::from_proto(request.called_user_id);
1171 let room_id = RoomId::from_proto(request.room_id);
1172 {
1173 let room = session
1174 .db()
1175 .await
1176 .cancel_call(room_id, session.connection_id, called_user_id)
1177 .await?;
1178 room_updated(&room, &session.peer);
1179 }
1180
1181 for connection_id in session
1182 .connection_pool()
1183 .await
1184 .user_connection_ids(called_user_id)
1185 {
1186 session
1187 .peer
1188 .send(
1189 connection_id,
1190 proto::CallCanceled {
1191 room_id: room_id.to_proto(),
1192 },
1193 )
1194 .trace_err();
1195 }
1196 response.send(proto::Ack {})?;
1197
1198 update_user_contacts(called_user_id, &session).await?;
1199 Ok(())
1200}
1201
1202async fn decline_call(message: proto::DeclineCall, session: Session) -> Result<()> {
1203 let room_id = RoomId::from_proto(message.room_id);
1204 {
1205 let room = session
1206 .db()
1207 .await
1208 .decline_call(Some(room_id), session.user_id)
1209 .await?
1210 .ok_or_else(|| anyhow!("failed to decline call"))?;
1211 room_updated(&room, &session.peer);
1212 }
1213
1214 for connection_id in session
1215 .connection_pool()
1216 .await
1217 .user_connection_ids(session.user_id)
1218 {
1219 session
1220 .peer
1221 .send(
1222 connection_id,
1223 proto::CallCanceled {
1224 room_id: room_id.to_proto(),
1225 },
1226 )
1227 .trace_err();
1228 }
1229 update_user_contacts(session.user_id, &session).await?;
1230 Ok(())
1231}
1232
1233async fn update_participant_location(
1234 request: proto::UpdateParticipantLocation,
1235 response: Response<proto::UpdateParticipantLocation>,
1236 session: Session,
1237) -> Result<()> {
1238 let room_id = RoomId::from_proto(request.room_id);
1239 let location = request
1240 .location
1241 .ok_or_else(|| anyhow!("invalid location"))?;
1242 let room = session
1243 .db()
1244 .await
1245 .update_room_participant_location(room_id, session.connection_id, location)
1246 .await?;
1247 room_updated(&room, &session.peer);
1248 response.send(proto::Ack {})?;
1249 Ok(())
1250}
1251
1252async fn share_project(
1253 request: proto::ShareProject,
1254 response: Response<proto::ShareProject>,
1255 session: Session,
1256) -> Result<()> {
1257 let (project_id, room) = &*session
1258 .db()
1259 .await
1260 .share_project(
1261 RoomId::from_proto(request.room_id),
1262 session.connection_id,
1263 &request.worktrees,
1264 )
1265 .await?;
1266 response.send(proto::ShareProjectResponse {
1267 project_id: project_id.to_proto(),
1268 })?;
1269 room_updated(&room, &session.peer);
1270
1271 Ok(())
1272}
1273
1274async fn unshare_project(message: proto::UnshareProject, session: Session) -> Result<()> {
1275 let project_id = ProjectId::from_proto(message.project_id);
1276
1277 let (room, guest_connection_ids) = &*session
1278 .db()
1279 .await
1280 .unshare_project(project_id, session.connection_id)
1281 .await?;
1282
1283 broadcast(
1284 Some(session.connection_id),
1285 guest_connection_ids.iter().copied(),
1286 |conn_id| session.peer.send(conn_id, message.clone()),
1287 );
1288 room_updated(&room, &session.peer);
1289
1290 Ok(())
1291}
1292
1293async fn join_project(
1294 request: proto::JoinProject,
1295 response: Response<proto::JoinProject>,
1296 session: Session,
1297) -> Result<()> {
1298 let project_id = ProjectId::from_proto(request.project_id);
1299 let guest_user_id = session.user_id;
1300
1301 tracing::info!(%project_id, "join project");
1302
1303 let (project, replica_id) = &mut *session
1304 .db()
1305 .await
1306 .join_project(project_id, session.connection_id)
1307 .await?;
1308
1309 let collaborators = project
1310 .collaborators
1311 .iter()
1312 .filter(|collaborator| collaborator.connection_id != session.connection_id)
1313 .map(|collaborator| collaborator.to_proto())
1314 .collect::<Vec<_>>();
1315 let worktrees = project
1316 .worktrees
1317 .iter()
1318 .map(|(id, worktree)| proto::WorktreeMetadata {
1319 id: *id,
1320 root_name: worktree.root_name.clone(),
1321 visible: worktree.visible,
1322 abs_path: worktree.abs_path.clone(),
1323 })
1324 .collect::<Vec<_>>();
1325
1326 for collaborator in &collaborators {
1327 session
1328 .peer
1329 .send(
1330 collaborator.peer_id.unwrap().into(),
1331 proto::AddProjectCollaborator {
1332 project_id: project_id.to_proto(),
1333 collaborator: Some(proto::Collaborator {
1334 peer_id: Some(session.connection_id.into()),
1335 replica_id: replica_id.0 as u32,
1336 user_id: guest_user_id.to_proto(),
1337 }),
1338 },
1339 )
1340 .trace_err();
1341 }
1342
1343 // First, we send the metadata associated with each worktree.
1344 response.send(proto::JoinProjectResponse {
1345 worktrees: worktrees.clone(),
1346 replica_id: replica_id.0 as u32,
1347 collaborators: collaborators.clone(),
1348 language_servers: project.language_servers.clone(),
1349 })?;
1350
1351 for (worktree_id, worktree) in mem::take(&mut project.worktrees) {
1352 #[cfg(any(test, feature = "test-support"))]
1353 const MAX_CHUNK_SIZE: usize = 2;
1354 #[cfg(not(any(test, feature = "test-support")))]
1355 const MAX_CHUNK_SIZE: usize = 256;
1356
1357 // Stream this worktree's entries.
1358 let message = proto::UpdateWorktree {
1359 project_id: project_id.to_proto(),
1360 worktree_id,
1361 abs_path: worktree.abs_path.clone(),
1362 root_name: worktree.root_name,
1363 updated_entries: worktree.entries,
1364 removed_entries: Default::default(),
1365 scan_id: worktree.scan_id,
1366 is_last_update: worktree.scan_id == worktree.completed_scan_id,
1367 };
1368 for update in proto::split_worktree_update(message, MAX_CHUNK_SIZE) {
1369 session.peer.send(session.connection_id, update.clone())?;
1370 }
1371
1372 // Stream this worktree's diagnostics.
1373 for summary in worktree.diagnostic_summaries {
1374 session.peer.send(
1375 session.connection_id,
1376 proto::UpdateDiagnosticSummary {
1377 project_id: project_id.to_proto(),
1378 worktree_id: worktree.id,
1379 summary: Some(summary),
1380 },
1381 )?;
1382 }
1383 }
1384
1385 for language_server in &project.language_servers {
1386 session.peer.send(
1387 session.connection_id,
1388 proto::UpdateLanguageServer {
1389 project_id: project_id.to_proto(),
1390 language_server_id: language_server.id,
1391 variant: Some(
1392 proto::update_language_server::Variant::DiskBasedDiagnosticsUpdated(
1393 proto::LspDiskBasedDiagnosticsUpdated {},
1394 ),
1395 ),
1396 },
1397 )?;
1398 }
1399
1400 Ok(())
1401}
1402
1403async fn leave_project(request: proto::LeaveProject, session: Session) -> Result<()> {
1404 let sender_id = session.connection_id;
1405 let project_id = ProjectId::from_proto(request.project_id);
1406
1407 let project = session
1408 .db()
1409 .await
1410 .leave_project(project_id, sender_id)
1411 .await?;
1412 tracing::info!(
1413 %project_id,
1414 host_user_id = %project.host_user_id,
1415 host_connection_id = %project.host_connection_id,
1416 "leave project"
1417 );
1418 project_left(&project, &session);
1419
1420 Ok(())
1421}
1422
1423async fn update_project(
1424 request: proto::UpdateProject,
1425 response: Response<proto::UpdateProject>,
1426 session: Session,
1427) -> Result<()> {
1428 let project_id = ProjectId::from_proto(request.project_id);
1429 let (room, guest_connection_ids) = &*session
1430 .db()
1431 .await
1432 .update_project(project_id, session.connection_id, &request.worktrees)
1433 .await?;
1434 broadcast(
1435 Some(session.connection_id),
1436 guest_connection_ids.iter().copied(),
1437 |connection_id| {
1438 session
1439 .peer
1440 .forward_send(session.connection_id, connection_id, request.clone())
1441 },
1442 );
1443 room_updated(&room, &session.peer);
1444 response.send(proto::Ack {})?;
1445
1446 Ok(())
1447}
1448
1449async fn update_worktree(
1450 request: proto::UpdateWorktree,
1451 response: Response<proto::UpdateWorktree>,
1452 session: Session,
1453) -> Result<()> {
1454 let guest_connection_ids = session
1455 .db()
1456 .await
1457 .update_worktree(&request, session.connection_id)
1458 .await?;
1459
1460 broadcast(
1461 Some(session.connection_id),
1462 guest_connection_ids.iter().copied(),
1463 |connection_id| {
1464 session
1465 .peer
1466 .forward_send(session.connection_id, connection_id, request.clone())
1467 },
1468 );
1469 response.send(proto::Ack {})?;
1470 Ok(())
1471}
1472
1473async fn update_diagnostic_summary(
1474 message: proto::UpdateDiagnosticSummary,
1475 session: Session,
1476) -> Result<()> {
1477 let guest_connection_ids = session
1478 .db()
1479 .await
1480 .update_diagnostic_summary(&message, session.connection_id)
1481 .await?;
1482
1483 broadcast(
1484 Some(session.connection_id),
1485 guest_connection_ids.iter().copied(),
1486 |connection_id| {
1487 session
1488 .peer
1489 .forward_send(session.connection_id, connection_id, message.clone())
1490 },
1491 );
1492
1493 Ok(())
1494}
1495
1496async fn start_language_server(
1497 request: proto::StartLanguageServer,
1498 session: Session,
1499) -> Result<()> {
1500 let guest_connection_ids = session
1501 .db()
1502 .await
1503 .start_language_server(&request, session.connection_id)
1504 .await?;
1505
1506 broadcast(
1507 Some(session.connection_id),
1508 guest_connection_ids.iter().copied(),
1509 |connection_id| {
1510 session
1511 .peer
1512 .forward_send(session.connection_id, connection_id, request.clone())
1513 },
1514 );
1515 Ok(())
1516}
1517
1518async fn update_language_server(
1519 request: proto::UpdateLanguageServer,
1520 session: Session,
1521) -> Result<()> {
1522 session.executor.record_backtrace();
1523 let project_id = ProjectId::from_proto(request.project_id);
1524 let project_connection_ids = session
1525 .db()
1526 .await
1527 .project_connection_ids(project_id, session.connection_id)
1528 .await?;
1529 broadcast(
1530 Some(session.connection_id),
1531 project_connection_ids.iter().copied(),
1532 |connection_id| {
1533 session
1534 .peer
1535 .forward_send(session.connection_id, connection_id, request.clone())
1536 },
1537 );
1538 Ok(())
1539}
1540
1541async fn forward_project_request<T>(
1542 request: T,
1543 response: Response<T>,
1544 session: Session,
1545) -> Result<()>
1546where
1547 T: EntityMessage + RequestMessage,
1548{
1549 session.executor.record_backtrace();
1550 let project_id = ProjectId::from_proto(request.remote_entity_id());
1551 let host_connection_id = {
1552 let collaborators = session
1553 .db()
1554 .await
1555 .project_collaborators(project_id, session.connection_id)
1556 .await?;
1557 collaborators
1558 .iter()
1559 .find(|collaborator| collaborator.is_host)
1560 .ok_or_else(|| anyhow!("host not found"))?
1561 .connection_id
1562 };
1563
1564 let payload = session
1565 .peer
1566 .forward_request(session.connection_id, host_connection_id, request)
1567 .await?;
1568
1569 response.send(payload)?;
1570 Ok(())
1571}
1572
1573async fn save_buffer(
1574 request: proto::SaveBuffer,
1575 response: Response<proto::SaveBuffer>,
1576 session: Session,
1577) -> Result<()> {
1578 let project_id = ProjectId::from_proto(request.project_id);
1579 let host_connection_id = {
1580 let collaborators = session
1581 .db()
1582 .await
1583 .project_collaborators(project_id, session.connection_id)
1584 .await?;
1585 collaborators
1586 .iter()
1587 .find(|collaborator| collaborator.is_host)
1588 .ok_or_else(|| anyhow!("host not found"))?
1589 .connection_id
1590 };
1591 let response_payload = session
1592 .peer
1593 .forward_request(session.connection_id, host_connection_id, request.clone())
1594 .await?;
1595
1596 let mut collaborators = session
1597 .db()
1598 .await
1599 .project_collaborators(project_id, session.connection_id)
1600 .await?;
1601 collaborators.retain(|collaborator| collaborator.connection_id != session.connection_id);
1602 let project_connection_ids = collaborators
1603 .iter()
1604 .map(|collaborator| collaborator.connection_id);
1605 broadcast(
1606 Some(host_connection_id),
1607 project_connection_ids,
1608 |conn_id| {
1609 session
1610 .peer
1611 .forward_send(host_connection_id, conn_id, response_payload.clone())
1612 },
1613 );
1614 response.send(response_payload)?;
1615 Ok(())
1616}
1617
1618async fn create_buffer_for_peer(
1619 request: proto::CreateBufferForPeer,
1620 session: Session,
1621) -> Result<()> {
1622 session.executor.record_backtrace();
1623 let peer_id = request.peer_id.ok_or_else(|| anyhow!("invalid peer id"))?;
1624 session
1625 .peer
1626 .forward_send(session.connection_id, peer_id.into(), request)?;
1627 Ok(())
1628}
1629
1630async fn update_buffer(
1631 request: proto::UpdateBuffer,
1632 response: Response<proto::UpdateBuffer>,
1633 session: Session,
1634) -> Result<()> {
1635 session.executor.record_backtrace();
1636 let project_id = ProjectId::from_proto(request.project_id);
1637 let project_connection_ids = session
1638 .db()
1639 .await
1640 .project_connection_ids(project_id, session.connection_id)
1641 .await?;
1642
1643 session.executor.record_backtrace();
1644
1645 broadcast(
1646 Some(session.connection_id),
1647 project_connection_ids.iter().copied(),
1648 |connection_id| {
1649 session
1650 .peer
1651 .forward_send(session.connection_id, connection_id, request.clone())
1652 },
1653 );
1654 response.send(proto::Ack {})?;
1655 Ok(())
1656}
1657
1658async fn update_buffer_file(request: proto::UpdateBufferFile, session: Session) -> Result<()> {
1659 let project_id = ProjectId::from_proto(request.project_id);
1660 let project_connection_ids = session
1661 .db()
1662 .await
1663 .project_connection_ids(project_id, session.connection_id)
1664 .await?;
1665
1666 broadcast(
1667 Some(session.connection_id),
1668 project_connection_ids.iter().copied(),
1669 |connection_id| {
1670 session
1671 .peer
1672 .forward_send(session.connection_id, connection_id, request.clone())
1673 },
1674 );
1675 Ok(())
1676}
1677
1678async fn buffer_reloaded(request: proto::BufferReloaded, session: Session) -> Result<()> {
1679 let project_id = ProjectId::from_proto(request.project_id);
1680 let project_connection_ids = session
1681 .db()
1682 .await
1683 .project_connection_ids(project_id, session.connection_id)
1684 .await?;
1685 broadcast(
1686 Some(session.connection_id),
1687 project_connection_ids.iter().copied(),
1688 |connection_id| {
1689 session
1690 .peer
1691 .forward_send(session.connection_id, connection_id, request.clone())
1692 },
1693 );
1694 Ok(())
1695}
1696
1697async fn buffer_saved(request: proto::BufferSaved, session: Session) -> Result<()> {
1698 let project_id = ProjectId::from_proto(request.project_id);
1699 let project_connection_ids = session
1700 .db()
1701 .await
1702 .project_connection_ids(project_id, session.connection_id)
1703 .await?;
1704 broadcast(
1705 Some(session.connection_id),
1706 project_connection_ids.iter().copied(),
1707 |connection_id| {
1708 session
1709 .peer
1710 .forward_send(session.connection_id, connection_id, request.clone())
1711 },
1712 );
1713 Ok(())
1714}
1715
1716async fn follow(
1717 request: proto::Follow,
1718 response: Response<proto::Follow>,
1719 session: Session,
1720) -> Result<()> {
1721 let project_id = ProjectId::from_proto(request.project_id);
1722 let leader_id = request
1723 .leader_id
1724 .ok_or_else(|| anyhow!("invalid leader id"))?
1725 .into();
1726 let follower_id = session.connection_id;
1727 {
1728 let project_connection_ids = session
1729 .db()
1730 .await
1731 .project_connection_ids(project_id, session.connection_id)
1732 .await?;
1733
1734 if !project_connection_ids.contains(&leader_id) {
1735 Err(anyhow!("no such peer"))?;
1736 }
1737 }
1738
1739 let mut response_payload = session
1740 .peer
1741 .forward_request(session.connection_id, leader_id, request)
1742 .await?;
1743 response_payload
1744 .views
1745 .retain(|view| view.leader_id != Some(follower_id.into()));
1746 response.send(response_payload)?;
1747 Ok(())
1748}
1749
1750async fn unfollow(request: proto::Unfollow, session: Session) -> Result<()> {
1751 let project_id = ProjectId::from_proto(request.project_id);
1752 let leader_id = request
1753 .leader_id
1754 .ok_or_else(|| anyhow!("invalid leader id"))?
1755 .into();
1756 let project_connection_ids = session
1757 .db()
1758 .await
1759 .project_connection_ids(project_id, session.connection_id)
1760 .await?;
1761 if !project_connection_ids.contains(&leader_id) {
1762 Err(anyhow!("no such peer"))?;
1763 }
1764 session
1765 .peer
1766 .forward_send(session.connection_id, leader_id, request)?;
1767 Ok(())
1768}
1769
1770async fn update_followers(request: proto::UpdateFollowers, session: Session) -> Result<()> {
1771 let project_id = ProjectId::from_proto(request.project_id);
1772 let project_connection_ids = session
1773 .db
1774 .lock()
1775 .await
1776 .project_connection_ids(project_id, session.connection_id)
1777 .await?;
1778
1779 let leader_id = request.variant.as_ref().and_then(|variant| match variant {
1780 proto::update_followers::Variant::CreateView(payload) => payload.leader_id,
1781 proto::update_followers::Variant::UpdateView(payload) => payload.leader_id,
1782 proto::update_followers::Variant::UpdateActiveView(payload) => payload.leader_id,
1783 });
1784 for follower_peer_id in request.follower_ids.iter().copied() {
1785 let follower_connection_id = follower_peer_id.into();
1786 if project_connection_ids.contains(&follower_connection_id)
1787 && Some(follower_peer_id) != leader_id
1788 {
1789 session.peer.forward_send(
1790 session.connection_id,
1791 follower_connection_id,
1792 request.clone(),
1793 )?;
1794 }
1795 }
1796 Ok(())
1797}
1798
1799async fn get_users(
1800 request: proto::GetUsers,
1801 response: Response<proto::GetUsers>,
1802 session: Session,
1803) -> Result<()> {
1804 let user_ids = request
1805 .user_ids
1806 .into_iter()
1807 .map(UserId::from_proto)
1808 .collect();
1809 let users = session
1810 .db()
1811 .await
1812 .get_users_by_ids(user_ids)
1813 .await?
1814 .into_iter()
1815 .map(|user| proto::User {
1816 id: user.id.to_proto(),
1817 avatar_url: format!("https://github.com/{}.png?size=128", user.github_login),
1818 github_login: user.github_login,
1819 })
1820 .collect();
1821 response.send(proto::UsersResponse { users })?;
1822 Ok(())
1823}
1824
1825async fn fuzzy_search_users(
1826 request: proto::FuzzySearchUsers,
1827 response: Response<proto::FuzzySearchUsers>,
1828 session: Session,
1829) -> Result<()> {
1830 let query = request.query;
1831 let users = match query.len() {
1832 0 => vec![],
1833 1 | 2 => session
1834 .db()
1835 .await
1836 .get_user_by_github_account(&query, None)
1837 .await?
1838 .into_iter()
1839 .collect(),
1840 _ => session.db().await.fuzzy_search_users(&query, 10).await?,
1841 };
1842 let users = users
1843 .into_iter()
1844 .filter(|user| user.id != session.user_id)
1845 .map(|user| proto::User {
1846 id: user.id.to_proto(),
1847 avatar_url: format!("https://github.com/{}.png?size=128", user.github_login),
1848 github_login: user.github_login,
1849 })
1850 .collect();
1851 response.send(proto::UsersResponse { users })?;
1852 Ok(())
1853}
1854
1855async fn request_contact(
1856 request: proto::RequestContact,
1857 response: Response<proto::RequestContact>,
1858 session: Session,
1859) -> Result<()> {
1860 let requester_id = session.user_id;
1861 let responder_id = UserId::from_proto(request.responder_id);
1862 if requester_id == responder_id {
1863 return Err(anyhow!("cannot add yourself as a contact"))?;
1864 }
1865
1866 session
1867 .db()
1868 .await
1869 .send_contact_request(requester_id, responder_id)
1870 .await?;
1871
1872 // Update outgoing contact requests of requester
1873 let mut update = proto::UpdateContacts::default();
1874 update.outgoing_requests.push(responder_id.to_proto());
1875 for connection_id in session
1876 .connection_pool()
1877 .await
1878 .user_connection_ids(requester_id)
1879 {
1880 session.peer.send(connection_id, update.clone())?;
1881 }
1882
1883 // Update incoming contact requests of responder
1884 let mut update = proto::UpdateContacts::default();
1885 update
1886 .incoming_requests
1887 .push(proto::IncomingContactRequest {
1888 requester_id: requester_id.to_proto(),
1889 should_notify: true,
1890 });
1891 for connection_id in session
1892 .connection_pool()
1893 .await
1894 .user_connection_ids(responder_id)
1895 {
1896 session.peer.send(connection_id, update.clone())?;
1897 }
1898
1899 response.send(proto::Ack {})?;
1900 Ok(())
1901}
1902
1903async fn respond_to_contact_request(
1904 request: proto::RespondToContactRequest,
1905 response: Response<proto::RespondToContactRequest>,
1906 session: Session,
1907) -> Result<()> {
1908 let responder_id = session.user_id;
1909 let requester_id = UserId::from_proto(request.requester_id);
1910 let db = session.db().await;
1911 if request.response == proto::ContactRequestResponse::Dismiss as i32 {
1912 db.dismiss_contact_notification(responder_id, requester_id)
1913 .await?;
1914 } else {
1915 let accept = request.response == proto::ContactRequestResponse::Accept as i32;
1916
1917 db.respond_to_contact_request(responder_id, requester_id, accept)
1918 .await?;
1919 let requester_busy = db.is_user_busy(requester_id).await?;
1920 let responder_busy = db.is_user_busy(responder_id).await?;
1921
1922 let pool = session.connection_pool().await;
1923 // Update responder with new contact
1924 let mut update = proto::UpdateContacts::default();
1925 if accept {
1926 update
1927 .contacts
1928 .push(contact_for_user(requester_id, false, requester_busy, &pool));
1929 }
1930 update
1931 .remove_incoming_requests
1932 .push(requester_id.to_proto());
1933 for connection_id in pool.user_connection_ids(responder_id) {
1934 session.peer.send(connection_id, update.clone())?;
1935 }
1936
1937 // Update requester with new contact
1938 let mut update = proto::UpdateContacts::default();
1939 if accept {
1940 update
1941 .contacts
1942 .push(contact_for_user(responder_id, true, responder_busy, &pool));
1943 }
1944 update
1945 .remove_outgoing_requests
1946 .push(responder_id.to_proto());
1947 for connection_id in pool.user_connection_ids(requester_id) {
1948 session.peer.send(connection_id, update.clone())?;
1949 }
1950 }
1951
1952 response.send(proto::Ack {})?;
1953 Ok(())
1954}
1955
1956async fn remove_contact(
1957 request: proto::RemoveContact,
1958 response: Response<proto::RemoveContact>,
1959 session: Session,
1960) -> Result<()> {
1961 let requester_id = session.user_id;
1962 let responder_id = UserId::from_proto(request.user_id);
1963 let db = session.db().await;
1964 db.remove_contact(requester_id, responder_id).await?;
1965
1966 let pool = session.connection_pool().await;
1967 // Update outgoing contact requests of requester
1968 let mut update = proto::UpdateContacts::default();
1969 update
1970 .remove_outgoing_requests
1971 .push(responder_id.to_proto());
1972 for connection_id in pool.user_connection_ids(requester_id) {
1973 session.peer.send(connection_id, update.clone())?;
1974 }
1975
1976 // Update incoming contact requests of responder
1977 let mut update = proto::UpdateContacts::default();
1978 update
1979 .remove_incoming_requests
1980 .push(requester_id.to_proto());
1981 for connection_id in pool.user_connection_ids(responder_id) {
1982 session.peer.send(connection_id, update.clone())?;
1983 }
1984
1985 response.send(proto::Ack {})?;
1986 Ok(())
1987}
1988
1989async fn update_diff_base(request: proto::UpdateDiffBase, session: Session) -> Result<()> {
1990 let project_id = ProjectId::from_proto(request.project_id);
1991 let project_connection_ids = session
1992 .db()
1993 .await
1994 .project_connection_ids(project_id, session.connection_id)
1995 .await?;
1996 broadcast(
1997 Some(session.connection_id),
1998 project_connection_ids.iter().copied(),
1999 |connection_id| {
2000 session
2001 .peer
2002 .forward_send(session.connection_id, connection_id, request.clone())
2003 },
2004 );
2005 Ok(())
2006}
2007
2008async fn get_private_user_info(
2009 _request: proto::GetPrivateUserInfo,
2010 response: Response<proto::GetPrivateUserInfo>,
2011 session: Session,
2012) -> Result<()> {
2013 let metrics_id = session
2014 .db()
2015 .await
2016 .get_user_metrics_id(session.user_id)
2017 .await?;
2018 let user = session
2019 .db()
2020 .await
2021 .get_user_by_id(session.user_id)
2022 .await?
2023 .ok_or_else(|| anyhow!("user not found"))?;
2024 response.send(proto::GetPrivateUserInfoResponse {
2025 metrics_id,
2026 staff: user.admin,
2027 })?;
2028 Ok(())
2029}
2030
2031fn to_axum_message(message: TungsteniteMessage) -> AxumMessage {
2032 match message {
2033 TungsteniteMessage::Text(payload) => AxumMessage::Text(payload),
2034 TungsteniteMessage::Binary(payload) => AxumMessage::Binary(payload),
2035 TungsteniteMessage::Ping(payload) => AxumMessage::Ping(payload),
2036 TungsteniteMessage::Pong(payload) => AxumMessage::Pong(payload),
2037 TungsteniteMessage::Close(frame) => AxumMessage::Close(frame.map(|frame| AxumCloseFrame {
2038 code: frame.code.into(),
2039 reason: frame.reason,
2040 })),
2041 }
2042}
2043
2044fn to_tungstenite_message(message: AxumMessage) -> TungsteniteMessage {
2045 match message {
2046 AxumMessage::Text(payload) => TungsteniteMessage::Text(payload),
2047 AxumMessage::Binary(payload) => TungsteniteMessage::Binary(payload),
2048 AxumMessage::Ping(payload) => TungsteniteMessage::Ping(payload),
2049 AxumMessage::Pong(payload) => TungsteniteMessage::Pong(payload),
2050 AxumMessage::Close(frame) => {
2051 TungsteniteMessage::Close(frame.map(|frame| TungsteniteCloseFrame {
2052 code: frame.code.into(),
2053 reason: frame.reason,
2054 }))
2055 }
2056 }
2057}
2058
2059fn build_initial_contacts_update(
2060 contacts: Vec<db::Contact>,
2061 pool: &ConnectionPool,
2062) -> proto::UpdateContacts {
2063 let mut update = proto::UpdateContacts::default();
2064
2065 for contact in contacts {
2066 match contact {
2067 db::Contact::Accepted {
2068 user_id,
2069 should_notify,
2070 busy,
2071 } => {
2072 update
2073 .contacts
2074 .push(contact_for_user(user_id, should_notify, busy, &pool));
2075 }
2076 db::Contact::Outgoing { user_id } => update.outgoing_requests.push(user_id.to_proto()),
2077 db::Contact::Incoming {
2078 user_id,
2079 should_notify,
2080 } => update
2081 .incoming_requests
2082 .push(proto::IncomingContactRequest {
2083 requester_id: user_id.to_proto(),
2084 should_notify,
2085 }),
2086 }
2087 }
2088
2089 update
2090}
2091
2092fn contact_for_user(
2093 user_id: UserId,
2094 should_notify: bool,
2095 busy: bool,
2096 pool: &ConnectionPool,
2097) -> proto::Contact {
2098 proto::Contact {
2099 user_id: user_id.to_proto(),
2100 online: pool.is_user_online(user_id),
2101 busy,
2102 should_notify,
2103 }
2104}
2105
2106fn room_updated(room: &proto::Room, peer: &Peer) {
2107 broadcast(
2108 None,
2109 room.participants
2110 .iter()
2111 .filter_map(|participant| Some(participant.peer_id?.into())),
2112 |peer_id| {
2113 peer.send(
2114 peer_id.into(),
2115 proto::RoomUpdated {
2116 room: Some(room.clone()),
2117 },
2118 )
2119 },
2120 );
2121}
2122
2123async fn update_user_contacts(user_id: UserId, session: &Session) -> Result<()> {
2124 let db = session.db().await;
2125 let contacts = db.get_contacts(user_id).await?;
2126 let busy = db.is_user_busy(user_id).await?;
2127
2128 let pool = session.connection_pool().await;
2129 let updated_contact = contact_for_user(user_id, false, busy, &pool);
2130 for contact in contacts {
2131 if let db::Contact::Accepted {
2132 user_id: contact_user_id,
2133 ..
2134 } = contact
2135 {
2136 for contact_conn_id in pool.user_connection_ids(contact_user_id) {
2137 session
2138 .peer
2139 .send(
2140 contact_conn_id,
2141 proto::UpdateContacts {
2142 contacts: vec![updated_contact.clone()],
2143 remove_contacts: Default::default(),
2144 incoming_requests: Default::default(),
2145 remove_incoming_requests: Default::default(),
2146 outgoing_requests: Default::default(),
2147 remove_outgoing_requests: Default::default(),
2148 },
2149 )
2150 .trace_err();
2151 }
2152 }
2153 }
2154 Ok(())
2155}
2156
2157async fn leave_room_for_session(session: &Session) -> Result<()> {
2158 let mut contacts_to_update = HashSet::default();
2159
2160 let room_id;
2161 let canceled_calls_to_user_ids;
2162 let live_kit_room;
2163 let delete_live_kit_room;
2164 if let Some(mut left_room) = session.db().await.leave_room(session.connection_id).await? {
2165 contacts_to_update.insert(session.user_id);
2166
2167 for project in left_room.left_projects.values() {
2168 project_left(project, session);
2169 }
2170
2171 room_updated(&left_room.room, &session.peer);
2172 room_id = RoomId::from_proto(left_room.room.id);
2173 canceled_calls_to_user_ids = mem::take(&mut left_room.canceled_calls_to_user_ids);
2174 live_kit_room = mem::take(&mut left_room.room.live_kit_room);
2175 delete_live_kit_room = left_room.room.participants.is_empty();
2176 } else {
2177 return Ok(());
2178 }
2179
2180 {
2181 let pool = session.connection_pool().await;
2182 for canceled_user_id in canceled_calls_to_user_ids {
2183 for connection_id in pool.user_connection_ids(canceled_user_id) {
2184 session
2185 .peer
2186 .send(
2187 connection_id,
2188 proto::CallCanceled {
2189 room_id: room_id.to_proto(),
2190 },
2191 )
2192 .trace_err();
2193 }
2194 contacts_to_update.insert(canceled_user_id);
2195 }
2196 }
2197
2198 for contact_user_id in contacts_to_update {
2199 update_user_contacts(contact_user_id, &session).await?;
2200 }
2201
2202 if let Some(live_kit) = session.live_kit_client.as_ref() {
2203 live_kit
2204 .remove_participant(live_kit_room.clone(), session.user_id.to_string())
2205 .await
2206 .trace_err();
2207
2208 if delete_live_kit_room {
2209 live_kit.delete_room(live_kit_room).await.trace_err();
2210 }
2211 }
2212
2213 Ok(())
2214}
2215
2216fn project_left(project: &db::LeftProject, session: &Session) {
2217 for connection_id in &project.connection_ids {
2218 if project.host_user_id == session.user_id {
2219 session
2220 .peer
2221 .send(
2222 *connection_id,
2223 proto::UnshareProject {
2224 project_id: project.id.to_proto(),
2225 },
2226 )
2227 .trace_err();
2228 } else {
2229 session
2230 .peer
2231 .send(
2232 *connection_id,
2233 proto::RemoveProjectCollaborator {
2234 project_id: project.id.to_proto(),
2235 peer_id: Some(session.connection_id.into()),
2236 },
2237 )
2238 .trace_err();
2239 }
2240 }
2241}
2242
2243pub trait ResultExt {
2244 type Ok;
2245
2246 fn trace_err(self) -> Option<Self::Ok>;
2247}
2248
2249impl<T, E> ResultExt for Result<T, E>
2250where
2251 E: std::fmt::Debug,
2252{
2253 type Ok = T;
2254
2255 fn trace_err(self) -> Option<T> {
2256 match self {
2257 Ok(value) => Some(value),
2258 Err(error) => {
2259 tracing::error!("{:?}", error);
2260 None
2261 }
2262 }
2263 }
2264}