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