rpc.rs

   1mod connection_pool;
   2
   3use crate::api::CloudflareIpCountryHeader;
   4use crate::llm::LlmTokenClaims;
   5use crate::{
   6    auth,
   7    db::{
   8        self, BufferId, Capability, Channel, ChannelId, ChannelRole, ChannelsForUser,
   9        CreatedChannelMessage, Database, InviteMemberResult, MembershipUpdated, MessageId,
  10        NotificationId, Project, ProjectId, RejoinedProject, RemoveChannelMemberResult, ReplicaId,
  11        RespondToChannelInvite, RoomId, ServerId, UpdatedChannelMessage, User, UserId,
  12    },
  13    executor::Executor,
  14    AppState, Config, Error, RateLimit, Result,
  15};
  16use anyhow::{anyhow, bail, Context as _};
  17use async_tungstenite::tungstenite::{
  18    protocol::CloseFrame as TungsteniteCloseFrame, Message as TungsteniteMessage,
  19};
  20use axum::{
  21    body::Body,
  22    extract::{
  23        ws::{CloseFrame as AxumCloseFrame, Message as AxumMessage},
  24        ConnectInfo, WebSocketUpgrade,
  25    },
  26    headers::{Header, HeaderName},
  27    http::StatusCode,
  28    middleware,
  29    response::IntoResponse,
  30    routing::get,
  31    Extension, Router, TypedHeader,
  32};
  33use chrono::Utc;
  34use collections::{HashMap, HashSet};
  35pub use connection_pool::{ConnectionPool, ZedVersion};
  36use core::fmt::{self, Debug, Formatter};
  37use http_client::HttpClient;
  38use open_ai::{OpenAiEmbeddingModel, OPEN_AI_API_URL};
  39use reqwest_client::ReqwestClient;
  40use sha2::Digest;
  41use supermaven_api::{CreateExternalUserRequest, SupermavenAdminApi};
  42
  43use futures::{
  44    channel::oneshot, future::BoxFuture, stream::FuturesUnordered, FutureExt, SinkExt, StreamExt,
  45    TryStreamExt,
  46};
  47use prometheus::{register_int_gauge, IntGauge};
  48use rpc::{
  49    proto::{
  50        self, Ack, AnyTypedEnvelope, EntityMessage, EnvelopedMessage, LiveKitConnectionInfo,
  51        RequestMessage, ShareProject, UpdateChannelBufferCollaborators,
  52    },
  53    Connection, ConnectionId, ErrorCode, ErrorCodeExt, ErrorExt, Peer, Receipt, TypedEnvelope,
  54};
  55use semantic_version::SemanticVersion;
  56use serde::{Serialize, Serializer};
  57use std::{
  58    any::TypeId,
  59    future::Future,
  60    marker::PhantomData,
  61    mem,
  62    net::SocketAddr,
  63    ops::{Deref, DerefMut},
  64    rc::Rc,
  65    sync::{
  66        atomic::{AtomicBool, Ordering::SeqCst},
  67        Arc, OnceLock,
  68    },
  69    time::{Duration, Instant},
  70};
  71use time::OffsetDateTime;
  72use tokio::sync::{watch, MutexGuard, Semaphore};
  73use tower::ServiceBuilder;
  74use tracing::{
  75    field::{self},
  76    info_span, instrument, Instrument,
  77};
  78
  79pub const RECONNECT_TIMEOUT: Duration = Duration::from_secs(30);
  80
  81// kubernetes gives terminated pods 10s to shutdown gracefully. After they're gone, we can clean up old resources.
  82pub const CLEANUP_TIMEOUT: Duration = Duration::from_secs(15);
  83
  84const MESSAGE_COUNT_PER_PAGE: usize = 100;
  85const MAX_MESSAGE_LEN: usize = 1024;
  86const NOTIFICATION_COUNT_PER_PAGE: usize = 50;
  87
  88type MessageHandler =
  89    Box<dyn Send + Sync + Fn(Box<dyn AnyTypedEnvelope>, Session) -> BoxFuture<'static, ()>>;
  90
  91struct Response<R> {
  92    peer: Arc<Peer>,
  93    receipt: Receipt<R>,
  94    responded: Arc<AtomicBool>,
  95}
  96
  97impl<R: RequestMessage> Response<R> {
  98    fn send(self, payload: R::Response) -> Result<()> {
  99        self.responded.store(true, SeqCst);
 100        self.peer.respond(self.receipt, payload)?;
 101        Ok(())
 102    }
 103}
 104
 105#[derive(Clone, Debug)]
 106pub enum Principal {
 107    User(User),
 108    Impersonated { user: User, admin: User },
 109}
 110
 111impl Principal {
 112    fn update_span(&self, span: &tracing::Span) {
 113        match &self {
 114            Principal::User(user) => {
 115                span.record("user_id", user.id.0);
 116                span.record("login", &user.github_login);
 117            }
 118            Principal::Impersonated { user, admin } => {
 119                span.record("user_id", user.id.0);
 120                span.record("login", &user.github_login);
 121                span.record("impersonator", &admin.github_login);
 122            }
 123        }
 124    }
 125}
 126
 127#[derive(Clone)]
 128struct Session {
 129    principal: Principal,
 130    connection_id: ConnectionId,
 131    db: Arc<tokio::sync::Mutex<DbHandle>>,
 132    peer: Arc<Peer>,
 133    connection_pool: Arc<parking_lot::Mutex<ConnectionPool>>,
 134    app_state: Arc<AppState>,
 135    supermaven_client: Option<Arc<SupermavenAdminApi>>,
 136    http_client: Arc<dyn HttpClient>,
 137    /// The GeoIP country code for the user.
 138    #[allow(unused)]
 139    geoip_country_code: Option<String>,
 140    _executor: Executor,
 141}
 142
 143impl Session {
 144    async fn db(&self) -> tokio::sync::MutexGuard<DbHandle> {
 145        #[cfg(test)]
 146        tokio::task::yield_now().await;
 147        let guard = self.db.lock().await;
 148        #[cfg(test)]
 149        tokio::task::yield_now().await;
 150        guard
 151    }
 152
 153    async fn connection_pool(&self) -> ConnectionPoolGuard<'_> {
 154        #[cfg(test)]
 155        tokio::task::yield_now().await;
 156        let guard = self.connection_pool.lock();
 157        ConnectionPoolGuard {
 158            guard,
 159            _not_send: PhantomData,
 160        }
 161    }
 162
 163    fn is_staff(&self) -> bool {
 164        match &self.principal {
 165            Principal::User(user) => user.admin,
 166            Principal::Impersonated { .. } => true,
 167        }
 168    }
 169
 170    pub async fn has_llm_subscription(
 171        &self,
 172        db: &MutexGuard<'_, DbHandle>,
 173    ) -> anyhow::Result<bool> {
 174        if self.is_staff() {
 175            return Ok(true);
 176        }
 177
 178        let user_id = self.user_id();
 179
 180        Ok(db.has_active_billing_subscription(user_id).await?)
 181    }
 182
 183    pub async fn current_plan(
 184        &self,
 185        _db: &MutexGuard<'_, DbHandle>,
 186    ) -> anyhow::Result<proto::Plan> {
 187        if self.is_staff() {
 188            Ok(proto::Plan::ZedPro)
 189        } else {
 190            Ok(proto::Plan::Free)
 191        }
 192    }
 193
 194    fn user_id(&self) -> UserId {
 195        match &self.principal {
 196            Principal::User(user) => user.id,
 197            Principal::Impersonated { user, .. } => user.id,
 198        }
 199    }
 200
 201    pub fn email(&self) -> Option<String> {
 202        match &self.principal {
 203            Principal::User(user) => user.email_address.clone(),
 204            Principal::Impersonated { user, .. } => user.email_address.clone(),
 205        }
 206    }
 207}
 208
 209impl Debug for Session {
 210    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
 211        let mut result = f.debug_struct("Session");
 212        match &self.principal {
 213            Principal::User(user) => {
 214                result.field("user", &user.github_login);
 215            }
 216            Principal::Impersonated { user, admin } => {
 217                result.field("user", &user.github_login);
 218                result.field("impersonator", &admin.github_login);
 219            }
 220        }
 221        result.field("connection_id", &self.connection_id).finish()
 222    }
 223}
 224
 225struct DbHandle(Arc<Database>);
 226
 227impl Deref for DbHandle {
 228    type Target = Database;
 229
 230    fn deref(&self) -> &Self::Target {
 231        self.0.as_ref()
 232    }
 233}
 234
 235pub struct Server {
 236    id: parking_lot::Mutex<ServerId>,
 237    peer: Arc<Peer>,
 238    pub(crate) connection_pool: Arc<parking_lot::Mutex<ConnectionPool>>,
 239    app_state: Arc<AppState>,
 240    handlers: HashMap<TypeId, MessageHandler>,
 241    teardown: watch::Sender<bool>,
 242}
 243
 244pub(crate) struct ConnectionPoolGuard<'a> {
 245    guard: parking_lot::MutexGuard<'a, ConnectionPool>,
 246    _not_send: PhantomData<Rc<()>>,
 247}
 248
 249#[derive(Serialize)]
 250pub struct ServerSnapshot<'a> {
 251    peer: &'a Peer,
 252    #[serde(serialize_with = "serialize_deref")]
 253    connection_pool: ConnectionPoolGuard<'a>,
 254}
 255
 256pub fn serialize_deref<S, T, U>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
 257where
 258    S: Serializer,
 259    T: Deref<Target = U>,
 260    U: Serialize,
 261{
 262    Serialize::serialize(value.deref(), serializer)
 263}
 264
 265impl Server {
 266    pub fn new(id: ServerId, app_state: Arc<AppState>) -> Arc<Self> {
 267        let mut server = Self {
 268            id: parking_lot::Mutex::new(id),
 269            peer: Peer::new(id.0 as u32),
 270            app_state: app_state.clone(),
 271            connection_pool: Default::default(),
 272            handlers: Default::default(),
 273            teardown: watch::channel(false).0,
 274        };
 275
 276        server
 277            .add_request_handler(ping)
 278            .add_request_handler(create_room)
 279            .add_request_handler(join_room)
 280            .add_request_handler(rejoin_room)
 281            .add_request_handler(leave_room)
 282            .add_request_handler(set_room_participant_role)
 283            .add_request_handler(call)
 284            .add_request_handler(cancel_call)
 285            .add_message_handler(decline_call)
 286            .add_request_handler(update_participant_location)
 287            .add_request_handler(share_project)
 288            .add_message_handler(unshare_project)
 289            .add_request_handler(join_project)
 290            .add_message_handler(leave_project)
 291            .add_request_handler(update_project)
 292            .add_request_handler(update_worktree)
 293            .add_message_handler(start_language_server)
 294            .add_message_handler(update_language_server)
 295            .add_message_handler(update_diagnostic_summary)
 296            .add_message_handler(update_worktree_settings)
 297            .add_request_handler(forward_read_only_project_request::<proto::GetHover>)
 298            .add_request_handler(forward_read_only_project_request::<proto::GetDefinition>)
 299            .add_request_handler(forward_read_only_project_request::<proto::GetTypeDefinition>)
 300            .add_request_handler(forward_read_only_project_request::<proto::GetReferences>)
 301            .add_request_handler(forward_find_search_candidates_request)
 302            .add_request_handler(forward_read_only_project_request::<proto::GetDocumentHighlights>)
 303            .add_request_handler(forward_read_only_project_request::<proto::GetProjectSymbols>)
 304            .add_request_handler(forward_read_only_project_request::<proto::OpenBufferForSymbol>)
 305            .add_request_handler(forward_read_only_project_request::<proto::OpenBufferById>)
 306            .add_request_handler(forward_read_only_project_request::<proto::SynchronizeBuffers>)
 307            .add_request_handler(forward_read_only_project_request::<proto::InlayHints>)
 308            .add_request_handler(forward_read_only_project_request::<proto::ResolveInlayHint>)
 309            .add_request_handler(forward_read_only_project_request::<proto::OpenBufferByPath>)
 310            .add_request_handler(forward_read_only_project_request::<proto::GitBranches>)
 311            .add_request_handler(forward_mutating_project_request::<proto::UpdateGitBranch>)
 312            .add_request_handler(forward_mutating_project_request::<proto::GetCompletions>)
 313            .add_request_handler(
 314                forward_mutating_project_request::<proto::ApplyCompletionAdditionalEdits>,
 315            )
 316            .add_request_handler(forward_mutating_project_request::<proto::OpenNewBuffer>)
 317            .add_request_handler(
 318                forward_mutating_project_request::<proto::ResolveCompletionDocumentation>,
 319            )
 320            .add_request_handler(forward_mutating_project_request::<proto::GetCodeActions>)
 321            .add_request_handler(forward_mutating_project_request::<proto::ApplyCodeAction>)
 322            .add_request_handler(forward_mutating_project_request::<proto::PrepareRename>)
 323            .add_request_handler(forward_mutating_project_request::<proto::PerformRename>)
 324            .add_request_handler(forward_mutating_project_request::<proto::ReloadBuffers>)
 325            .add_request_handler(forward_mutating_project_request::<proto::FormatBuffers>)
 326            .add_request_handler(forward_mutating_project_request::<proto::CreateProjectEntry>)
 327            .add_request_handler(forward_mutating_project_request::<proto::RenameProjectEntry>)
 328            .add_request_handler(forward_mutating_project_request::<proto::CopyProjectEntry>)
 329            .add_request_handler(forward_mutating_project_request::<proto::DeleteProjectEntry>)
 330            .add_request_handler(forward_mutating_project_request::<proto::ExpandProjectEntry>)
 331            .add_request_handler(forward_mutating_project_request::<proto::OnTypeFormatting>)
 332            .add_request_handler(forward_mutating_project_request::<proto::SaveBuffer>)
 333            .add_request_handler(forward_mutating_project_request::<proto::BlameBuffer>)
 334            .add_request_handler(forward_mutating_project_request::<proto::MultiLspQuery>)
 335            .add_request_handler(forward_mutating_project_request::<proto::RestartLanguageServers>)
 336            .add_request_handler(forward_mutating_project_request::<proto::LinkedEditingRange>)
 337            .add_message_handler(create_buffer_for_peer)
 338            .add_request_handler(update_buffer)
 339            .add_message_handler(broadcast_project_message_from_host::<proto::RefreshInlayHints>)
 340            .add_message_handler(broadcast_project_message_from_host::<proto::UpdateBufferFile>)
 341            .add_message_handler(broadcast_project_message_from_host::<proto::BufferReloaded>)
 342            .add_message_handler(broadcast_project_message_from_host::<proto::BufferSaved>)
 343            .add_message_handler(broadcast_project_message_from_host::<proto::UpdateDiffBase>)
 344            .add_request_handler(get_users)
 345            .add_request_handler(fuzzy_search_users)
 346            .add_request_handler(request_contact)
 347            .add_request_handler(remove_contact)
 348            .add_request_handler(respond_to_contact_request)
 349            .add_message_handler(subscribe_to_channels)
 350            .add_request_handler(create_channel)
 351            .add_request_handler(delete_channel)
 352            .add_request_handler(invite_channel_member)
 353            .add_request_handler(remove_channel_member)
 354            .add_request_handler(set_channel_member_role)
 355            .add_request_handler(set_channel_visibility)
 356            .add_request_handler(rename_channel)
 357            .add_request_handler(join_channel_buffer)
 358            .add_request_handler(leave_channel_buffer)
 359            .add_message_handler(update_channel_buffer)
 360            .add_request_handler(rejoin_channel_buffers)
 361            .add_request_handler(get_channel_members)
 362            .add_request_handler(respond_to_channel_invite)
 363            .add_request_handler(join_channel)
 364            .add_request_handler(join_channel_chat)
 365            .add_message_handler(leave_channel_chat)
 366            .add_request_handler(send_channel_message)
 367            .add_request_handler(remove_channel_message)
 368            .add_request_handler(update_channel_message)
 369            .add_request_handler(get_channel_messages)
 370            .add_request_handler(get_channel_messages_by_id)
 371            .add_request_handler(get_notifications)
 372            .add_request_handler(mark_notification_as_read)
 373            .add_request_handler(move_channel)
 374            .add_request_handler(follow)
 375            .add_message_handler(unfollow)
 376            .add_message_handler(update_followers)
 377            .add_request_handler(get_private_user_info)
 378            .add_request_handler(get_llm_api_token)
 379            .add_request_handler(accept_terms_of_service)
 380            .add_message_handler(acknowledge_channel_message)
 381            .add_message_handler(acknowledge_buffer_version)
 382            .add_request_handler(get_supermaven_api_key)
 383            .add_request_handler(forward_mutating_project_request::<proto::OpenContext>)
 384            .add_request_handler(forward_mutating_project_request::<proto::CreateContext>)
 385            .add_request_handler(forward_mutating_project_request::<proto::SynchronizeContexts>)
 386            .add_message_handler(broadcast_project_message_from_host::<proto::AdvertiseContexts>)
 387            .add_message_handler(update_context)
 388            .add_request_handler({
 389                let app_state = app_state.clone();
 390                move |request, response, session| {
 391                    let app_state = app_state.clone();
 392                    async move {
 393                        count_language_model_tokens(request, response, session, &app_state.config)
 394                            .await
 395                    }
 396                }
 397            })
 398            .add_request_handler(get_cached_embeddings)
 399            .add_request_handler({
 400                let app_state = app_state.clone();
 401                move |request, response, session| {
 402                    compute_embeddings(
 403                        request,
 404                        response,
 405                        session,
 406                        app_state.config.openai_api_key.clone(),
 407                    )
 408                }
 409            });
 410
 411        Arc::new(server)
 412    }
 413
 414    pub async fn start(&self) -> Result<()> {
 415        let server_id = *self.id.lock();
 416        let app_state = self.app_state.clone();
 417        let peer = self.peer.clone();
 418        let timeout = self.app_state.executor.sleep(CLEANUP_TIMEOUT);
 419        let pool = self.connection_pool.clone();
 420        let live_kit_client = self.app_state.live_kit_client.clone();
 421
 422        let span = info_span!("start server");
 423        self.app_state.executor.spawn_detached(
 424            async move {
 425                tracing::info!("waiting for cleanup timeout");
 426                timeout.await;
 427                tracing::info!("cleanup timeout expired, retrieving stale rooms");
 428                if let Some((room_ids, channel_ids)) = app_state
 429                    .db
 430                    .stale_server_resource_ids(&app_state.config.zed_environment, server_id)
 431                    .await
 432                    .trace_err()
 433                {
 434                    tracing::info!(stale_room_count = room_ids.len(), "retrieved stale rooms");
 435                    tracing::info!(
 436                        stale_channel_buffer_count = channel_ids.len(),
 437                        "retrieved stale channel buffers"
 438                    );
 439
 440                    for channel_id in channel_ids {
 441                        if let Some(refreshed_channel_buffer) = app_state
 442                            .db
 443                            .clear_stale_channel_buffer_collaborators(channel_id, server_id)
 444                            .await
 445                            .trace_err()
 446                        {
 447                            for connection_id in refreshed_channel_buffer.connection_ids {
 448                                peer.send(
 449                                    connection_id,
 450                                    proto::UpdateChannelBufferCollaborators {
 451                                        channel_id: channel_id.to_proto(),
 452                                        collaborators: refreshed_channel_buffer
 453                                            .collaborators
 454                                            .clone(),
 455                                    },
 456                                )
 457                                .trace_err();
 458                            }
 459                        }
 460                    }
 461
 462                    for room_id in room_ids {
 463                        let mut contacts_to_update = HashSet::default();
 464                        let mut canceled_calls_to_user_ids = Vec::new();
 465                        let mut live_kit_room = String::new();
 466                        let mut delete_live_kit_room = false;
 467
 468                        if let Some(mut refreshed_room) = app_state
 469                            .db
 470                            .clear_stale_room_participants(room_id, server_id)
 471                            .await
 472                            .trace_err()
 473                        {
 474                            tracing::info!(
 475                                room_id = room_id.0,
 476                                new_participant_count = refreshed_room.room.participants.len(),
 477                                "refreshed room"
 478                            );
 479                            room_updated(&refreshed_room.room, &peer);
 480                            if let Some(channel) = refreshed_room.channel.as_ref() {
 481                                channel_updated(channel, &refreshed_room.room, &peer, &pool.lock());
 482                            }
 483                            contacts_to_update
 484                                .extend(refreshed_room.stale_participant_user_ids.iter().copied());
 485                            contacts_to_update
 486                                .extend(refreshed_room.canceled_calls_to_user_ids.iter().copied());
 487                            canceled_calls_to_user_ids =
 488                                mem::take(&mut refreshed_room.canceled_calls_to_user_ids);
 489                            live_kit_room = mem::take(&mut refreshed_room.room.live_kit_room);
 490                            delete_live_kit_room = refreshed_room.room.participants.is_empty();
 491                        }
 492
 493                        {
 494                            let pool = pool.lock();
 495                            for canceled_user_id in canceled_calls_to_user_ids {
 496                                for connection_id in pool.user_connection_ids(canceled_user_id) {
 497                                    peer.send(
 498                                        connection_id,
 499                                        proto::CallCanceled {
 500                                            room_id: room_id.to_proto(),
 501                                        },
 502                                    )
 503                                    .trace_err();
 504                                }
 505                            }
 506                        }
 507
 508                        for user_id in contacts_to_update {
 509                            let busy = app_state.db.is_user_busy(user_id).await.trace_err();
 510                            let contacts = app_state.db.get_contacts(user_id).await.trace_err();
 511                            if let Some((busy, contacts)) = busy.zip(contacts) {
 512                                let pool = pool.lock();
 513                                let updated_contact = contact_for_user(user_id, busy, &pool);
 514                                for contact in contacts {
 515                                    if let db::Contact::Accepted {
 516                                        user_id: contact_user_id,
 517                                        ..
 518                                    } = contact
 519                                    {
 520                                        for contact_conn_id in
 521                                            pool.user_connection_ids(contact_user_id)
 522                                        {
 523                                            peer.send(
 524                                                contact_conn_id,
 525                                                proto::UpdateContacts {
 526                                                    contacts: vec![updated_contact.clone()],
 527                                                    remove_contacts: Default::default(),
 528                                                    incoming_requests: Default::default(),
 529                                                    remove_incoming_requests: Default::default(),
 530                                                    outgoing_requests: Default::default(),
 531                                                    remove_outgoing_requests: Default::default(),
 532                                                },
 533                                            )
 534                                            .trace_err();
 535                                        }
 536                                    }
 537                                }
 538                            }
 539                        }
 540
 541                        if let Some(live_kit) = live_kit_client.as_ref() {
 542                            if delete_live_kit_room {
 543                                live_kit.delete_room(live_kit_room).await.trace_err();
 544                            }
 545                        }
 546                    }
 547                }
 548
 549                app_state
 550                    .db
 551                    .delete_stale_servers(&app_state.config.zed_environment, server_id)
 552                    .await
 553                    .trace_err();
 554            }
 555            .instrument(span),
 556        );
 557        Ok(())
 558    }
 559
 560    pub fn teardown(&self) {
 561        self.peer.teardown();
 562        self.connection_pool.lock().reset();
 563        let _ = self.teardown.send(true);
 564    }
 565
 566    #[cfg(test)]
 567    pub fn reset(&self, id: ServerId) {
 568        self.teardown();
 569        *self.id.lock() = id;
 570        self.peer.reset(id.0 as u32);
 571        let _ = self.teardown.send(false);
 572    }
 573
 574    #[cfg(test)]
 575    pub fn id(&self) -> ServerId {
 576        *self.id.lock()
 577    }
 578
 579    fn add_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
 580    where
 581        F: 'static + Send + Sync + Fn(TypedEnvelope<M>, Session) -> Fut,
 582        Fut: 'static + Send + Future<Output = Result<()>>,
 583        M: EnvelopedMessage,
 584    {
 585        let prev_handler = self.handlers.insert(
 586            TypeId::of::<M>(),
 587            Box::new(move |envelope, session| {
 588                let envelope = envelope.into_any().downcast::<TypedEnvelope<M>>().unwrap();
 589                let received_at = envelope.received_at;
 590                tracing::info!("message received");
 591                let start_time = Instant::now();
 592                let future = (handler)(*envelope, session);
 593                async move {
 594                    let result = future.await;
 595                    let total_duration_ms = received_at.elapsed().as_micros() as f64 / 1000.0;
 596                    let processing_duration_ms = start_time.elapsed().as_micros() as f64 / 1000.0;
 597                    let queue_duration_ms = total_duration_ms - processing_duration_ms;
 598                    let payload_type = M::NAME;
 599
 600                    match result {
 601                        Err(error) => {
 602                            tracing::error!(
 603                                ?error,
 604                                total_duration_ms,
 605                                processing_duration_ms,
 606                                queue_duration_ms,
 607                                payload_type,
 608                                "error handling message"
 609                            )
 610                        }
 611                        Ok(()) => tracing::info!(
 612                            total_duration_ms,
 613                            processing_duration_ms,
 614                            queue_duration_ms,
 615                            "finished handling message"
 616                        ),
 617                    }
 618                }
 619                .boxed()
 620            }),
 621        );
 622        if prev_handler.is_some() {
 623            panic!("registered a handler for the same message twice");
 624        }
 625        self
 626    }
 627
 628    fn add_message_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
 629    where
 630        F: 'static + Send + Sync + Fn(M, Session) -> Fut,
 631        Fut: 'static + Send + Future<Output = Result<()>>,
 632        M: EnvelopedMessage,
 633    {
 634        self.add_handler(move |envelope, session| handler(envelope.payload, session));
 635        self
 636    }
 637
 638    fn add_request_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
 639    where
 640        F: 'static + Send + Sync + Fn(M, Response<M>, Session) -> Fut,
 641        Fut: Send + Future<Output = Result<()>>,
 642        M: RequestMessage,
 643    {
 644        let handler = Arc::new(handler);
 645        self.add_handler(move |envelope, session| {
 646            let receipt = envelope.receipt();
 647            let handler = handler.clone();
 648            async move {
 649                let peer = session.peer.clone();
 650                let responded = Arc::new(AtomicBool::default());
 651                let response = Response {
 652                    peer: peer.clone(),
 653                    responded: responded.clone(),
 654                    receipt,
 655                };
 656                match (handler)(envelope.payload, response, session).await {
 657                    Ok(()) => {
 658                        if responded.load(std::sync::atomic::Ordering::SeqCst) {
 659                            Ok(())
 660                        } else {
 661                            Err(anyhow!("handler did not send a response"))?
 662                        }
 663                    }
 664                    Err(error) => {
 665                        let proto_err = match &error {
 666                            Error::Internal(err) => err.to_proto(),
 667                            _ => ErrorCode::Internal.message(format!("{}", error)).to_proto(),
 668                        };
 669                        peer.respond_with_error(receipt, proto_err)?;
 670                        Err(error)
 671                    }
 672                }
 673            }
 674        })
 675    }
 676
 677    #[allow(clippy::too_many_arguments)]
 678    pub fn handle_connection(
 679        self: &Arc<Self>,
 680        connection: Connection,
 681        address: String,
 682        principal: Principal,
 683        zed_version: ZedVersion,
 684        geoip_country_code: Option<String>,
 685        send_connection_id: Option<oneshot::Sender<ConnectionId>>,
 686        executor: Executor,
 687    ) -> impl Future<Output = ()> {
 688        let this = self.clone();
 689        let span = info_span!("handle connection", %address,
 690            connection_id=field::Empty,
 691            user_id=field::Empty,
 692            login=field::Empty,
 693            impersonator=field::Empty,
 694            geoip_country_code=field::Empty
 695        );
 696        principal.update_span(&span);
 697        if let Some(country_code) = geoip_country_code.as_ref() {
 698            span.record("geoip_country_code", country_code);
 699        }
 700
 701        let mut teardown = self.teardown.subscribe();
 702        async move {
 703            if *teardown.borrow() {
 704                tracing::error!("server is tearing down");
 705                return
 706            }
 707            let (connection_id, handle_io, mut incoming_rx) = this
 708                .peer
 709                .add_connection(connection, {
 710                    let executor = executor.clone();
 711                    move |duration| executor.sleep(duration)
 712                });
 713            tracing::Span::current().record("connection_id", format!("{}", connection_id));
 714
 715            tracing::info!("connection opened");
 716
 717            let user_agent = format!("Zed Server/{}", env!("CARGO_PKG_VERSION"));
 718            let http_client = match ReqwestClient::user_agent(&user_agent) {
 719                Ok(http_client) => Arc::new(http_client),
 720                Err(error) => {
 721                    tracing::error!(?error, "failed to create HTTP client");
 722                    return;
 723                }
 724            };
 725
 726            let supermaven_client = this.app_state.config.supermaven_admin_api_key.clone().map(|supermaven_admin_api_key| Arc::new(SupermavenAdminApi::new(
 727                    supermaven_admin_api_key.to_string(),
 728                    http_client.clone(),
 729                )));
 730
 731            let session = Session {
 732                principal: principal.clone(),
 733                connection_id,
 734                db: Arc::new(tokio::sync::Mutex::new(DbHandle(this.app_state.db.clone()))),
 735                peer: this.peer.clone(),
 736                connection_pool: this.connection_pool.clone(),
 737                app_state: this.app_state.clone(),
 738                http_client,
 739                geoip_country_code,
 740                _executor: executor.clone(),
 741                supermaven_client,
 742            };
 743
 744            if let Err(error) = this.send_initial_client_update(connection_id, &principal, zed_version, send_connection_id, &session).await {
 745                tracing::error!(?error, "failed to send initial client update");
 746                return;
 747            }
 748
 749            let handle_io = handle_io.fuse();
 750            futures::pin_mut!(handle_io);
 751
 752            // Handlers for foreground messages are pushed into the following `FuturesUnordered`.
 753            // This prevents deadlocks when e.g., client A performs a request to client B and
 754            // client B performs a request to client A. If both clients stop processing further
 755            // messages until their respective request completes, they won't have a chance to
 756            // respond to the other client's request and cause a deadlock.
 757            //
 758            // This arrangement ensures we will attempt to process earlier messages first, but fall
 759            // back to processing messages arrived later in the spirit of making progress.
 760            let mut foreground_message_handlers = FuturesUnordered::new();
 761            let concurrent_handlers = Arc::new(Semaphore::new(256));
 762            loop {
 763                let next_message = async {
 764                    let permit = concurrent_handlers.clone().acquire_owned().await.unwrap();
 765                    let message = incoming_rx.next().await;
 766                    (permit, message)
 767                }.fuse();
 768                futures::pin_mut!(next_message);
 769                futures::select_biased! {
 770                    _ = teardown.changed().fuse() => return,
 771                    result = handle_io => {
 772                        if let Err(error) = result {
 773                            tracing::error!(?error, "error handling I/O");
 774                        }
 775                        break;
 776                    }
 777                    _ = foreground_message_handlers.next() => {}
 778                    next_message = next_message => {
 779                        let (permit, message) = next_message;
 780                        if let Some(message) = message {
 781                            let type_name = message.payload_type_name();
 782                            // note: we copy all the fields from the parent span so we can query them in the logs.
 783                            // (https://github.com/tokio-rs/tracing/issues/2670).
 784                            let span = tracing::info_span!("receive message", %connection_id, %address, type_name,
 785                                user_id=field::Empty,
 786                                login=field::Empty,
 787                                impersonator=field::Empty,
 788                            );
 789                            principal.update_span(&span);
 790                            let span_enter = span.enter();
 791                            if let Some(handler) = this.handlers.get(&message.payload_type_id()) {
 792                                let is_background = message.is_background();
 793                                let handle_message = (handler)(message, session.clone());
 794                                drop(span_enter);
 795
 796                                let handle_message = async move {
 797                                    handle_message.await;
 798                                    drop(permit);
 799                                }.instrument(span);
 800                                if is_background {
 801                                    executor.spawn_detached(handle_message);
 802                                } else {
 803                                    foreground_message_handlers.push(handle_message);
 804                                }
 805                            } else {
 806                                tracing::error!("no message handler");
 807                            }
 808                        } else {
 809                            tracing::info!("connection closed");
 810                            break;
 811                        }
 812                    }
 813                }
 814            }
 815
 816            drop(foreground_message_handlers);
 817            tracing::info!("signing out");
 818            if let Err(error) = connection_lost(session, teardown, executor).await {
 819                tracing::error!(?error, "error signing out");
 820            }
 821
 822        }.instrument(span)
 823    }
 824
 825    async fn send_initial_client_update(
 826        &self,
 827        connection_id: ConnectionId,
 828        principal: &Principal,
 829        zed_version: ZedVersion,
 830        mut send_connection_id: Option<oneshot::Sender<ConnectionId>>,
 831        session: &Session,
 832    ) -> Result<()> {
 833        self.peer.send(
 834            connection_id,
 835            proto::Hello {
 836                peer_id: Some(connection_id.into()),
 837            },
 838        )?;
 839        tracing::info!("sent hello message");
 840        if let Some(send_connection_id) = send_connection_id.take() {
 841            let _ = send_connection_id.send(connection_id);
 842        }
 843
 844        match principal {
 845            Principal::User(user) | Principal::Impersonated { user, admin: _ } => {
 846                if !user.connected_once {
 847                    self.peer.send(connection_id, proto::ShowContacts {})?;
 848                    self.app_state
 849                        .db
 850                        .set_user_connected_once(user.id, true)
 851                        .await?;
 852                }
 853
 854                update_user_plan(user.id, session).await?;
 855
 856                let contacts = self.app_state.db.get_contacts(user.id).await?;
 857
 858                {
 859                    let mut pool = self.connection_pool.lock();
 860                    pool.add_connection(connection_id, user.id, user.admin, zed_version);
 861                    self.peer.send(
 862                        connection_id,
 863                        build_initial_contacts_update(contacts, &pool),
 864                    )?;
 865                }
 866
 867                if should_auto_subscribe_to_channels(zed_version) {
 868                    subscribe_user_to_channels(user.id, session).await?;
 869                }
 870
 871                if let Some(incoming_call) =
 872                    self.app_state.db.incoming_call_for_user(user.id).await?
 873                {
 874                    self.peer.send(connection_id, incoming_call)?;
 875                }
 876
 877                update_user_contacts(user.id, session).await?;
 878            }
 879        }
 880
 881        Ok(())
 882    }
 883
 884    pub async fn invite_code_redeemed(
 885        self: &Arc<Self>,
 886        inviter_id: UserId,
 887        invitee_id: UserId,
 888    ) -> Result<()> {
 889        if let Some(user) = self.app_state.db.get_user_by_id(inviter_id).await? {
 890            if let Some(code) = &user.invite_code {
 891                let pool = self.connection_pool.lock();
 892                let invitee_contact = contact_for_user(invitee_id, false, &pool);
 893                for connection_id in pool.user_connection_ids(inviter_id) {
 894                    self.peer.send(
 895                        connection_id,
 896                        proto::UpdateContacts {
 897                            contacts: vec![invitee_contact.clone()],
 898                            ..Default::default()
 899                        },
 900                    )?;
 901                    self.peer.send(
 902                        connection_id,
 903                        proto::UpdateInviteInfo {
 904                            url: format!("{}{}", self.app_state.config.invite_link_prefix, &code),
 905                            count: user.invite_count as u32,
 906                        },
 907                    )?;
 908                }
 909            }
 910        }
 911        Ok(())
 912    }
 913
 914    pub async fn invite_count_updated(self: &Arc<Self>, user_id: UserId) -> Result<()> {
 915        if let Some(user) = self.app_state.db.get_user_by_id(user_id).await? {
 916            if let Some(invite_code) = &user.invite_code {
 917                let pool = self.connection_pool.lock();
 918                for connection_id in pool.user_connection_ids(user_id) {
 919                    self.peer.send(
 920                        connection_id,
 921                        proto::UpdateInviteInfo {
 922                            url: format!(
 923                                "{}{}",
 924                                self.app_state.config.invite_link_prefix, invite_code
 925                            ),
 926                            count: user.invite_count as u32,
 927                        },
 928                    )?;
 929                }
 930            }
 931        }
 932        Ok(())
 933    }
 934
 935    pub async fn refresh_llm_tokens_for_user(self: &Arc<Self>, user_id: UserId) {
 936        let pool = self.connection_pool.lock();
 937        for connection_id in pool.user_connection_ids(user_id) {
 938            self.peer
 939                .send(connection_id, proto::RefreshLlmToken {})
 940                .trace_err();
 941        }
 942    }
 943
 944    pub async fn snapshot<'a>(self: &'a Arc<Self>) -> ServerSnapshot<'a> {
 945        ServerSnapshot {
 946            connection_pool: ConnectionPoolGuard {
 947                guard: self.connection_pool.lock(),
 948                _not_send: PhantomData,
 949            },
 950            peer: &self.peer,
 951        }
 952    }
 953}
 954
 955impl<'a> Deref for ConnectionPoolGuard<'a> {
 956    type Target = ConnectionPool;
 957
 958    fn deref(&self) -> &Self::Target {
 959        &self.guard
 960    }
 961}
 962
 963impl<'a> DerefMut for ConnectionPoolGuard<'a> {
 964    fn deref_mut(&mut self) -> &mut Self::Target {
 965        &mut self.guard
 966    }
 967}
 968
 969impl<'a> Drop for ConnectionPoolGuard<'a> {
 970    fn drop(&mut self) {
 971        #[cfg(test)]
 972        self.check_invariants();
 973    }
 974}
 975
 976fn broadcast<F>(
 977    sender_id: Option<ConnectionId>,
 978    receiver_ids: impl IntoIterator<Item = ConnectionId>,
 979    mut f: F,
 980) where
 981    F: FnMut(ConnectionId) -> anyhow::Result<()>,
 982{
 983    for receiver_id in receiver_ids {
 984        if Some(receiver_id) != sender_id {
 985            if let Err(error) = f(receiver_id) {
 986                tracing::error!("failed to send to {:?} {}", receiver_id, error);
 987            }
 988        }
 989    }
 990}
 991
 992pub struct ProtocolVersion(u32);
 993
 994impl Header for ProtocolVersion {
 995    fn name() -> &'static HeaderName {
 996        static ZED_PROTOCOL_VERSION: OnceLock<HeaderName> = OnceLock::new();
 997        ZED_PROTOCOL_VERSION.get_or_init(|| HeaderName::from_static("x-zed-protocol-version"))
 998    }
 999
