Add room creation from channel join

co-authored-by: max <max@zed.dev>
This commit is contained in:
Mikayla Maki 2023-07-31 16:48:24 -07:00
parent 92fa879b0c
commit 003a711dea
No known key found for this signature in database
7 changed files with 229 additions and 181 deletions

View file

@ -1833,14 +1833,21 @@ impl Database {
.await?;
let room = self.get_room(room_id, &tx).await?;
if room.participants.is_empty() {
room::Entity::delete_by_id(room_id).exec(&*tx).await?;
}
let deleted = if room.participants.is_empty() {
let result = room::Entity::delete_by_id(room_id)
.filter(room::Column::ChannelId.is_null())
.exec(&*tx)
.await?;
result.rows_affected > 0
} else {
false
};
let left_room = LeftRoom {
room,
left_projects,
canceled_calls_to_user_ids,
deleted,
};
if left_room.room.participants.is_empty() {
@ -3065,14 +3072,21 @@ impl Database {
// channels
pub async fn create_root_channel(&self, name: &str, creator_id: UserId) -> Result<ChannelId> {
self.create_channel(name, None, creator_id).await
pub async fn create_root_channel(
&self,
name: &str,
live_kit_room: &str,
creator_id: UserId,
) -> Result<ChannelId> {
self.create_channel(name, None, live_kit_room, creator_id)
.await
}
pub async fn create_channel(
&self,
name: &str,
parent: Option<ChannelId>,
live_kit_room: &str,
creator_id: UserId,
) -> Result<ChannelId> {
self.transaction(move |tx| async move {
@ -3106,7 +3120,7 @@ impl Database {
room::ActiveModel {
channel_id: ActiveValue::Set(Some(channel.id)),
live_kit_room: ActiveValue::Set(format!("channel-{}", channel.id)),
live_kit_room: ActiveValue::Set(live_kit_room.to_string()),
..Default::default()
}
.insert(&*tx)
@ -3731,6 +3745,7 @@ pub struct LeftRoom {
pub room: proto::Room,
pub left_projects: HashMap<ProjectId, LeftProject>,
pub canceled_calls_to_user_ids: Vec<UserId>,
pub deleted: bool,
}
pub struct RefreshedRoom {

View file

@ -899,19 +899,22 @@ test_both_dbs!(test_channels_postgres, test_channels_sqlite, db, {
.unwrap()
.user_id;
let zed_id = db.create_root_channel("zed", a_id).await.unwrap();
let crdb_id = db.create_channel("crdb", Some(zed_id), a_id).await.unwrap();
let zed_id = db.create_root_channel("zed", "1", a_id).await.unwrap();
let crdb_id = db
.create_channel("crdb", Some(zed_id), "2", a_id)
.await
.unwrap();
let livestreaming_id = db
.create_channel("livestreaming", Some(zed_id), a_id)
.create_channel("livestreaming", Some(zed_id), "3", a_id)
.await
.unwrap();
let replace_id = db
.create_channel("replace", Some(zed_id), a_id)
.create_channel("replace", Some(zed_id), "4", a_id)
.await
.unwrap();
let rust_id = db.create_root_channel("rust", a_id).await.unwrap();
let rust_id = db.create_root_channel("rust", "5", a_id).await.unwrap();
let cargo_id = db
.create_channel("cargo", Some(rust_id), a_id)
.create_channel("cargo", Some(rust_id), "6", a_id)
.await
.unwrap();
@ -988,7 +991,10 @@ test_both_dbs!(
.unwrap()
.user_id;
let channel_1 = db.create_root_channel("channel_1", user_1).await.unwrap();
let channel_1 = db
.create_root_channel("channel_1", "1", user_1)
.await
.unwrap();
let room_1 = db.get_channel_room(channel_1).await.unwrap();
// can join a room with membership to its channel

View file

@ -186,7 +186,7 @@ impl Server {
server
.add_request_handler(ping)
.add_request_handler(create_room_request)
.add_request_handler(create_room)
.add_request_handler(join_room)
.add_request_handler(rejoin_room)
.add_request_handler(leave_room)
@ -859,12 +859,42 @@ async fn ping(_: proto::Ping, response: Response<proto::Ping>, _session: Session
Ok(())
}
async fn create_room_request(
async fn create_room(
_request: proto::CreateRoom,
response: Response<proto::CreateRoom>,
session: Session,
) -> Result<()> {
let (room, live_kit_connection_info) = create_room(&session).await?;
let live_kit_room = nanoid::nanoid!(30);
let live_kit_connection_info = {
let live_kit_room = live_kit_room.clone();
let live_kit = session.live_kit_client.as_ref();
util::async_iife!({
let live_kit = live_kit?;
live_kit
.create_room(live_kit_room.clone())
.await
.trace_err()?;
let token = live_kit
.room_token(&live_kit_room, &session.user_id.to_string())
.trace_err()?;
Some(proto::LiveKitConnectionInfo {
server_url: live_kit.url().into(),
token,
})
})
}
.await;
let room = session
.db()
.await
.create_room(session.user_id, session.connection_id, &live_kit_room)
.await?;
response.send(proto::CreateRoomResponse {
room: Some(room.clone()),
@ -1259,11 +1289,12 @@ async fn update_participant_location(
let location = request
.location
.ok_or_else(|| anyhow!("invalid location"))?;
let room = session
.db()
.await
let db = session.db().await;
let room = db
.update_room_participant_location(room_id, session.connection_id, location)
.await?;
room_updated(&room, &session.peer);
response.send(proto::Ack {})?;
Ok(())
@ -2067,10 +2098,17 @@ async fn create_channel(
session: Session,
) -> Result<()> {
let db = session.db().await;
let live_kit_room = format!("channel-{}", nanoid::nanoid!(30));
if let Some(live_kit) = session.live_kit_client.as_ref() {
live_kit.create_room(live_kit_room.clone()).await?;
}
let id = db
.create_channel(
&request.name,
request.parent_id.map(|id| ChannelId::from_proto(id)),
&live_kit_room,
session.user_id,
)
.await?;
@ -2160,21 +2198,39 @@ async fn join_channel(
response: Response<proto::JoinChannel>,
session: Session,
) -> Result<()> {
let db = session.db().await;
let channel_id = ChannelId::from_proto(request.channel_id);
todo!();
// db.check_channel_membership(session.user_id, channel_id)
// .await?;
{
let db = session.db().await;
let room_id = db.get_channel_room(channel_id).await?;
let (room, live_kit_connection_info) = create_room(&session).await?;
let room = db
.join_room(
room_id,
session.user_id,
Some(channel_id),
session.connection_id,
)
.await?;
// db.set_channel_room(channel_id, room.id).await?;
let live_kit_connection_info = session.live_kit_client.as_ref().and_then(|live_kit| {
let token = live_kit
.room_token(&room.live_kit_room, &session.user_id.to_string())
.trace_err()?;
response.send(proto::CreateRoomResponse {
room: Some(room.clone()),
live_kit_connection_info,
})?;
Some(LiveKitConnectionInfo {
server_url: live_kit.url().into(),
token,
})
});
response.send(proto::JoinRoomResponse {
room: Some(room.clone()),
live_kit_connection_info,
})?;
room_updated(&room, &session.peer);
}
update_user_contacts(session.user_id, &session).await?;
@ -2367,7 +2423,7 @@ async fn leave_room_for_session(session: &Session) -> Result<()> {
room_id = RoomId::from_proto(left_room.room.id);
canceled_calls_to_user_ids = mem::take(&mut left_room.canceled_calls_to_user_ids);
live_kit_room = mem::take(&mut left_room.room.live_kit_room);
delete_live_kit_room = left_room.room.participants.is_empty();
delete_live_kit_room = left_room.deleted;
} else {
return Ok(());
}
@ -2435,42 +2491,6 @@ fn project_left(project: &db::LeftProject, session: &Session) {
}
}
async fn create_room(session: &Session) -> Result<(proto::Room, Option<LiveKitConnectionInfo>)> {
let live_kit_room = nanoid::nanoid!(30);
let live_kit_connection_info = {
let live_kit_room = live_kit_room.clone();
let live_kit = session.live_kit_client.as_ref();
util::async_iife!({
let live_kit = live_kit?;
live_kit
.create_room(live_kit_room.clone())
.await
.trace_err()?;
let token = live_kit
.room_token(&live_kit_room, &session.user_id.to_string())
.trace_err()?;
Some(proto::LiveKitConnectionInfo {
server_url: live_kit.url().into(),
token,
})
})
}
.await;
let room = session
.db()
.await
.create_room(session.user_id, session.connection_id, &live_kit_room)
.await?;
Ok((room, live_kit_connection_info))
}
pub trait ResultExt {
type Ok;

View file

@ -108,17 +108,52 @@ async fn test_channel_room(
.await
.unwrap();
active_call_b
.update(cx_b, |active_call, cx| active_call.join_channel(zed_id, cx))
.await
.unwrap();
deterministic.run_until_parked();
let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
room_a.read_with(cx_a, |room, _| assert!(room.is_connected()));
assert_eq!(
room_participants(&room_a, cx_a),
RoomParticipants {
remote: vec!["user_b".to_string()],
pending: vec![]
}
);
let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
room_b.read_with(cx_b, |room, _| assert!(room.is_connected()));
assert_eq!(
room_participants(&room_b, cx_b),
RoomParticipants {
remote: vec!["user_a".to_string()],
pending: vec![]
}
);
// Make sure that leaving and rejoining works
active_call_a
.update(cx_a, |active_call, cx| active_call.hang_up(cx))
.await
.unwrap();
active_call_b
.update(cx_b, |active_call, cx| active_call.hang_up(cx))
.await
.unwrap();
// Make sure room exists?
active_call_a
.update(cx_a, |active_call, cx| active_call.join_channel(zed_id, cx))
.await
.unwrap();
active_call_b
.update(cx_b, |active_call, cx| active_call.join_channel(zed_id, cx))
.await
@ -126,42 +161,23 @@ async fn test_channel_room(
deterministic.run_until_parked();
let active_call_b = cx_b.read(ActiveCall::global);
let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
room_a.read_with(cx_a, |room, _| assert!(room.is_connected()));
assert_eq!(
room_participants(&room_a, cx_a),
RoomParticipants {
remote: vec!["user_b".to_string()],
pending: vec![]
}
);
let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
room_b.read_with(cx_b, |room, _| assert!(room.is_connected()));
assert_eq!(
room_participants(&room_b, cx_b),
RoomParticipants {
remote: vec!["user_a".to_string(), "user_b".to_string()],
remote: vec!["user_a".to_string()],
pending: vec![]
}
);
}
// TODO:
// Invariants to test:
// 1. Dag structure is maintained for all operations (can't make a cycle)
// 2. Can't be a member of a super channel, and accept a membership of a sub channel (by definition, a noop)
// #[gpui::test]
// async fn test_block_cycle_creation(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
// // deterministic.forbid_parking();
// // let mut server = TestServer::start(&deterministic).await;
// // let client_a = server.create_client(cx, "user_a").await;
// // let a_id = crate::db::UserId(client_a.user_id().unwrap() as i32);
// // let db = server._test_db.db();
// // let zed_id = db.create_root_channel("zed", a_id).await.unwrap();
// // let first_id = db.create_channel("first", Some(zed_id)).await.unwrap();
// // let second_id = db
// // .create_channel("second_id", Some(first_id))
// // .await
// // .unwrap();
// }
/*
Linear things:
- A way of expressing progress to the team
- A way for us to agree on a scope
- A way to figure out what we're supposed to be doing
*/