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