From 0d1d267213b7494730dd9ae6abbdbb00e2bed34d Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 11 Nov 2022 15:41:56 +0100 Subject: [PATCH] Move `Store::decline_call` to `Db::decline_call` --- crates/collab/src/db.rs | 18 ++++++++++++++++ crates/collab/src/rpc.rs | 28 +++++++++++++++---------- crates/collab/src/rpc/store.rs | 38 +++------------------------------- 3 files changed, 38 insertions(+), 46 deletions(-) diff --git a/crates/collab/src/db.rs b/crates/collab/src/db.rs index a98621d8942f54e0a92848d6c2bc8080ed998356..10f1dd04424ba7ed1c0fa07e685c285f8769b1eb 100644 --- a/crates/collab/src/db.rs +++ b/crates/collab/src/db.rs @@ -1027,6 +1027,24 @@ where }) } + pub async fn decline_call(&self, room_id: RoomId, user_id: UserId) -> Result { + test_support!(self, { + let mut tx = self.pool.begin().await?; + sqlx::query( + " + DELETE FROM room_participants + WHERE room_id = $1 AND user_id = $2 AND connection_id IS NULL + ", + ) + .bind(room_id) + .bind(user_id) + .execute(&mut tx) + .await?; + + self.commit_room_transaction(room_id, tx).await + }) + } + pub async fn join_room( &self, room_id: RoomId, diff --git a/crates/collab/src/rpc.rs b/crates/collab/src/rpc.rs index c7c222ee1c30511f6bf296e62b502d5025292b2f..652ac5917b0e80b7745b8fd17673db8e4dcc1753 100644 --- a/crates/collab/src/rpc.rs +++ b/crates/collab/src/rpc.rs @@ -800,19 +800,25 @@ impl Server { } async fn decline_call(self: Arc, message: Message) -> Result<()> { - let recipient_user_id = message.sender_user_id; + let room = self + .app_state + .db + .decline_call( + RoomId::from_proto(message.payload.room_id), + message.sender_user_id, + ) + .await?; + for recipient_id in self + .store() + .await + .connection_ids_for_user(message.sender_user_id) { - let mut store = self.store().await; - let (room, recipient_connection_ids) = - store.decline_call(message.payload.room_id, message.sender_connection_id)?; - for recipient_id in recipient_connection_ids { - self.peer - .send(recipient_id, proto::CallCanceled {}) - .trace_err(); - } - self.room_updated(room); + self.peer + .send(recipient_id, proto::CallCanceled {}) + .trace_err(); } - self.update_user_contacts(recipient_user_id).await?; + self.room_updated(&room); + self.update_user_contacts(message.sender_user_id).await?; Ok(()) } diff --git a/crates/collab/src/rpc/store.rs b/crates/collab/src/rpc/store.rs index 610a653dc9841fdca10f672789aafb3735558f99..d64464f601be06f89ac583e3edd26e08716ac806 100644 --- a/crates/collab/src/rpc/store.rs +++ b/crates/collab/src/rpc/store.rs @@ -162,8 +162,9 @@ impl Store { result.room = Some(Cow::Owned(left_room.room.into_owned())); result.canceled_call_connection_ids = left_room.canceled_call_connection_ids; } else if connected_user.connection_ids.len() == 1 { - let (room, _) = self.decline_call(room_id, connection_id)?; - result.room = Some(Cow::Owned(room.clone())); + todo!() + // let (room, _) = self.decline_call(room_id, connection_id)?; + // result.room = Some(Cow::Owned(room.clone())); } } @@ -390,39 +391,6 @@ impl Store { // Ok((room, recipient.connection_ids.clone())) } - pub fn decline_call( - &mut self, - room_id: RoomId, - recipient_connection_id: ConnectionId, - ) -> Result<(&proto::Room, Vec)> { - todo!() - // let called_user_id = self.user_id_for_connection(recipient_connection_id)?; - // let recipient = self - // .connected_users - // .get_mut(&called_user_id) - // .ok_or_else(|| anyhow!("no such connection"))?; - // if let Some(active_call) = recipient.active_call { - // anyhow::ensure!(active_call.room_id == room_id, "no such room"); - // anyhow::ensure!( - // active_call.connection_id.is_none(), - // "cannot decline a call after joining room" - // ); - // recipient.active_call.take(); - // let recipient_connection_ids = self - // .connection_ids_for_user(called_user_id) - // .collect::>(); - // let room = self - // .rooms - // .get_mut(&active_call.room_id) - // .ok_or_else(|| anyhow!("no such room"))?; - // room.pending_participant_user_ids - // .retain(|user_id| UserId::from_proto(*user_id) != called_user_id); - // Ok((room, recipient_connection_ids)) - // } else { - // Err(anyhow!("user is not being called")) - // } - } - pub fn unshare_project( &mut self, project_id: ProjectId,