Introduce the ability of creating rooms on the server

This commit is contained in:
Antonio Scandurra 2022-09-23 15:05:32 +02:00
parent 0b1e372d11
commit ebb5ffcedc
12 changed files with 302 additions and 128 deletions

View file

@ -34,6 +34,7 @@ use project::{
DiagnosticSummary, Project, ProjectPath, ProjectStore, WorktreeId,
};
use rand::prelude::*;
use room::Room;
use rpc::PeerId;
use serde_json::json;
use settings::{Formatter, Settings};
@ -90,14 +91,24 @@ async fn test_share_project_in_room(
)
.await;
let room_a = Room::create(client_a.clone()).await.unwrap();
let room_a = cx_a
.update(|cx| Room::create(client_a.clone(), cx))
.await
.unwrap();
let (project_a, worktree_id) = client_a.build_local_project("/a", cx_a).await;
// room.publish_project(project_a.clone()).await.unwrap();
let incoming_calls_b = client_b.user_store.incoming_calls();
let user_b_joined = room_a.invite(client_b.user_id().unwrap());
let mut incoming_calls_b = client_b
.user_store
.read_with(cx_b, |user, _| user.incoming_calls());
let user_b_joined = room_a.update(cx_a, |room, cx| {
room.invite(client_b.user_id().unwrap(), cx)
});
let call_b = incoming_calls_b.next().await.unwrap();
let room_b = Room::join(call_b.room_id, client_b.clone()).await.unwrap();
let room_b = cx_b
.update(|cx| Room::join(call_b.room_id, client_b.clone(), cx))
.await
.unwrap();
user_b_joined.await.unwrap();
}

View file

@ -151,6 +151,7 @@ impl Server {
server
.add_request_handler(Server::ping)
.add_request_handler(Server::create_room)
.add_request_handler(Server::register_project)
.add_request_handler(Server::unregister_project)
.add_request_handler(Server::join_project)
@ -593,6 +594,16 @@ impl Server {
Ok(())
}
async fn create_room(
self: Arc<Server>,
request: TypedEnvelope<proto::CreateRoom>,
response: Response<proto::CreateRoom>,
) -> Result<()> {
let room_id = self.store().await.create_room(request.sender_id)?;
response.send(proto::CreateRoomResponse { id: room_id })?;
Ok(())
}
async fn register_project(
self: Arc<Server>,
request: TypedEnvelope<proto::RegisterProject>,

View file

@ -6,6 +6,7 @@ use serde::Serialize;
use std::{mem, path::PathBuf, str, time::Duration};
use time::OffsetDateTime;
use tracing::instrument;
use util::post_inc;
pub type RoomId = u64;
@ -13,7 +14,8 @@ pub type RoomId = u64;
pub struct Store {
connections: BTreeMap<ConnectionId, ConnectionState>,
connections_by_user_id: BTreeMap<UserId, HashSet<ConnectionId>>,
rooms: BTreeMap<RoomId, Room>,
next_room_id: RoomId,
rooms: BTreeMap<RoomId, proto::Room>,
projects: BTreeMap<ProjectId, Project>,
#[serde(skip)]
channels: BTreeMap<ChannelId, Channel>,
@ -23,22 +25,12 @@ pub struct Store {
struct ConnectionState {
user_id: UserId,
admin: bool,
rooms: BTreeSet<RoomId>,
projects: BTreeSet<ProjectId>,
requested_projects: HashSet<ProjectId>,
channels: HashSet<ChannelId>,
}
#[derive(Serialize)]
struct Room {
participants: HashMap<ConnectionId, Participant>,
}
#[derive(Serialize)]
struct Participant {
user_id: UserId,
shared_projects: HashSet<ProjectId>,
}
#[derive(Serialize)]
pub struct Project {
pub online: bool,
@ -148,6 +140,7 @@ impl Store {
ConnectionState {
user_id,
admin,
rooms: Default::default(),
projects: Default::default(),
requested_projects: Default::default(),
channels: Default::default(),
@ -335,6 +328,29 @@ impl Store {
metadata
}
pub fn create_room(&mut self, creator_connection_id: ConnectionId) -> Result<RoomId> {
let connection = self
.connections
.get_mut(&creator_connection_id)
.ok_or_else(|| anyhow!("no such connection"))?;
let mut room = proto::Room::default();
room.participants.push(proto::Participant {
user_id: connection.user_id.to_proto(),
peer_id: creator_connection_id.0,
project_ids: Default::default(),
location: Some(proto::ParticipantLocation {
variant: Some(proto::participant_location::Variant::External(
proto::participant_location::External {},
)),
}),
});
let room_id = post_inc(&mut self.next_room_id);
self.rooms.insert(room_id, room);
connection.rooms.insert(room_id);
Ok(room_id)
}
pub fn register_project(
&mut self,
host_connection_id: ConnectionId,