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 receipt = request.receipt();
658 let channel_id = ChannelId::from_proto(request.payload.channel_id);
659 let user_id;
660 let connection_ids;
661 {
662 let state = self.state.read().await;
663 user_id = state.user_id_for_connection(request.sender_id)?;
664 if let Some(channel) = state.channels.get(&channel_id) {
665 connection_ids = channel.connection_ids();
666 } else {
667 return Ok(());
668 }
669 }
670
671 // Validate the message body.
672 let body = request.payload.body.trim().to_string();
673 if body.len() > MAX_MESSAGE_LEN {
674 self.peer
675 .respond_with_error(
676 receipt,
677 proto::Error {
678 message: "message is too long".to_string(),
679 },
680 )
681 .await?;
682 return Ok(());
683 }
684 if body.is_empty() {
685 self.peer
686 .respond_with_error(
687 receipt,
688 proto::Error {
689 message: "message can't be blank".to_string(),
690 },
691 )
692 .await?;
693 return Ok(());
694 }
695
696 let timestamp = OffsetDateTime::now_utc();
697 let message_id = self
698 .app_state
699 .db
700 .create_channel_message(channel_id, user_id, &body, timestamp)
701 .await?
702 .to_proto();
703 let message = proto::ChannelMessageSent {
704 channel_id: channel_id.to_proto(),
705 message: Some(proto::ChannelMessage {
706 sender_id: user_id.to_proto(),
707 id: message_id,
708 body,
709 timestamp: timestamp.unix_timestamp() as u64,
710 }),
711 };
712 broadcast(request.sender_id, connection_ids, |conn_id| {
713 self.peer.send(conn_id, message.clone())
714 })
715 .await?;
716 self.peer
717 .respond(
718 receipt,
719 proto::SendChannelMessageResponse {
720 message_id,
721 timestamp: timestamp.unix_timestamp() as u64,
722 },
723 )
724 .await?;
725 Ok(())
726 }
727
728 async fn get_channel_messages(
729 self: Arc<Self>,
730 request: TypedEnvelope<proto::GetChannelMessages>,
731 ) -> tide::Result<()> {
732 let user_id = self
733 .state
734 .read()
735 .await
736 .user_id_for_connection(request.sender_id)?;
737 let channel_id = ChannelId::from_proto(request.payload.channel_id);
738 if !self
739 .app_state
740 .db
741 .can_user_access_channel(user_id, channel_id)
742 .await?
743 {
744 Err(anyhow!("access denied"))?;
745 }
746
747 let messages = self
748 .app_state
749 .db
750 .get_channel_messages(
751 channel_id,
752 MESSAGE_COUNT_PER_PAGE,
753 Some(MessageId::from_proto(request.payload.before_message_id)),
754 )
755 .await?
756 .into_iter()
757 .map(|msg| proto::ChannelMessage {
758 id: msg.id.to_proto(),
759 body: msg.body,
760 timestamp: msg.sent_at.unix_timestamp() as u64,
761 sender_id: msg.sender_id.to_proto(),
762 })
763 .collect::<Vec<_>>();
764 self.peer
765 .respond(
766 request.receipt(),
767 proto::GetChannelMessagesResponse {
768 done: messages.len() < MESSAGE_COUNT_PER_PAGE,
769 messages,
770 },
771 )
772 .await?;
773 Ok(())
774 }
775
776 async fn broadcast_in_worktree<T: proto::EnvelopedMessage>(
777 &self,
778 worktree_id: u64,
779 message: &TypedEnvelope<T>,
780 ) -> tide::Result<()> {
781 let connection_ids = self
782 .state
783 .read()
784 .await
785 .read_worktree(worktree_id, message.sender_id)?
786 .connection_ids();
787
788 broadcast(message.sender_id, connection_ids, |conn_id| {
789 self.peer
790 .forward_send(message.sender_id, conn_id, message.payload.clone())
791 })
792 .await?;
793
794 Ok(())
795 }
796}
797
798pub async fn broadcast<F, T>(
799 sender_id: ConnectionId,
800 receiver_ids: Vec<ConnectionId>,
801 mut f: F,
802) -> anyhow::Result<()>
803where
804 F: FnMut(ConnectionId) -> T,
805 T: Future<Output = anyhow::Result<()>>,
806{
807 let futures = receiver_ids
808 .into_iter()
809 .filter(|id| *id != sender_id)
810 .map(|id| f(id));
811 futures::future::try_join_all(futures).await?;
812 Ok(())
813}
814
815impl ServerState {
816 fn join_channel(&mut self, connection_id: ConnectionId, channel_id: ChannelId) {
817 if let Some(connection) = self.connections.get_mut(&connection_id) {
818 connection.channels.insert(channel_id);
819 self.channels
820 .entry(channel_id)
821 .or_default()
822 .connection_ids
823 .insert(connection_id);
824 }
825 }
826
827 fn leave_channel(&mut self, connection_id: ConnectionId, channel_id: ChannelId) {
828 if let Some(connection) = self.connections.get_mut(&connection_id) {
829 connection.channels.remove(&channel_id);
830 if let hash_map::Entry::Occupied(mut entry) = self.channels.entry(channel_id) {
831 entry.get_mut().connection_ids.remove(&connection_id);
832 if entry.get_mut().connection_ids.is_empty() {
833 entry.remove();
834 }
835 }
836 }
837 }
838
839 fn user_id_for_connection(&self, connection_id: ConnectionId) -> tide::Result<UserId> {
840 Ok(self
841 .connections
842 .get(&connection_id)
843 .ok_or_else(|| anyhow!("unknown connection"))?
844 .user_id)
845 }
846
847 // Add the given connection as a guest of the given worktree
848 fn join_worktree(
849 &mut self,
850 connection_id: ConnectionId,
851 worktree_id: u64,
852 access_token: &str,
853 ) -> Option<(ReplicaId, &Worktree)> {
854 if let Some(worktree) = self.worktrees.get_mut(&worktree_id) {
855 if access_token == worktree.access_token {
856 if let Some(connection) = self.connections.get_mut(&connection_id) {
857 connection.worktrees.insert(worktree_id);
858 }
859
860 let mut replica_id = 1;
861 while worktree.active_replica_ids.contains(&replica_id) {
862 replica_id += 1;
863 }
864 worktree.active_replica_ids.insert(replica_id);
865 worktree
866 .guest_connection_ids
867 .insert(connection_id, replica_id);
868 Some((replica_id, worktree))
869 } else {
870 None
871 }
872 } else {
873 None
874 }
875 }
876
877 fn read_worktree(
878 &self,
879 worktree_id: u64,
880 connection_id: ConnectionId,
881 ) -> tide::Result<&Worktree> {
882 let worktree = self
883 .worktrees
884 .get(&worktree_id)
885 .ok_or_else(|| anyhow!("worktree not found"))?;
886
887 if worktree.host_connection_id == Some(connection_id)
888 || worktree.guest_connection_ids.contains_key(&connection_id)
889 {
890 Ok(worktree)
891 } else {
892 Err(anyhow!(
893 "{} is not a member of worktree {}",
894 connection_id,
895 worktree_id
896 ))?
897 }
898 }
899
900 fn write_worktree(
901 &mut self,
902 worktree_id: u64,
903 connection_id: ConnectionId,
904 ) -> tide::Result<&mut Worktree> {
905 let worktree = self
906 .worktrees
907 .get_mut(&worktree_id)
908 .ok_or_else(|| anyhow!("worktree not found"))?;
909
910 if worktree.host_connection_id == Some(connection_id)
911 || worktree.guest_connection_ids.contains_key(&connection_id)
912 {
913 Ok(worktree)
914 } else {
915 Err(anyhow!(
916 "{} is not a member of worktree {}",
917 connection_id,
918 worktree_id
919 ))?
920 }
921 }
922}
923
924impl Worktree {
925 pub fn connection_ids(&self) -> Vec<ConnectionId> {
926 self.guest_connection_ids
927 .keys()
928 .copied()
929 .chain(self.host_connection_id)
930 .collect()
931 }
932
933 fn host_connection_id(&self) -> tide::Result<ConnectionId> {
934 Ok(self
935 .host_connection_id
936 .ok_or_else(|| anyhow!("host disconnected from worktree"))?)
937 }
938}
939
940impl Channel {
941 fn connection_ids(&self) -> Vec<ConnectionId> {
942 self.connection_ids.iter().copied().collect()
943 }
944}
945
946pub fn add_routes(app: &mut tide::Server<Arc<AppState>>, rpc: &Arc<Peer>) {
947 let server = Server::new(app.state().clone(), rpc.clone(), None);
948 app.at("/rpc").with(auth::VerifyToken).get(move |request: Request<Arc<AppState>>| {
949 let user_id = request.ext::<UserId>().copied();
950 let server = server.clone();
951 async move {
952 const WEBSOCKET_GUID: &str = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
953
954 let connection_upgrade = header_contains_ignore_case(&request, CONNECTION, "upgrade");
955 let upgrade_to_websocket = header_contains_ignore_case(&request, UPGRADE, "websocket");
956 let upgrade_requested = connection_upgrade && upgrade_to_websocket;
957
958 if !upgrade_requested {
959 return Ok(Response::new(StatusCode::UpgradeRequired));
960 }
961
962 let header = match request.header("Sec-Websocket-Key") {
963 Some(h) => h.as_str(),
964 None => return Err(anyhow!("expected sec-websocket-key"))?,
965 };
966
967 let mut response = Response::new(StatusCode::SwitchingProtocols);
968 response.insert_header(UPGRADE, "websocket");
969 response.insert_header(CONNECTION, "Upgrade");
970 let hash = Sha1::new().chain(header).chain(WEBSOCKET_GUID).finalize();
971 response.insert_header("Sec-Websocket-Accept", base64::encode(&hash[..]));
972 response.insert_header("Sec-Websocket-Version", "13");
973
974 let http_res: &mut tide::http::Response = response.as_mut();
975 let upgrade_receiver = http_res.recv_upgrade().await;
976 let addr = request.remote().unwrap_or("unknown").to_string();
977 let user_id = user_id.ok_or_else(|| anyhow!("user_id is not present on request. ensure auth::VerifyToken middleware is present"))?;
978 task::spawn(async move {
979 if let Some(stream) = upgrade_receiver.await {
980 let stream = WebSocketStream::from_raw_socket(stream, Role::Server, None).await;
981 server.handle_connection(stream, addr, user_id).await;
982 }
983 });
984
985 Ok(response)
986 }
987 });
988}
989
990fn header_contains_ignore_case<T>(
991 request: &tide::Request<T>,
992 header_name: HeaderName,
993 value: &str,
994) -> bool {
995 request
996 .header(header_name)
997 .map(|h| {
998 h.as_str()
999 .split(',')
1000 .any(|s| s.trim().eq_ignore_ascii_case(value.trim()))
1001 })
1002 .unwrap_or(false)
1003}
1004
1005#[cfg(test)]
1006mod tests {
1007 use super::*;
1008 use crate::{
1009 auth,
1010 db::{tests::TestDb, UserId},
1011 github, AppState, Config,
1012 };
1013 use async_std::{sync::RwLockReadGuard, task};
1014 use gpui::TestAppContext;
1015 use postage::mpsc;
1016 use serde_json::json;
1017 use sqlx::types::time::OffsetDateTime;
1018 use std::{path::Path, sync::Arc, time::Duration};
1019 use zed::{
1020 channel::{Channel, ChannelDetails, ChannelList},
1021 editor::{Editor, Insert},
1022 fs::{FakeFs, Fs as _},
1023 language::LanguageRegistry,
1024 rpc::Client,
1025 settings, test,
1026 user::UserStore,
1027 worktree::Worktree,
1028 };
1029 use zrpc::Peer;
1030
1031 #[gpui::test]
1032 async fn test_share_worktree(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
1033 let (window_b, _) = cx_b.add_window(|_| EmptyView);
1034 let settings = cx_b.read(settings::test).1;
1035 let lang_registry = Arc::new(LanguageRegistry::new());
1036
1037 // Connect to a server as 2 clients.
1038 let mut server = TestServer::start().await;
1039 let (_, client_a) = server.create_client(&mut cx_a, "user_a").await;
1040 let (_, client_b) = server.create_client(&mut cx_b, "user_b").await;
1041
1042 cx_a.foreground().forbid_parking();
1043
1044 // Share a local worktree as client A
1045 let fs = Arc::new(FakeFs::new());
1046 fs.insert_tree(
1047 "/a",
1048 json!({
1049 "a.txt": "a-contents",
1050 "b.txt": "b-contents",
1051 }),
1052 )
1053 .await;
1054 let worktree_a = Worktree::open_local(
1055 "/a".as_ref(),
1056 lang_registry.clone(),
1057 fs,
1058 &mut cx_a.to_async(),
1059 )
1060 .await
1061 .unwrap();
1062 worktree_a
1063 .read_with(&cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1064 .await;
1065 let (worktree_id, worktree_token) = worktree_a
1066 .update(&mut cx_a, |tree, cx| {
1067 tree.as_local_mut().unwrap().share(client_a.clone(), cx)
1068 })
1069 .await
1070 .unwrap();
1071
1072 // Join that worktree as client B, and see that a guest has joined as client A.
1073 let worktree_b = Worktree::open_remote(
1074 client_b.clone(),
1075 worktree_id,
1076 worktree_token,
1077 lang_registry.clone(),
1078 &mut cx_b.to_async(),
1079 )
1080 .await
1081 .unwrap();
1082 let replica_id_b = worktree_b.read_with(&cx_b, |tree, _| tree.replica_id());
1083 worktree_a
1084 .condition(&cx_a, |tree, _| {
1085 tree.peers()
1086 .values()
1087 .any(|replica_id| *replica_id == replica_id_b)
1088 })
1089 .await;
1090
1091 // Open the same file as client B and client A.
1092 let buffer_b = worktree_b
1093 .update(&mut cx_b, |worktree, cx| worktree.open_buffer("b.txt", cx))
1094 .await
1095 .unwrap();
1096 buffer_b.read_with(&cx_b, |buf, _| assert_eq!(buf.text(), "b-contents"));
1097 worktree_a.read_with(&cx_a, |tree, cx| assert!(tree.has_open_buffer("b.txt", cx)));
1098 let buffer_a = worktree_a
1099 .update(&mut cx_a, |tree, cx| tree.open_buffer("b.txt", cx))
1100 .await
1101 .unwrap();
1102
1103 // Create a selection set as client B and see that selection set as client A.
1104 let editor_b = cx_b.add_view(window_b, |cx| Editor::for_buffer(buffer_b, settings, cx));
1105 buffer_a
1106 .condition(&cx_a, |buffer, _| buffer.selection_sets().count() == 1)
1107 .await;
1108
1109 // Edit the buffer as client B and see that edit as client A.
1110 editor_b.update(&mut cx_b, |editor, cx| {
1111 editor.insert(&Insert("ok, ".into()), cx)
1112 });
1113 buffer_a
1114 .condition(&cx_a, |buffer, _| buffer.text() == "ok, b-contents")
1115 .await;
1116
1117 // Remove the selection set as client B, see those selections disappear as client A.
1118 cx_b.update(move |_| drop(editor_b));
1119 buffer_a
1120 .condition(&cx_a, |buffer, _| buffer.selection_sets().count() == 0)
1121 .await;
1122
1123 // Close the buffer as client A, see that the buffer is closed.
1124 drop(buffer_a);
1125 worktree_a
1126 .condition(&cx_a, |tree, cx| !tree.has_open_buffer("b.txt", cx))
1127 .await;
1128
1129 // Dropping the worktree removes client B from client A's peers.
1130 cx_b.update(move |_| drop(worktree_b));
1131 worktree_a
1132 .condition(&cx_a, |tree, _| tree.peers().is_empty())
1133 .await;
1134 }
1135
1136 #[gpui::test]
1137 async fn test_propagate_saves_and_fs_changes_in_shared_worktree(
1138 mut cx_a: TestAppContext,
1139 mut cx_b: TestAppContext,
1140 mut cx_c: TestAppContext,
1141 ) {
1142 cx_a.foreground().forbid_parking();
1143 let lang_registry = Arc::new(LanguageRegistry::new());
1144
1145 // Connect to a server as 3 clients.
1146 let mut server = TestServer::start().await;
1147 let (_, client_a) = server.create_client(&mut cx_a, "user_a").await;
1148 let (_, client_b) = server.create_client(&mut cx_b, "user_b").await;
1149 let (_, client_c) = server.create_client(&mut cx_c, "user_c").await;
1150
1151 let fs = Arc::new(FakeFs::new());
1152
1153 // Share a worktree as client A.
1154 fs.insert_tree(
1155 "/a",
1156 json!({
1157 "file1": "",
1158 "file2": ""
1159 }),
1160 )
1161 .await;
1162
1163 let worktree_a = Worktree::open_local(
1164 "/a".as_ref(),
1165 lang_registry.clone(),
1166 fs.clone(),
1167 &mut cx_a.to_async(),
1168 )
1169 .await
1170 .unwrap();
1171 worktree_a
1172 .read_with(&cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1173 .await;
1174 let (worktree_id, worktree_token) = worktree_a
1175 .update(&mut cx_a, |tree, cx| {
1176 tree.as_local_mut().unwrap().share(client_a.clone(), cx)
1177 })
1178 .await
1179 .unwrap();
1180
1181 // Join that worktree as clients B and C.
1182 let worktree_b = Worktree::open_remote(
1183 client_b.clone(),
1184 worktree_id,
1185 worktree_token.clone(),
1186 lang_registry.clone(),
1187 &mut cx_b.to_async(),
1188 )
1189 .await
1190 .unwrap();
1191 let worktree_c = Worktree::open_remote(
1192 client_c.clone(),
1193 worktree_id,
1194 worktree_token,
1195 lang_registry.clone(),
1196 &mut cx_c.to_async(),
1197 )
1198 .await
1199 .unwrap();
1200
1201 // Open and edit a buffer as both guests B and C.
1202 let buffer_b = worktree_b
1203 .update(&mut cx_b, |tree, cx| tree.open_buffer("file1", cx))
1204 .await
1205 .unwrap();
1206 let buffer_c = worktree_c
1207 .update(&mut cx_c, |tree, cx| tree.open_buffer("file1", cx))
1208 .await
1209 .unwrap();
1210 buffer_b.update(&mut cx_b, |buf, cx| buf.edit([0..0], "i-am-b, ", cx));
1211 buffer_c.update(&mut cx_c, |buf, cx| buf.edit([0..0], "i-am-c, ", cx));
1212
1213 // Open and edit that buffer as the host.
1214 let buffer_a = worktree_a
1215 .update(&mut cx_a, |tree, cx| tree.open_buffer("file1", cx))
1216 .await
1217 .unwrap();
1218
1219 buffer_a
1220 .condition(&mut cx_a, |buf, _| buf.text() == "i-am-c, i-am-b, ")
1221 .await;
1222 buffer_a.update(&mut cx_a, |buf, cx| {
1223 buf.edit([buf.len()..buf.len()], "i-am-a", cx)
1224 });
1225
1226 // Wait for edits to propagate
1227 buffer_a
1228 .condition(&mut cx_a, |buf, _| buf.text() == "i-am-c, i-am-b, i-am-a")
1229 .await;
1230 buffer_b
1231 .condition(&mut cx_b, |buf, _| buf.text() == "i-am-c, i-am-b, i-am-a")
1232 .await;
1233 buffer_c
1234 .condition(&mut cx_c, |buf, _| buf.text() == "i-am-c, i-am-b, i-am-a")
1235 .await;
1236
1237 // Edit the buffer as the host and concurrently save as guest B.
1238 let save_b = buffer_b.update(&mut cx_b, |buf, cx| buf.save(cx).unwrap());
1239 buffer_a.update(&mut cx_a, |buf, cx| buf.edit([0..0], "hi-a, ", cx));
1240 save_b.await.unwrap();
1241 assert_eq!(
1242 fs.load("/a/file1".as_ref()).await.unwrap(),
1243 "hi-a, i-am-c, i-am-b, i-am-a"
1244 );
1245 buffer_a.read_with(&cx_a, |buf, _| assert!(!buf.is_dirty()));
1246 buffer_b.read_with(&cx_b, |buf, _| assert!(!buf.is_dirty()));
1247 buffer_c.condition(&cx_c, |buf, _| !buf.is_dirty()).await;
1248
1249 // Make changes on host's file system, see those changes on the guests.
1250 fs.rename("/a/file2".as_ref(), "/a/file3".as_ref())
1251 .await
1252 .unwrap();
1253 fs.insert_file(Path::new("/a/file4"), "4".into())
1254 .await
1255 .unwrap();
1256
1257 worktree_b
1258 .condition(&cx_b, |tree, _| tree.file_count() == 3)
1259 .await;
1260 worktree_c
1261 .condition(&cx_c, |tree, _| tree.file_count() == 3)
1262 .await;
1263 worktree_b.read_with(&cx_b, |tree, _| {
1264 assert_eq!(
1265 tree.paths()
1266 .map(|p| p.to_string_lossy())
1267 .collect::<Vec<_>>(),
1268 &["file1", "file3", "file4"]
1269 )
1270 });
1271 worktree_c.read_with(&cx_c, |tree, _| {
1272 assert_eq!(
1273 tree.paths()
1274 .map(|p| p.to_string_lossy())
1275 .collect::<Vec<_>>(),
1276 &["file1", "file3", "file4"]
1277 )
1278 });
1279 }
1280
1281 #[gpui::test]
1282 async fn test_buffer_conflict_after_save(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
1283 cx_a.foreground().forbid_parking();
1284 let lang_registry = Arc::new(LanguageRegistry::new());
1285
1286 // Connect to a server as 2 clients.
1287 let mut server = TestServer::start().await;
1288 let (_, client_a) = server.create_client(&mut cx_a, "user_a").await;
1289 let (_, client_b) = server.create_client(&mut cx_b, "user_b").await;
1290
1291 // Share a local worktree as client A
1292 let fs = Arc::new(FakeFs::new());
1293 fs.save(Path::new("/a.txt"), &"a-contents".into())
1294 .await
1295 .unwrap();
1296 let worktree_a = Worktree::open_local(
1297 "/".as_ref(),
1298 lang_registry.clone(),
1299 fs,
1300 &mut cx_a.to_async(),
1301 )
1302 .await
1303 .unwrap();
1304 worktree_a
1305 .read_with(&cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1306 .await;
1307 let (worktree_id, worktree_token) = worktree_a
1308 .update(&mut cx_a, |tree, cx| {
1309 tree.as_local_mut().unwrap().share(client_a.clone(), cx)
1310 })
1311 .await
1312 .unwrap();
1313
1314 // Join that worktree as client B, and see that a guest has joined as client A.
1315 let worktree_b = Worktree::open_remote(
1316 client_b.clone(),
1317 worktree_id,
1318 worktree_token,
1319 lang_registry.clone(),
1320 &mut cx_b.to_async(),
1321 )
1322 .await
1323 .unwrap();
1324
1325 let buffer_b = worktree_b
1326 .update(&mut cx_b, |worktree, cx| worktree.open_buffer("a.txt", cx))
1327 .await
1328 .unwrap();
1329 let mtime = buffer_b.read_with(&cx_b, |buf, _| buf.file().unwrap().mtime);
1330
1331 buffer_b.update(&mut cx_b, |buf, cx| buf.edit([0..0], "world ", cx));
1332 buffer_b.read_with(&cx_b, |buf, _| {
1333 assert!(buf.is_dirty());
1334 assert!(!buf.has_conflict());
1335 });
1336
1337 buffer_b
1338 .update(&mut cx_b, |buf, cx| buf.save(cx))
1339 .unwrap()
1340 .await
1341 .unwrap();
1342 worktree_b
1343 .condition(&cx_b, |_, cx| {
1344 buffer_b.read(cx).file().unwrap().mtime != mtime
1345 })
1346 .await;
1347 buffer_b.read_with(&cx_b, |buf, _| {
1348 assert!(!buf.is_dirty());
1349 assert!(!buf.has_conflict());
1350 });
1351
1352 buffer_b.update(&mut cx_b, |buf, cx| buf.edit([0..0], "hello ", cx));
1353 buffer_b.read_with(&cx_b, |buf, _| {
1354 assert!(buf.is_dirty());
1355 assert!(!buf.has_conflict());
1356 });
1357 }
1358
1359 #[gpui::test]
1360 async fn test_editing_while_guest_opens_buffer(
1361 mut cx_a: TestAppContext,
1362 mut cx_b: TestAppContext,
1363 ) {
1364 cx_a.foreground().forbid_parking();
1365 let lang_registry = Arc::new(LanguageRegistry::new());
1366
1367 // Connect to a server as 2 clients.
1368 let mut server = TestServer::start().await;
1369 let (_, client_a) = server.create_client(&mut cx_a, "user_a").await;
1370 let (_, client_b) = server.create_client(&mut cx_b, "user_b").await;
1371
1372 // Share a local worktree as client A
1373 let fs = Arc::new(FakeFs::new());
1374 fs.save(Path::new("/a.txt"), &"a-contents".into())
1375 .await
1376 .unwrap();
1377 let worktree_a = Worktree::open_local(
1378 "/".as_ref(),
1379 lang_registry.clone(),
1380 fs,
1381 &mut cx_a.to_async(),
1382 )
1383 .await
1384 .unwrap();
1385 worktree_a
1386 .read_with(&cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1387 .await;
1388 let (worktree_id, worktree_token) = worktree_a
1389 .update(&mut cx_a, |tree, cx| {
1390 tree.as_local_mut().unwrap().share(client_a.clone(), cx)
1391 })
1392 .await
1393 .unwrap();
1394
1395 // Join that worktree as client B, and see that a guest has joined as client A.
1396 let worktree_b = Worktree::open_remote(
1397 client_b.clone(),
1398 worktree_id,
1399 worktree_token,
1400 lang_registry.clone(),
1401 &mut cx_b.to_async(),
1402 )
1403 .await
1404 .unwrap();
1405
1406 let buffer_a = worktree_a
1407 .update(&mut cx_a, |tree, cx| tree.open_buffer("a.txt", cx))
1408 .await
1409 .unwrap();
1410 let buffer_b = cx_b
1411 .background()
1412 .spawn(worktree_b.update(&mut cx_b, |worktree, cx| worktree.open_buffer("a.txt", cx)));
1413
1414 task::yield_now().await;
1415 buffer_a.update(&mut cx_a, |buf, cx| buf.edit([0..0], "z", cx));
1416
1417 let text = buffer_a.read_with(&cx_a, |buf, _| buf.text());
1418 let buffer_b = buffer_b.await.unwrap();
1419 buffer_b.condition(&cx_b, |buf, _| buf.text() == text).await;
1420 }
1421
1422 #[gpui::test]
1423 async fn test_peer_disconnection(mut cx_a: TestAppContext, cx_b: TestAppContext) {
1424 cx_a.foreground().forbid_parking();
1425 let lang_registry = Arc::new(LanguageRegistry::new());
1426
1427 // Connect to a server as 2 clients.
1428 let mut server = TestServer::start().await;
1429 let (_, client_a) = server.create_client(&mut cx_a, "user_a").await;
1430 let (_, client_b) = server.create_client(&mut cx_a, "user_b").await;
1431
1432 // Share a local worktree as client A
1433 let fs = Arc::new(FakeFs::new());
1434 fs.insert_tree(
1435 "/a",
1436 json!({
1437 "a.txt": "a-contents",
1438 "b.txt": "b-contents",
1439 }),
1440 )
1441 .await;
1442 let worktree_a = Worktree::open_local(
1443 "/a".as_ref(),
1444 lang_registry.clone(),
1445 fs,
1446 &mut cx_a.to_async(),
1447 )
1448 .await
1449 .unwrap();
1450 worktree_a
1451 .read_with(&cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1452 .await;
1453 let (worktree_id, worktree_token) = worktree_a
1454 .update(&mut cx_a, |tree, cx| {
1455 tree.as_local_mut().unwrap().share(client_a.clone(), cx)
1456 })
1457 .await
1458 .unwrap();
1459
1460 // Join that worktree as client B, and see that a guest has joined as client A.
1461 let _worktree_b = Worktree::open_remote(
1462 client_b.clone(),
1463 worktree_id,
1464 worktree_token,
1465 lang_registry.clone(),
1466 &mut cx_b.to_async(),
1467 )
1468 .await
1469 .unwrap();
1470 worktree_a
1471 .condition(&cx_a, |tree, _| tree.peers().len() == 1)
1472 .await;
1473
1474 // Drop client B's connection and ensure client A observes client B leaving the worktree.
1475 client_b.disconnect().await.unwrap();
1476 worktree_a
1477 .condition(&cx_a, |tree, _| tree.peers().len() == 0)
1478 .await;
1479 }
1480
1481 #[gpui::test]
1482 async fn test_basic_chat(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
1483 cx_a.foreground().forbid_parking();
1484
1485 // Connect to a server as 2 clients.
1486 let mut server = TestServer::start().await;
1487 let (user_id_a, client_a) = server.create_client(&mut cx_a, "user_a").await;
1488 let (user_id_b, client_b) = server.create_client(&mut cx_b, "user_b").await;
1489
1490 // Create an org that includes these 2 users.
1491 let db = &server.app_state.db;
1492 let org_id = db.create_org("Test Org", "test-org").await.unwrap();
1493 db.add_org_member(org_id, user_id_a, false).await.unwrap();
1494 db.add_org_member(org_id, user_id_b, false).await.unwrap();
1495
1496 // Create a channel that includes all the users.
1497 let channel_id = db.create_org_channel(org_id, "test-channel").await.unwrap();
1498 db.add_channel_member(channel_id, user_id_a, false)
1499 .await
1500 .unwrap();
1501 db.add_channel_member(channel_id, user_id_b, false)
1502 .await
1503 .unwrap();
1504 db.create_channel_message(
1505 channel_id,
1506 user_id_b,
1507 "hello A, it's B.",
1508 OffsetDateTime::now_utc(),
1509 )
1510 .await
1511 .unwrap();
1512
1513 let user_store_a = Arc::new(UserStore::new(client_a.clone()));
1514 let channels_a = cx_a.add_model(|cx| ChannelList::new(user_store_a, client_a, cx));
1515 channels_a
1516 .condition(&mut cx_a, |list, _| list.available_channels().is_some())
1517 .await;
1518 channels_a.read_with(&cx_a, |list, _| {
1519 assert_eq!(
1520 list.available_channels().unwrap(),
1521 &[ChannelDetails {
1522 id: channel_id.to_proto(),
1523 name: "test-channel".to_string()
1524 }]
1525 )
1526 });
1527 let channel_a = channels_a.update(&mut cx_a, |this, cx| {
1528 this.get_channel(channel_id.to_proto(), cx).unwrap()
1529 });
1530 channel_a.read_with(&cx_a, |channel, _| assert!(channel.messages().is_empty()));
1531 channel_a
1532 .condition(&cx_a, |channel, _| {
1533 channel_messages(channel)
1534 == [("user_b".to_string(), "hello A, it's B.".to_string())]
1535 })
1536 .await;
1537
1538 let user_store_b = Arc::new(UserStore::new(client_b.clone()));
1539 let channels_b = cx_b.add_model(|cx| ChannelList::new(user_store_b, client_b, cx));
1540 channels_b
1541 .condition(&mut cx_b, |list, _| list.available_channels().is_some())
1542 .await;
1543 channels_b.read_with(&cx_b, |list, _| {
1544 assert_eq!(
1545 list.available_channels().unwrap(),
1546 &[ChannelDetails {
1547 id: channel_id.to_proto(),
1548 name: "test-channel".to_string()
1549 }]
1550 )
1551 });
1552
1553 let channel_b = channels_b.update(&mut cx_b, |this, cx| {
1554 this.get_channel(channel_id.to_proto(), cx).unwrap()
1555 });
1556 channel_b.read_with(&cx_b, |channel, _| assert!(channel.messages().is_empty()));
1557 channel_b
1558 .condition(&cx_b, |channel, _| {
1559 channel_messages(channel)
1560 == [("user_b".to_string(), "hello A, it's B.".to_string())]
1561 })
1562 .await;
1563
1564 channel_a
1565 .update(&mut cx_a, |channel, cx| {
1566 channel
1567 .send_message("oh, hi B.".to_string(), cx)
1568 .unwrap()
1569 .detach();
1570 let task = channel.send_message("sup".to_string(), cx).unwrap();
1571 assert_eq!(
1572 channel
1573 .pending_messages()
1574 .iter()
1575 .map(|m| &m.body)
1576 .collect::<Vec<_>>(),
1577 &["oh, hi B.", "sup"]
1578 );
1579 task
1580 })
1581 .await
1582 .unwrap();
1583
1584 channel_a
1585 .condition(&cx_a, |channel, _| channel.pending_messages().is_empty())
1586 .await;
1587 channel_b
1588 .condition(&cx_b, |channel, _| {
1589 channel_messages(channel)
1590 == [
1591 ("user_b".to_string(), "hello A, it's B.".to_string()),
1592 ("user_a".to_string(), "oh, hi B.".to_string()),
1593 ("user_a".to_string(), "sup".to_string()),
1594 ]
1595 })
1596 .await;
1597
1598 assert_eq!(
1599 server.state().await.channels[&channel_id]
1600 .connection_ids
1601 .len(),
1602 2
1603 );
1604 cx_b.update(|_| drop(channel_b));
1605 server
1606 .condition(|state| state.channels[&channel_id].connection_ids.len() == 1)
1607 .await;
1608
1609 cx_a.update(|_| drop(channel_a));
1610 server
1611 .condition(|state| !state.channels.contains_key(&channel_id))
1612 .await;
1613
1614 fn channel_messages(channel: &Channel) -> Vec<(String, String)> {
1615 channel
1616 .messages()
1617 .cursor::<(), ()>()
1618 .map(|m| (m.sender.github_login.clone(), m.body.clone()))
1619 .collect()
1620 }
1621 }
1622
1623 #[gpui::test]
1624 async fn test_chat_message_validation(mut cx_a: TestAppContext) {
1625 cx_a.foreground().forbid_parking();
1626
1627 let mut server = TestServer::start().await;
1628 let (user_id_a, client_a) = server.create_client(&mut cx_a, "user_a").await;
1629
1630 let db = &server.app_state.db;
1631 let org_id = db.create_org("Test Org", "test-org").await.unwrap();
1632 let channel_id = db.create_org_channel(org_id, "test-channel").await.unwrap();
1633 db.add_org_member(org_id, user_id_a, false).await.unwrap();
1634 db.add_channel_member(channel_id, user_id_a, false)
1635 .await
1636 .unwrap();
1637
1638 let user_store_a = Arc::new(UserStore::new(client_a.clone()));
1639 let channels_a = cx_a.add_model(|cx| ChannelList::new(user_store_a, client_a, cx));
1640 channels_a
1641 .condition(&mut cx_a, |list, _| list.available_channels().is_some())
1642 .await;
1643 let channel_a = channels_a.update(&mut cx_a, |this, cx| {
1644 this.get_channel(channel_id.to_proto(), cx).unwrap()
1645 });
1646
1647 // Messages aren't allowed to be too long.
1648 channel_a
1649 .update(&mut cx_a, |channel, cx| {
1650 let long_body = "this is long.\n".repeat(1024);
1651 channel.send_message(long_body, cx).unwrap()
1652 })
1653 .await
1654 .unwrap_err();
1655
1656 // Messages aren't allowed to be blank.
1657 channel_a
1658 .update(&mut cx_a, |channel, cx| {
1659 channel.send_message(String::new(), cx).unwrap()
1660 })
1661 .await
1662 .unwrap_err();
1663
1664 // Leading and trailing whitespace are trimmed.
1665 channel_a
1666 .update(&mut cx_a, |channel, cx| {
1667 channel
1668 .send_message("\n surrounded by whitespace \n".to_string(), cx)
1669 .unwrap()
1670 })
1671 .await
1672 .unwrap();
1673 assert_eq!(
1674 db.get_channel_messages(channel_id, 10, None)
1675 .await
1676 .unwrap()
1677 .iter()
1678 .map(|m| &m.body)
1679 .collect::<Vec<_>>(),
1680 &["surrounded by whitespace"]
1681 );
1682 }
1683
1684 struct TestServer {
1685 peer: Arc<Peer>,
1686 app_state: Arc<AppState>,
1687 server: Arc<Server>,
1688 notifications: mpsc::Receiver<()>,
1689 _test_db: TestDb,
1690 }
1691
1692 impl TestServer {
1693 async fn start() -> Self {
1694 let test_db = TestDb::new();
1695 let app_state = Self::build_app_state(&test_db).await;
1696 let peer = Peer::new();
1697 let notifications = mpsc::channel(128);
1698 let server = Server::new(app_state.clone(), peer.clone(), Some(notifications.0));
1699 Self {
1700 peer,
1701 app_state,
1702 server,
1703 notifications: notifications.1,
1704 _test_db: test_db,
1705 }
1706 }
1707
1708 async fn create_client(
1709 &mut self,
1710 cx: &mut TestAppContext,
1711 name: &str,
1712 ) -> (UserId, Arc<Client>) {
1713 let user_id = self.app_state.db.create_user(name, false).await.unwrap();
1714 let client = Client::new();
1715 let (client_conn, server_conn) = test::Channel::bidirectional();
1716 cx.background()
1717 .spawn(
1718 self.server
1719 .handle_connection(server_conn, name.to_string(), user_id),
1720 )
1721 .detach();
1722 client
1723 .add_connection(user_id.to_proto(), client_conn, cx.to_async())
1724 .await
1725 .unwrap();
1726 (user_id, client)
1727 }
1728
1729 async fn build_app_state(test_db: &TestDb) -> Arc<AppState> {
1730 let mut config = Config::default();
1731 config.session_secret = "a".repeat(32);
1732 config.database_url = test_db.url.clone();
1733 let github_client = github::AppClient::test();
1734 Arc::new(AppState {
1735 db: test_db.db().clone(),
1736 handlebars: Default::default(),
1737 auth_client: auth::build_client("", ""),
1738 repo_client: github::RepoClient::test(&github_client),
1739 github_client,
1740 config,
1741 })
1742 }
1743
1744 async fn state<'a>(&'a self) -> RwLockReadGuard<'a, ServerState> {
1745 self.server.state.read().await
1746 }
1747
1748 async fn condition<F>(&mut self, mut predicate: F)
1749 where
1750 F: FnMut(&ServerState) -> bool,
1751 {
1752 async_std::future::timeout(Duration::from_millis(500), async {
1753 while !(predicate)(&*self.server.state.read().await) {
1754 self.notifications.recv().await;
1755 }
1756 })
1757 .await
1758 .expect("condition timed out");
1759 }
1760 }
1761
1762 impl Drop for TestServer {
1763 fn drop(&mut self) {
1764 task::block_on(self.peer.reset());
1765 }
1766 }
1767
1768 struct EmptyView;
1769
1770 impl gpui::Entity for EmptyView {
1771 type Event = ();
1772 }
1773
1774 impl gpui::View for EmptyView {
1775 fn ui_name() -> &'static str {
1776 "empty view"
1777 }
1778
1779 fn render(&self, _: &mut gpui::RenderContext<Self>) -> gpui::ElementBox {
1780 gpui::Element::boxed(gpui::elements::Empty)
1781 }
1782 }
1783}