From 944d6554deb85dcb8ab14d1a05d4b0f77b707230 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 1 Dec 2022 16:26:13 +0100 Subject: [PATCH] Implement `Database::unshare_project` --- crates/collab/src/db.rs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/crates/collab/src/db.rs b/crates/collab/src/db.rs index 96ca4e9530..fc377ff7ac 100644 --- a/crates/collab/src/db.rs +++ b/crates/collab/src/db.rs @@ -1527,22 +1527,23 @@ impl Database { connection_id: ConnectionId, ) -> Result)>> { self.transact(|tx| async move { - todo!() - // let guest_connection_ids = self.get_guest_connection_ids(project_id, &mut tx).await?; - // let room_id: RoomId = sqlx::query_scalar( - // " - // DELETE FROM projects - // WHERE id = $1 AND host_connection_id = $2 - // RETURNING room_id - // ", - // ) - // .bind(project_id) - // .bind(connection_id.0 as i32) - // .fetch_one(&mut tx) - // .await?; - // let room = self.get_room(room_id, &mut tx).await?; - // self.commit_room_transaction(room_id, tx, (room, guest_connection_ids)) - // .await + let guest_connection_ids = self.project_guest_connection_ids(project_id, &tx).await?; + + let project = project::Entity::find_by_id(project_id) + .one(&tx) + .await? + .ok_or_else(|| anyhow!("project not found"))?; + if project.host_connection_id == connection_id.0 as i32 { + let room_id = project.room_id; + project::Entity::delete(project.into_active_model()) + .exec(&tx) + .await?; + let room = self.get_room(room_id, &tx).await?; + self.commit_room_transaction(room_id, tx, (room, guest_connection_ids)) + .await + } else { + Err(anyhow!("cannot unshare a project hosted by another user"))? + } }) .await }