rpc.rs

   1use super::{
   2    auth,
   3    db::{ChannelId, MessageId, UserId},
   4    AppState,
   5};
   6use anyhow::anyhow;
   7use async_std::{sync::RwLock, task};
   8use async_tungstenite::{tungstenite::protocol::Role, WebSocketStream};
   9use futures::{future::BoxFuture, FutureExt};
  10use postage::{mpsc, prelude::Sink as _, prelude::Stream as _};
  11use sha1::{Digest as _, Sha1};
  12use std::{
  13    any::TypeId,
  14    collections::{hash_map, HashMap, HashSet},
  15    future::Future,
  16    mem,
  17    sync::Arc,
  18    time::Instant,
  19};
  20use surf::StatusCode;
  21use tide::log;
  22use tide::{
  23    http::headers::{HeaderName, CONNECTION, UPGRADE},
  24    Request, Response,
  25};
  26use time::OffsetDateTime;
  27use zrpc::{
  28    auth::random_token,
  29    proto::{self, AnyTypedEnvelope, EnvelopedMessage},
  30    Conn, ConnectionId, Peer, TypedEnvelope,
  31};
  32
  33type ReplicaId = u16;
  34
  35type MessageHandler = Box<
  36    dyn Send
  37        + Sync
  38        + Fn(Arc<Server>, Box<dyn AnyTypedEnvelope>) -> BoxFuture<'static, tide::Result<()>>,
  39>;
  40
  41pub struct Server {
  42    peer: Arc<Peer>,
  43    state: RwLock<ServerState>,
  44    app_state: Arc<AppState>,
  45    handlers: HashMap<TypeId, MessageHandler>,
  46    notifications: Option<mpsc::Sender<()>>,
  47}
  48
  49#[derive(Default)]
  50struct ServerState {
  51    connections: HashMap<ConnectionId, Connection>,
  52    pub worktrees: HashMap<u64, Worktree>,
  53    channels: HashMap<ChannelId, Channel>,
  54    next_worktree_id: u64,
  55}
  56
  57struct Connection {
  58    user_id: UserId,
  59    worktrees: HashSet<u64>,
  60    channels: HashSet<ChannelId>,
  61}
  62
  63struct Worktree {
  64    host_connection_id: Option<ConnectionId>,
  65    guest_connection_ids: HashMap<ConnectionId, ReplicaId>,
  66    active_replica_ids: HashSet<ReplicaId>,
  67    access_token: String,
  68    root_name: String,
  69    entries: HashMap<u64, proto::Entry>,
  70}
  71
  72#[derive(Default)]
  73struct Channel {
  74    connection_ids: HashSet<ConnectionId>,
  75}
  76
  77const MESSAGE_COUNT_PER_PAGE: usize = 100;
  78const MAX_MESSAGE_LEN: usize = 1024;
  79
  80impl Server {
  81    pub fn new(
  82        app_state: Arc<AppState>,
  83        peer: Arc<Peer>,
  84        notifications: Option<mpsc::Sender<()>>,
  85    ) -> Arc<Self> {
  86        let mut server = Self {
  87            peer,
  88            app_state,
  89            state: Default::default(),
  90            handlers: Default::default(),
  91            notifications,
  92        };
  93
  94        server
  95            .add_handler(Server::ping)
  96            .add_handler(Server::share_worktree)
  97            .add_handler(Server::join_worktree)
  98            .add_handler(Server::update_worktree)
  99            .add_handler(Server::close_worktree)
 100            .add_handler(Server::open_buffer)
 101            .add_handler(Server::close_buffer)
 102            .add_handler(Server::update_buffer)
 103            .add_handler(Server::buffer_saved)
 104            .add_handler(Server::save_buffer)
 105            .add_handler(Server::get_channels)
 106            .add_handler(Server::get_users)
 107            .add_handler(Server::join_channel)
 108            .add_handler(Server::leave_channel)
 109            .add_handler(Server::send_channel_message)
 110            .add_handler(Server::get_channel_messages);
 111
 112        Arc::new(server)
 113    }
 114
 115    fn add_handler<F, Fut, M>(&mut self, handler: F) -> &mut Self
 116    where
 117        F: 'static + Send + Sync + Fn(Arc<Self>, TypedEnvelope<M>) -> Fut,
 118        Fut: 'static + Send + Future<Output = tide::Result<()>>,
 119        M: EnvelopedMessage,
 120    {
 121        let prev_handler = self.handlers.insert(
 122            TypeId::of::<M>(),
 123            Box::new(move |server, envelope| {
 124                let envelope = envelope.into_any().downcast::<TypedEnvelope<M>>().unwrap();
 125                (handler)(server, *envelope).boxed()
 126            }),
 127        );
 128        if prev_handler.is_some() {
 129            panic!("registered a handler for the same message twice");
 130        }
 131        self
 132    }
 133
 134    pub fn handle_connection(
 135        self: &Arc<Self>,
 136        connection: Conn,
 137        addr: String,
 138        user_id: UserId,
 139    ) -> impl Future<Output = ()> {
 140        let this = self.clone();
 141        async move {
 142            let (connection_id, handle_io, mut incoming_rx) =
 143                this.peer.add_connection(connection).await;
 144            this.add_connection(connection_id, user_id).await;
 145
 146            let handle_io = handle_io.fuse();
 147            futures::pin_mut!(handle_io);
 148            loop {
 149                let next_message = incoming_rx.recv().fuse();
 150                futures::pin_mut!(next_message);
 151                futures::select_biased! {
 152                    message = next_message => {
 153                        if let Some(message) = message {
 154                            let start_time = Instant::now();
 155                            log::info!("RPC message received: {}", message.payload_type_name());
 156                            if let Some(handler) = this.handlers.get(&message.payload_type_id()) {
 157                                if let Err(err) = (handler)(this.clone(), message).await {
 158                                    log::error!("error handling message: {:?}", err);
 159                                } else {
 160                                    log::info!("RPC message handled. duration:{:?}", start_time.elapsed());
 161                                }
 162
 163                                if let Some(mut notifications) = this.notifications.clone() {
 164                                    let _ = notifications.send(()).await;
 165                                }
 166                            } else {
 167                                log::warn!("unhandled message: {}", message.payload_type_name());
 168                            }
 169                        } else {
 170                            log::info!("rpc connection closed {:?}", addr);
 171                            break;
 172                        }
 173                    }
 174                    handle_io = handle_io => {
 175                        if let Err(err) = handle_io {
 176                            log::error!("error handling rpc connection {:?} - {:?}", addr, err);
 177                        }
 178                        break;
 179                    }
 180                }
 181            }
 182
 183            if let Err(err) = this.sign_out(connection_id).await {
 184                log::error!("error signing out connection {:?} - {:?}", addr, err);
 185            }
 186        }
 187    }
 188
 189    async fn sign_out(self: &Arc<Self>, connection_id: zrpc::ConnectionId) -> tide::Result<()> {
 190        self.peer.disconnect(connection_id).await;
 191        let worktree_ids = self.remove_connection(connection_id).await;
 192        for worktree_id in worktree_ids {
 193            let state = self.state.read().await;
 194            if let Some(worktree) = state.worktrees.get(&worktree_id) {
 195                broadcast(connection_id, worktree.connection_ids(), |conn_id| {
 196                    self.peer.send(
 197                        conn_id,
 198                        proto::RemovePeer {
 199                            worktree_id,
 200                            peer_id: connection_id.0,
 201                        },
 202                    )
 203                })
 204                .await?;
 205            }
 206        }
 207        Ok(())
 208    }
 209
 210    // Add a new connection associated with a given user.
 211    async fn add_connection(&self, connection_id: ConnectionId, user_id: UserId) {
 212        self.state.write().await.connections.insert(
 213            connection_id,
 214            Connection {
 215                user_id,
 216                worktrees: Default::default(),
 217                channels: Default::default(),
 218            },
 219        );
 220    }
 221
 222    // Remove the given connection and its association with any worktrees.
 223    async fn remove_connection(&self, connection_id: ConnectionId) -> Vec<u64> {
 224        let mut worktree_ids = Vec::new();
 225        let mut state = self.state.write().await;
 226        if let Some(connection) = state.connections.remove(&connection_id) {
 227            for channel_id in connection.channels {
 228                if let Some(channel) = state.channels.get_mut(&channel_id) {
 229                    channel.connection_ids.remove(&connection_id);
 230                }
 231            }
 232            for worktree_id in connection.worktrees {
 233                if let Some(worktree) = state.worktrees.get_mut(&worktree_id) {
 234                    if worktree.host_connection_id == Some(connection_id) {
 235                        worktree_ids.push(worktree_id);
 236                    } else if let Some(replica_id) =
 237                        worktree.guest_connection_ids.remove(&connection_id)
 238                    {
 239                        worktree.active_replica_ids.remove(&replica_id);
 240                        worktree_ids.push(worktree_id);
 241                    }
 242                }
 243            }
 244        }
 245        worktree_ids
 246    }
 247
 248    async fn ping(self: Arc<Server>, request: TypedEnvelope<proto::Ping>) -> tide::Result<()> {
 249        self.peer.respond(request.receipt(), proto::Ack {}).await?;
 250        Ok(())
 251    }
 252
 253    async fn share_worktree(
 254        self: Arc<Server>,
 255        mut request: TypedEnvelope<proto::ShareWorktree>,
 256    ) -> tide::Result<()> {
 257        let mut state = self.state.write().await;
 258        let worktree_id = state.next_worktree_id;
 259        state.next_worktree_id += 1;
 260        let access_token = random_token();
 261        let worktree = request
 262            .payload
 263            .worktree
 264            .as_mut()
 265            .ok_or_else(|| anyhow!("missing worktree"))?;
 266        let entries = mem::take(&mut worktree.entries)
 267            .into_iter()
 268            .map(|entry| (entry.id, entry))
 269            .collect();
 270        state.worktrees.insert(
 271            worktree_id,
 272            Worktree {
 273                host_connection_id: Some(request.sender_id),
 274                guest_connection_ids: Default::default(),
 275                active_replica_ids: Default::default(),
 276                access_token: access_token.clone(),
 277                root_name: mem::take(&mut worktree.root_name),
 278                entries,
 279            },
 280        );
 281
 282        self.peer
 283            .respond(
 284                request.receipt(),
 285                proto::ShareWorktreeResponse {
 286                    worktree_id,
 287                    access_token,
 288                },
 289            )
 290            .await?;
 291        Ok(())
 292    }
 293
 294    async fn join_worktree(
 295        self: Arc<Server>,
 296        request: TypedEnvelope<proto::OpenWorktree>,
 297    ) -> tide::Result<()> {
 298        let worktree_id = request.payload.worktree_id;
 299        let access_token = &request.payload.access_token;
 300
 301        let mut state = self.state.write().await;
 302        if let Some((peer_replica_id, worktree)) =
 303            state.join_worktree(request.sender_id, worktree_id, access_token)
 304        {
 305            let mut peers = Vec::new();
 306            if let Some(host_connection_id) = worktree.host_connection_id {
 307                peers.push(proto::Peer {
 308                    peer_id: host_connection_id.0,
 309                    replica_id: 0,
 310                });
 311            }
 312            for (peer_conn_id, peer_replica_id) in &worktree.guest_connection_ids {
 313                if *peer_conn_id != request.sender_id {
 314                    peers.push(proto::Peer {
 315                        peer_id: peer_conn_id.0,
 316                        replica_id: *peer_replica_id as u32,
 317                    });
 318                }
 319            }
 320
 321            broadcast(request.sender_id, worktree.connection_ids(), |conn_id| {
 322                self.peer.send(
 323                    conn_id,
 324                    proto::AddPeer {
 325                        worktree_id,
 326                        peer: Some(proto::Peer {
 327                            peer_id: request.sender_id.0,
 328                            replica_id: peer_replica_id as u32,
 329                        }),
 330                    },
 331                )
 332            })
 333            .await?;
 334            self.peer
 335                .respond(
 336                    request.receipt(),
 337                    proto::OpenWorktreeResponse {
 338                        worktree_id,
 339                        worktree: Some(proto::Worktree {
 340                            root_name: worktree.root_name.clone(),
 341                            entries: worktree.entries.values().cloned().collect(),
 342                        }),
 343                        replica_id: peer_replica_id as u32,
 344                        peers,
 345                    },
 346                )
 347                .await?;
 348        } else {
 349            self.peer
 350                .respond(
 351                    request.receipt(),
 352                    proto::OpenWorktreeResponse {
 353                        worktree_id,
 354                        worktree: None,
 355                        replica_id: 0,
 356                        peers: Vec::new(),
 357                    },
 358                )
 359                .await?;
 360        }
 361
 362        Ok(())
 363    }
 364
 365    async fn update_worktree(
 366        self: Arc<Server>,
 367        request: TypedEnvelope<proto::UpdateWorktree>,
 368    ) -> tide::Result<()> {
 369        {
 370            let mut state = self.state.write().await;
 371            let worktree = state.write_worktree(request.payload.worktree_id, request.sender_id)?;
 372            for entry_id in &request.payload.removed_entries {
 373                worktree.entries.remove(&entry_id);
 374            }
 375
 376            for entry in &request.payload.updated_entries {
 377                worktree.entries.insert(entry.id, entry.clone());
 378            }
 379        }
 380
 381        self.broadcast_in_worktree(request.payload.worktree_id, &request)
 382            .await?;
 383        Ok(())
 384    }
 385
 386    async fn close_worktree(
 387        self: Arc<Server>,
 388        request: TypedEnvelope<proto::CloseWorktree>,
 389    ) -> tide::Result<()> {
 390        let connection_ids;
 391        {
 392            let mut state = self.state.write().await;
 393            let worktree = state.write_worktree(request.payload.worktree_id, request.sender_id)?;
 394            connection_ids = worktree.connection_ids();
 395            if worktree.host_connection_id == Some(request.sender_id) {
 396                worktree.host_connection_id = None;
 397            } else if let Some(replica_id) =
 398                worktree.guest_connection_ids.remove(&request.sender_id)
 399            {
 400                worktree.active_replica_ids.remove(&replica_id);
 401            }
 402        }
 403
 404        broadcast(request.sender_id, connection_ids, |conn_id| {
 405            self.peer.send(
 406                conn_id,
 407                proto::RemovePeer {
 408                    worktree_id: request.payload.worktree_id,
 409                    peer_id: request.sender_id.0,
 410                },
 411            )
 412        })
 413        .await?;
 414
 415        Ok(())
 416    }
 417
 418    async fn open_buffer(
 419        self: Arc<Server>,
 420        request: TypedEnvelope<proto::OpenBuffer>,
 421    ) -> tide::Result<()> {
 422        let receipt = request.receipt();
 423        let worktree_id = request.payload.worktree_id;
 424        let host_connection_id = self
 425            .state
 426            .read()
 427            .await
 428            .read_worktree(worktree_id, request.sender_id)?
 429            .host_connection_id()?;
 430
 431        let response = self
 432            .peer
 433            .forward_request(request.sender_id, host_connection_id, request.payload)
 434            .await?;
 435        self.peer.respond(receipt, response).await?;
 436        Ok(())
 437    }
 438
 439    async fn close_buffer(
 440        self: Arc<Server>,
 441        request: TypedEnvelope<proto::CloseBuffer>,
 442    ) -> tide::Result<()> {
 443        let host_connection_id = self
 444            .state
 445            .read()
 446            .await
 447            .read_worktree(request.payload.worktree_id, request.sender_id)?
 448            .host_connection_id()?;
 449
 450        self.peer
 451            .forward_send(request.sender_id, host_connection_id, request.payload)
 452            .await?;
 453
 454        Ok(())
 455    }
 456
 457    async fn save_buffer(
 458        self: Arc<Server>,
 459        request: TypedEnvelope<proto::SaveBuffer>,
 460    ) -> tide::Result<()> {
 461        let host;
 462        let guests;
 463        {
 464            let state = self.state.read().await;
 465            let worktree = state.read_worktree(request.payload.worktree_id, request.sender_id)?;
 466            host = worktree.host_connection_id()?;
 467            guests = worktree
 468                .guest_connection_ids
 469                .keys()
 470                .copied()
 471                .collect::<Vec<_>>();
 472        }
 473
 474        let sender = request.sender_id;
 475        let receipt = request.receipt();
 476        let response = self
 477            .peer
 478            .forward_request(sender, host, request.payload.clone())
 479            .await?;
 480
 481        broadcast(host, guests, |conn_id| {
 482            let response = response.clone();
 483            let peer = &self.peer;
 484            async move {
 485                if conn_id == sender {
 486                    peer.respond(receipt, response).await
 487                } else {
 488                    peer.forward_send(host, conn_id, response).await
 489                }
 490            }
 491        })
 492        .await?;
 493
 494        Ok(())
 495    }
 496
 497    async fn update_buffer(
 498        self: Arc<Server>,
 499        request: TypedEnvelope<proto::UpdateBuffer>,
 500    ) -> tide::Result<()> {
 501        self.broadcast_in_worktree(request.payload.worktree_id, &request)
 502            .await
 503    }
 504
 505    async fn buffer_saved(
 506        self: Arc<Server>,
 507        request: TypedEnvelope<proto::BufferSaved>,
 508    ) -> tide::Result<()> {
 509        self.broadcast_in_worktree(request.payload.worktree_id, &request)
 510            .await
 511    }
 512
 513    async fn get_channels(
 514        self: Arc<Server>,
 515        request: TypedEnvelope<proto::GetChannels>,
 516    ) -> tide::Result<()> {
 517        let user_id = self
 518            .state
 519            .read()
 520            .await
 521            .user_id_for_connection(request.sender_id)?;
 522        let channels = self.app_state.db.get_accessible_channels(user_id).await?;
 523        self.peer
 524            .respond(
 525                request.receipt(),
 526                proto::GetChannelsResponse {
 527                    channels: channels
 528                        .into_iter()
 529                        .map(|chan| proto::Channel {
 530                            id: chan.id.to_proto(),
 531                            name: chan.name,
 532                        })
 533                        .collect(),
 534                },
 535            )
 536            .await?;
 537        Ok(())
 538    }
 539
 540    async fn get_users(
 541        self: Arc<Server>,
 542        request: TypedEnvelope<proto::GetUsers>,
 543    ) -> tide::Result<()> {
 544        let user_id = self
 545            .state
 546            .read()
 547            .await
 548            .user_id_for_connection(request.sender_id)?;
 549        let receipt = request.receipt();
 550        let user_ids = request.payload.user_ids.into_iter().map(UserId::from_proto);
 551        let users = self
 552            .app_state
 553            .db
 554            .get_users_by_ids(user_id, user_ids)
 555            .await?
 556            .into_iter()
 557            .map(|user| proto::User {
 558                id: user.id.to_proto(),
 559                github_login: user.github_login,
 560                avatar_url: String::new(),
 561            })
 562            .collect();
 563        self.peer
 564            .respond(receipt, proto::GetUsersResponse { users })
 565            .await?;
 566        Ok(())
 567    }
 568
 569    async fn join_channel(
 570        self: Arc<Self>,
 571        request: TypedEnvelope<proto::JoinChannel>,
 572    ) -> tide::Result<()> {
 573        let user_id = self
 574            .state
 575            .read()
 576            .await
 577            .user_id_for_connection(request.sender_id)?;
 578        let channel_id = ChannelId::from_proto(request.payload.channel_id);
 579        if !self
 580            .app_state
 581            .db
 582            .can_user_access_channel(user_id, channel_id)
 583            .await?
 584        {
 585            Err(anyhow!("access denied"))?;
 586        }
 587
 588        self.state
 589            .write()
 590            .await
 591            .join_channel(request.sender_id, channel_id);
 592        let messages = self
 593            .app_state
 594            .db
 595            .get_channel_messages(channel_id, MESSAGE_COUNT_PER_PAGE, None)
 596            .await?
 597            .into_iter()
 598            .map(|msg| proto::ChannelMessage {
 599                id: msg.id.to_proto(),
 600                body: msg.body,
 601                timestamp: msg.sent_at.unix_timestamp() as u64,
 602                sender_id: msg.sender_id.to_proto(),
 603            })
 604            .collect::<Vec<_>>();
 605        self.peer
 606            .respond(
 607                request.receipt(),
 608                proto::JoinChannelResponse {
 609                    done: messages.len() < MESSAGE_COUNT_PER_PAGE,
 610                    messages,
 611                },
 612            )
 613            .await?;
 614        Ok(())
 615    }
 616
 617    async fn leave_channel(
 618        self: Arc<Self>,
 619        request: TypedEnvelope<proto::LeaveChannel>,
 620    ) -> tide::Result<()> {
 621        let user_id = self
 622            .state
 623            .read()
 624            .await
 625            .user_id_for_connection(request.sender_id)?;
 626        let channel_id = ChannelId::from_proto(request.payload.channel_id);
 627        if !self
 628            .app_state
 629            .db
 630            .can_user_access_channel(user_id, channel_id)
 631            .await?
 632        {
 633            Err(anyhow!("access denied"))?;
 634        }
 635
 636        self.state
 637            .write()
 638            .await
 639            .leave_channel(request.sender_id, channel_id);
 640
 641        Ok(())
 642    }
 643
 644    async fn send_channel_message(
 645        self: Arc<Self>,
 646        request: TypedEnvelope<proto::SendChannelMessage>,
 647    ) -> tide::Result<()> {
 648        let receipt = request.receipt();
 649        let channel_id = ChannelId::from_proto(request.payload.channel_id);
 650        let user_id;
 651        let connection_ids;
 652        {
 653            let state = self.state.read().await;
 654            user_id = state.user_id_for_connection(request.sender_id)?;
 655            if let Some(channel) = state.channels.get(&channel_id) {
 656                connection_ids = channel.connection_ids();
 657            } else {
 658                return Ok(());
 659            }
 660        }
 661
 662        // Validate the message body.
 663        let body = request.payload.body.trim().to_string();
 664        if body.len() > MAX_MESSAGE_LEN {
 665            self.peer
 666                .respond_with_error(
 667                    receipt,
 668                    proto::Error {
 669                        message: "message is too long".to_string(),
 670                    },
 671                )
 672                .await?;
 673            return Ok(());
 674        }
 675        if body.is_empty() {
 676            self.peer
 677                .respond_with_error(
 678                    receipt,
 679                    proto::Error {
 680                        message: "message can't be blank".to_string(),
 681                    },
 682                )
 683                .await?;
 684            return Ok(());
 685        }
 686
 687        let timestamp = OffsetDateTime::now_utc();
 688        let message_id = self
 689            .app_state
 690            .db
 691            .create_channel_message(channel_id, user_id, &body, timestamp)
 692            .await?
 693            .to_proto();
 694        let message = proto::ChannelMessage {
 695            sender_id: user_id.to_proto(),
 696            id: message_id,
 697            body,
 698            timestamp: timestamp.unix_timestamp() as u64,
 699        };
 700        broadcast(request.sender_id, connection_ids, |conn_id| {
 701            self.peer.send(
 702                conn_id,
 703                proto::ChannelMessageSent {
 704                    channel_id: channel_id.to_proto(),
 705                    message: Some(message.clone()),
 706                },
 707            )
 708        })
 709        .await?;
 710        self.peer
 711            .respond(
 712                receipt,
 713                proto::SendChannelMessageResponse {
 714                    message: Some(message),
 715                },
 716            )
 717            .await?;
 718        Ok(())
 719    }
 720
 721    async fn get_channel_messages(
 722        self: Arc<Self>,
 723        request: TypedEnvelope<proto::GetChannelMessages>,
 724    ) -> tide::Result<()> {
 725        let user_id = self
 726            .state
 727            .read()
 728            .await
 729            .user_id_for_connection(request.sender_id)?;
 730        let channel_id = ChannelId::from_proto(request.payload.channel_id);
 731        if !self
 732            .app_state
 733            .db
 734            .can_user_access_channel(user_id, channel_id)
 735            .await?
 736        {
 737            Err(anyhow!("access denied"))?;
 738        }
 739
 740        let messages = self
 741            .app_state
 742            .db
 743            .get_channel_messages(
 744                channel_id,
 745                MESSAGE_COUNT_PER_PAGE,
 746                Some(MessageId::from_proto(request.payload.before_message_id)),
 747            )
 748            .await?
 749            .into_iter()
 750            .map(|msg| proto::ChannelMessage {
 751                id: msg.id.to_proto(),
 752                body: msg.body,
 753                timestamp: msg.sent_at.unix_timestamp() as u64,
 754                sender_id: msg.sender_id.to_proto(),
 755            })
 756            .collect::<Vec<_>>();
 757        self.peer
 758            .respond(
 759                request.receipt(),
 760                proto::GetChannelMessagesResponse {
 761                    done: messages.len() < MESSAGE_COUNT_PER_PAGE,
 762                    messages,
 763                },
 764            )
 765            .await?;
 766        Ok(())
 767    }
 768
 769    async fn broadcast_in_worktree<T: proto::EnvelopedMessage>(
 770        &self,
 771        worktree_id: u64,
 772        message: &TypedEnvelope<T>,
 773    ) -> tide::Result<()> {
 774        let connection_ids = self
 775            .state
 776            .read()
 777            .await
 778            .read_worktree(worktree_id, message.sender_id)?
 779            .connection_ids();
 780
 781        broadcast(message.sender_id, connection_ids, |conn_id| {
 782            self.peer
 783                .forward_send(message.sender_id, conn_id, message.payload.clone())
 784        })
 785        .await?;
 786
 787        Ok(())
 788    }
 789}
 790
 791pub async fn broadcast<F, T>(
 792    sender_id: ConnectionId,
 793    receiver_ids: Vec<ConnectionId>,
 794    mut f: F,
 795) -> anyhow::Result<()>
 796where
 797    F: FnMut(ConnectionId) -> T,
 798    T: Future<Output = anyhow::Result<()>>,
 799{
 800    let futures = receiver_ids
 801        .into_iter()
 802        .filter(|id| *id != sender_id)
 803        .map(|id| f(id));
 804    futures::future::try_join_all(futures).await?;
 805    Ok(())
 806}
 807
 808impl ServerState {
 809    fn join_channel(&mut self, connection_id: ConnectionId, channel_id: ChannelId) {
 810        if let Some(connection) = self.connections.get_mut(&connection_id) {
 811            connection.channels.insert(channel_id);
 812            self.channels
 813                .entry(channel_id)
 814                .or_default()
 815                .connection_ids
 816                .insert(connection_id);
 817        }
 818    }
 819
 820    fn leave_channel(&mut self, connection_id: ConnectionId, channel_id: ChannelId) {
 821        if let Some(connection) = self.connections.get_mut(&connection_id) {
 822            connection.channels.remove(&channel_id);
 823            if let hash_map::Entry::Occupied(mut entry) = self.channels.entry(channel_id) {
 824                entry.get_mut().connection_ids.remove(&connection_id);
 825                if entry.get_mut().connection_ids.is_empty() {
 826                    entry.remove();
 827                }
 828            }
 829        }
 830    }
 831
 832    fn user_id_for_connection(&self, connection_id: ConnectionId) -> tide::Result<UserId> {
 833        Ok(self
 834            .connections
 835            .get(&connection_id)
 836            .ok_or_else(|| anyhow!("unknown connection"))?
 837            .user_id)
 838    }
 839
 840    // Add the given connection as a guest of the given worktree
 841    fn join_worktree(
 842        &mut self,
 843        connection_id: ConnectionId,
 844        worktree_id: u64,
 845        access_token: &str,
 846    ) -> Option<(ReplicaId, &Worktree)> {
 847        if let Some(worktree) = self.worktrees.get_mut(&worktree_id) {
 848            if access_token == worktree.access_token {
 849                if let Some(connection) = self.connections.get_mut(&connection_id) {
 850                    connection.worktrees.insert(worktree_id);
 851                }
 852
 853                let mut replica_id = 1;
 854                while worktree.active_replica_ids.contains(&replica_id) {
 855                    replica_id += 1;
 856                }
 857                worktree.active_replica_ids.insert(replica_id);
 858                worktree
 859                    .guest_connection_ids
 860                    .insert(connection_id, replica_id);
 861                Some((replica_id, worktree))
 862            } else {
 863                None
 864            }
 865        } else {
 866            None
 867        }
 868    }
 869
 870    fn read_worktree(
 871        &self,
 872        worktree_id: u64,
 873        connection_id: ConnectionId,
 874    ) -> tide::Result<&Worktree> {
 875        let worktree = self
 876            .worktrees
 877            .get(&worktree_id)
 878            .ok_or_else(|| anyhow!("worktree not found"))?;
 879
 880        if worktree.host_connection_id == Some(connection_id)
 881            || worktree.guest_connection_ids.contains_key(&connection_id)
 882        {
 883            Ok(worktree)
 884        } else {
 885            Err(anyhow!(
 886                "{} is not a member of worktree {}",
 887                connection_id,
 888                worktree_id
 889            ))?
 890        }
 891    }
 892
 893    fn write_worktree(
 894        &mut self,
 895        worktree_id: u64,
 896        connection_id: ConnectionId,
 897    ) -> tide::Result<&mut Worktree> {
 898        let worktree = self
 899            .worktrees
 900            .get_mut(&worktree_id)
 901            .ok_or_else(|| anyhow!("worktree not found"))?;
 902
 903        if worktree.host_connection_id == Some(connection_id)
 904            || worktree.guest_connection_ids.contains_key(&connection_id)
 905        {
 906            Ok(worktree)
 907        } else {
 908            Err(anyhow!(
 909                "{} is not a member of worktree {}",
 910                connection_id,
 911                worktree_id
 912            ))?
 913        }
 914    }
 915}
 916
 917impl Worktree {
 918    pub fn connection_ids(&self) -> Vec<ConnectionId> {
 919        self.guest_connection_ids
 920            .keys()
 921            .copied()
 922            .chain(self.host_connection_id)
 923            .collect()
 924    }
 925
 926    fn host_connection_id(&self) -> tide::Result<ConnectionId> {
 927        Ok(self
 928            .host_connection_id
 929            .ok_or_else(|| anyhow!("host disconnected from worktree"))?)
 930    }
 931}
 932
 933impl Channel {
 934    fn connection_ids(&self) -> Vec<ConnectionId> {
 935        self.connection_ids.iter().copied().collect()
 936    }
 937}
 938
 939pub fn add_routes(app: &mut tide::Server<Arc<AppState>>, rpc: &Arc<Peer>) {
 940    let server = Server::new(app.state().clone(), rpc.clone(), None);
 941    app.at("/rpc").with(auth::VerifyToken).get(move |request: Request<Arc<AppState>>| {
 942        let user_id = request.ext::<UserId>().copied();
 943        let server = server.clone();
 944        async move {
 945            const WEBSOCKET_GUID: &str = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
 946
 947            let connection_upgrade = header_contains_ignore_case(&request, CONNECTION, "upgrade");
 948            let upgrade_to_websocket = header_contains_ignore_case(&request, UPGRADE, "websocket");
 949            let upgrade_requested = connection_upgrade && upgrade_to_websocket;
 950
 951            if !upgrade_requested {
 952                return Ok(Response::new(StatusCode::UpgradeRequired));
 953            }
 954
 955            let header = match request.header("Sec-Websocket-Key") {
 956                Some(h) => h.as_str(),
 957                None => return Err(anyhow!("expected sec-websocket-key"))?,
 958            };
 959
 960            let mut response = Response::new(StatusCode::SwitchingProtocols);
 961            response.insert_header(UPGRADE, "websocket");
 962            response.insert_header(CONNECTION, "Upgrade");
 963            let hash = Sha1::new().chain(header).chain(WEBSOCKET_GUID).finalize();
 964            response.insert_header("Sec-Websocket-Accept", base64::encode(&hash[..]));
 965            response.insert_header("Sec-Websocket-Version", "13");
 966
 967            let http_res: &mut tide::http::Response = response.as_mut();
 968            let upgrade_receiver = http_res.recv_upgrade().await;
 969            let addr = request.remote().unwrap_or("unknown").to_string();
 970            let user_id = user_id.ok_or_else(|| anyhow!("user_id is not present on request. ensure auth::VerifyToken middleware is present"))?;
 971            task::spawn(async move {
 972                if let Some(stream) = upgrade_receiver.await {
 973                    server.handle_connection(Conn::new(WebSocketStream::from_raw_socket(stream, Role::Server, None).await), addr, user_id).await;
 974                }
 975            });
 976
 977            Ok(response)
 978        }
 979    });
 980}
 981
 982fn header_contains_ignore_case<T>(
 983    request: &tide::Request<T>,
 984    header_name: HeaderName,
 985    value: &str,
 986) -> bool {
 987    request
 988        .header(header_name)
 989        .map(|h| {
 990            h.as_str()
 991                .split(',')
 992                .any(|s| s.trim().eq_ignore_ascii_case(value.trim()))
 993        })
 994        .unwrap_or(false)
 995}
 996
 997#[cfg(test)]
 998mod tests {
 999    use super::*;
1000    use crate::{
1001        auth,
1002        db::{tests::TestDb, UserId},
1003        github, AppState, Config,
1004    };
1005    use async_std::{sync::RwLockReadGuard, task};
1006    use gpui::TestAppContext;
1007    use parking_lot::Mutex;
1008    use postage::{mpsc, watch};
1009    use serde_json::json;
1010    use sqlx::types::time::OffsetDateTime;
1011    use std::{
1012        path::Path,
1013        sync::{
1014            atomic::{AtomicBool, Ordering::SeqCst},
1015            Arc,
1016        },
1017        time::Duration,
1018    };
1019    use zed::{
1020        channel::{Channel, ChannelDetails, ChannelList},
1021        editor::{Editor, Insert},
1022        fs::{FakeFs, Fs as _},
1023        language::LanguageRegistry,
1024        rpc::{self, Client},
1025        settings,
1026        user::UserStore,
1027        worktree::Worktree,
1028    };
1029    use zrpc::Peer;
1030
1031    #[gpui::test]
1032    async fn test_share_worktree(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
1033        let (window_b, _) = cx_b.add_window(|_| EmptyView);
1034        let settings = cx_b.read(settings::test).1;
1035        let lang_registry = Arc::new(LanguageRegistry::new());
1036
1037        // Connect to a server as 2 clients.
1038        let mut server = TestServer::start().await;
1039        let (_, client_a) = server.create_client(&mut cx_a, "user_a").await;
1040        let (_, client_b) = server.create_client(&mut cx_b, "user_b").await;
1041
1042        cx_a.foreground().forbid_parking();
1043
1044        // Share a local worktree as client A
1045        let fs = Arc::new(FakeFs::new());
1046        fs.insert_tree(
1047            "/a",
1048            json!({
1049                "a.txt": "a-contents",
1050                "b.txt": "b-contents",
1051            }),
1052        )
1053        .await;
1054        let worktree_a = Worktree::open_local(
1055            "/a".as_ref(),
1056            lang_registry.clone(),
1057            fs,
1058            &mut cx_a.to_async(),
1059        )
1060        .await
1061        .unwrap();
1062        worktree_a
1063            .read_with(&cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1064            .await;
1065        let (worktree_id, worktree_token) = worktree_a
1066            .update(&mut cx_a, |tree, cx| {
1067                tree.as_local_mut().unwrap().share(client_a.clone(), cx)
1068            })
1069            .await
1070            .unwrap();
1071
1072        // Join that worktree as client B, and see that a guest has joined as client A.
1073        let worktree_b = Worktree::open_remote(
1074            client_b.clone(),
1075            worktree_id,
1076            worktree_token,
1077            lang_registry.clone(),
1078            &mut cx_b.to_async(),
1079        )
1080        .await
1081        .unwrap();
1082        let replica_id_b = worktree_b.read_with(&cx_b, |tree, _| tree.replica_id());
1083        worktree_a
1084            .condition(&cx_a, |tree, _| {
1085                tree.peers()
1086                    .values()
1087                    .any(|replica_id| *replica_id == replica_id_b)
1088            })
1089            .await;
1090
1091        // Open the same file as client B and client A.
1092        let buffer_b = worktree_b
1093            .update(&mut cx_b, |worktree, cx| worktree.open_buffer("b.txt", cx))
1094            .await
1095            .unwrap();
1096        buffer_b.read_with(&cx_b, |buf, _| assert_eq!(buf.text(), "b-contents"));
1097        worktree_a.read_with(&cx_a, |tree, cx| assert!(tree.has_open_buffer("b.txt", cx)));
1098        let buffer_a = worktree_a
1099            .update(&mut cx_a, |tree, cx| tree.open_buffer("b.txt", cx))
1100            .await
1101            .unwrap();
1102
1103        // Create a selection set as client B and see that selection set as client A.
1104        let editor_b = cx_b.add_view(window_b, |cx| Editor::for_buffer(buffer_b, settings, cx));
1105        buffer_a
1106            .condition(&cx_a, |buffer, _| buffer.selection_sets().count() == 1)
1107            .await;
1108
1109        // Edit the buffer as client B and see that edit as client A.
1110        editor_b.update(&mut cx_b, |editor, cx| {
1111            editor.insert(&Insert("ok, ".into()), cx)
1112        });
1113        buffer_a
1114            .condition(&cx_a, |buffer, _| buffer.text() == "ok, b-contents")
1115            .await;
1116
1117        // Remove the selection set as client B, see those selections disappear as client A.
1118        cx_b.update(move |_| drop(editor_b));
1119        buffer_a
1120            .condition(&cx_a, |buffer, _| buffer.selection_sets().count() == 0)
1121            .await;
1122
1123        // Close the buffer as client A, see that the buffer is closed.
1124        drop(buffer_a);
1125        worktree_a
1126            .condition(&cx_a, |tree, cx| !tree.has_open_buffer("b.txt", cx))
1127            .await;
1128
1129        // Dropping the worktree removes client B from client A's peers.
1130        cx_b.update(move |_| drop(worktree_b));
1131        worktree_a
1132            .condition(&cx_a, |tree, _| tree.peers().is_empty())
1133            .await;
1134    }
1135
1136    #[gpui::test]
1137    async fn test_propagate_saves_and_fs_changes_in_shared_worktree(
1138        mut cx_a: TestAppContext,
1139        mut cx_b: TestAppContext,
1140        mut cx_c: TestAppContext,
1141    ) {
1142        cx_a.foreground().forbid_parking();
1143        let lang_registry = Arc::new(LanguageRegistry::new());
1144
1145        // Connect to a server as 3 clients.
1146        let mut server = TestServer::start().await;
1147        let (_, client_a) = server.create_client(&mut cx_a, "user_a").await;
1148        let (_, client_b) = server.create_client(&mut cx_b, "user_b").await;
1149        let (_, client_c) = server.create_client(&mut cx_c, "user_c").await;
1150
1151        let fs = Arc::new(FakeFs::new());
1152
1153        // Share a worktree as client A.
1154        fs.insert_tree(
1155            "/a",
1156            json!({
1157                "file1": "",
1158                "file2": ""
1159            }),
1160        )
1161        .await;
1162
1163        let worktree_a = Worktree::open_local(
1164            "/a".as_ref(),
1165            lang_registry.clone(),
1166            fs.clone(),
1167            &mut cx_a.to_async(),
1168        )
1169        .await
1170        .unwrap();
1171        worktree_a
1172            .read_with(&cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1173            .await;
1174        let (worktree_id, worktree_token) = worktree_a
1175            .update(&mut cx_a, |tree, cx| {
1176                tree.as_local_mut().unwrap().share(client_a.clone(), cx)
1177            })
1178            .await
1179            .unwrap();
1180
1181        // Join that worktree as clients B and C.
1182        let worktree_b = Worktree::open_remote(
1183            client_b.clone(),
1184            worktree_id,
1185            worktree_token.clone(),
1186            lang_registry.clone(),
1187            &mut cx_b.to_async(),
1188        )
1189        .await
1190        .unwrap();
1191        let worktree_c = Worktree::open_remote(
1192            client_c.clone(),
1193            worktree_id,
1194            worktree_token,
1195            lang_registry.clone(),
1196            &mut cx_c.to_async(),
1197        )
1198        .await
1199        .unwrap();
1200
1201        // Open and edit a buffer as both guests B and C.
1202        let buffer_b = worktree_b
1203            .update(&mut cx_b, |tree, cx| tree.open_buffer("file1", cx))
1204            .await
1205            .unwrap();
1206        let buffer_c = worktree_c
1207            .update(&mut cx_c, |tree, cx| tree.open_buffer("file1", cx))
1208            .await
1209            .unwrap();
1210        buffer_b.update(&mut cx_b, |buf, cx| buf.edit([0..0], "i-am-b, ", cx));
1211        buffer_c.update(&mut cx_c, |buf, cx| buf.edit([0..0], "i-am-c, ", cx));
1212
1213        // Open and edit that buffer as the host.
1214        let buffer_a = worktree_a
1215            .update(&mut cx_a, |tree, cx| tree.open_buffer("file1", cx))
1216            .await
1217            .unwrap();
1218
1219        buffer_a
1220            .condition(&mut cx_a, |buf, _| buf.text() == "i-am-c, i-am-b, ")
1221            .await;
1222        buffer_a.update(&mut cx_a, |buf, cx| {
1223            buf.edit([buf.len()..buf.len()], "i-am-a", cx)
1224        });
1225
1226        // Wait for edits to propagate
1227        buffer_a
1228            .condition(&mut cx_a, |buf, _| buf.text() == "i-am-c, i-am-b, i-am-a")
1229            .await;
1230        buffer_b
1231            .condition(&mut cx_b, |buf, _| buf.text() == "i-am-c, i-am-b, i-am-a")
1232            .await;
1233        buffer_c
1234            .condition(&mut cx_c, |buf, _| buf.text() == "i-am-c, i-am-b, i-am-a")
1235            .await;
1236
1237        // Edit the buffer as the host and concurrently save as guest B.
1238        let save_b = buffer_b.update(&mut cx_b, |buf, cx| buf.save(cx).unwrap());
1239        buffer_a.update(&mut cx_a, |buf, cx| buf.edit([0..0], "hi-a, ", cx));
1240        save_b.await.unwrap();
1241        assert_eq!(
1242            fs.load("/a/file1".as_ref()).await.unwrap(),
1243            "hi-a, i-am-c, i-am-b, i-am-a"
1244        );
1245        buffer_a.read_with(&cx_a, |buf, _| assert!(!buf.is_dirty()));
1246        buffer_b.read_with(&cx_b, |buf, _| assert!(!buf.is_dirty()));
1247        buffer_c.condition(&cx_c, |buf, _| !buf.is_dirty()).await;
1248
1249        // Make changes on host's file system, see those changes on the guests.
1250        fs.rename("/a/file2".as_ref(), "/a/file3".as_ref())
1251            .await
1252            .unwrap();
1253        fs.insert_file(Path::new("/a/file4"), "4".into())
1254            .await
1255            .unwrap();
1256
1257        worktree_b
1258            .condition(&cx_b, |tree, _| tree.file_count() == 3)
1259            .await;
1260        worktree_c
1261            .condition(&cx_c, |tree, _| tree.file_count() == 3)
1262            .await;
1263        worktree_b.read_with(&cx_b, |tree, _| {
1264            assert_eq!(
1265                tree.paths()
1266                    .map(|p| p.to_string_lossy())
1267                    .collect::<Vec<_>>(),
1268                &["file1", "file3", "file4"]
1269            )
1270        });
1271        worktree_c.read_with(&cx_c, |tree, _| {
1272            assert_eq!(
1273                tree.paths()
1274                    .map(|p| p.to_string_lossy())
1275                    .collect::<Vec<_>>(),
1276                &["file1", "file3", "file4"]
1277            )
1278        });
1279    }
1280
1281    #[gpui::test]
1282    async fn test_buffer_conflict_after_save(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
1283        cx_a.foreground().forbid_parking();
1284        let lang_registry = Arc::new(LanguageRegistry::new());
1285
1286        // Connect to a server as 2 clients.
1287        let mut server = TestServer::start().await;
1288        let (_, client_a) = server.create_client(&mut cx_a, "user_a").await;
1289        let (_, client_b) = server.create_client(&mut cx_b, "user_b").await;
1290
1291        // Share a local worktree as client A
1292        let fs = Arc::new(FakeFs::new());
1293        fs.save(Path::new("/a.txt"), &"a-contents".into())
1294            .await
1295            .unwrap();
1296        let worktree_a = Worktree::open_local(
1297            "/".as_ref(),
1298            lang_registry.clone(),
1299            fs,
1300            &mut cx_a.to_async(),
1301        )
1302        .await
1303        .unwrap();
1304        worktree_a
1305            .read_with(&cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1306            .await;
1307        let (worktree_id, worktree_token) = worktree_a
1308            .update(&mut cx_a, |tree, cx| {
1309                tree.as_local_mut().unwrap().share(client_a.clone(), cx)
1310            })
1311            .await
1312            .unwrap();
1313
1314        // Join that worktree as client B, and see that a guest has joined as client A.
1315        let worktree_b = Worktree::open_remote(
1316            client_b.clone(),
1317            worktree_id,
1318            worktree_token,
1319            lang_registry.clone(),
1320            &mut cx_b.to_async(),
1321        )
1322        .await
1323        .unwrap();
1324
1325        let buffer_b = worktree_b
1326            .update(&mut cx_b, |worktree, cx| worktree.open_buffer("a.txt", cx))
1327            .await
1328            .unwrap();
1329        let mtime = buffer_b.read_with(&cx_b, |buf, _| buf.file().unwrap().mtime);
1330
1331        buffer_b.update(&mut cx_b, |buf, cx| buf.edit([0..0], "world ", cx));
1332        buffer_b.read_with(&cx_b, |buf, _| {
1333            assert!(buf.is_dirty());
1334            assert!(!buf.has_conflict());
1335        });
1336
1337        buffer_b
1338            .update(&mut cx_b, |buf, cx| buf.save(cx))
1339            .unwrap()
1340            .await
1341            .unwrap();
1342        worktree_b
1343            .condition(&cx_b, |_, cx| {
1344                buffer_b.read(cx).file().unwrap().mtime != mtime
1345            })
1346            .await;
1347        buffer_b.read_with(&cx_b, |buf, _| {
1348            assert!(!buf.is_dirty());
1349            assert!(!buf.has_conflict());
1350        });
1351
1352        buffer_b.update(&mut cx_b, |buf, cx| buf.edit([0..0], "hello ", cx));
1353        buffer_b.read_with(&cx_b, |buf, _| {
1354            assert!(buf.is_dirty());
1355            assert!(!buf.has_conflict());
1356        });
1357    }
1358
1359    #[gpui::test]
1360    async fn test_editing_while_guest_opens_buffer(
1361        mut cx_a: TestAppContext,
1362        mut cx_b: TestAppContext,
1363    ) {
1364        cx_a.foreground().forbid_parking();
1365        let lang_registry = Arc::new(LanguageRegistry::new());
1366
1367        // Connect to a server as 2 clients.
1368        let mut server = TestServer::start().await;
1369        let (_, client_a) = server.create_client(&mut cx_a, "user_a").await;
1370        let (_, client_b) = server.create_client(&mut cx_b, "user_b").await;
1371
1372        // Share a local worktree as client A
1373        let fs = Arc::new(FakeFs::new());
1374        fs.save(Path::new("/a.txt"), &"a-contents".into())
1375            .await
1376            .unwrap();
1377        let worktree_a = Worktree::open_local(
1378            "/".as_ref(),
1379            lang_registry.clone(),
1380            fs,
1381            &mut cx_a.to_async(),
1382        )
1383        .await
1384        .unwrap();
1385        worktree_a
1386            .read_with(&cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1387            .await;
1388        let (worktree_id, worktree_token) = worktree_a
1389            .update(&mut cx_a, |tree, cx| {
1390                tree.as_local_mut().unwrap().share(client_a.clone(), cx)
1391            })
1392            .await
1393            .unwrap();
1394
1395        // Join that worktree as client B, and see that a guest has joined as client A.
1396        let worktree_b = Worktree::open_remote(
1397            client_b.clone(),
1398            worktree_id,
1399            worktree_token,
1400            lang_registry.clone(),
1401            &mut cx_b.to_async(),
1402        )
1403        .await
1404        .unwrap();
1405
1406        let buffer_a = worktree_a
1407            .update(&mut cx_a, |tree, cx| tree.open_buffer("a.txt", cx))
1408            .await
1409            .unwrap();
1410        let buffer_b = cx_b
1411            .background()
1412            .spawn(worktree_b.update(&mut cx_b, |worktree, cx| worktree.open_buffer("a.txt", cx)));
1413
1414        task::yield_now().await;
1415        buffer_a.update(&mut cx_a, |buf, cx| buf.edit([0..0], "z", cx));
1416
1417        let text = buffer_a.read_with(&cx_a, |buf, _| buf.text());
1418        let buffer_b = buffer_b.await.unwrap();
1419        buffer_b.condition(&cx_b, |buf, _| buf.text() == text).await;
1420    }
1421
1422    #[gpui::test]
1423    async fn test_peer_disconnection(mut cx_a: TestAppContext, cx_b: TestAppContext) {
1424        cx_a.foreground().forbid_parking();
1425        let lang_registry = Arc::new(LanguageRegistry::new());
1426
1427        // Connect to a server as 2 clients.
1428        let mut server = TestServer::start().await;
1429        let (_, client_a) = server.create_client(&mut cx_a, "user_a").await;
1430        let (_, client_b) = server.create_client(&mut cx_a, "user_b").await;
1431
1432        // Share a local worktree as client A
1433        let fs = Arc::new(FakeFs::new());
1434        fs.insert_tree(
1435            "/a",
1436            json!({
1437                "a.txt": "a-contents",
1438                "b.txt": "b-contents",
1439            }),
1440        )
1441        .await;
1442        let worktree_a = Worktree::open_local(
1443            "/a".as_ref(),
1444            lang_registry.clone(),
1445            fs,
1446            &mut cx_a.to_async(),
1447        )
1448        .await
1449        .unwrap();
1450        worktree_a
1451            .read_with(&cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1452            .await;
1453        let (worktree_id, worktree_token) = worktree_a
1454            .update(&mut cx_a, |tree, cx| {
1455                tree.as_local_mut().unwrap().share(client_a.clone(), cx)
1456            })
1457            .await
1458            .unwrap();
1459
1460        // Join that worktree as client B, and see that a guest has joined as client A.
1461        let _worktree_b = Worktree::open_remote(
1462            client_b.clone(),
1463            worktree_id,
1464            worktree_token,
1465            lang_registry.clone(),
1466            &mut cx_b.to_async(),
1467        )
1468        .await
1469        .unwrap();
1470        worktree_a
1471            .condition(&cx_a, |tree, _| tree.peers().len() == 1)
1472            .await;
1473
1474        // Drop client B's connection and ensure client A observes client B leaving the worktree.
1475        client_b.disconnect(&cx_b.to_async()).await.unwrap();
1476        worktree_a
1477            .condition(&cx_a, |tree, _| tree.peers().len() == 0)
1478            .await;
1479    }
1480
1481    #[gpui::test]
1482    async fn test_basic_chat(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
1483        cx_a.foreground().forbid_parking();
1484
1485        // Connect to a server as 2 clients.
1486        let mut server = TestServer::start().await;
1487        let (user_id_a, client_a) = server.create_client(&mut cx_a, "user_a").await;
1488        let (user_id_b, client_b) = server.create_client(&mut cx_b, "user_b").await;
1489
1490        // Create an org that includes these 2 users.
1491        let db = &server.app_state.db;
1492        let org_id = db.create_org("Test Org", "test-org").await.unwrap();
1493        db.add_org_member(org_id, user_id_a, false).await.unwrap();
1494        db.add_org_member(org_id, user_id_b, false).await.unwrap();
1495
1496        // Create a channel that includes all the users.
1497        let channel_id = db.create_org_channel(org_id, "test-channel").await.unwrap();
1498        db.add_channel_member(channel_id, user_id_a, false)
1499            .await
1500            .unwrap();
1501        db.add_channel_member(channel_id, user_id_b, false)
1502            .await
1503            .unwrap();
1504        db.create_channel_message(
1505            channel_id,
1506            user_id_b,
1507            "hello A, it's B.",
1508            OffsetDateTime::now_utc(),
1509        )
1510        .await
1511        .unwrap();
1512
1513        let user_store_a = Arc::new(UserStore::new(client_a.clone()));
1514        let channels_a = cx_a.add_model(|cx| ChannelList::new(user_store_a, client_a, cx));
1515        channels_a
1516            .condition(&mut cx_a, |list, _| list.available_channels().is_some())
1517            .await;
1518        channels_a.read_with(&cx_a, |list, _| {
1519            assert_eq!(
1520                list.available_channels().unwrap(),
1521                &[ChannelDetails {
1522                    id: channel_id.to_proto(),
1523                    name: "test-channel".to_string()
1524                }]
1525            )
1526        });
1527        let channel_a = channels_a.update(&mut cx_a, |this, cx| {
1528            this.get_channel(channel_id.to_proto(), cx).unwrap()
1529        });
1530        channel_a.read_with(&cx_a, |channel, _| assert!(channel.messages().is_empty()));
1531        channel_a
1532            .condition(&cx_a, |channel, _| {
1533                channel_messages(channel)
1534                    == [("user_b".to_string(), "hello A, it's B.".to_string())]
1535            })
1536            .await;
1537
1538        let user_store_b = Arc::new(UserStore::new(client_b.clone()));
1539        let channels_b = cx_b.add_model(|cx| ChannelList::new(user_store_b, client_b, cx));
1540        channels_b
1541            .condition(&mut cx_b, |list, _| list.available_channels().is_some())
1542            .await;
1543        channels_b.read_with(&cx_b, |list, _| {
1544            assert_eq!(
1545                list.available_channels().unwrap(),
1546                &[ChannelDetails {
1547                    id: channel_id.to_proto(),
1548                    name: "test-channel".to_string()
1549                }]
1550            )
1551        });
1552
1553        let channel_b = channels_b.update(&mut cx_b, |this, cx| {
1554            this.get_channel(channel_id.to_proto(), cx).unwrap()
1555        });
1556        channel_b.read_with(&cx_b, |channel, _| assert!(channel.messages().is_empty()));
1557        channel_b
1558            .condition(&cx_b, |channel, _| {
1559                channel_messages(channel)
1560                    == [("user_b".to_string(), "hello A, it's B.".to_string())]
1561            })
1562            .await;
1563
1564        channel_a
1565            .update(&mut cx_a, |channel, cx| {
1566                channel
1567                    .send_message("oh, hi B.".to_string(), cx)
1568                    .unwrap()
1569                    .detach();
1570                let task = channel.send_message("sup".to_string(), cx).unwrap();
1571                assert_eq!(
1572                    channel
1573                        .pending_messages()
1574                        .iter()
1575                        .map(|m| &m.body)
1576                        .collect::<Vec<_>>(),
1577                    &["oh, hi B.", "sup"]
1578                );
1579                task
1580            })
1581            .await
1582            .unwrap();
1583
1584        channel_a
1585            .condition(&cx_a, |channel, _| channel.pending_messages().is_empty())
1586            .await;
1587        channel_b
1588            .condition(&cx_b, |channel, _| {
1589                channel_messages(channel)
1590                    == [
1591                        ("user_b".to_string(), "hello A, it's B.".to_string()),
1592                        ("user_a".to_string(), "oh, hi B.".to_string()),
1593                        ("user_a".to_string(), "sup".to_string()),
1594                    ]
1595            })
1596            .await;
1597
1598        assert_eq!(
1599            server.state().await.channels[&channel_id]
1600                .connection_ids
1601                .len(),
1602            2
1603        );
1604        cx_b.update(|_| drop(channel_b));
1605        server
1606            .condition(|state| state.channels[&channel_id].connection_ids.len() == 1)
1607            .await;
1608
1609        cx_a.update(|_| drop(channel_a));
1610        server
1611            .condition(|state| !state.channels.contains_key(&channel_id))
1612            .await;
1613
1614        fn channel_messages(channel: &Channel) -> Vec<(String, String)> {
1615            channel
1616                .messages()
1617                .cursor::<(), ()>()
1618                .map(|m| (m.sender.github_login.clone(), m.body.clone()))
1619                .collect()
1620        }
1621    }
1622
1623    #[gpui::test]
1624    async fn test_chat_message_validation(mut cx_a: TestAppContext) {
1625        cx_a.foreground().forbid_parking();
1626
1627        let mut server = TestServer::start().await;
1628        let (user_id_a, client_a) = server.create_client(&mut cx_a, "user_a").await;
1629
1630        let db = &server.app_state.db;
1631        let org_id = db.create_org("Test Org", "test-org").await.unwrap();
1632        let channel_id = db.create_org_channel(org_id, "test-channel").await.unwrap();
1633        db.add_org_member(org_id, user_id_a, false).await.unwrap();
1634        db.add_channel_member(channel_id, user_id_a, false)
1635            .await
1636            .unwrap();
1637
1638        let user_store_a = Arc::new(UserStore::new(client_a.clone()));
1639        let channels_a = cx_a.add_model(|cx| ChannelList::new(user_store_a, client_a, cx));
1640        channels_a
1641            .condition(&mut cx_a, |list, _| list.available_channels().is_some())
1642            .await;
1643        let channel_a = channels_a.update(&mut cx_a, |this, cx| {
1644            this.get_channel(channel_id.to_proto(), cx).unwrap()
1645        });
1646
1647        // Messages aren't allowed to be too long.
1648        channel_a
1649            .update(&mut cx_a, |channel, cx| {
1650                let long_body = "this is long.\n".repeat(1024);
1651                channel.send_message(long_body, cx).unwrap()
1652            })
1653            .await
1654            .unwrap_err();
1655
1656        // Messages aren't allowed to be blank.
1657        channel_a.update(&mut cx_a, |channel, cx| {
1658            channel.send_message(String::new(), cx).unwrap_err()
1659        });
1660
1661        // Leading and trailing whitespace are trimmed.
1662        channel_a
1663            .update(&mut cx_a, |channel, cx| {
1664                channel
1665                    .send_message("\n surrounded by whitespace  \n".to_string(), cx)
1666                    .unwrap()
1667            })
1668            .await
1669            .unwrap();
1670        assert_eq!(
1671            db.get_channel_messages(channel_id, 10, None)
1672                .await
1673                .unwrap()
1674                .iter()
1675                .map(|m| &m.body)
1676                .collect::<Vec<_>>(),
1677            &["surrounded by whitespace"]
1678        );
1679    }
1680
1681    #[gpui::test]
1682    async fn test_chat_reconnection(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
1683        cx_a.foreground().forbid_parking();
1684
1685        // Connect to a server as 2 clients.
1686        let mut server = TestServer::start().await;
1687        let (user_id_a, client_a) = server.create_client(&mut cx_a, "user_a").await;
1688        let (user_id_b, client_b) = server.create_client(&mut cx_b, "user_b").await;
1689        let mut status_b = client_b.status();
1690
1691        // Create an org that includes these 2 users.
1692        let db = &server.app_state.db;
1693        let org_id = db.create_org("Test Org", "test-org").await.unwrap();
1694        db.add_org_member(org_id, user_id_a, false).await.unwrap();
1695        db.add_org_member(org_id, user_id_b, false).await.unwrap();
1696
1697        // Create a channel that includes all the users.
1698        let channel_id = db.create_org_channel(org_id, "test-channel").await.unwrap();
1699        db.add_channel_member(channel_id, user_id_a, false)
1700            .await
1701            .unwrap();
1702        db.add_channel_member(channel_id, user_id_b, false)
1703            .await
1704            .unwrap();
1705        db.create_channel_message(
1706            channel_id,
1707            user_id_b,
1708            "hello A, it's B.",
1709            OffsetDateTime::now_utc(),
1710        )
1711        .await
1712        .unwrap();
1713
1714        let user_store_a = Arc::new(UserStore::new(client_a.clone()));
1715        let channels_a = cx_a.add_model(|cx| ChannelList::new(user_store_a, client_a, cx));
1716        channels_a
1717            .condition(&mut cx_a, |list, _| list.available_channels().is_some())
1718            .await;
1719
1720        channels_a.read_with(&cx_a, |list, _| {
1721            assert_eq!(
1722                list.available_channels().unwrap(),
1723                &[ChannelDetails {
1724                    id: channel_id.to_proto(),
1725                    name: "test-channel".to_string()
1726                }]
1727            )
1728        });
1729        let channel_a = channels_a.update(&mut cx_a, |this, cx| {
1730            this.get_channel(channel_id.to_proto(), cx).unwrap()
1731        });
1732        channel_a.read_with(&cx_a, |channel, _| assert!(channel.messages().is_empty()));
1733        channel_a
1734            .condition(&cx_a, |channel, _| {
1735                channel_messages(channel)
1736                    == [("user_b".to_string(), "hello A, it's B.".to_string())]
1737            })
1738            .await;
1739
1740        let user_store_b = Arc::new(UserStore::new(client_b.clone()));
1741        let channels_b = cx_b.add_model(|cx| ChannelList::new(user_store_b, client_b, cx));
1742        channels_b
1743            .condition(&mut cx_b, |list, _| list.available_channels().is_some())
1744            .await;
1745        channels_b.read_with(&cx_b, |list, _| {
1746            assert_eq!(
1747                list.available_channels().unwrap(),
1748                &[ChannelDetails {
1749                    id: channel_id.to_proto(),
1750                    name: "test-channel".to_string()
1751                }]
1752            )
1753        });
1754
1755        let channel_b = channels_b.update(&mut cx_b, |this, cx| {
1756            this.get_channel(channel_id.to_proto(), cx).unwrap()
1757        });
1758        channel_b.read_with(&cx_b, |channel, _| assert!(channel.messages().is_empty()));
1759        channel_b
1760            .condition(&cx_b, |channel, _| {
1761                channel_messages(channel)
1762                    == [("user_b".to_string(), "hello A, it's B.".to_string())]
1763            })
1764            .await;
1765
1766        // Disconnect client B, ensuring we can still access its cached channel data.
1767        server.forbid_connections();
1768        server.disconnect_client(user_id_b);
1769        while !matches!(
1770            status_b.recv().await,
1771            Some(rpc::Status::ReconnectionError { .. })
1772        ) {}
1773
1774        channels_b.read_with(&cx_b, |channels, _| {
1775            assert_eq!(
1776                channels.available_channels().unwrap(),
1777                [ChannelDetails {
1778                    id: channel_id.to_proto(),
1779                    name: "test-channel".to_string()
1780                }]
1781            )
1782        });
1783        channel_b.read_with(&cx_b, |channel, _| {
1784            assert_eq!(
1785                channel_messages(channel),
1786                [("user_b".to_string(), "hello A, it's B.".to_string())]
1787            )
1788        });
1789
1790        // Send a message from client A while B is disconnected.
1791        channel_a
1792            .update(&mut cx_a, |channel, cx| {
1793                channel
1794                    .send_message("oh, hi B.".to_string(), cx)
1795                    .unwrap()
1796                    .detach();
1797                let task = channel.send_message("sup".to_string(), cx).unwrap();
1798                assert_eq!(
1799                    channel
1800                        .pending_messages()
1801                        .iter()
1802                        .map(|m| &m.body)
1803                        .collect::<Vec<_>>(),
1804                    &["oh, hi B.", "sup"]
1805                );
1806                task
1807            })
1808            .await
1809            .unwrap();
1810
1811        // Give client B a chance to reconnect.
1812        server.allow_connections();
1813        cx_b.foreground().advance_clock(Duration::from_secs(10));
1814
1815        // Verify that B sees the new messages upon reconnection.
1816        channel_b
1817            .condition(&cx_b, |channel, _| {
1818                channel_messages(channel)
1819                    == [
1820                        ("user_b".to_string(), "hello A, it's B.".to_string()),
1821                        ("user_a".to_string(), "oh, hi B.".to_string()),
1822                        ("user_a".to_string(), "sup".to_string()),
1823                    ]
1824            })
1825            .await;
1826
1827        // Ensure client A and B can communicate normally after reconnection.
1828        channel_a
1829            .update(&mut cx_a, |channel, cx| {
1830                channel.send_message("you online?".to_string(), cx).unwrap()
1831            })
1832            .await
1833            .unwrap();
1834        channel_b
1835            .condition(&cx_b, |channel, _| {
1836                channel_messages(channel)
1837                    == [
1838                        ("user_b".to_string(), "hello A, it's B.".to_string()),
1839                        ("user_a".to_string(), "oh, hi B.".to_string()),
1840                        ("user_a".to_string(), "sup".to_string()),
1841                        ("user_a".to_string(), "you online?".to_string()),
1842                    ]
1843            })
1844            .await;
1845
1846        channel_b
1847            .update(&mut cx_b, |channel, cx| {
1848                channel.send_message("yep".to_string(), cx).unwrap()
1849            })
1850            .await
1851            .unwrap();
1852        channel_a
1853            .condition(&cx_a, |channel, _| {
1854                channel_messages(channel)
1855                    == [
1856                        ("user_b".to_string(), "hello A, it's B.".to_string()),
1857                        ("user_a".to_string(), "oh, hi B.".to_string()),
1858                        ("user_a".to_string(), "sup".to_string()),
1859                        ("user_a".to_string(), "you online?".to_string()),
1860                        ("user_b".to_string(), "yep".to_string()),
1861                    ]
1862            })
1863            .await;
1864
1865        fn channel_messages(channel: &Channel) -> Vec<(String, String)> {
1866            channel
1867                .messages()
1868                .cursor::<(), ()>()
1869                .map(|m| (m.sender.github_login.clone(), m.body.clone()))
1870                .collect()
1871        }
1872    }
1873
1874    struct TestServer {
1875        peer: Arc<Peer>,
1876        app_state: Arc<AppState>,
1877        server: Arc<Server>,
1878        notifications: mpsc::Receiver<()>,
1879        connection_killers: Arc<Mutex<HashMap<UserId, watch::Sender<Option<()>>>>>,
1880        forbid_connections: Arc<AtomicBool>,
1881        _test_db: TestDb,
1882    }
1883
1884    impl TestServer {
1885        async fn start() -> Self {
1886            let test_db = TestDb::new();
1887            let app_state = Self::build_app_state(&test_db).await;
1888            let peer = Peer::new();
1889            let notifications = mpsc::channel(128);
1890            let server = Server::new(app_state.clone(), peer.clone(), Some(notifications.0));
1891            Self {
1892                peer,
1893                app_state,
1894                server,
1895                notifications: notifications.1,
1896                connection_killers: Default::default(),
1897                forbid_connections: Default::default(),
1898                _test_db: test_db,
1899            }
1900        }
1901
1902        async fn create_client(
1903            &mut self,
1904            cx: &mut TestAppContext,
1905            name: &str,
1906        ) -> (UserId, Arc<Client>) {
1907            let client_user_id = self.app_state.db.create_user(name, false).await.unwrap();
1908            let client_name = name.to_string();
1909            let mut client = Client::new();
1910            let server = self.server.clone();
1911            let connection_killers = self.connection_killers.clone();
1912            let forbid_connections = self.forbid_connections.clone();
1913            Arc::get_mut(&mut client)
1914                .unwrap()
1915                .set_login_and_connect_callbacks(
1916                    move |cx| {
1917                        cx.spawn(|_| async move {
1918                            let access_token = "the-token".to_string();
1919                            Ok((client_user_id.0 as u64, access_token))
1920                        })
1921                    },
1922                    move |user_id, access_token, cx| {
1923                        assert_eq!(user_id, client_user_id.0 as u64);
1924                        assert_eq!(access_token, "the-token");
1925
1926                        let server = server.clone();
1927                        let connection_killers = connection_killers.clone();
1928                        let forbid_connections = forbid_connections.clone();
1929                        let client_name = client_name.clone();
1930                        cx.spawn(move |cx| async move {
1931                            if forbid_connections.load(SeqCst) {
1932                                Err(anyhow!("server is forbidding connections"))
1933                            } else {
1934                                let (client_conn, server_conn, kill_conn) = Conn::in_memory();
1935                                connection_killers.lock().insert(client_user_id, kill_conn);
1936                                cx.background()
1937                                    .spawn(server.handle_connection(
1938                                        server_conn,
1939                                        client_name,
1940                                        client_user_id,
1941                                    ))
1942                                    .detach();
1943                                Ok(client_conn)
1944                            }
1945                        })
1946                    },
1947                );
1948
1949            client
1950                .authenticate_and_connect(&cx.to_async())
1951                .await
1952                .unwrap();
1953            (client_user_id, client)
1954        }
1955
1956        fn disconnect_client(&self, user_id: UserId) {
1957            if let Some(mut kill_conn) = self.connection_killers.lock().remove(&user_id) {
1958                let _ = kill_conn.try_send(Some(()));
1959            }
1960        }
1961
1962        fn forbid_connections(&self) {
1963            self.forbid_connections.store(true, SeqCst);
1964        }
1965
1966        fn allow_connections(&self) {
1967            self.forbid_connections.store(false, SeqCst);
1968        }
1969
1970        async fn build_app_state(test_db: &TestDb) -> Arc<AppState> {
1971            let mut config = Config::default();
1972            config.session_secret = "a".repeat(32);
1973            config.database_url = test_db.url.clone();
1974            let github_client = github::AppClient::test();
1975            Arc::new(AppState {
1976                db: test_db.db().clone(),
1977                handlebars: Default::default(),
1978                auth_client: auth::build_client("", ""),
1979                repo_client: github::RepoClient::test(&github_client),
1980                github_client,
1981                config,
1982            })
1983        }
1984
1985        async fn state<'a>(&'a self) -> RwLockReadGuard<'a, ServerState> {
1986            self.server.state.read().await
1987        }
1988
1989        async fn condition<F>(&mut self, mut predicate: F)
1990        where
1991            F: FnMut(&ServerState) -> bool,
1992        {
1993            async_std::future::timeout(Duration::from_millis(500), async {
1994                while !(predicate)(&*self.server.state.read().await) {
1995                    self.notifications.recv().await;
1996                }
1997            })
1998            .await
1999            .expect("condition timed out");
2000        }
2001    }
2002
2003    impl Drop for TestServer {
2004        fn drop(&mut self) {
2005            task::block_on(self.peer.reset());
2006        }
2007    }
2008
2009    struct EmptyView;
2010
2011    impl gpui::Entity for EmptyView {
2012        type Event = ();
2013    }
2014
2015    impl gpui::View for EmptyView {
2016        fn ui_name() -> &'static str {
2017            "empty view"
2018        }
2019
2020        fn render(&mut self, _: &mut gpui::RenderContext<Self>) -> gpui::ElementBox {
2021            gpui::Element::boxed(gpui::elements::Empty)
2022        }
2023    }
2024}