Automatically fetch remote participant users in Room

This commit is contained in:
Antonio Scandurra 2022-10-03 16:09:49 +02:00
parent da6106db8e
commit ad323d6e3b
3 changed files with 72 additions and 71 deletions

View file

@ -1,7 +1,6 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use client::proto; use client::{proto, User};
use gpui::ModelHandle; use std::sync::Arc;
use project::Project;
pub enum ParticipantLocation { pub enum ParticipantLocation {
Project { project_id: u64 }, Project { project_id: u64 },
@ -21,7 +20,7 @@ impl ParticipantLocation {
} }
pub struct RemoteParticipant { pub struct RemoteParticipant {
pub user_id: u64, pub user: Arc<User>,
pub projects: Vec<ModelHandle<Project>>, pub project_ids: Vec<u64>,
pub location: ParticipantLocation, pub location: ParticipantLocation,
} }

View file

@ -19,7 +19,7 @@ pub struct Room {
client: Arc<Client>, client: Arc<Client>,
user_store: ModelHandle<UserStore>, user_store: ModelHandle<UserStore>,
_subscriptions: Vec<client::Subscription>, _subscriptions: Vec<client::Subscription>,
_load_pending_users: Option<Task<()>>, _pending_room_update: Option<Task<()>>,
} }
impl Entity for Room { impl Entity for Room {
@ -58,7 +58,7 @@ impl Room {
remote_participants: Default::default(), remote_participants: Default::default(),
pending_users: Default::default(), pending_users: Default::default(),
_subscriptions: vec![client.add_message_handler(cx.handle(), Self::handle_room_updated)], _subscriptions: vec![client.add_message_handler(cx.handle(), Self::handle_room_updated)],
_load_pending_users: None, _pending_room_update: None,
client, client,
user_store, user_store,
} }
@ -133,32 +133,51 @@ impl Room {
Ok(()) Ok(())
} }
fn apply_room_update(&mut self, room: proto::Room, cx: &mut ModelContext<Self>) -> Result<()> { fn apply_room_update(
// TODO: compute diff instead of clearing participants &mut self,
self.remote_participants.clear(); mut room: proto::Room,
for participant in room.participants { cx: &mut ModelContext<Self>,
if Some(participant.user_id) != self.client.user_id() { ) -> Result<()> {
self.remote_participants.insert( // Filter ourselves out from the room's participants.
PeerId(participant.peer_id), room.participants
RemoteParticipant { .retain(|participant| Some(participant.user_id) != self.client.user_id());
user_id: participant.user_id,
projects: Default::default(), // TODO: populate projects
location: ParticipantLocation::from_proto(participant.location)?,
},
);
}
}
let pending_users = self.user_store.update(cx, move |user_store, cx| { let participant_user_ids = room
user_store.get_users(room.pending_user_ids, cx) .participants
.iter()
.map(|p| p.user_id)
.collect::<Vec<_>>();
let (participants, pending_users) = self.user_store.update(cx, move |user_store, cx| {
(
user_store.get_users(participant_user_ids, cx),
user_store.get_users(room.pending_user_ids, cx),
)
}); });
self._load_pending_users = Some(cx.spawn(|this, mut cx| async move { self._pending_room_update = Some(cx.spawn(|this, mut cx| async move {
if let Some(pending_users) = pending_users.await.log_err() { let (participants, pending_users) = futures::join!(participants, pending_users);
this.update(&mut cx, |this, cx| {
this.update(&mut cx, |this, cx| {
if let Some(participants) = participants.log_err() {
// TODO: compute diff instead of clearing participants
this.remote_participants.clear();
for (participant, user) in room.participants.into_iter().zip(participants) {
this.remote_participants.insert(
PeerId(participant.peer_id),
RemoteParticipant {
user,
project_ids: participant.project_ids,
location: ParticipantLocation::from_proto(participant.location)
.unwrap_or(ParticipantLocation::External),
},
);
}
}
if let Some(pending_users) = pending_users.log_err() {
this.pending_users = pending_users; this.pending_users = pending_users;
cx.notify(); cx.notify();
}); }
} });
})); }));
cx.notify(); cx.notify();

View file

@ -82,7 +82,7 @@ async fn test_basic_calls(
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
room_participants(&room_a, &client_a, cx_a).await, room_participants(&room_a, cx_a),
RoomParticipants { RoomParticipants {
remote: Default::default(), remote: Default::default(),
pending: Default::default() pending: Default::default()
@ -100,7 +100,7 @@ async fn test_basic_calls(
deterministic.run_until_parked(); deterministic.run_until_parked();
assert_eq!( assert_eq!(
room_participants(&room_a, &client_a, cx_a).await, room_participants(&room_a, cx_a),
RoomParticipants { RoomParticipants {
remote: Default::default(), remote: Default::default(),
pending: vec!["user_b".to_string()] pending: vec!["user_b".to_string()]
@ -127,14 +127,14 @@ async fn test_basic_calls(
deterministic.run_until_parked(); deterministic.run_until_parked();
assert_eq!( assert_eq!(
room_participants(&room_a, &client_a, cx_a).await, room_participants(&room_a, cx_a),
RoomParticipants { RoomParticipants {
remote: vec!["user_b".to_string()], remote: vec!["user_b".to_string()],
pending: Default::default() pending: Default::default()
} }
); );
assert_eq!( assert_eq!(
room_participants(&room_b, &client_b, cx_b).await, room_participants(&room_b, cx_b),
RoomParticipants { RoomParticipants {
remote: vec!["user_a".to_string()], remote: vec!["user_a".to_string()],
pending: Default::default() pending: Default::default()
@ -152,14 +152,14 @@ async fn test_basic_calls(
deterministic.run_until_parked(); deterministic.run_until_parked();
assert_eq!( assert_eq!(
room_participants(&room_a, &client_a, cx_a).await, room_participants(&room_a, cx_a),
RoomParticipants { RoomParticipants {
remote: vec!["user_b".to_string()], remote: vec!["user_b".to_string()],
pending: vec!["user_c".to_string()] pending: vec!["user_c".to_string()]
} }
); );
assert_eq!( assert_eq!(
room_participants(&room_b, &client_b, cx_b).await, room_participants(&room_b, cx_b),
RoomParticipants { RoomParticipants {
remote: vec!["user_a".to_string()], remote: vec!["user_a".to_string()],
pending: vec!["user_c".to_string()] pending: vec!["user_c".to_string()]
@ -176,14 +176,14 @@ async fn test_basic_calls(
deterministic.run_until_parked(); deterministic.run_until_parked();
assert_eq!( assert_eq!(
room_participants(&room_a, &client_a, cx_a).await, room_participants(&room_a, cx_a),
RoomParticipants { RoomParticipants {
remote: vec!["user_b".to_string()], remote: vec!["user_b".to_string()],
pending: Default::default() pending: Default::default()
} }
); );
assert_eq!( assert_eq!(
room_participants(&room_b, &client_b, cx_b).await, room_participants(&room_b, cx_b),
RoomParticipants { RoomParticipants {
remote: vec!["user_a".to_string()], remote: vec!["user_a".to_string()],
pending: Default::default() pending: Default::default()
@ -194,14 +194,14 @@ async fn test_basic_calls(
room_a.update(cx_a, |room, cx| room.leave(cx)).unwrap(); room_a.update(cx_a, |room, cx| room.leave(cx)).unwrap();
deterministic.run_until_parked(); deterministic.run_until_parked();
assert_eq!( assert_eq!(
room_participants(&room_a, &client_a, cx_a).await, room_participants(&room_a, cx_a),
RoomParticipants { RoomParticipants {
remote: Default::default(), remote: Default::default(),
pending: Default::default() pending: Default::default()
} }
); );
assert_eq!( assert_eq!(
room_participants(&room_b, &client_b, cx_b).await, room_participants(&room_b, cx_b),
RoomParticipants { RoomParticipants {
remote: Default::default(), remote: Default::default(),
pending: Default::default() pending: Default::default()
@ -245,14 +245,14 @@ async fn test_leaving_room_on_disconnection(
.unwrap(); .unwrap();
deterministic.run_until_parked(); deterministic.run_until_parked();
assert_eq!( assert_eq!(
room_participants(&room_a, &client_a, cx_a).await, room_participants(&room_a, cx_a),
RoomParticipants { RoomParticipants {
remote: vec!["user_b".to_string()], remote: vec!["user_b".to_string()],
pending: Default::default() pending: Default::default()
} }
); );
assert_eq!( assert_eq!(
room_participants(&room_b, &client_b, cx_b).await, room_participants(&room_b, cx_b),
RoomParticipants { RoomParticipants {
remote: vec!["user_a".to_string()], remote: vec!["user_a".to_string()],
pending: Default::default() pending: Default::default()
@ -262,14 +262,14 @@ async fn test_leaving_room_on_disconnection(
server.disconnect_client(client_a.current_user_id(cx_a)); server.disconnect_client(client_a.current_user_id(cx_a));
cx_a.foreground().advance_clock(rpc::RECEIVE_TIMEOUT); cx_a.foreground().advance_clock(rpc::RECEIVE_TIMEOUT);
assert_eq!( assert_eq!(
room_participants(&room_a, &client_a, cx_a).await, room_participants(&room_a, cx_a),
RoomParticipants { RoomParticipants {
remote: Default::default(), remote: Default::default(),
pending: Default::default() pending: Default::default()
} }
); );
assert_eq!( assert_eq!(
room_participants(&room_b, &client_b, cx_b).await, room_participants(&room_b, cx_b),
RoomParticipants { RoomParticipants {
remote: Default::default(), remote: Default::default(),
pending: Default::default() pending: Default::default()
@ -5822,34 +5822,17 @@ struct RoomParticipants {
pending: Vec<String>, pending: Vec<String>,
} }
async fn room_participants( fn room_participants(room: &ModelHandle<Room>, cx: &mut TestAppContext) -> RoomParticipants {
room: &ModelHandle<Room>, room.read_with(cx, |room, _| RoomParticipants {
client: &TestClient, remote: room
cx: &mut TestAppContext, .remote_participants()
) -> RoomParticipants { .iter()
let remote_users = room.update(cx, |room, cx| { .map(|(_, participant)| participant.user.github_login.clone())
room.remote_participants() .collect(),
.values() pending: room
.map(|participant| { .pending_users()
client .iter()
.user_store
.update(cx, |users, cx| users.get_user(participant.user_id, cx))
})
.collect::<Vec<_>>()
});
let remote_users = futures::future::try_join_all(remote_users).await.unwrap();
let pending_users = room.read_with(cx, |room, _| {
room.pending_users().iter().cloned().collect::<Vec<_>>()
});
RoomParticipants {
remote: remote_users
.into_iter()
.map(|user| user.github_login.clone()) .map(|user| user.github_login.clone())
.collect(), .collect(),
pending: pending_users })
.into_iter()
.map(|user| user.github_login.clone())
.collect(),
}
} }