Introduce the ability of creating rooms on the server
This commit is contained in:
@@ -1,19 +1,31 @@
|
||||
use client::User;
|
||||
use anyhow::{anyhow, Result};
|
||||
use client::proto;
|
||||
use gpui::ModelHandle;
|
||||
use project::Project;
|
||||
|
||||
pub enum Location {
|
||||
Project { project_id: usize },
|
||||
pub enum ParticipantLocation {
|
||||
Project { project_id: u64 },
|
||||
External,
|
||||
}
|
||||
|
||||
impl ParticipantLocation {
|
||||
pub fn from_proto(location: Option<proto::ParticipantLocation>) -> Result<Self> {
|
||||
match location.and_then(|l| l.variant) {
|
||||
Some(proto::participant_location::Variant::Project(project)) => Ok(Self::Project {
|
||||
project_id: project.id,
|
||||
}),
|
||||
Some(proto::participant_location::Variant::External(_)) => Ok(Self::External),
|
||||
None => Err(anyhow!("participant location was not provided")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LocalParticipant {
|
||||
user: User,
|
||||
projects: Vec<ModelHandle<Project>>,
|
||||
pub projects: Vec<ModelHandle<Project>>,
|
||||
}
|
||||
|
||||
pub struct RemoteParticipant {
|
||||
user: User,
|
||||
projects: Vec<ModelHandle<Project>>,
|
||||
location: Location,
|
||||
pub user_id: u64,
|
||||
pub projects: Vec<ModelHandle<Project>>,
|
||||
pub location: ParticipantLocation,
|
||||
}
|
||||
|
||||
+52
-10
@@ -1,11 +1,12 @@
|
||||
mod participant;
|
||||
|
||||
use anyhow::Result;
|
||||
use client::{Client, PeerId};
|
||||
use gpui::{Entity, ModelHandle};
|
||||
use participant::{LocalParticipant, RemoteParticipant};
|
||||
use anyhow::{anyhow, Result};
|
||||
use client::{proto, Client, PeerId};
|
||||
use collections::HashMap;
|
||||
use gpui::{Entity, ModelContext, ModelHandle, MutableAppContext, Task};
|
||||
use participant::{LocalParticipant, ParticipantLocation, RemoteParticipant};
|
||||
use project::Project;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub enum Event {
|
||||
PeerChangedActiveProject,
|
||||
@@ -23,15 +24,56 @@ impl Entity for Room {
|
||||
}
|
||||
|
||||
impl Room {
|
||||
pub async fn create(client: Arc<Client>) -> Result<u64> {
|
||||
todo!()
|
||||
pub fn create(
|
||||
client: Arc<Client>,
|
||||
cx: &mut MutableAppContext,
|
||||
) -> Task<Result<ModelHandle<Self>>> {
|
||||
cx.spawn(|mut cx| async move {
|
||||
let room = client.request(proto::CreateRoom {}).await?;
|
||||
Ok(cx.add_model(|cx| Self::new(room.id, client, cx)))
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn join(id: u64, client: Arc<Client>) -> Result<Self> {
|
||||
todo!()
|
||||
pub fn join(
|
||||
id: u64,
|
||||
client: Arc<Client>,
|
||||
cx: &mut MutableAppContext,
|
||||
) -> Task<Result<ModelHandle<Self>>> {
|
||||
cx.spawn(|mut cx| async move {
|
||||
let response = client.request(proto::JoinRoom { id }).await?;
|
||||
let room_proto = response.room.ok_or_else(|| anyhow!("invalid room"))?;
|
||||
let room = cx.add_model(|cx| Self::new(id, client, cx));
|
||||
room.update(&mut cx, |room, cx| room.refresh(room_proto, cx))?;
|
||||
Ok(room)
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn invite(&mut self, user_id: u64) -> Result<()> {
|
||||
fn new(id: u64, client: Arc<Client>, _: &mut ModelContext<Self>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
local_participant: LocalParticipant {
|
||||
projects: Default::default(),
|
||||
},
|
||||
remote_participants: Default::default(),
|
||||
client,
|
||||
}
|
||||
}
|
||||
|
||||
fn refresh(&mut self, room: proto::Room, cx: &mut ModelContext<Self>) -> Result<()> {
|
||||
for participant in room.participants {
|
||||
self.remote_participants.insert(
|
||||
PeerId(participant.peer_id),
|
||||
RemoteParticipant {
|
||||
user_id: participant.user_id,
|
||||
projects: Default::default(), // TODO: populate projects
|
||||
location: ParticipantLocation::from_proto(participant.location)?,
|
||||
},
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn invite(&mut self, user_id: u64, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user