1000    fn decode<'i, I>(values: &mut I) -> Result<Self, axum::headers::Error>
1001    where
1002        Self: Sized,
1003        I: Iterator<Item = &'i axum::http::HeaderValue>,
1004    {
1005        let version = values
1006            .next()
1007            .ok_or_else(axum::headers::Error::invalid)?
1008            .to_str()
1009            .map_err(|_| axum::headers::Error::invalid())?
1010            .parse()
1011            .map_err(|_| axum::headers::Error::invalid())?;
1012        Ok(Self(version))
1013    }
1014
1015    fn encode<E: Extend<axum::http::HeaderValue>>(&self, values: &mut E) {
1016        values.extend([self.0.to_string().parse().unwrap()]);
1017    }
1018}
1019
1020pub struct AppVersionHeader(SemanticVersion);
1021impl Header for AppVersionHeader {
1022    fn name() -> &'static HeaderName {
1023        static ZED_APP_VERSION: OnceLock<HeaderName> = OnceLock::new();
1024        ZED_APP_VERSION.get_or_init(|| HeaderName::from_static("x-zed-app-version"))
1025    }
1026
1027    fn decode<'i, I>(values: &mut I) -> Result<Self, axum::headers::Error>
1028    where
1029        Self: Sized,
1030        I: Iterator<Item = &'i axum::http::HeaderValue>,
1031    {
1032        let version = values
1033            .next()
1034            .ok_or_else(axum::headers::Error::invalid)?
1035            .to_str()
1036            .map_err(|_| axum::headers::Error::invalid())?
1037            .parse()
1038            .map_err(|_| axum::headers::Error::invalid())?;
1039        Ok(Self(version))
1040    }
1041
1042    fn encode<E: Extend<axum::http::HeaderValue>>(&self, values: &mut E) {
1043        values.extend([self.0.to_string().parse().unwrap()]);
1044    }
1045}
1046
1047pub fn routes(server: Arc<Server>) -> Router<(), Body> {
1048    Router::new()
1049        .route("/rpc", get(handle_websocket_request))
1050        .layer(
1051            ServiceBuilder::new()
1052                .layer(Extension(server.app_state.clone()))
1053                .layer(middleware::from_fn(auth::validate_header)),
1054        )
1055        .route("/metrics", get(handle_metrics))
1056        .layer(Extension(server))
1057}
1058
1059pub async fn handle_websocket_request(
1060    TypedHeader(ProtocolVersion(protocol_version)): TypedHeader<ProtocolVersion>,
1061    app_version_header: Option<TypedHeader<AppVersionHeader>>,
1062    ConnectInfo(socket_address): ConnectInfo<SocketAddr>,
1063    Extension(server): Extension<Arc<Server>>,
1064    Extension(principal): Extension<Principal>,
1065    country_code_header: Option<TypedHeader<CloudflareIpCountryHeader>>,
1066    ws: WebSocketUpgrade,
1067) -> axum::response::Response {
1068    if protocol_version != rpc::PROTOCOL_VERSION {
1069        return (
1070            StatusCode::UPGRADE_REQUIRED,
1071            "client must be upgraded".to_string(),
1072        )
1073            .into_response();
1074    }
1075
1076    let Some(version) = app_version_header.map(|header| ZedVersion(header.0 .0)) else {
1077        return (
1078            StatusCode::UPGRADE_REQUIRED,
1079            "no version header found".to_string(),
1080        )
1081            .into_response();
1082    };
1083
1084    if !version.can_collaborate() {
1085        return (
1086            StatusCode::UPGRADE_REQUIRED,
1087            "client must be upgraded".to_string(),
1088        )
1089            .into_response();
1090    }
1091
1092    let socket_address = socket_address.to_string();
1093    ws.on_upgrade(move |socket| {
1094        let socket = socket
1095            .map_ok(to_tungstenite_message)
1096            .err_into()
1097            .with(|message| async move { to_axum_message(message) });
1098        let connection = Connection::new(Box::pin(socket));
1099        async move {
1100            server
1101                .handle_connection(
1102                    connection,
1103                    socket_address,
1104                    principal,
1105                    version,
1106                    country_code_header.map(|header| header.to_string()),
1107                    None,
1108                    Executor::Production,
1109                )
1110                .await;
1111        }
1112    })
1113}
1114
1115pub async fn handle_metrics(Extension(server): Extension<Arc<Server>>) -> Result<String> {
1116    static CONNECTIONS_METRIC: OnceLock<IntGauge> = OnceLock::new();
1117    let connections_metric = CONNECTIONS_METRIC
1118        .get_or_init(|| register_int_gauge!("connections", "number of connections").unwrap());
1119
1120    let connections = server
1121        .connection_pool
1122        .lock()
1123        .connections()
1124        .filter(|connection| !connection.admin)
1125        .count();
1126    connections_metric.set(connections as _);
1127
1128    static SHARED_PROJECTS_METRIC: OnceLock<IntGauge> = OnceLock::new();
1129    let shared_projects_metric = SHARED_PROJECTS_METRIC.get_or_init(|| {
1130        register_int_gauge!(
1131            "shared_projects",
1132            "number of open projects with one or more guests"
1133        )
1134        .unwrap()
1135    });
1136
1137    let shared_projects = server.app_state.db.project_count_excluding_admins().await?;
1138    shared_projects_metric.set(shared_projects as _);
1139
1140    let encoder = prometheus::TextEncoder::new();
1141    let metric_families = prometheus::gather();
1142    let encoded_metrics = encoder
1143        .encode_to_string(&metric_families)
1144        .map_err(|err| anyhow!("{}", err))?;
1145    Ok(encoded_metrics)
1146}
1147
1148#[instrument(err, skip(executor))]
1149async fn connection_lost(
1150    session: Session,
1151    mut teardown: watch::Receiver<bool>,
1152    executor: Executor,
1153) -> Result<()> {
1154    session.peer.disconnect(session.connection_id);
1155    session
1156        .connection_pool()
1157        .await
1158        .remove_connection(session.connection_id)?;
1159
1160    session
1161        .db()
1162        .await
1163        .connection_lost(session.connection_id)
1164        .await
1165        .trace_err();
1166
1167    futures::select_biased! {
1168        _ = executor.sleep(RECONNECT_TIMEOUT).fuse() => {
1169
1170            log::info!("connection lost, removing all resources for user:{}, connection:{:?}", session.user_id(), session.connection_id);
1171            leave_room_for_session(&session, session.connection_id).await.trace_err();
1172            leave_channel_buffers_for_session(&session)
1173                .await
1174                .trace_err();
1175
1176            if !session
1177                .connection_pool()
1178                .await
1179                .is_user_online(session.user_id())
1180            {
1181                let db = session.db().await;
1182                if let Some(room) = db.decline_call(None, session.user_id()).await.trace_err().flatten() {
1183                    room_updated(&room, &session.peer);
1184                }
1185            }
1186
1187            update_user_contacts(session.user_id(), &session).await?;
1188        },
1189        _ = teardown.changed().fuse() => {}
1190    }
1191
1192    Ok(())
1193}
1194
1195/// Acknowledges a ping from a client, used to keep the connection alive.
1196async fn ping(_: proto::Ping, response: Response<proto::Ping>, _session: Session) -> Result<()> {
1197    response.send(proto::Ack {})?;
1198    Ok(())
1199}
1200
1201/// Creates a new room for calling (outside of channels)
1202async fn create_room(
1203    _request: proto::CreateRoom,
1204    response: Response<proto::CreateRoom>,
1205    session: Session,
1206) -> Result<()> {
1207    let live_kit_room = nanoid::nanoid!(30);
1208
1209    let live_kit_connection_info = util::maybe!(async {
1210        let live_kit = session.app_state.live_kit_client.as_ref();
1211        let live_kit = live_kit?;
1212        let user_id = session.user_id().to_string();
1213
1214        let token = live_kit
1215            .room_token(&live_kit_room, &user_id.to_string())
1216            .trace_err()?;
1217
1218        Some(proto::LiveKitConnectionInfo {
1219            server_url: live_kit.url().into(),
1220            token,
1221            can_publish: true,
1222        })
1223    })
1224    .await;
1225
1226    let room = session
1227        .db()
1228        .await
1229        .create_room(session.user_id(), session.connection_id, &live_kit_room)
1230        .await?;
1231
1232    response.send(proto::CreateRoomResponse {
1233        room: Some(room.clone()),
1234        live_kit_connection_info,
1235    })?;
1236
1237    update_user_contacts(session.user_id(), &session).await?;
1238    Ok(())
1239}
1240
1241/// Join a room from an invitation. Equivalent to joining a channel if there is one.
1242async fn join_room(
1243    request: proto::JoinRoom,
1244    response: Response<proto::JoinRoom>,
1245    session: Session,
1246) -> Result<()> {
1247    let room_id = RoomId::from_proto(request.id);
1248
1249    let channel_id = session.db().await.channel_id_for_room(room_id).await?;
1250
1251    if let Some(channel_id) = channel_id {
1252        return join_channel_internal(channel_id, Box::new(response), session).await;
1253    }
1254
1255    let joined_room = {
1256        let room = session
1257            .db()
1258            .await
1259            .join_room(room_id, session.user_id(), session.connection_id)
1260            .await?;
1261        room_updated(&room.room, &session.peer);
1262        room.into_inner()
1263    };
1264
1265    for connection_id in session
1266        .connection_pool()
1267        .await
1268        .user_connection_ids(session.user_id())
1269    {
1270        session
1271            .peer
1272            .send(
1273                connection_id,
1274                proto::CallCanceled {
1275                    room_id: room_id.to_proto(),
1276                },
1277            )
1278            .trace_err();
1279    }
1280
1281    let live_kit_connection_info =
1282        if let Some(live_kit) = session.app_state.live_kit_client.as_ref() {
1283            live_kit
1284                .room_token(
1285                    &joined_room.room.live_kit_room,
1286                    &session.user_id().to_string(),
1287                )
1288                .trace_err()
1289                .map(|token| proto::LiveKitConnectionInfo {
1290                    server_url: live_kit.url().into(),
1291                    token,
1292                    can_publish: true,
1293                })
1294        } else {
1295            None
1296        };
1297
1298    response.send(proto::JoinRoomResponse {
1299        room: Some(joined_room.room),
1300        channel_id: None,
1301        live_kit_connection_info,
1302    })?;
1303
1304    update_user_contacts(session.user_id(), &session).await?;
1305    Ok(())
1306}
1307
1308/// Rejoin room is used to reconnect to a room after connection errors.
1309async fn rejoin_room(
1310    request: proto::RejoinRoom,
1311    response: Response<proto::RejoinRoom>,
1312    session: Session,
1313) -> Result<()> {
1314    let room;
1315    let channel;
1316    {
1317        let mut rejoined_room = session
1318            .db()
1319            .await
1320            .rejoin_room(request, session.user_id(), session.connection_id)
1321            .await?;
1322
1323        response.send(proto::RejoinRoomResponse {
1324            room: Some(rejoined_room.room.clone()),
1325            reshared_projects: rejoined_room
1326                .reshared_projects
1327                .iter()
1328                .map(|project| proto::ResharedProject {
1329                    id: project.id.to_proto(),
1330                    collaborators: project
1331                        .collaborators
1332                        .iter()
1333                        .map(|collaborator| collaborator.to_proto())
1334                        .collect(),
1335                })
1336                .collect(),
1337            rejoined_projects: rejoined_room
1338                .rejoined_projects
1339                .iter()
1340                .map(|rejoined_project| rejoined_project.to_proto())
1341                .collect(),
1342        })?;
1343        room_updated(&rejoined_room.room, &session.peer);
1344
1345        for project in &rejoined_room.reshared_projects {
1346            for collaborator in &project.collaborators {
1347                session
1348                    .peer
1349                    .send(
1350                        collaborator.connection_id,
1351                        proto::UpdateProjectCollaborator {
1352                            project_id: project.id.to_proto(),
1353                            old_peer_id: Some(project.old_connection_id.into()),
1354                            new_peer_id: Some(session.connection_id.into()),
1355                        },
1356                    )
1357                    .trace_err();
1358            }
1359
1360            broadcast(
1361                Some(session.connection_id),
1362                project
1363                    .collaborators
1364                    .iter()
1365                    .map(|collaborator| collaborator.connection_id),
1366                |connection_id| {
1367                    session.peer.forward_send(
1368                        session.connection_id,
1369                        connection_id,
1370                        proto::UpdateProject {
1371                            project_id: project.id.to_proto(),
1372                            worktrees: project.worktrees.clone(),
1373                        },
1374                    )
1375                },
1376            );
1377        }
1378
1379        notify_rejoined_projects(&mut rejoined_room.rejoined_projects, &session)?;
1380
1381        let rejoined_room = rejoined_room.into_inner();
1382
1383        room = rejoined_room.room;
1384        channel = rejoined_room.channel;
1385    }
1386
1387    if let Some(channel) = channel {
1388        channel_updated(
1389            &channel,
1390            &room,
1391            &session.peer,
1392            &*session.connection_pool().await,
1393        );
1394    }
1395
1396    update_user_contacts(session.user_id(), &session).await?;
1397    Ok(())
1398}
1399
1400fn notify_rejoined_projects(
1401    rejoined_projects: &mut Vec<RejoinedProject>,
1402    session: &Session,
1403) -> Result<()> {
1404    for project in rejoined_projects.iter() {
1405        for collaborator in &project.collaborators {
1406            session
1407                .peer
1408                .send(
1409                    collaborator.connection_id,
1410                    proto::UpdateProjectCollaborator {
1411                        project_id: project.id.to_proto(),
1412                        old_peer_id: Some(project.old_connection_id.into()),
1413                        new_peer_id: Some(session.connection_id.into()),
1414                    },
1415                )
1416                .trace_err();
1417        }
1418    }
1419
1420    for project in rejoined_projects {
1421        for worktree in mem::take(&mut project.worktrees) {
1422            // Stream this worktree's entries.
1423            let message = proto::UpdateWorktree {
1424                project_id: project.id.to_proto(),
1425                worktree_id: worktree.id,
1426                abs_path: worktree.abs_path.clone(),
1427                root_name: worktree.root_name,
1428                updated_entries: worktree.updated_entries,
1429                removed_entries: worktree.removed_entries,
1430                scan_id: worktree.scan_id,
1431                is_last_update: worktree.completed_scan_id == worktree.scan_id,
1432                updated_repositories: worktree.updated_repositories,
1433                removed_repositories: worktree.removed_repositories,
1434            };
1435            for update in proto::split_worktree_update(message) {
1436                session.peer.send(session.connection_id, update.clone())?;
1437            }
1438
1439            // Stream this worktree's diagnostics.
1440            for summary in worktree.diagnostic_summaries {
1441                session.peer.send(
1442                    session.connection_id,
1443                    proto::UpdateDiagnosticSummary {
1444                        project_id: project.id.to_proto(),
1445                        worktree_id: worktree.id,
1446                        summary: Some(summary),
1447                    },
1448                )?;
1449            }
1450
1451            for settings_file in worktree.settings_files {
1452                session.peer.send(
1453                    session.connection_id,
1454                    proto::UpdateWorktreeSettings {
1455                        project_id: project.id.to_proto(),
1456                        worktree_id: worktree.id,
1457                        path: settings_file.path,
1458                        content: Some(settings_file.content),
1459                        kind: Some(settings_file.kind.to_proto().into()),
1460                    },
1461                )?;
1462            }
1463        }
1464
1465        for language_server in &project.language_servers {
1466            session.peer.send(
1467                session.connection_id,
1468                proto::UpdateLanguageServer {
1469                    project_id: project.id.to_proto(),
1470                    language_server_id: language_server.id,
1471                    variant: Some(
1472                        proto::update_language_server::Variant::DiskBasedDiagnosticsUpdated(
1473                            proto::LspDiskBasedDiagnosticsUpdated {},
1474                        ),
1475                    ),
1476                },
1477            )?;
1478        }
1479    }
1480    Ok(())
1481}
1482
1483/// leave room disconnects from the room.
1484async fn leave_room(
1485    _: proto::LeaveRoom,
1486    response: Response<proto::LeaveRoom>,
1487    session: Session,
1488) -> Result<()> {
1489    leave_room_for_session(&session, session.connection_id).await?;
1490    response.send(proto::Ack {})?;
1491    Ok(())
1492}
1493
1494/// Updates the permissions of someone else in the room.
1495async fn set_room_participant_role(
1496    request: proto::SetRoomParticipantRole,
1497    response: Response<proto::SetRoomParticipantRole>,
1498    session: Session,
1499) -> Result<()> {
1500    let user_id = UserId::from_proto(request.user_id);
1501    let role = ChannelRole::from(request.role());
1502
1503    let (live_kit_room, can_publish) = {
1504        let room = session
1505            .db()
1506            .await
1507            .set_room_participant_role(
1508                session.user_id(),
1509                RoomId::from_proto(request.room_id),
1510                user_id,
1511                role,
1512            )
1513            .await?;
1514
1515        let live_kit_room = room.live_kit_room.clone();
1516        let can_publish = ChannelRole::from(request.role()).can_use_microphone();
1517        room_updated(&room, &session.peer);
1518        (live_kit_room, can_publish)
1519    };
1520
1521    if let Some(live_kit) = session.app_state.live_kit_client.as_ref() {
1522        live_kit
1523            .update_participant(
1524                live_kit_room.clone(),
1525                request.user_id.to_string(),
1526                live_kit_server::proto::ParticipantPermission {
1527                    can_subscribe: true,
1528                    can_publish,
1529                    can_publish_data: can_publish,
1530                    hidden: false,
1531                    recorder: false,
1532                },
1533            )
1534            .await
1535            .trace_err();
1536    }
1537
1538    response.send(proto::Ack {})?;
1539    Ok(())
1540}
1541
1542/// Call someone else into the current room
1543async fn call(
1544    request: proto::Call,
1545    response: Response<proto::Call>,
1546    session: Session,
1547) -> Result<()> {
1548    let room_id = RoomId::from_proto(request.room_id);
1549    let calling_user_id = session.user_id();
1550    let calling_connection_id = session.connection_id;
1551    let called_user_id = UserId::from_proto(request.called_user_id);
1552    let initial_project_id = request.initial_project_id.map(ProjectId::from_proto);
1553    if !session
1554        .db()
1555        .await
1556        .has_contact(calling_user_id, called_user_id)
1557        .await?
1558    {
1559        return Err(anyhow!("cannot call a user who isn't a contact"))?;
1560    }
1561
1562    let incoming_call = {
1563        let (room, incoming_call) = &mut *session
1564            .db()
1565            .await
1566            .call(
1567                room_id,
1568                calling_user_id,
1569                calling_connection_id,
1570                called_user_id,
1571                initial_project_id,
1572            )
1573            .await?;
1574        room_updated(room, &session.peer);
1575        mem::take(incoming_call)
1576    };
1577    update_user_contacts(called_user_id, &session).await?;
1578
1579    let mut calls = session
1580        .connection_pool()
1581        .await
1582        .user_connection_ids(called_user_id)
1583        .map(|connection_id| session.peer.request(connection_id, incoming_call.clone()))
1584        .collect::<FuturesUnordered<_>>();
1585
1586    while let Some(call_response) = calls.next().await {
1587        match call_response.as_ref() {
1588            Ok(_) => {
1589                response.send(proto::Ack {})?;
1590                return Ok(());
1591            }
1592            Err(_) => {
1593                call_response.trace_err();
1594            }
1595        }
1596    }
1597
1598    {
1599        let room = session
1600            .db()
1601            .await
1602            .call_failed(room_id, called_user_id)
1603            .await?;
1604        room_updated(&room, &session.peer);
1605    }
1606    update_user_contacts(called_user_id, &session).await?;
1607
1608    Err(anyhow!("failed to ring user"))?
1609}
1610
1611/// Cancel an outgoing call.
1612async fn cancel_call(
1613    request: proto::CancelCall,
1614    response: Response<proto::CancelCall>,
1615    session: Session,
1616) -> Result<()> {
1617    let called_user_id = UserId::from_proto(request.called_user_id);
1618    let room_id = RoomId::from_proto(request.room_id);
1619    {
1620        let room = session
1621            .db()
1622            .await
1623            .cancel_call(room_id, session.connection_id, called_user_id)
1624            .await?;
1625        room_updated(&room, &session.peer);
1626    }
1627
1628    for connection_id in session
1629        .connection_pool()
1630        .await
1631        .user_connection_ids(called_user_id)
1632    {
1633        session
1634            .peer
1635            .send(
1636                connection_id,
1637                proto::CallCanceled {
1638                    room_id: room_id.to_proto(),
1639                },
1640            )
1641            .trace_err();
1642    }
1643    response.send(proto::Ack {})?;
1644
1645    update_user_contacts(called_user_id, &session).await?;
1646    Ok(())
1647}
1648
1649/// Decline an incoming call.
1650async fn decline_call(message: proto::DeclineCall, session: Session) -> Result<()> {
1651    let room_id = RoomId::from_proto(message.room_id);
1652    {
1653        let room = session
1654            .db()
1655            .await
1656            .decline_call(Some(room_id), session.user_id())
1657            .await?
1658            .ok_or_else(|| anyhow!("failed to decline call"))?;
1659        room_updated(&room, &session.peer);
1660    }
1661
1662    for connection_id in session
1663        .connection_pool()
1664        .await
1665        .user_connection_ids(session.user_id())
1666    {
1667        session
1668            .peer
1669            .send(
1670                connection_id,
1671                proto::CallCanceled {
1672                    room_id: room_id.to_proto(),
1673                },
1674            )
1675            .trace_err();
1676    }
1677    update_user_contacts(session.user_id(), &session).await?;
1678    Ok(())
1679}
1680
1681/// Updates other participants in the room with your current location.
1682async fn update_participant_location(
1683    request: proto::UpdateParticipantLocation,
1684    response: Response<proto::UpdateParticipantLocation>,
1685    session: Session,
1686) -> Result<()> {
1687    let room_id = RoomId::from_proto(request.room_id);
1688    let location = request
1689        .location
1690        .ok_or_else(|| anyhow!("invalid location"))?;
1691
1692    let db = session.db().await;
1693    let room = db
1694        .update_room_participant_location(room_id, session.connection_id, location)
1695        .await?;
1696
1697    room_updated(&room, &session.peer);
1698    response.send(proto::Ack {})?;
1699    Ok(())
1700}
1701
1702/// Share a project into the room.
1703async fn share_project(
1704    request: proto::ShareProject,
1705    response: Response<proto::ShareProject>,
1706    session: Session,
1707) -> Result<()> {
1708    let (project_id, room) = &*session
1709        .db()
1710        .await
1711        .share_project(
1712            RoomId::from_proto(request.room_id),
1713            session.connection_id,
1714            &request.worktrees,
1715            request.is_ssh_project,
1716        )
1717        .await?;
1718    response.send(proto::ShareProjectResponse {
1719        project_id: project_id.to_proto(),
1720    })?;
1721    room_updated(room, &session.peer);
1722
1723    Ok(())
1724}
1725
1726/// Unshare a project from the room.
1727async fn unshare_project(message: proto::UnshareProject, session: Session) -> Result<()> {
1728    let project_id = ProjectId::from_proto(message.project_id);
1729    unshare_project_internal(project_id, session.connection_id, &session).await
1730}
1731
1732async fn unshare_project_internal(
1733    project_id: ProjectId,
1734    connection_id: ConnectionId,
1735    session: &Session,
1736) -> Result<()> {
1737    let delete = {
1738        let room_guard = session
1739            .db()
1740            .await
1741            .unshare_project(project_id, connection_id)
1742            .await?;
1743
1744        let (delete, room, guest_connection_ids) = &*room_guard;
1745
1746        let message = proto::UnshareProject {
1747            project_id: project_id.to_proto(),
1748        };
1749
1750        broadcast(
1751            Some(connection_id),
1752            guest_connection_ids.iter().copied(),
1753            |conn_id| session.peer.send(conn_id, message.clone()),
1754        );
1755        if let Some(room) = room {
1756            room_updated(room, &session.peer);
1757        }
1758
1759        *delete
1760    };
1761
1762    if delete {
1763        let db = session.db().await;
1764        db.delete_project(project_id).await?;
1765    }
1766
1767    Ok(())
1768}
1769
1770/// Join someone elses shared project.
1771async fn join_project(
1772    request: proto::JoinProject,
1773    response: Response<proto::JoinProject>,
1774    session: Session,
1775) -> Result<()> {
1776    let project_id = ProjectId::from_proto(request.project_id);
1777
1778    tracing::info!(%project_id, "join project");
1779
1780    let db = session.db().await;
1781    let (project, replica_id) = &mut *db
1782        .join_project(project_id, session.connection_id, session.user_id())
1783        .await?;
1784    drop(db);
1785    tracing::info!(%project_id, "join remote project");
1786    join_project_internal(response, session, project, replica_id)
1787}
1788
1789trait JoinProjectInternalResponse {
1790    fn send(self, result: proto::JoinProjectResponse) -> Result<()>;
1791}
1792impl JoinProjectInternalResponse for Response<proto::JoinProject> {
1793    fn send(self, result: proto::JoinProjectResponse) -> Result<()> {
1794        Response::<proto::JoinProject>::send(self, result)
1795    }
1796}
1797
1798fn join_project_internal(
1799    response: impl JoinProjectInternalResponse,
1800    session: Session,
1801    project: &mut Project,
1802    replica_id: &ReplicaId,
1803) -> Result<()> {
1804    let collaborators = project
1805        .collaborators
1806        .iter()
1807        .filter(|collaborator| collaborator.connection_id != session.connection_id)
1808        .map(|collaborator| collaborator.to_proto())
1809        .collect::<Vec<_>>();
1810    let project_id = project.id;
1811    let guest_user_id = session.user_id();
1812
1813    let worktrees = project
1814        .worktrees
1815        .iter()
1816        .map(|(id, worktree)| proto::WorktreeMetadata {
1817            id: *id,
1818            root_name: worktree.root_name.clone(),
1819            visible: worktree.visible,
1820            abs_path: worktree.abs_path.clone(),
1821        })
1822        .collect::<Vec<_>>();
1823
1824    let add_project_collaborator = proto::AddProjectCollaborator {
1825        project_id: project_id.to_proto(),
1826        collaborator: Some(proto::Collaborator {
1827            peer_id: Some(session.connection_id.into()),
1828            replica_id: replica_id.0 as u32,
1829            user_id: guest_user_id.to_proto(),
1830        }),
1831    };
1832
1833    for collaborator in &collaborators {
1834        session
1835            .peer
1836            .send(
1837                collaborator.peer_id.unwrap().into(),
1838                add_project_collaborator.clone(),
1839            )
1840            .trace_err();
1841    }
1842
1843    // First, we send the metadata associated with each worktree.
1844    response.send(proto::JoinProjectResponse {
1845        project_id: project.id.0 as u64,
1846        worktrees: worktrees.clone(),
1847        replica_id: replica_id.0 as u32,
1848        collaborators: collaborators.clone(),
1849        language_servers: project.language_servers.clone(),
1850        role: project.role.into(),
1851    })?;
1852
1853    for (worktree_id, worktree) in mem::take(&mut project.worktrees) {
1854        // Stream this worktree's entries.
1855        let message = proto::UpdateWorktree {
1856            project_id: project_id.to_proto(),
1857            worktree_id,
1858            abs_path: worktree.abs_path.clone(),
1859            root_name: worktree.root_name,
1860            updated_entries: worktree.entries,
1861            removed_entries: Default::default(),
1862            scan_id: worktree.scan_id,
1863            is_last_update: worktree.scan_id == worktree.completed_scan_id,
1864            updated_repositories: worktree.repository_entries.into_values().collect(),
1865            removed_repositories: Default::default(),
1866        };
1867        for update in proto::split_worktree_update(message) {
1868            session.peer.send(session.connection_id, update.clone())?;
1869        }
1870
1871        // Stream this worktree's diagnostics.
1872        for summary in worktree.diagnostic_summaries {
1873            session.peer.send(
1874                session.connection_id,
1875                proto::UpdateDiagnosticSummary {
1876                    project_id: project_id.to_proto(),
1877                    worktree_id: worktree.id,
1878                    summary: Some(summary),
1879                },
1880            )?;
1881        }
1882
1883        for settings_file in worktree.settings_files {
1884            session.peer.send(
1885                session.connection_id,
1886                proto::UpdateWorktreeSettings {
1887                    project_id: project_id.to_proto(),
1888                    worktree_id: worktree.id,
1889                    path: settings_file.path,
1890                    content: Some(settings_file.content),
1891                    kind: Some(settings_file.kind.to_proto() as i32),
1892                },
1893            )?;
1894        }
1895    }
1896
1897    for language_server in &project.language_servers {
1898        session.peer.send(
1899            session.connection_id,
1900            proto::UpdateLanguageServer {
1901                project_id: project_id.to_proto(),
1902                language_server_id: language_server.id,
1903                variant: Some(
1904                    proto::update_language_server::Variant::DiskBasedDiagnosticsUpdated(
1905                        proto::LspDiskBasedDiagnosticsUpdated {},
1906                    ),
1907                ),
1908            },
1909        )?;
1910    }
1911
1912    Ok(())
1913}
1914
1915/// Leave someone elses shared project.
1916async fn leave_project(request: proto::LeaveProject, session: Session) -> Result<()> {
1917    let sender_id = session.connection_id;
1918    let project_id = ProjectId::from_proto(request.project_id);
1919    let db = session.db().await;
1920
1921    let (room, project) = &*db.leave_project(project_id, sender_id).await?;
1922    tracing::info!(
1923        %project_id,
1924        "leave project"
1925    );
1926
1927    project_left(project, &session);
1928    if let Some(room) = room {
1929        room_updated(room, &session.peer);
1930    }
1931
1932    Ok(())
1933}
1934
1935/// Updates other participants with changes to the project
1936async fn update_project(
1937    request: proto::UpdateProject,
1938    response: Response<proto::UpdateProject>,
1939    session: Session,
1940) -> Result<()> {
1941    let project_id = ProjectId::from_proto(request.project_id);
1942    let (room, guest_connection_ids) = &*session
1943        .db()
1944        .await
1945        .update_project(project_id, session.connection_id, &request.worktrees)
1946        .await?;
1947    broadcast(
1948        Some(session.connection_id),
1949        guest_connection_ids.iter().copied(),
1950        |connection_id| {
1951            session
1952                .peer
1953                .forward_send(session.connection_id, connection_id, request.clone())
1954        },
1955    );
1956    if let Some(room) = room {
1957        room_updated(room, &session.peer);
1958    }
1959    response.send(proto::Ack {})?;
1960
1961    Ok(())
1962}
1963
1964/// Updates other participants with changes to the worktree
1965async fn update_worktree(
1966    request: proto::UpdateWorktree,
1967    response: Response<proto::UpdateWorktree>,
1968    session: Session,
1969) -> Result<()> {
1970    let guest_connection_ids = session
1971        .db()
1972        .await
1973        .update_worktree(&request, session.connection_id)
1974        .await?;
1975
1976    broadcast(
1977        Some(session.connection_id),
1978        guest_connection_ids.iter().copied(),
1979        |connection_id| {
1980            session
1981                .peer
1982                .forward_send(session.connection_id, connection_id, request.clone())
1983        },
1984    );
1985    response.send(proto::Ack {})?;
1986    Ok(())
1987}
1988
1989/// Updates other participants with changes to the diagnostics
1990async fn update_diagnostic_summary(
1991    message: proto::UpdateDiagnosticSummary,
1992    session: Session,
1993) -> Result<()> {
1994    let guest_connection_ids = session
1995        .db()
1996        .await
1997        .update_diagnostic_summary(&message, session.connection_id)
1998        .await?;
1999
2000    broadcast(
2001        Some(session.connection_id),
2002        guest_connection_ids.iter().copied(),
2003        |connection_id| {
2004            session
2005                .peer
2006                .forward_send(session.connection_id, connection_id, message.clone())
2007        },
2008    );
2009
2010    Ok(())
2011}
2012
2013/// Updates other participants with changes to the worktree settings
2014async fn update_worktree_settings(
2015    message: proto::UpdateWorktreeSettings,
2016    session: Session,
2017) -> Result<()> {
2018    let guest_connection_ids = session
2019        .db()
2020        .await
2021        .update_worktree_settings(&message, session.connection_id)
2022        .await?;
2023
2024    broadcast(
2025        Some(session.connection_id),
2026        guest_connection_ids.iter().copied(),
2027        |connection_id| {
2028            session
2029                .peer
2030                .forward_send(session.connection_id, connection_id, message.clone())
2031        },
2032    );
2033
2034    Ok(())
2035}
2036
2037/// Notify other participants that a  language server has started.
2038async fn start_language_server(
2039    request: proto::StartLanguageServer,
2040    session: Session,
2041) -> Result<()> {
2042    let guest_connection_ids = session
2043        .db()
2044        .await
2045        .start_language_server(&request, session.connection_id)
2046        .await?;
2047
2048    broadcast(
2049        Some(session.connection_id),
2050        guest_connection_ids.iter().copied(),
2051        |connection_id| {
2052            session
2053                .peer
2054                .forward_send(session.connection_id, connection_id, request.clone())
2055        },
2056    );
2057    Ok(())
2058}
2059
2060/// Notify other participants that a language server has changed.
2061async fn update_language_server(
2062    request: proto::UpdateLanguageServer,
2063    session: Session,
2064) -> Result<()> {
2065    let project_id = ProjectId::from_proto(request.project_id);
2066    let project_connection_ids = session
2067        .db()
2068        .await
2069        .project_connection_ids(project_id, session.connection_id, true)
2070        .await?;
2071    broadcast(
2072        Some(session.connection_id),
2073        project_connection_ids.iter().copied(),
2074        |connection_id| {
2075            session
2076                .peer
2077                .forward_send(session.connection_id, connection_id, request.clone())
2078        },
2079    );
2080    Ok(())
2081}
2082
2083/// forward a project request to the host. These requests should be read only
2084/// as guests are allowed to send them.
2085async fn forward_read_only_project_request<T>(
2086    request: T,
2087    response: Response<T>,
2088    session: Session,
2089) -> Result<()>
2090where
2091    T: EntityMessage + RequestMessage,
2092{
2093    let project_id = ProjectId::from_proto(request.remote_entity_id());
2094    let host_connection_id = session
2095        .db()
2096        .await
2097        .host_for_read_only_project_request(project_id, session.connection_id)
2098        .await?;
2099    let payload = session
2100        .peer
2101        .forward_request(session.connection_id, host_connection_id, request)
2102        .await?;
2103    response.send(payload)?;
2104    Ok(())
2105}
2106
2107async fn forward_find_search_candidates_request(
2108    request: proto::FindSearchCandidates,
2109    response: Response<proto::FindSearchCandidates>,
2110    session: Session,
2111) -> Result<()> {
2112    let project_id = ProjectId::from_proto(request.remote_entity_id());
2113    let host_connection_id = session
2114        .db()
2115        .await
2116        .host_for_read_only_project_request(project_id, session.connection_id)
2117        .await?;
2118    let payload = session
2119        .peer
2120        .forward_request(session.connection_id, host_connection_id, request)
2121        .await?;
2122    response.send(payload)?;
2123    Ok(())
2124}
2125
2126/// forward a project request to the host. These requests are disallowed
2127/// for guests.
2128async fn forward_mutating_project_request<T>(
2129    request: T,
2130    response: Response<T>,
2131    session: Session,
2132) -> Result<()>
2133where
2134    T: EntityMessage + RequestMessage,
2135{
2136    let project_id = ProjectId::from_proto(request.remote_entity_id());
2137
2138    let host_connection_id = session
2139        .db()
2140        .await
2141        .host_for_mutating_project_request(project_id, session.connection_id)
2142        .await?;
2143    let payload = session
2144        .peer
2145        .forward_request(session.connection_id, host_connection_id, request)
2146        .await?;
2147    response.send(payload)?;
2148    Ok(())
2149}
2150
2151/// Notify other participants that a new buffer has been created
2152async fn create_buffer_for_peer(
2153    request: proto::CreateBufferForPeer,
2154    session: Session,
2155) -> Result<()> {
2156    session
2157        .db()
2158        .await
2159        .check_user_is_project_host(
2160            ProjectId::from_proto(request.project_id),
2161            session.connection_id,
2162        )
2163        .await?;
2164    let peer_id = request.peer_id.ok_or_else(|| anyhow!("invalid peer id"))?;
2165    session
2166        .peer
2167        .forward_send(session.connection_id, peer_id.into(), request)?;
2168    Ok(())
2169}
2170
2171/// Notify other participants that a buffer has been updated. This is
2172/// allowed for guests as long as the update is limited to selections.
2173async fn update_buffer(
2174    request: proto::UpdateBuffer,
2175    response: Response<proto::UpdateBuffer>,
2176    session: Session,
2177) -> Result<()> {
2178    let project_id = ProjectId::from_proto(request.project_id);
2179    let mut capability = Capability::ReadOnly;
2180
2181    for op in request.operations.iter() {
2182        match op.variant {
2183            None | Some(proto::operation::Variant::UpdateSelections(_)) => {}
2184            Some(_) => capability = Capability::ReadWrite,
2185        }
2186    }
2187
2188    let host = {
2189        let guard = session
2190            .db()
2191            .await
2192            .connections_for_buffer_update(project_id, session.connection_id, capability)
2193            .await?;
2194
2195        let (host, guests) = &*guard;
2196
2197        broadcast(
2198            Some(session.connection_id),
2199            guests.clone(),
2200            |connection_id| {
2201                session
2202                    .peer
2203                    .forward_send(session.connection_id, connection_id, request.clone())
2204            },
2205        );
2206
2207        *host
2208    };
2209
2210    if host != session.connection_id {
2211        session
2212            .peer
2213            .forward_request(session.connection_id, host, request.clone())
2214            .await?;
2215    }
2216
2217    response.send(proto::Ack {})?;
2218    Ok(())
2219}
2220
2221async fn update_context(message: proto::UpdateContext, session: Session) -> Result<()> {
2222    let project_id = ProjectId::from_proto(message.project_id);
2223
2224    let operation = message.operation.as_ref().context("invalid operation")?;
2225    let capability = match operation.variant.as_ref() {
2226        Some(proto::context_operation::Variant::BufferOperation(buffer_op)) => {
2227            if let Some(buffer_op) = buffer_op.operation.as_ref() {
2228                match buffer_op.variant {
2229                    None | Some(proto::operation::Variant::UpdateSelections(_)) => {
2230                        Capability::ReadOnly
2231                    }
2232                    _ => Capability::ReadWrite,
2233                }
2234            } else {
2235                Capability::ReadWrite
2236            }
2237        }
2238        Some(_) => Capability::ReadWrite,
2239        None => Capability::ReadOnly,
2240    };
2241
2242    let guard = session
2243        .db()
2244        .await
2245        .connections_for_buffer_update(project_id, session.connection_id, capability)
2246        .await?;
2247
2248    let (host, guests) = &*guard;
2249
2250    broadcast(
2251        Some(session.connection_id),
2252        guests.iter().chain([host]).copied(),
2253        |connection_id| {
2254            session
2255                .peer
2256                .forward_send(session.connection_id, connection_id, message.clone())
2257        },
2258    );
2259
2260    Ok(())
2261}
2262
2263/// Notify other participants that a project has been updated.
2264async fn broadcast_project_message_from_host<T: EntityMessage<Entity = ShareProject>>(
2265    request: T,
2266    session: Session,
2267) -> Result<()> {
2268    let project_id = ProjectId::from_proto(request.remote_entity_id());
2269    let project_connection_ids = session
2270        .db()
2271        .await
2272        .project_connection_ids(project_id, session.connection_id, false)
2273        .await?;
2274
2275    broadcast(
2276        Some(session.connection_id),
2277        project_connection_ids.iter().copied(),
2278        |connection_id| {
2279            session
2280                .peer
2281                .forward_send(session.connection_id, connection_id, request.clone())
2282        },
2283    );
2284    Ok(())
2285}
2286
2287/// Start following another user in a call.
2288async fn follow(
2289    request: proto::Follow,
2290    response: Response<proto::Follow>,
2291    session: Session,
2292) -> Result<()> {
2293    let room_id = RoomId::from_proto(request.room_id);
2294    let project_id = request.project_id.map(ProjectId::from_proto);
2295    let leader_id = request
2296        .leader_id
2297        .ok_or_else(|| anyhow!("invalid leader id"))?
2298        .into();
2299    let follower_id = session.connection_id;
2300
2301    session
2302        .db()
2303        .await
2304        .check_room_participants(room_id, leader_id, session.connection_id)
2305        .await?;
2306
2307    let response_payload = session
2308        .peer
2309        .forward_request(session.connection_id, leader_id, request)
2310        .await?;
2311    response.send(response_payload)?;
2312
2313    if let Some(project_id) = project_id {
2314        let room = session
2315            .db()
2316            .await
2317            .follow(room_id, project_id, leader_id, follower_id)
2318            .await?;
2319        room_updated(&room, &session.peer);
2320    }
2321
2322    Ok(())
2323}
2324
2325/// Stop following another user in a call.
2326async fn unfollow(request: proto::Unfollow, session: Session) -> Result<()> {
2327    let room_id = RoomId::from_proto(request.room_id);
2328    let project_id = request.project_id.map(ProjectId::from_proto);
2329    let leader_id = request
2330        .leader_id
2331        .ok_or_else(|| anyhow!("invalid leader id"))?
2332        .into();
2333    let follower_id = session.connection_id;
2334
2335    session
2336        .db()
2337        .await
2338        .check_room_participants(room_id, leader_id, session.connection_id)
2339        .await?;
2340
2341    session
2342        .peer
2343        .forward_send(session.connection_id, leader_id, request)?;
2344
2345    if let Some(project_id) = project_id {
2346        let room = session
2347            .db()
2348            .await
2349            .unfollow(room_id, project_id, leader_id, follower_id)
2350            .await?;
2351        room_updated(&room, &session.peer);
2352    }
2353
2354    Ok(())
2355}
2356
2357/// Notify everyone following you of your current location.
2358async fn update_followers(request: proto::UpdateFollowers, session: Session) -> Result<()> {
2359    let room_id = RoomId::from_proto(request.room_id);
2360    let database = session.db.lock().await;
2361
2362    let connection_ids = if let Some(project_id) = request.project_id {
2363        let project_id = ProjectId::from_proto(project_id);
2364        database
2365            .project_connection_ids(project_id, session.connection_id, true)
2366            .await?
2367    } else {
2368        database
2369            .room_connection_ids(room_id, session.connection_id)
2370            .await?
2371    };
2372
2373    // For now, don't send view update messages back to that view's current leader.
2374    let peer_id_to_omit = request.variant.as_ref().and_then(|variant| match variant {
2375        proto::update_followers::Variant::UpdateView(payload) => payload.leader_id,
2376        _ => None,
2377    });
2378
2379    for connection_id in connection_ids.iter().cloned() {
2380        if Some(connection_id.into()) != peer_id_to_omit && connection_id != session.connection_id {
2381            session
2382                .peer
2383                .forward_send(session.connection_id, connection_id, request.clone())?;
2384        }
2385    }
2386    Ok(())
2387}
2388
2389/// Get public data about users.
2390async fn get_users(
2391    request: proto::GetUsers,
2392    response: Response<proto::GetUsers>,
2393    session: Session,
2394) -> Result<()> {
2395    let user_ids = request
2396        .user_ids
2397        .into_iter()
2398        .map(UserId::from_proto)
2399        .collect();
2400    let users = session
2401        .db()
2402        .await
2403        .get_users_by_ids(user_ids)
2404        .await?
2405        .into_iter()
2406        .map(|user| proto::User {
2407            id: user.id.to_proto(),
2408            avatar_url: format!("https://github.com/{}.png?size=128", user.github_login),
2409            github_login: user.github_login,
2410        })
2411        .collect();
2412    response.send(proto::UsersResponse { users })?;
2413    Ok(())
2414}
2415
2416/// Search for users (to invite) buy Github login
2417async fn fuzzy_search_users(
2418    request: proto::FuzzySearchUsers,
2419    response: Response<proto::FuzzySearchUsers>,
2420    session: Session,
2421) -> Result<()> {
2422    let query = request.query;
2423    let users = match query.len() {
2424        0 => vec![],
2425        1 | 2 => session
2426            .db()
2427            .await
2428            .get_user_by_github_login(&query)
2429            .await?
2430            .into_iter()
2431            .collect(),
2432        _ => session.db().await.fuzzy_search_users(&query, 10).await?,
2433    };
2434    let users = users
2435        .into_iter()
2436        .filter(|user| user.id != session.user_id())
2437        .map(|user| proto::User {
2438            id: user.id.to_proto(),
2439            avatar_url: format!("https://github.com/{}.png?size=128", user.github_login),
2440            github_login: user.github_login,
2441        })
2442        .collect();
2443    response.send(proto::UsersResponse { users })?;
2444    Ok(())
2445}
2446
2447/// Send a contact request to another user.
2448async fn request_contact(
2449    request: proto::RequestContact,
2450    response: Response<proto::RequestContact>,
2451    session: Session,
2452) -> Result<()> {
2453    let requester_id = session.user_id();
2454    let responder_id = UserId::from_proto(request.responder_id);
2455    if requester_id == responder_id {
2456        return Err(anyhow!("cannot add yourself as a contact"))?;
2457    }
2458
2459    let notifications = session
2460        .db()
2461        .await
2462        .send_contact_request(requester_id, responder_id)
2463        .await?;
2464
2465    // Update outgoing contact requests of requester
2466    let mut update = proto::UpdateContacts::default();
2467    update.outgoing_requests.push(responder_id.to_proto());
2468    for connection_id in session
2469        .connection_pool()
2470        .await
2471        .user_connection_ids(requester_id)
2472    {
2473        session.peer.send(connection_id, update.clone())?;
2474    }
2475
2476    // Update incoming contact requests of responder
2477    let mut update = proto::UpdateContacts::default();
2478    update
2479        .incoming_requests
2480        .push(proto::IncomingContactRequest {
2481            requester_id: requester_id.to_proto(),
2482        });
2483    let connection_pool = session.connection_pool().await;
2484    for connection_id in connection_pool.user_connection_ids(responder_id) {
2485        session.peer.send(connection_id, update.clone())?;
2486    }
2487
2488    send_notifications(&connection_pool, &session.peer, notifications);
2489
2490    response.send(proto::Ack {})?;
2491    Ok(())
2492}
2493
2494/// Accept or decline a contact request
2495async fn respond_to_contact_request(
2496    request: proto::RespondToContactRequest,
2497    response: Response<proto::RespondToContactRequest>,
2498    session: Session,
2499) -> Result<()> {
2500    let responder_id = session.user_id();
2501    let requester_id = UserId::from_proto(request.requester_id);
2502    let db = session.db().await;
2503    if request.response == proto::ContactRequestResponse::Dismiss as i32 {
2504        db.dismiss_contact_notification(responder_id, requester_id)
2505            .await?;
2506    } else {
2507        let accept = request.response == proto::ContactRequestResponse::Accept as i32;
2508
2509        let notifications = db
2510            .respond_to_contact_request(responder_id, requester_id, accept)
2511            .await?;
2512        let requester_busy = db.is_user_busy(requester_id).await?;
2513        let responder_busy = db.is_user_busy(responder_id).await?;
2514
2515        let pool = session.connection_pool().await;
2516        // Update responder with new contact
2517        let mut update = proto::UpdateContacts::default();
2518        if accept {
2519            update
2520                .contacts
2521                .push(contact_for_user(requester_id, requester_busy, &pool));
2522        }
2523        update
2524            .remove_incoming_requests
2525            .push(requester_id.to_proto());
2526        for connection_id in pool.user_connection_ids(responder_id) {
2527            session.peer.send(connection_id, update.clone())?;
2528        }
2529
2530        // Update requester with new contact
2531        let mut update = proto::UpdateContacts::default();
2532        if accept {
2533            update
2534                .contacts
2535                .push(contact_for_user(responder_id, responder_busy, &pool));
2536        }
2537        update
2538            .remove_outgoing_requests
2539            .push(responder_id.to_proto());
2540
2541        for connection_id in pool.user_connection_ids(requester_id) {
2542            session.peer.send(connection_id, update.clone())?;
2543        }
2544
2545        send_notifications(&pool, &session.peer, notifications);
2546    }
2547
2548    response.send(proto::Ack {})?;
2549    Ok(())
2550}
2551
2552/// Remove a contact.
2553async fn remove_contact(
2554    request: proto::RemoveContact,
2555    response: Response<proto::RemoveContact>,
2556    session: Session,
2557) -> Result<()> {
2558    let requester_id = session.user_id();
2559    let responder_id = UserId::from_proto(request.user_id);
2560    let db = session.db().await;
2561    let (contact_accepted, deleted_notification_id) =
2562        db.remove_contact(requester_id, responder_id).await?;
2563
2564    let pool = session.connection_pool().await;
2565    // Update outgoing contact requests of requester
2566    let mut update = proto::UpdateContacts::default();
2567    if contact_accepted {
2568        update.remove_contacts.push(responder_id.to_proto());
2569    } else {
2570        update
2571            .remove_outgoing_requests
2572            .push(responder_id.to_proto());
2573    }
2574    for connection_id in pool.user_connection_ids(requester_id) {
2575        session.peer.send(connection_id, update.clone())?;
2576    }
2577
2578    // Update incoming contact requests of responder
2579    let mut update = proto::UpdateContacts::default();
2580    if contact_accepted {
2581        update.remove_contacts.push(requester_id.to_proto());
2582    } else {
2583        update
2584            .remove_incoming_requests
2585            .push(requester_id.to_proto());
2586    }
2587    for connection_id in pool.user_connection_ids(responder_id) {
2588        session.peer.send(connection_id, update.clone())?;
2589        if let Some(notification_id) = deleted_notification_id {
2590            session.peer.send(
2591                connection_id,
2592                proto::DeleteNotification {
2593                    notification_id: notification_id.to_proto(),
2594                },
2595            )?;
2596        }
2597    }
2598
2599    response.send(proto::Ack {})?;
2600    Ok(())
2601}
2602
2603fn should_auto_subscribe_to_channels(version: ZedVersion) -> bool {
2604    version.0.minor() < 139
2605}
2606
2607async fn update_user_plan(_user_id: UserId, session: &Session) -> Result<()> {
2608    let plan = session.current_plan(&session.db().await).await?;
2609
2610    session
2611        .peer
2612        .send(
2613            session.connection_id,
2614            proto::UpdateUserPlan { plan: plan.into() },
2615        )
2616        .trace_err();
2617
2618    Ok(())
2619}
2620
2621async fn subscribe_to_channels(_: proto::SubscribeToChannels, session: Session) -> Result<()> {
2622    subscribe_user_to_channels(session.user_id(), &session).await?;
2623    Ok(())
2624}
2625
2626async fn subscribe_user_to_channels(user_id: UserId, session: &Session) -> Result<(), Error> {
2627    let channels_for_user = session.db().await.get_channels_for_user(user_id).await?;
2628    let mut pool = session.connection_pool().await;
2629    for membership in &channels_for_user.channel_memberships {
2630        pool.subscribe_to_channel(user_id, membership.channel_id, membership.role)
2631    }
2632    session.peer.send(
2633        session.connection_id,
2634        build_update_user_channels(&channels_for_user),
2635    )?;
2636    session.peer.send(
2637        session.connection_id,
2638        build_channels_update(channels_for_user),
2639    )?;
2640    Ok(())
2641}
2642
2643/// Creates a new channel.
2644async fn create_channel(
2645    request: proto::CreateChannel,
2646    response: Response<proto::CreateChannel>,
2647    session: Session,
2648) -> Result<()> {
2649    let db = session.db().await;
2650
2651    let parent_id = request.parent_id.map(ChannelId::from_proto);
2652    let (channel, membership) = db
2653        .create_channel(&request.name, parent_id, session.user_id())
2654        .await?;
2655
2656    let root_id = channel.root_id();
2657    let channel = Channel::from_model(channel);
2658
2659    response.send(proto::CreateChannelResponse {
2660        channel: Some(channel.to_proto()),
2661        parent_id: request.parent_id,
2662    })?;
2663
2664    let mut connection_pool = session.connection_pool().await;
2665    if let Some(membership) = membership {
2666        connection_pool.subscribe_to_channel(
2667            membership.user_id,
2668            membership.channel_id,
2669            membership.role,
2670        );
2671        let update = proto::UpdateUserChannels {
2672            channel_memberships: vec![proto::ChannelMembership {
2673                channel_id: membership.channel_id.to_proto(),
2674                role: membership.role.into(),
2675            }],
2676            ..Default::default()
2677        };
2678        for connection_id in connection_pool.user_connection_ids(membership.user_id) {
2679            session.peer.send(connection_id, update.clone())?;
2680        }
2681    }
2682
2683    for (connection_id, role) in connection_pool.channel_connection_ids(root_id) {
2684        if !role.can_see_channel(channel.visibility) {
2685            continue;
2686        }
2687
2688        let update = proto::UpdateChannels {
2689            channels: vec![channel.to_proto()],
2690            ..Default::default()
2691        };
2692        session.peer.send(connection_id, update.clone())?;
2693    }
2694
2695    Ok(())
2696}
2697
2698/// Delete a channel
2699async fn delete_channel(
2700    request: proto::DeleteChannel,
2701    response: Response<proto::DeleteChannel>,
2702    session: Session,
2703) -> Result<()> {
2704    let db = session.db().await;
2705
2706    let channel_id = request.channel_id;
2707    let (root_channel, removed_channels) = db
2708        .delete_channel(ChannelId::from_proto(channel_id), session.user_id())
2709        .await?;
2710    response.send(proto::Ack {})?;
2711
2712    // Notify members of removed channels
2713    let mut update = proto::UpdateChannels::default();
2714    update
2715        .delete_channels
2716        .extend(removed_channels.into_iter().map(|id| id.to_proto()));
2717
2718    let connection_pool = session.connection_pool().await;
2719    for (connection_id, _) in connection_pool.channel_connection_ids(root_channel) {
2720        session.peer.send(connection_id, update.clone())?;
2721    }
2722
2723    Ok(())
2724}
2725
2726/// Invite someone to join a channel.
2727async fn invite_channel_member(
2728    request: proto::InviteChannelMember,
2729    response: Response<proto::InviteChannelMember>,
2730    session: Session,
2731) -> Result<()> {
2732    let db = session.db().await;
2733    let channel_id = ChannelId::from_proto(request.channel_id);
2734    let invitee_id = UserId::from_proto(request.user_id);
2735    let InviteMemberResult {
2736        channel,
2737        notifications,
2738    } = db
2739        .invite_channel_member(
2740            channel_id,
2741            invitee_id,
2742            session.user_id(),
2743            request.role().into(),
2744        )
2745        .await?;
2746
2747    let update = proto::UpdateChannels {
2748        channel_invitations: vec![channel.to_proto()],
2749        ..Default::default()
2750    };
2751
2752    let connection_pool = session.connection_pool().await;
2753    for connection_id in connection_pool.user_connection_ids(invitee_id) {
2754        session.peer.send(connection_id, update.clone())?;
2755    }
2756
2757    send_notifications(&connection_pool, &session.peer, notifications);
2758
2759    response.send(proto::Ack {})?;
2760    Ok(())
2761}
2762
2763/// remove someone from a channel
2764async fn remove_channel_member(
2765    request: proto::RemoveChannelMember,
2766    response: Response<proto::RemoveChannelMember>,
2767    session: Session,
2768) -> Result<()> {
2769    let db = session.db().await;
2770    let channel_id = ChannelId::from_proto(request.channel_id);
2771    let member_id = UserId::from_proto(request.user_id);
2772
2773    let RemoveChannelMemberResult {
2774        membership_update,
2775        notification_id,
2776    } = db
2777        .remove_channel_member(channel_id, member_id, session.user_id())
2778        .await?;
2779
2780    let mut connection_pool = session.connection_pool().await;
2781    notify_membership_updated(
2782        &mut connection_pool,
2783        membership_update,
2784        member_id,
2785        &session.peer,
2786    );
2787    for connection_id in connection_pool.user_connection_ids(member_id) {
2788        if let Some(notification_id) = notification_id {
2789            session
2790                .peer
2791                .send(
2792                    connection_id,
2793                    proto::DeleteNotification {
2794                        notification_id: notification_id.to_proto(),
2795                    },
2796                )
2797                .trace_err();
2798        }
2799    }
2800
2801    response.send(proto::Ack {})?;
2802    Ok(())
2803}
2804
2805/// Toggle the channel between public and private.
2806/// Care is taken to maintain the invariant that public channels only descend from public channels,
2807/// (though members-only channels can appear at any point in the hierarchy).
2808async fn set_channel_visibility(
2809    request: proto::SetChannelVisibility,
2810    response: Response<proto::SetChannelVisibility>,
2811    session: Session,
2812) -> Result<()> {
2813    let db = session.db().await;
2814    let channel_id = ChannelId::from_proto(request.channel_id);
2815    let visibility = request.visibility().into();
2816
2817    let channel_model = db
2818        .set_channel_visibility(channel_id, visibility, session.user_id())
2819        .await?;
2820    let root_id = channel_model.root_id();
2821    let channel = Channel::from_model(channel_model);
2822
2823    let mut connection_pool = session.connection_pool().await;
2824    for (user_id, role) in connection_pool
2825        .channel_user_ids(root_id)
2826        .collect::<Vec<_>>()
2827        .into_iter()
2828    {
2829        let update = if role.can_see_channel(channel.visibility) {
2830            connection_pool.subscribe_to_channel(user_id, channel_id, role);
2831            proto::UpdateChannels {
2832                channels: vec![channel.to_proto()],
2833                ..Default::default()
2834            }
2835        } else {
2836            connection_pool.unsubscribe_from_channel(&user_id, &channel_id);
2837            proto::UpdateChannels {
2838                delete_channels: vec![channel.id.to_proto()],
2839                ..Default::default()
2840            }
2841        };
2842
2843        for connection_id in connection_pool.user_connection_ids(user_id) {
2844            session.peer.send(connection_id, update.clone())?;
2845        }
2846    }
2847
2848    response.send(proto::Ack {})?;
2849    Ok(())
2850}
2851
2852/// Alter the role for a user in the channel.
2853async fn set_channel_member_role(
2854    request: proto::SetChannelMemberRole,
2855    response: Response<proto::SetChannelMemberRole>,
2856    session: Session,
2857) -> Result<()> {
2858    let db = session.db().await;
2859    let channel_id = ChannelId::from_proto(request.channel_id);
2860    let member_id = UserId::from_proto(request.user_id);
2861    let result = db
2862        .set_channel_member_role(
2863            channel_id,
2864            session.user_id(),
2865            member_id,
2866            request.role().into(),
2867        )
2868        .await?;
2869
2870    match result {
2871        db::SetMemberRoleResult::MembershipUpdated(membership_update) => {
2872            let mut connection_pool = session.connection_pool().await;
2873            notify_membership_updated(
2874                &mut connection_pool,
2875                membership_update,
2876                member_id,
2877                &session.peer,
2878            )
2879        }
2880        db::SetMemberRoleResult::InviteUpdated(channel) => {
2881            let update = proto::UpdateChannels {
2882                channel_invitations: vec![channel.to_proto()],
2883                ..Default::default()
2884            };
2885
2886            for connection_id in session
2887                .connection_pool()
2888                .await
2889                .user_connection_ids(member_id)
2890            {
2891                session.peer.send(connection_id, update.clone())?;
2892            }
2893        }
2894    }
2895
2896    response.send(proto::Ack {})?;
2897    Ok(())
2898}
2899
2900/// Change the name of a channel
2901async fn rename_channel(
2902    request: proto::RenameChannel,
2903    response: Response<proto::RenameChannel>,
2904    session: Session,
2905) -> Result<()> {
2906    let db = session.db().await;
2907    let channel_id = ChannelId::from_proto(request.channel_id);
2908    let channel_model = db
2909        .rename_channel(channel_id, session.user_id(), &request.name)
2910        .await?;
2911    let root_id = channel_model.root_id();
2912    let channel = Channel::from_model(channel_model);
2913
2914    response.send(proto::RenameChannelResponse {
2915        channel: Some(channel.to_proto()),
2916    })?;
2917
2918    let connection_pool = session.connection_pool().await;
2919    let update = proto::UpdateChannels {
2920        channels: vec![channel.to_proto()],
2921        ..Default::default()
2922    };
2923    for (connection_id, role) in connection_pool.channel_connection_ids(root_id) {
2924        if role.can_see_channel(channel.visibility) {
2925            session.peer.send(connection_id, update.clone())?;
2926        }
2927    }
2928
2929    Ok(())
2930}
2931
2932/// Move a channel to a new parent.
2933async fn move_channel(
2934    request: proto::MoveChannel,
2935    response: Response<proto::MoveChannel>,
2936    session: Session,
2937) -> Result<()> {
2938    let channel_id = ChannelId::from_proto(request.channel_id);
2939    let to = ChannelId::from_proto(request.to);
2940
2941    let (root_id, channels) = session
2942        .db()
2943        .await
2944        .move_channel(channel_id, to, session.user_id())
2945        .await?;
2946
2947    let connection_pool = session.connection_pool().await;
2948    for (connection_id, role) in connection_pool.channel_connection_ids(root_id) {
2949        let channels = channels
2950            .iter()
2951            .filter_map(|channel| {
2952                if role.can_see_channel(channel.visibility) {
2953                    Some(channel.to_proto())
2954                } else {
2955                    None
2956                }
2957            })
2958            .collect::<Vec<_>>();
2959        if channels.is_empty() {
2960            continue;
2961        }
2962
2963        let update = proto::UpdateChannels {
2964            channels,
2965            ..Default::default()
2966        };
2967
2968        session.peer.send(connection_id, update.clone())?;
2969    }
2970
2971    response.send(Ack {})?;
2972    Ok(())
2973}
2974
2975/// Get the list of channel members
2976async fn get_channel_members(
2977    request: proto::GetChannelMembers,
2978    response: Response<proto::GetChannelMembers>,
2979    session: Session,
2980) -> Result<()> {
2981    let db = session.db().await;
2982    let channel_id = ChannelId::from_proto(request.channel_id);
2983    let limit = if request.limit == 0 {
2984        u16::MAX as u64
2985    } else {
2986        request.limit
2987    };
2988    let (members, users) = db
2989        .get_channel_participant_details(channel_id, &request.query, limit, session.user_id())
2990        .await?;
2991    response.send(proto::GetChannelMembersResponse { members, users })?;
2992    Ok(())
2993}
2994
2995/// Accept or decline a channel invitation.
2996async fn respond_to_channel_invite(
2997    request: proto::RespondToChannelInvite,
2998    response: Response<proto::RespondToChannelInvite>,
2999    session: Session,
3000) -> Result<()> {
3001    let db = session.db().await;
3002    let channel_id = ChannelId::from_proto(request.channel_id);
3003    let RespondToChannelInvite {
3004        membership_update,
3005        notifications,
3006    } = db
3007        .respond_to_channel_invite(channel_id, session.user_id(), request.accept)
3008        .await?;
3009
3010    let mut connection_pool = session.connection_pool().await;
3011    if let Some(membership_update) = membership_update {
3012        notify_membership_updated(
3013            &mut connection_pool,
3014            membership_update,
3015            session.user_id(),
3016            &session.peer,
3017        );
3018    } else {
3019        let update = proto::UpdateChannels {
3020            remove_channel_invitations: vec![channel_id.to_proto()],
3021            ..Default::default()
3022        };
3023
3024        for connection_id in connection_pool.user_connection_ids(session.user_id()) {
3025            session.peer.send(connection_id, update.clone())?;
3026        }
3027    };
3028
3029    send_notifications(&connection_pool, &session.peer, notifications);
3030
3031    response.send(proto::Ack {})?;
3032
3033    Ok(())
3034}
3035
3036/// Join the channels' room
3037async fn join_channel(
3038    request: proto::JoinChannel,
3039    response: Response<proto::JoinChannel>,
3040    session: Session,
3041) -> Result<()> {
3042    let channel_id = ChannelId::from_proto(request.channel_id);
3043    join_channel_internal(channel_id, Box::new(response), session).await
3044}
3045
3046trait JoinChannelInternalResponse {
3047    fn send(self, result: proto::JoinRoomResponse) -> Result<()>;
3048}
3049impl JoinChannelInternalResponse for Response<proto::JoinChannel> {
3050    fn send(self, result: proto::JoinRoomResponse) -> Result<()> {
3051        Response::<proto::JoinChannel>::send(self, result)
3052    }
3053}
3054impl JoinChannelInternalResponse for Response<proto::JoinRoom> {
3055    fn send(self, result: proto::JoinRoomResponse) -> Result<()> {
3056        Response::<proto::JoinRoom>::send(self, result)
3057    }
3058}
3059
3060async fn join_channel_internal(
3061    channel_id: ChannelId,
3062    response: Box<impl JoinChannelInternalResponse>,
3063    session: Session,
3064) -> Result<()> {
3065    let joined_room = {
3066        let mut db = session.db().await;
3067        // If zed quits without leaving the room, and the user re-opens zed before the
3068        // RECONNECT_TIMEOUT, we need to make sure that we kick the user out of the previous
3069        // room they were in.
3070        if let Some(connection) = db.stale_room_connection(session.user_id()).await? {
3071            tracing::info!(
3072                stale_connection_id = %connection,
3073                "cleaning up stale connection",
3074            );
3075            drop(db);
3076            leave_room_for_session(&session, connection).await?;
3077            db = session.db().await;
3078        }
3079
3080        let (joined_room, membership_updated, role) = db
3081            .join_channel(channel_id, session.user_id(), session.connection_id)
3082            .await?;
3083
3084        let live_kit_connection_info =
3085            session
3086                .app_state
3087                .live_kit_client
3088                .as_ref()
3089                .and_then(|live_kit| {
3090                    let (can_publish, token) = if role == ChannelRole::Guest {
3091                        (
3092                            false,
3093                            live_kit
3094                                .guest_token(
3095                                    &joined_room.room.live_kit_room,
3096                                    &session.user_id().to_string(),
3097                                )
3098                                .trace_err()?,
3099                        )
3100                    } else {
3101                        (
3102                            true,
3103                            live_kit
3104                                .room_token(
3105                                    &joined_room.room.live_kit_room,
3106                                    &session.user_id().to_string(),
3107                                )
3108                                .trace_err()?,
3109                        )
3110                    };
3111
3112                    Some(LiveKitConnectionInfo {
3113                        server_url: live_kit.url().into(),
3114                        token,
3115                        can_publish,
3116                    })
3117                });
3118
3119        response.send(proto::JoinRoomResponse {
3120            room: Some(joined_room.room.clone()),
3121            channel_id: joined_room
3122                .channel
3123                .as_ref()
3124                .map(|channel| channel.id.to_proto()),
3125            live_kit_connection_info,
3126        })?;
3127
3128        let mut connection_pool = session.connection_pool().await;
3129        if let Some(membership_updated) = membership_updated {
3130            notify_membership_updated(
3131                &mut connection_pool,
3132                membership_updated,
3133                session.user_id(),
3134                &session.peer,
3135            );
3136        }
3137
3138        room_updated(&joined_room.room, &session.peer);
3139
3140        joined_room
3141    };
3142
3143    channel_updated(
3144        &joined_room
3145            .channel
3146            .ok_or_else(|| anyhow!("channel not returned"))?,
3147        &joined_room.room,
3148        &session.peer,
3149        &*session.connection_pool().await,
3150    );
3151
3152    update_user_contacts(session.user_id(), &session).await?;
3153    Ok(())
3154}
3155
3156/// Start editing the channel notes
3157async fn join_channel_buffer(
3158    request: proto::JoinChannelBuffer,
3159    response: Response<proto::JoinChannelBuffer>,
3160    session: Session,
3161) -> Result<()> {
3162    let db = session.db().await;
3163    let channel_id = ChannelId::from_proto(request.channel_id);
3164
3165    let open_response = db
3166        .join_channel_buffer(channel_id, session.user_id(), session.connection_id)
3167        .await?;
3168
3169    let collaborators = open_response.collaborators.clone();
3170    response.send(open_response)?;
3171
3172    let update = UpdateChannelBufferCollaborators {
3173        channel_id: channel_id.to_proto(),
3174        collaborators: collaborators.clone(),
3175    };
3176    channel_buffer_updated(
3177        session.connection_id,
3178        collaborators
3179            .iter()
3180            .filter_map(|collaborator| Some(collaborator.peer_id?.into())),
3181        &update,
3182        &session.peer,
3183    );
3184
3185    Ok(())
3186}
3187
3188/// Edit the channel notes
3189async fn update_channel_buffer(
3190    request: proto::UpdateChannelBuffer,
3191    session: Session,
3192) -> Result<()> {
3193    let db = session.db().await;
3194    let channel_id = ChannelId::from_proto(request.channel_id);
3195
3196    let (collaborators, epoch, version) = db
3197        .update_channel_buffer(channel_id, session.user_id(), &request.operations)
3198        .await?;
3199
3200    channel_buffer_updated(
3201        session.connection_id,
3202        collaborators.clone(),
3203        &proto::UpdateChannelBuffer {
3204            channel_id: channel_id.to_proto(),
3205            operations: request.operations,
3206        },
3207        &session.peer,
3208    );
3209
3210    let pool = &*session.connection_pool().await;
3211
3212    let non_collaborators =
3213        pool.channel_connection_ids(channel_id)
3214            .filter_map(|(connection_id, _)| {
3215                if collaborators.contains(&connection_id) {
3216                    None
3217                } else {
3218                    Some(connection_id)
3219                }
3220            });
3221
3222    broadcast(None, non_collaborators, |peer_id| {
3223        session.peer.send(
3224            peer_id,
3225            proto::UpdateChannels {
3226                latest_channel_buffer_versions: vec![proto::ChannelBufferVersion {
3227                    channel_id: channel_id.to_proto(),
3228                    epoch: epoch as u64,
3229                    version: version.clone(),
3230                }],
3231                ..Default::default()
3232            },
3233        )
3234    });
3235
3236    Ok(())
3237}
3238
3239/// Rejoin the channel notes after a connection blip
3240async fn rejoin_channel_buffers(
3241    request: proto::RejoinChannelBuffers,
3242    response: Response<proto::RejoinChannelBuffers>,
3243    session: Session,
3244) -> Result<()> {
3245    let db = session.db().await;
3246    let buffers = db
3247        .rejoin_channel_buffers(&request.buffers, session.user_id(), session.connection_id)
3248        .await?;
3249
3250    for rejoined_buffer in &buffers {
3251        let collaborators_to_notify = rejoined_buffer
3252            .buffer
3253            .collaborators
3254            .iter()
3255            .filter_map(|c| Some(c.peer_id?.into()));
3256        channel_buffer_updated(
3257            session.connection_id,
3258            collaborators_to_notify,
3259            &proto::UpdateChannelBufferCollaborators {
3260                channel_id: rejoined_buffer.buffer.channel_id,
3261                collaborators: rejoined_buffer.buffer.collaborators.clone(),
3262            },
3263            &session.peer,
3264        );
3265    }
3266
3267    response.send(proto::RejoinChannelBuffersResponse {
3268        buffers: buffers.into_iter().map(|b| b.buffer).collect(),
3269    })?;
3270
3271    Ok(())
3272}
3273
3274/// Stop editing the channel notes
3275async fn leave_channel_buffer(
3276    request: proto::LeaveChannelBuffer,
3277    response: Response<proto::LeaveChannelBuffer>,
3278    session: Session,
3279) -> Result<()> {
3280    let db = session.db().await;
3281    let channel_id = ChannelId::from_proto(request.channel_id);
3282
3283    let left_buffer = db
3284        .leave_channel_buffer(channel_id, session.connection_id)
3285        .await?;
3286
3287    response.send(Ack {})?;
3288
3289    channel_buffer_updated(
3290        session.connection_id,
3291        left_buffer.connections,
3292        &proto::UpdateChannelBufferCollaborators {
3293            channel_id: channel_id.to_proto(),
3294            collaborators: left_buffer.collaborators,
3295        },
3296        &session.peer,
3297    );
3298
3299    Ok(())
3300}
3301
3302fn channel_buffer_updated<T: EnvelopedMessage>(
3303    sender_id: ConnectionId,
3304    collaborators: impl IntoIterator<Item = ConnectionId>,
3305    message: &T,
3306    peer: &Peer,
3307) {
3308    broadcast(Some(sender_id), collaborators, |peer_id| {
3309        peer.send(peer_id, message.clone())
3310    });
3311}
3312
3313fn send_notifications(
3314    connection_pool: &ConnectionPool,
3315    peer: &Peer,
3316    notifications: db::NotificationBatch,
3317) {
3318    for (user_id, notification) in notifications {
3319        for connection_id in connection_pool.user_connection_ids(user_id) {
3320            if let Err(error) = peer.send(
3321                connection_id,
3322                proto::AddNotification {
3323                    notification: Some(notification.clone()),
3324                },
3325            ) {
3326                tracing::error!(
3327                    "failed to send notification to {:?} {}",
3328                    connection_id,
3329                    error
3330                );
3331            }
3332        }
3333    }
3334}
3335
3336/// Send a message to the channel
3337async fn send_channel_message(
3338    request: proto::SendChannelMessage,
3339    response: Response<proto::SendChannelMessage>,
3340    session: Session,
3341) -> Result<()> {
3342    // Validate the message body.
3343    let body = request.body.trim().to_string();
3344    if body.len() > MAX_MESSAGE_LEN {
3345        return Err(anyhow!("message is too long"))?;
3346    }
3347    if body.is_empty() {
3348        return Err(anyhow!("message can't be blank"))?;
3349    }
3350
3351    // TODO: adjust mentions if body is trimmed
3352
3353    let timestamp = OffsetDateTime::now_utc();
3354    let nonce = request
3355        .nonce
3356        .ok_or_else(|| anyhow!("nonce can't be blank"))?;
3357
3358    let channel_id = ChannelId::from_proto(request.channel_id);
3359    let CreatedChannelMessage {
3360        message_id,
3361        participant_connection_ids,
3362        notifications,
3363    } = session
3364        .db()
3365        .await
3366        .create_channel_message(
3367            channel_id,
3368            session.user_id(),
3369            &body,
3370            &request.mentions,
3371            timestamp,
3372            nonce.clone().into(),
3373            request.reply_to_message_id.map(MessageId::from_proto),
3374        )
3375        .await?;
3376
3377    let message = proto::ChannelMessage {
3378        sender_id: session.user_id().to_proto(),
3379        id: message_id.to_proto(),
3380        body,
3381        mentions: request.mentions,
3382        timestamp: timestamp.unix_timestamp() as u64,
3383        nonce: Some(nonce),
3384        reply_to_message_id: request.reply_to_message_id,
3385        edited_at: None,
3386    };
3387    broadcast(
3388        Some(session.connection_id),
3389        participant_connection_ids.clone(),
3390        |connection| {
3391            session.peer.send(
3392                connection,
3393                proto::ChannelMessageSent {
3394                    channel_id: channel_id.to_proto(),
3395                    message: Some(message.clone()),
3396                },
3397            )
3398        },
3399    );
3400    response.send(proto::SendChannelMessageResponse {
3401        message: Some(message),
3402    })?;
3403
3404    let pool = &*session.connection_pool().await;
3405    let non_participants =
3406        pool.channel_connection_ids(channel_id)
3407            .filter_map(|(connection_id, _)| {
3408                if participant_connection_ids.contains(&connection_id) {
3409                    None
3410                } else {
3411                    Some(connection_id)
3412                }
3413            });
3414    broadcast(None, non_participants, |peer_id| {
3415        session.peer.send(
3416            peer_id,
3417            proto::UpdateChannels {
3418                latest_channel_message_ids: vec![proto::ChannelMessageId {
3419                    channel_id: channel_id.to_proto(),
3420                    message_id: message_id.to_proto(),
3421                }],
3422                ..Default::default()
3423            },
3424        )
3425    });
3426    send_notifications(pool, &session.peer, notifications);
3427
3428    Ok(())
3429}
3430
3431/// Delete a channel message
3432async fn remove_channel_message(
3433    request: proto::RemoveChannelMessage,
3434    response: Response<proto::RemoveChannelMessage>,
3435    session: Session,
3436) -> Result<()> {
3437    let channel_id = ChannelId::from_proto(request.channel_id);
3438    let message_id = MessageId::from_proto(request.message_id);
3439    let (connection_ids, existing_notification_ids) = session
3440        .db()
3441        .await
3442        .remove_channel_message(channel_id, message_id, session.user_id())
3443        .await?;
3444
3445    broadcast(
3446        Some(session.connection_id),
3447        connection_ids,
3448        move |connection| {
3449            session.peer.send(connection, request.clone())?;
3450
3451            for notification_id in &existing_notification_ids {
3452                session.peer.send(
3453                    connection,
3454                    proto::DeleteNotification {
3455                        notification_id: (*notification_id).to_proto(),
3456                    },
3457                )?;
3458            }
3459
3460            Ok(())
3461        },
3462    );
3463    response.send(proto::Ack {})?;
3464    Ok(())
3465}
3466
3467async fn update_channel_message(
3468    request: proto::UpdateChannelMessage,
3469    response: Response<proto::UpdateChannelMessage>,
3470    session: Session,
3471) -> Result<()> {
3472    let channel_id = ChannelId::from_proto(request.channel_id);
3473    let message_id = MessageId::from_proto(request.message_id);
3474    let updated_at = OffsetDateTime::now_utc();
3475    let UpdatedChannelMessage {
3476        message_id,
3477        participant_connection_ids,
3478        notifications,
3479        reply_to_message_id,
3480        timestamp,
3481        deleted_mention_notification_ids,
3482        updated_mention_notifications,
3483    } = session
3484        .db()
3485        .await
3486        .update_channel_message(
3487            channel_id,
3488            message_id,
3489            session.user_id(),
3490            request.body.as_str(),
3491            &request.mentions,
3492            updated_at,
3493        )
3494        .await?;
3495
3496    let nonce = request
3497        .nonce
3498        .clone()
3499        .ok_or_else(|| anyhow!("nonce can't be blank"))?;
3500
3501    let message = proto::ChannelMessage {
3502        sender_id: session.user_id().to_proto(),
3503        id: message_id.to_proto(),
3504        body: request.body.clone(),
3505        mentions: request.mentions.clone(),
3506        timestamp: timestamp.assume_utc().unix_timestamp() as u64,
3507        nonce: Some(nonce),
3508        reply_to_message_id: reply_to_message_id.map(|id| id.to_proto()),
3509        edited_at: Some(updated_at.unix_timestamp() as u64),
3510    };
3511
3512    response.send(proto::Ack {})?;
3513
3514    let pool = &*session.connection_pool().await;
3515    broadcast(
3516        Some(session.connection_id),
3517        participant_connection_ids,
3518        |connection| {
3519            session.peer.send(
3520                connection,
3521                proto::ChannelMessageUpdate {
3522                    channel_id: channel_id.to_proto(),
3523                    message: Some(message.clone()),
3524                },
3525            )?;
3526
3527            for notification_id in &deleted_mention_notification_ids {
3528                session.peer.send(
3529                    connection,
3530                    proto::DeleteNotification {
3531                        notification_id: (*notification_id).to_proto(),
3532                    },
3533                )?;
3534            }
3535
3536            for notification in &updated_mention_notifications {
3537                session.peer.send(
3538                    connection,
3539                    proto::UpdateNotification {
3540                        notification: Some(notification.clone()),
3541                    },
3542                )?;
3543            }
3544
3545            Ok(())
3546        },
3547    );
3548
3549    send_notifications(pool, &session.peer, notifications);
3550
3551    Ok(())
3552}
3553
3554/// Mark a channel message as read
3555async fn acknowledge_channel_message(
3556    request: proto::AckChannelMessage,
3557    session: Session,
3558) -> Result<()> {
3559    let channel_id = ChannelId::from_proto(request.channel_id);
3560    let message_id = MessageId::from_proto(request.message_id);
3561    let notifications = session
3562        .db()
3563        .await
3564        .observe_channel_message(channel_id, session.user_id(), message_id)
3565        .await?;
3566    send_notifications(
3567        &*session.connection_pool().await,
3568        &session.peer,
3569        notifications,
3570    );
3571    Ok(())
3572}
3573
3574/// Mark a buffer version as synced
3575async fn acknowledge_buffer_version(
3576    request: proto::AckBufferOperation,
3577    session: Session,
3578) -> Result<()> {
3579    let buffer_id = BufferId::from_proto(request.buffer_id);
3580    session
3581        .db()
3582        .await
3583        .observe_buffer_version(
3584            buffer_id,
3585            session.user_id(),
3586            request.epoch as i32,
3587            &request.version,
3588        )
3589        .await?;
3590    Ok(())
3591}
3592
3593async fn count_language_model_tokens(
3594    request: proto::CountLanguageModelTokens,
3595    response: Response<proto::CountLanguageModelTokens>,
3596    session: Session,
3597    config: &Config,
3598) -> Result<()> {
3599    authorize_access_to_legacy_llm_endpoints(&session).await?;
3600
3601    let rate_limit: Box<dyn RateLimit> = match session.current_plan(&session.db().await).await? {
3602        proto::Plan::ZedPro => Box::new(ZedProCountLanguageModelTokensRateLimit),
3603        proto::Plan::Free => Box::new(FreeCountLanguageModelTokensRateLimit),
3604    };
3605
3606    session
3607        .app_state
3608        .rate_limiter
3609        .check(&*rate_limit, session.user_id())
3610        .await?;
3611
3612    let result = match proto::LanguageModelProvider::from_i32(request.provider) {
3613        Some(proto::LanguageModelProvider::Google) => {
3614            let api_key = config
3615                .google_ai_api_key
3616                .as_ref()
3617                .context("no Google AI API key configured on the server")?;
3618            google_ai::count_tokens(
3619                session.http_client.as_ref(),
3620                google_ai::API_URL,
3621                api_key,
3622                serde_json::from_str(&request.request)?,
3623                None,
3624            )
3625            .await?
3626        }
3627        _ => return Err(anyhow!("unsupported provider"))?,
3628    };
3629
3630    response.send(proto::CountLanguageModelTokensResponse {
3631        token_count: result.total_tokens as u32,
3632    })?;
3633
3634    Ok(())
3635}
3636
3637struct ZedProCountLanguageModelTokensRateLimit;
3638
3639impl RateLimit for ZedProCountLanguageModelTokensRateLimit {
3640    fn capacity(&self) -> usize {
3641        std::env::var("COUNT_LANGUAGE_MODEL_TOKENS_RATE_LIMIT_PER_HOUR")
3642            .ok()
3643            .and_then(|v| v.parse().ok())
3644            .unwrap_or(600) // Picked arbitrarily
3645    }
3646
3647    fn refill_duration(&self) -> chrono::Duration {
3648        chrono::Duration::hours(1)
3649    }
3650
3651    fn db_name(&self) -> &'static str {
3652        "zed-pro:count-language-model-tokens"
3653    }
3654}
3655
3656struct FreeCountLanguageModelTokensRateLimit;
3657
3658impl RateLimit for FreeCountLanguageModelTokensRateLimit {
3659    fn capacity(&self) -> usize {
3660        std::env::var("COUNT_LANGUAGE_MODEL_TOKENS_RATE_LIMIT_PER_HOUR_FREE")
3661            .ok()
3662            .and_then(|v| v.parse().ok())
3663            .unwrap_or(600 / 10) // Picked arbitrarily
3664    }
3665
3666    fn refill_duration(&self) -> chrono::Duration {
3667        chrono::Duration::hours(1)
3668    }
3669
3670    fn db_name(&self) -> &'static str {
3671        "free:count-language-model-tokens"
3672    }
3673}
3674
3675struct ZedProComputeEmbeddingsRateLimit;
3676
3677impl RateLimit for ZedProComputeEmbeddingsRateLimit {
3678    fn capacity(&self) -> usize {
3679        std::env::var("EMBED_TEXTS_RATE_LIMIT_PER_HOUR")
3680            .ok()
3681            .and_then(|v| v.parse().ok())
3682            .unwrap_or(5000) // Picked arbitrarily
3683    }
3684
3685    fn refill_duration(&self) -> chrono::Duration {
3686        chrono::Duration::hours(1)
3687    }
3688
3689    fn db_name(&self) -> &'static str {
3690        "zed-pro:compute-embeddings"
3691    }
3692}
3693
3694struct FreeComputeEmbeddingsRateLimit;
3695
3696impl RateLimit for FreeComputeEmbeddingsRateLimit {
3697    fn capacity(&self) -> usize {
3698        std::env::var("EMBED_TEXTS_RATE_LIMIT_PER_HOUR_FREE")
3699            .ok()
3700            .and_then(|v| v.parse().ok())
3701            .unwrap_or(5000 / 10) // Picked arbitrarily
3702    }
3703
3704    fn refill_duration(&self) -> chrono::Duration {
3705        chrono::Duration::hours(1)
3706    }
3707
3708    fn db_name(&self) -> &'static str {
3709        "free:compute-embeddings"
3710    }
3711}
3712
3713async fn compute_embeddings(
3714    request: proto::ComputeEmbeddings,
3715    response: Response<proto::ComputeEmbeddings>,
3716    session: Session,
3717    api_key: Option<Arc<str>>,
3718) -> Result<()> {
3719    let api_key = api_key.context("no OpenAI API key configured on the server")?;
3720    authorize_access_to_legacy_llm_endpoints(&session).await?;
3721
3722    let rate_limit: Box<dyn RateLimit> = match session.current_plan(&session.db().await).await? {
3723        proto::Plan::ZedPro => Box::new(ZedProComputeEmbeddingsRateLimit),
3724        proto::Plan::Free => Box::new(FreeComputeEmbeddingsRateLimit),
3725    };
3726
3727    session
3728        .app_state
3729        .rate_limiter
3730        .check(&*rate_limit, session.user_id())
3731        .await?;
3732
3733    let embeddings = match request.model.as_str() {
3734        "openai/text-embedding-3-small" => {
3735            open_ai::embed(
3736                session.http_client.as_ref(),
3737                OPEN_AI_API_URL,
3738                &api_key,
3739                OpenAiEmbeddingModel::TextEmbedding3Small,
3740                request.texts.iter().map(|text| text.as_str()),
3741            )
3742            .await?
3743        }
3744        provider => return Err(anyhow!("unsupported embedding provider {:?}", provider))?,
3745    };
3746
3747    let embeddings = request
3748        .texts
3749        .iter()
3750        .map(|text| {
3751            let mut hasher = sha2::Sha256::new();
3752            hasher.update(text.as_bytes());
3753            let result = hasher.finalize();
3754            result.to_vec()
3755        })
3756        .zip(
3757            embeddings
3758                .data
3759                .into_iter()
3760                .map(|embedding| embedding.embedding),
3761        )
3762        .collect::<HashMap<_, _>>();
3763
3764    let db = session.db().await;
3765    db.save_embeddings(&request.model, &embeddings)
3766        .await
3767        .context("failed to save embeddings")
3768        .trace_err();
3769
3770    response.send(proto::ComputeEmbeddingsResponse {
3771        embeddings: embeddings
3772            .into_iter()
3773            .map(|(digest, dimensions)| proto::Embedding { digest, dimensions })
3774            .collect(),
3775    })?;
3776    Ok(())
3777}
3778
3779async fn get_cached_embeddings(
3780    request: proto::GetCachedEmbeddings,
3781    response: Response<proto::GetCachedEmbeddings>,
3782    session: Session,
3783) -> Result<()> {
3784    authorize_access_to_legacy_llm_endpoints(&session).await?;
3785
3786    let db = session.db().await;
3787    let embeddings = db.get_embeddings(&request.model, &request.digests).await?;
3788
3789    response.send(proto::GetCachedEmbeddingsResponse {
3790        embeddings: embeddings
3791            .into_iter()
3792            .map(|(digest, dimensions)| proto::Embedding { digest, dimensions })
3793            .collect(),
3794    })?;
3795    Ok(())
3796}
3797
3798/// This is leftover from before the LLM service.
3799///
3800/// The endpoints protected by this check will be moved there eventually.
3801async fn authorize_access_to_legacy_llm_endpoints(session: &Session) -> Result<(), Error> {
3802    if session.is_staff() {
3803        Ok(())
3804    } else {
3805        Err(anyhow!("permission denied"))?
3806    }
3807}
3808
3809/// Get a Supermaven API key for the user
3810async fn get_supermaven_api_key(
3811    _request: proto::GetSupermavenApiKey,
3812    response: Response<proto::GetSupermavenApiKey>,
3813    session: Session,
3814) -> Result<()> {
3815    let user_id: String = session.user_id().to_string();
3816    if !session.is_staff() {
3817        return Err(anyhow!("supermaven not enabled for this account"))?;
3818    }
3819
3820    let email = session
3821        .email()
3822        .ok_or_else(|| anyhow!("user must have an email"))?;
3823
3824    let supermaven_admin_api = session
3825        .supermaven_client
3826        .as_ref()
3827        .ok_or_else(|| anyhow!("supermaven not configured"))?;
3828
3829    let result = supermaven_admin_api
3830        .try_get_or_create_user(CreateExternalUserRequest { id: user_id, email })
3831        .await?;
3832
3833    response.send(proto::GetSupermavenApiKeyResponse {
3834        api_key: result.api_key,
3835    })?;
3836
3837    Ok(())
3838}
3839
3840/// Start receiving chat updates for a channel
3841async fn join_channel_chat(
3842    request: proto::JoinChannelChat,
3843    response: Response<proto::JoinChannelChat>,
3844    session: Session,
3845) -> Result<()> {
3846    let channel_id = ChannelId::from_proto(request.channel_id);
3847
3848    let db = session.db().await;
3849    db.join_channel_chat(channel_id, session.connection_id, session.user_id())
3850        .await?;
3851    let messages = db
3852        .get_channel_messages(channel_id, session.user_id(), MESSAGE_COUNT_PER_PAGE, None)
3853        .await?;
3854    response.send(proto::JoinChannelChatResponse {
3855        done: messages.len() < MESSAGE_COUNT_PER_PAGE,
3856        messages,
3857    })?;
3858    Ok(())
3859}
3860
3861/// Stop receiving chat updates for a channel
3862async fn leave_channel_chat(request: proto::LeaveChannelChat, session: Session) -> Result<()> {
3863    let channel_id = ChannelId::from_proto(request.channel_id);
3864    session
3865        .db()
3866        .await
3867        .leave_channel_chat(channel_id, session.connection_id, session.user_id())
3868        .await?;
3869    Ok(())
3870}
3871
3872/// Retrieve the chat history for a channel
3873async fn get_channel_messages(
3874    request: proto::GetChannelMessages,
3875    response: Response<proto::GetChannelMessages>,
3876    session: Session,
3877) -> Result<()> {
3878    let channel_id = ChannelId::from_proto(request.channel_id);
3879    let messages = session
3880        .db()
3881        .await
3882        .get_channel_messages(
3883            channel_id,
3884            session.user_id(),
3885            MESSAGE_COUNT_PER_PAGE,
3886            Some(MessageId::from_proto(request.before_message_id)),
3887        )
3888        .await?;
3889    response.send(proto::GetChannelMessagesResponse {
3890        done: messages.len() < MESSAGE_COUNT_PER_PAGE,
3891        messages,
3892    })?;
3893    Ok(())
3894}
3895
3896/// Retrieve specific chat messages
3897async fn get_channel_messages_by_id(
3898    request: proto::GetChannelMessagesById,
3899    response: Response<proto::GetChannelMessagesById>,
3900    session: Session,
3901) -> Result<()> {
3902    let message_ids = request
3903        .message_ids
3904        .iter()
3905        .map(|id| MessageId::from_proto(*id))
3906        .collect::<Vec<_>>();
3907    let messages = session
3908        .db()
3909        .await
3910        .get_channel_messages_by_id(session.user_id(), &message_ids)
3911        .await?;
3912    response.send(proto::GetChannelMessagesResponse {
3913        done: messages.len() < MESSAGE_COUNT_PER_PAGE,
3914        messages,
3915    })?;
3916    Ok(())
3917}
3918
3919/// Retrieve the current users notifications
3920async fn get_notifications(
3921    request: proto::GetNotifications,
3922    response: Response<proto::GetNotifications>,
3923    session: Session,
3924) -> Result<()> {
3925    let notifications = session
3926        .db()
3927        .await
3928        .get_notifications(
3929            session.user_id(),
3930            NOTIFICATION_COUNT_PER_PAGE,
3931            request.before_id.map(db::NotificationId::from_proto),
3932        )
3933        .await?;
3934    response.send(proto::GetNotificationsResponse {
3935        done: notifications.len() < NOTIFICATION_COUNT_PER_PAGE,
3936        notifications,
3937    })?;
3938    Ok(())
3939}
3940
3941/// Mark notifications as read
3942async fn mark_notification_as_read(
3943    request: proto::MarkNotificationRead,
3944    response: Response<proto::MarkNotificationRead>,
3945    session: Session,
3946) -> Result<()> {
3947    let database = &session.db().await;
3948    let notifications = database
3949        .mark_notification_as_read_by_id(
3950            session.user_id(),
3951            NotificationId::from_proto(request.notification_id),
3952        )
3953        .await?;
3954    send_notifications(
3955        &*session.connection_pool().await,
3956        &session.peer,
3957        notifications,
3958    );
3959    response.send(proto::Ack {})?;
3960    Ok(())
3961}
3962
3963/// Get the current users information
3964async fn get_private_user_info(
3965    _request: proto::GetPrivateUserInfo,
3966    response: Response<proto::GetPrivateUserInfo>,
3967    session: Session,
3968) -> Result<()> {
3969    let db = session.db().await;
3970
3971    let metrics_id = db.get_user_metrics_id(session.user_id()).await?;
3972    let user = db
3973        .get_user_by_id(session.user_id())
3974        .await?
3975        .ok_or_else(|| anyhow!("user not found"))?;
3976    let flags = db.get_user_flags(session.user_id()).await?;
3977
3978    response.send(proto::GetPrivateUserInfoResponse {
3979        metrics_id,
3980        staff: user.admin,
3981        flags,
3982        accepted_tos_at: user.accepted_tos_at.map(|t| t.and_utc().timestamp() as u64),
3983    })?;
3984    Ok(())
3985}
3986
3987/// Accept the terms of service (tos) on behalf of the current user
3988async fn accept_terms_of_service(
3989    _request: proto::AcceptTermsOfService,
3990    response: Response<proto::AcceptTermsOfService>,
3991    session: Session,
3992) -> Result<()> {
3993    let db = session.db().await;
3994
3995    let accepted_tos_at = Utc::now();
3996    db.set_user_accepted_tos_at(session.user_id(), Some(accepted_tos_at.naive_utc()))
3997        .await?;
3998
3999    response.send(proto::AcceptTermsOfServiceResponse {
4000        accepted_tos_at: accepted_tos_at.timestamp() as u64,
4001    })?;
4002    Ok(())
4003}
4004
4005/// The minimum account age an account must have in order to use the LLM service.
4006const MIN_ACCOUNT_AGE_FOR_LLM_USE: chrono::Duration = chrono::Duration::days(30);
4007
4008async fn get_llm_api_token(
4009    _request: proto::GetLlmToken,
4010    response: Response<proto::GetLlmToken>,
4011    session: Session,
4012) -> Result<()> {
4013    let db = session.db().await;
4014
4015    let flags = db.get_user_flags(session.user_id()).await?;
4016    let has_language_models_feature_flag = flags.iter().any(|flag| flag == "language-models");
4017    let has_llm_closed_beta_feature_flag = flags.iter().any(|flag| flag == "llm-closed-beta");
4018
4019    if !session.is_staff() && !has_language_models_feature_flag {
4020        Err(anyhow!("permission denied"))?
4021    }
4022
4023    let user_id = session.user_id();
4024    let user = db
4025        .get_user_by_id(user_id)
4026        .await?
4027        .ok_or_else(|| anyhow!("user {} not found", user_id))?;
4028
4029    if user.accepted_tos_at.is_none() {
4030        Err(anyhow!("terms of service not accepted"))?
4031    }
4032
4033    let mut account_created_at = user.created_at;
4034    if let Some(github_created_at) = user.github_user_created_at {
4035        account_created_at = account_created_at.min(github_created_at);
4036    }
4037    if Utc::now().naive_utc() - account_created_at < MIN_ACCOUNT_AGE_FOR_LLM_USE {
4038        Err(anyhow!("account too young"))?
4039    }
4040
4041    let billing_preferences = db.get_billing_preferences(user.id).await?;
4042
4043    let token = LlmTokenClaims::create(
4044        &user,
4045        session.is_staff(),
4046        billing_preferences,
4047        has_llm_closed_beta_feature_flag,
4048        session.has_llm_subscription(&db).await?,
4049        session.current_plan(&db).await?,
4050        &session.app_state.config,
4051    )?;
4052    response.send(proto::GetLlmTokenResponse { token })?;
4053    Ok(())
4054}
4055
4056fn to_axum_message(message: TungsteniteMessage) -> anyhow::Result<AxumMessage> {
4057    let message = match message {
4058        TungsteniteMessage::Text(payload) => AxumMessage::Text(payload),
4059        TungsteniteMessage::Binary(payload) => AxumMessage::Binary(payload),
4060        TungsteniteMessage::Ping(payload) => AxumMessage::Ping(payload),
4061        TungsteniteMessage::Pong(payload) => AxumMessage::Pong(payload),
4062        TungsteniteMessage::Close(frame) => AxumMessage::Close(frame.map(|frame| AxumCloseFrame {
4063            code: frame.code.into(),
4064            reason: frame.reason,
4065        })),
4066        // We should never receive a frame while reading the message, according
4067        // to the `tungstenite` maintainers:
4068        //
4069        // > It cannot occur when you read messages from the WebSocket, but it
4070        // > can be used when you want to send the raw frames (e.g. you want to
4071        // > send the frames to the WebSocket without composing the full message first).
4072        // >
4073        // > — https://github.com/snapview/tungstenite-rs/issues/268
4074        TungsteniteMessage::Frame(_) => {
4075            bail!("received an unexpected frame while reading the message")
4076        }
4077    };
4078
4079    Ok(message)
4080}
4081
4082fn to_tungstenite_message(message: AxumMessage) -> TungsteniteMessage {
4083    match message {
4084        AxumMessage::Text(payload) => TungsteniteMessage::Text(payload),
4085        AxumMessage::Binary(payload) => TungsteniteMessage::Binary(payload),
4086        AxumMessage::Ping(payload) => TungsteniteMessage::Ping(payload),
4087        AxumMessage::Pong(payload) => TungsteniteMessage::Pong(payload),
4088        AxumMessage::Close(frame) => {
4089            TungsteniteMessage::Close(frame.map(|frame| TungsteniteCloseFrame {
4090                code: frame.code.into(),
4091                reason: frame.reason,
4092            }))
4093        }
4094    }
4095}
4096
4097fn notify_membership_updated(
4098    connection_pool: &mut ConnectionPool,
4099    result: MembershipUpdated,
4100    user_id: UserId,
4101    peer: &Peer,
4102) {
4103    for membership in &result.new_channels.channel_memberships {
4104        connection_pool.subscribe_to_channel(user_id, membership.channel_id, membership.role)
4105    }
4106    for channel_id in &result.removed_channels {
4107        connection_pool.unsubscribe_from_channel(&user_id, channel_id)
4108    }
4109
4110    let user_channels_update = proto::UpdateUserChannels {
4111        channel_memberships: result
4112            .new_channels
4113            .channel_memberships
4114            .iter()
4115            .map(|cm| proto::ChannelMembership {
4116                channel_id: cm.channel_id.to_proto(),
4117                role: cm.role.into(),
4118            })
4119            .collect(),
4120        ..Default::default()
4121    };
4122
4123    let mut update = build_channels_update(result.new_channels);
4124    update.delete_channels = result
4125        .removed_channels
4126        .into_iter()
4127        .map(|id| id.to_proto())
4128        .collect();
4129    update.remove_channel_invitations = vec![result.channel_id.to_proto()];
4130
4131    for connection_id in connection_pool.user_connection_ids(user_id) {
4132        peer.send(connection_id, user_channels_update.clone())
4133            .trace_err();
4134        peer.send(connection_id, update.clone()).trace_err();
4135    }
4136}
4137
4138fn build_update_user_channels(channels: &ChannelsForUser) -> proto::UpdateUserChannels {
4139    proto::UpdateUserChannels {
4140        channel_memberships: channels
4141            .channel_memberships
4142            .iter()
4143            .map(|m| proto::ChannelMembership {
4144                channel_id: m.channel_id.to_proto(),
4145                role: m.role.into(),
4146            })
4147            .collect(),
4148        observed_channel_buffer_version: channels.observed_buffer_versions.clone(),
4149        observed_channel_message_id: channels.observed_channel_messages.clone(),
4150    }
4151}
4152
4153fn build_channels_update(channels: ChannelsForUser) -> proto::UpdateChannels {
4154    let mut update = proto::UpdateChannels::default();
4155
4156    for channel in channels.channels {
4157        update.channels.push(channel.to_proto());
4158    }
4159
4160    update.latest_channel_buffer_versions = channels.latest_buffer_versions;
4161    update.latest_channel_message_ids = channels.latest_channel_messages;
4162
4163    for (channel_id, participants) in channels.channel_participants {
4164        update
4165            .channel_participants
4166            .push(proto::ChannelParticipants {
4167                channel_id: channel_id.to_proto(),
4168                participant_user_ids: participants.into_iter().map(|id| id.to_proto()).collect(),
4169            });
4170    }
4171
4172    for channel in channels.invited_channels {
4173        update.channel_invitations.push(channel.to_proto());
4174    }
4175
4176    update
4177}
4178
4179fn build_initial_contacts_update(
4180    contacts: Vec<db::Contact>,
4181    pool: &ConnectionPool,
4182) -> proto::UpdateContacts {
4183    let mut update = proto::UpdateContacts::default();
4184
4185    for contact in contacts {
4186        match contact {
4187            db::Contact::Accepted { user_id, busy } => {
4188                update.contacts.push(contact_for_user(user_id, busy, pool));
4189            }
4190            db::Contact::Outgoing { user_id } => update.outgoing_requests.push(user_id.to_proto()),
4191            db::Contact::Incoming { user_id } => {
4192                update
4193                    .incoming_requests
4194                    .push(proto::IncomingContactRequest {
4195                        requester_id: user_id.to_proto(),
4196                    })
4197            }
4198        }
4199    }
4200
4201    update
4202}
4203
4204fn contact_for_user(user_id: UserId, busy: bool, pool: &ConnectionPool) -> proto::Contact {
4205    proto::Contact {
4206        user_id: user_id.to_proto(),
4207        online: pool.is_user_online(user_id),
4208        busy,
4209    }
4210}
4211
4212fn room_updated(room: &proto::Room, peer: &Peer) {
4213    broadcast(
4214        None,
4215        room.participants
4216            .iter()
4217            .filter_map(|participant| Some(participant.peer_id?.into())),
4218        |peer_id| {
4219            peer.send(
4220                peer_id,
4221                proto::RoomUpdated {
4222                    room: Some(room.clone()),
4223                },
4224            )
4225        },
4226    );
4227}
4228
4229fn channel_updated(
4230    channel: &db::channel::Model,
4231    room: &proto::Room,
4232    peer: &Peer,
4233    pool: &ConnectionPool,
4234) {
4235    let participants = room
4236        .participants
4237        .iter()
4238        .map(|p| p.user_id)
4239        .collect::<Vec<_>>();
4240
4241    broadcast(
4242        None,
4243        pool.channel_connection_ids(channel.root_id())
4244            .filter_map(|(channel_id, role)| {
4245                role.can_see_channel(channel.visibility)
4246                    .then_some(channel_id)
4247            }),
4248        |peer_id| {
4249            peer.send(
4250                peer_id,
4251                proto::UpdateChannels {
4252                    channel_participants: vec![proto::ChannelParticipants {
4253                        channel_id: channel.id.to_proto(),
4254                        participant_user_ids: participants.clone(),
4255                    }],
4256                    ..Default::default()
4257                },
4258            )
4259        },
4260    );
4261}
4262
4263async fn update_user_contacts(user_id: UserId, session: &Session) -> Result<()> {
4264    let db = session.db().await;
4265
4266    let contacts = db.get_contacts(user_id).await?;
4267    let busy = db.is_user_busy(user_id).await?;
4268
4269    let pool = session.connection_pool().await;
4270    let updated_contact = contact_for_user(user_id, busy, &pool);
4271    for contact in contacts {
4272        if let db::Contact::Accepted {
4273            user_id: contact_user_id,
4274            ..
4275        } = contact
4276        {
4277            for contact_conn_id in pool.user_connection_ids(contact_user_id) {
4278                session
4279                    .peer
4280                    .send(
4281                        contact_conn_id,
4282                        proto::UpdateContacts {
4283                            contacts: vec![updated_contact.clone()],
4284                            remove_contacts: Default::default(),
4285                            incoming_requests: Default::default(),
4286                            remove_incoming_requests: Default::default(),
4287                            outgoing_requests: Default::default(),
4288                            remove_outgoing_requests: Default::default(),
4289                        },
4290                    )
4291                    .trace_err();
4292            }
4293        }
4294    }
4295    Ok(())
4296}
4297
4298async fn leave_room_for_session(session: &Session, connection_id: ConnectionId) -> Result<()> {
4299    let mut contacts_to_update = HashSet::default();
4300
4301    let room_id;
4302    let canceled_calls_to_user_ids;
4303    let live_kit_room;
4304    let delete_live_kit_room;
4305    let room;
4306    let channel;
4307
4308    if let Some(mut left_room) = session.db().await.leave_room(connection_id).await? {
4309        contacts_to_update.insert(session.user_id());
4310
4311        for project in left_room.left_projects.values() {
4312            project_left(project, session);
4313        }
4314
4315        room_id = RoomId::from_proto(left_room.room.id);
4316        canceled_calls_to_user_ids = mem::take(&mut left_room.canceled_calls_to_user_ids);
4317        live_kit_room = mem::take(&mut left_room.room.live_kit_room);
4318        delete_live_kit_room = left_room.deleted;
4319        room = mem::take(&mut left_room.room);
4320        channel = mem::take(&mut left_room.channel);
4321
4322        room_updated(&room, &session.peer);
4323    } else {
4324        return Ok(());
4325    }
4326
4327    if let Some(channel) = channel {
4328        channel_updated(
4329            &channel,
4330            &room,
4331            &session.peer,
4332            &*session.connection_pool().await,
4333        );
4334    }
4335
4336    {
4337        let pool = session.connection_pool().await;
4338        for canceled_user_id in canceled_calls_to_user_ids {
4339            for connection_id in pool.user_connection_ids(canceled_user_id) {
4340                session
4341                    .peer
4342                    .send(
4343                        connection_id,
4344                        proto::CallCanceled {
4345                            room_id: room_id.to_proto(),
4346                        },
4347                    )
4348                    .trace_err();
4349            }
4350            contacts_to_update.insert(canceled_user_id);
4351        }
4352    }
4353
4354    for contact_user_id in contacts_to_update {
4355        update_user_contacts(contact_user_id, session).await?;
4356    }
4357
4358    if let Some(live_kit) = session.app_state.live_kit_client.as_ref() {
4359        live_kit
4360            .remove_participant(live_kit_room.clone(), session.user_id().to_string())
4361            .await
4362            .trace_err();
4363
4364        if delete_live_kit_room {
4365            live_kit.delete_room(live_kit_room).await.trace_err();
4366        }
4367    }
4368
4369    Ok(())
4370}
4371
4372async fn leave_channel_buffers_for_session(session: &Session) -> Result<()> {
4373    let left_channel_buffers = session
4374        .db()
4375        .await
4376        .leave_channel_buffers(session.connection_id)
4377        .await?;
4378
4379    for left_buffer in left_channel_buffers {
4380        channel_buffer_updated(
4381            session.connection_id,
4382            left_buffer.connections,
4383            &proto::UpdateChannelBufferCollaborators {
4384                channel_id: left_buffer.channel_id.to_proto(),
4385                collaborators: left_buffer.collaborators,
4386            },
4387            &session.peer,
4388        );
4389    }
4390
4391    Ok(())
4392}
4393
4394fn project_left(project: &db::LeftProject, session: &Session) {
4395    for connection_id in &project.connection_ids {
4396        if project.should_unshare {
4397            session
4398                .peer
4399                .send(
4400                    *connection_id,
4401                    proto::UnshareProject {
4402                        project_id: project.id.to_proto(),
4403                    },
4404                )
4405                .trace_err();
4406        } else {
4407            session
4408                .peer
4409                .send(
4410                    *connection_id,
4411                    proto::RemoveProjectCollaborator {
4412                        project_id: project.id.to_proto(),
4413                        peer_id: Some(session.connection_id.into()),
4414                    },
4415                )
4416                .trace_err();
4417        }
4418    }
4419}
4420
4421pub trait ResultExt {
4422    type Ok;
4423
4424    fn trace_err(self) -> Option<Self::Ok>;
4425}
4426
4427impl<T, E> ResultExt for Result<T, E>
4428where
4429    E: std::fmt::Debug,
4430{
4431    type Ok = T;
4432
4433    #[track_caller]
4434    fn trace_err(self) -> Option<T> {
4435        match self {
4436            Ok(value) => Some(value),
4437            Err(error) => {
4438                tracing::error!("{:?}", error);
4439                None
4440            }
4441        }
4442    }
4443}