Move `Store::decline_call` to `Db::decline_call`

Antonio Scandurra created

Change summary

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(-)

Detailed changes

crates/collab/src/db.rs 🔗

@@ -1027,6 +1027,24 @@ where
         })
     }
 
+    pub async fn decline_call(&self, room_id: RoomId, user_id: UserId) -> Result<proto::Room> {
+        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,

crates/collab/src/rpc.rs 🔗

@@ -800,19 +800,25 @@ impl Server {
     }
 
     async fn decline_call(self: Arc<Server>, message: Message<proto::DeclineCall>) -> 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(())
     }
 

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<ConnectionId>)> {
-        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::<Vec<_>>();
-        //     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,