Get basic calls working again with sea-orm

This commit is contained in:
Antonio Scandurra 2022-12-01 16:06:01 +01:00
parent aebc6326a9
commit 256e3e8e0f

View file

@ -987,32 +987,22 @@ impl Database {
initial_project_id: Option<ProjectId>, initial_project_id: Option<ProjectId>,
) -> Result<RoomGuard<(proto::Room, proto::IncomingCall)>> { ) -> Result<RoomGuard<(proto::Room, proto::IncomingCall)>> {
self.transact(|tx| async move { self.transact(|tx| async move {
todo!() room_participant::ActiveModel {
// sqlx::query( room_id: ActiveValue::set(room_id),
// " user_id: ActiveValue::set(called_user_id),
// INSERT INTO room_participants ( calling_user_id: ActiveValue::set(calling_user_id),
// room_id, calling_connection_id: ActiveValue::set(calling_connection_id.0 as i32),
// user_id, initial_project_id: ActiveValue::set(initial_project_id),
// calling_user_id, ..Default::default()
// calling_connection_id, }
// initial_project_id .insert(&tx)
// ) .await?;
// VALUES ($1, $2, $3, $4, $5)
// ",
// )
// .bind(room_id)
// .bind(called_user_id)
// .bind(calling_user_id)
// .bind(calling_connection_id.0 as i32)
// .bind(initial_project_id)
// .execute(&mut tx)
// .await?;
// let room = self.get_room(room_id, &mut tx).await?; let room = self.get_room(room_id, &tx).await?;
// let incoming_call = Self::build_incoming_call(&room, called_user_id) let incoming_call = Self::build_incoming_call(&room, called_user_id)
// .ok_or_else(|| anyhow!("failed to build incoming call"))?; .ok_or_else(|| anyhow!("failed to build incoming call"))?;
// self.commit_room_transaction(room_id, tx, (room, incoming_call)) self.commit_room_transaction(room_id, tx, (room, incoming_call))
// .await .await
}) })
.await .await
} }
@ -1023,20 +1013,16 @@ impl Database {
called_user_id: UserId, called_user_id: UserId,
) -> Result<RoomGuard<proto::Room>> { ) -> Result<RoomGuard<proto::Room>> {
self.transact(|tx| async move { self.transact(|tx| async move {
todo!() room_participant::Entity::delete_many()
// sqlx::query( .filter(
// " room_participant::Column::RoomId
// DELETE FROM room_participants .eq(room_id)
// WHERE room_id = $1 AND user_id = $2 .and(room_participant::Column::UserId.eq(called_user_id)),
// ", )
// ) .exec(&tx)
// .bind(room_id) .await?;
// .bind(called_user_id) let room = self.get_room(room_id, &tx).await?;
// .execute(&mut tx) self.commit_room_transaction(room_id, tx, room).await
// .await?;
// let room = self.get_room(room_id, &mut tx).await?;
// self.commit_room_transaction(room_id, tx, room).await
}) })
.await .await
} }
@ -1047,23 +1033,27 @@ impl Database {
user_id: UserId, user_id: UserId,
) -> Result<RoomGuard<proto::Room>> { ) -> Result<RoomGuard<proto::Room>> {
self.transact(|tx| async move { self.transact(|tx| async move {
todo!() let participant = room_participant::Entity::find()
// let room_id = sqlx::query_scalar( .filter(
// " room_participant::Column::UserId
// DELETE FROM room_participants .eq(user_id)
// WHERE user_id = $1 AND answering_connection_id IS NULL .and(room_participant::Column::AnsweringConnectionId.is_null()),
// RETURNING room_id )
// ", .one(&tx)
// ) .await?
// .bind(user_id) .ok_or_else(|| anyhow!("could not decline call"))?;
// .fetch_one(&mut tx) let room_id = participant.room_id;
// .await?;
// if expected_room_id.map_or(false, |expected_room_id| expected_room_id != room_id) {
// return Err(anyhow!("declining call on unexpected room"))?;
// }
// let room = self.get_room(room_id, &mut tx).await?; if expected_room_id.map_or(false, |expected_room_id| expected_room_id != room_id) {
// self.commit_room_transaction(room_id, tx, room).await return Err(anyhow!("declining call on unexpected room"))?;
}
room_participant::Entity::delete(participant.into_active_model())
.exec(&tx)
.await?;
let room = self.get_room(room_id, &tx).await?;
self.commit_room_transaction(room_id, tx, room).await
}) })
.await .await
} }
@ -1075,24 +1065,30 @@ impl Database {
called_user_id: UserId, called_user_id: UserId,
) -> Result<RoomGuard<proto::Room>> { ) -> Result<RoomGuard<proto::Room>> {
self.transact(|tx| async move { self.transact(|tx| async move {
todo!() let participant = room_participant::Entity::find()
// let room_id = sqlx::query_scalar( .filter(
// " room_participant::Column::UserId
// DELETE FROM room_participants .eq(called_user_id)
// WHERE user_id = $1 AND calling_connection_id = $2 AND answering_connection_id IS NULL .and(
// RETURNING room_id room_participant::Column::CallingConnectionId
// ", .eq(calling_connection_id.0 as i32),
// ) )
// .bind(called_user_id) .and(room_participant::Column::AnsweringConnectionId.is_null()),
// .bind(calling_connection_id.0 as i32) )
// .fetch_one(&mut tx) .one(&tx)
// .await?; .await?
// if expected_room_id.map_or(false, |expected_room_id| expected_room_id != room_id) { .ok_or_else(|| anyhow!("could not cancel call"))?;
// return Err(anyhow!("canceling call on unexpected room"))?; let room_id = participant.room_id;
// } if expected_room_id.map_or(false, |expected_room_id| expected_room_id != room_id) {
return Err(anyhow!("canceling call on unexpected room"))?;
}
// let room = self.get_room(room_id, &mut tx).await?; room_participant::Entity::delete(participant.into_active_model())
// self.commit_room_transaction(room_id, tx, room).await .exec(&tx)
.await?;
let room = self.get_room(room_id, &tx).await?;
self.commit_room_transaction(room_id, tx, room).await
}) })
.await .await
} }
@ -1104,23 +1100,25 @@ impl Database {
connection_id: ConnectionId, connection_id: ConnectionId,
) -> Result<RoomGuard<proto::Room>> { ) -> Result<RoomGuard<proto::Room>> {
self.transact(|tx| async move { self.transact(|tx| async move {
todo!() let result = room_participant::Entity::update_many()
// sqlx::query( .filter(
// " room_participant::Column::RoomId
// UPDATE room_participants .eq(room_id)
// SET answering_connection_id = $1 .and(room_participant::Column::UserId.eq(user_id))
// WHERE room_id = $2 AND user_id = $3 .and(room_participant::Column::AnsweringConnectionId.is_null()),
// RETURNING 1 )
// ", .col_expr(
// ) room_participant::Column::AnsweringConnectionId,
// .bind(connection_id.0 as i32) connection_id.0.into(),
// .bind(room_id) )
// .bind(user_id) .exec(&tx)
// .fetch_one(&mut tx) .await?;
// .await?; if result.rows_affected == 0 {
Err(anyhow!("room does not exist or was already joined"))?
// let room = self.get_room(room_id, &mut tx).await?; } else {
// self.commit_room_transaction(room_id, tx, room).await let room = self.get_room(room_id, &tx).await?;
self.commit_room_transaction(room_id, tx, room).await
}
}) })
.await .await
} }
@ -1130,124 +1128,117 @@ impl Database {
connection_id: ConnectionId, connection_id: ConnectionId,
) -> Result<Option<RoomGuard<LeftRoom>>> { ) -> Result<Option<RoomGuard<LeftRoom>>> {
self.transact(|tx| async move { self.transact(|tx| async move {
todo!() let leaving_participant = room_participant::Entity::find()
// // Leave room. .filter(room_participant::Column::AnsweringConnectionId.eq(connection_id.0))
// let room_id = sqlx::query_scalar::<_, RoomId>( .one(&tx)
// " .await?;
// DELETE FROM room_participants
// WHERE answering_connection_id = $1
// RETURNING room_id
// ",
// )
// .bind(connection_id.0 as i32)
// .fetch_optional(&mut tx)
// .await?;
// if let Some(room_id) = room_id { if let Some(leaving_participant) = leaving_participant {
// // Cancel pending calls initiated by the leaving user. // Leave room.
// let canceled_calls_to_user_ids: Vec<UserId> = sqlx::query_scalar( let room_id = leaving_participant.room_id;
// " room_participant::Entity::delete_by_id(leaving_participant.id)
// DELETE FROM room_participants .exec(&tx)
// WHERE calling_connection_id = $1 AND answering_connection_id IS NULL .await?;
// RETURNING user_id
// ",
// )
// .bind(connection_id.0 as i32)
// .fetch_all(&mut tx)
// .await?;
// let project_ids = sqlx::query_scalar::<_, ProjectId>( // Cancel pending calls initiated by the leaving user.
// " let called_participants = room_participant::Entity::find()
// SELECT project_id .filter(
// FROM project_collaborators room_participant::Column::CallingConnectionId
// WHERE connection_id = $1 .eq(connection_id.0)
// ", .and(room_participant::Column::AnsweringConnectionId.is_null()),
// ) )
// .bind(connection_id.0 as i32) .all(&tx)
// .fetch_all(&mut tx) .await?;
// .await?; room_participant::Entity::delete_many()
.filter(
room_participant::Column::Id
.is_in(called_participants.iter().map(|participant| participant.id)),
)
.exec(&tx)
.await?;
let canceled_calls_to_user_ids = called_participants
.into_iter()
.map(|participant| participant.user_id)
.collect();
// // Leave projects. // Detect left projects.
// let mut left_projects = HashMap::default(); #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
// if !project_ids.is_empty() { enum QueryProjectIds {
// let mut params = "?,".repeat(project_ids.len()); ProjectId,
// params.pop(); }
// let query = format!( let project_ids: Vec<ProjectId> = project_collaborator::Entity::find()
// " .select_only()
// SELECT * .column_as(
// FROM project_collaborators project_collaborator::Column::ProjectId,
// WHERE project_id IN ({params}) QueryProjectIds::ProjectId,
// " )
// ); .filter(project_collaborator::Column::ConnectionId.eq(connection_id.0))
// let mut query = sqlx::query_as::<_, ProjectCollaborator>(&query); .into_values::<_, QueryProjectIds>()
// for project_id in project_ids { .all(&tx)
// query = query.bind(project_id); .await?;
// } let mut left_projects = HashMap::default();
let mut collaborators = project_collaborator::Entity::find()
.filter(project_collaborator::Column::ProjectId.is_in(project_ids))
.stream(&tx)
.await?;
while let Some(collaborator) = collaborators.next().await {
let collaborator = collaborator?;
let left_project =
left_projects
.entry(collaborator.project_id)
.or_insert(LeftProject {
id: collaborator.project_id,
host_user_id: Default::default(),
connection_ids: Default::default(),
host_connection_id: Default::default(),
});
// let mut project_collaborators = query.fetch(&mut tx); let collaborator_connection_id =
// while let Some(collaborator) = project_collaborators.next().await { ConnectionId(collaborator.connection_id as u32);
// let collaborator = collaborator?; if collaborator_connection_id != connection_id {
// let left_project = left_project.connection_ids.push(collaborator_connection_id);
// left_projects }
// .entry(collaborator.project_id)
// .or_insert(LeftProject {
// id: collaborator.project_id,
// host_user_id: Default::default(),
// connection_ids: Default::default(),
// host_connection_id: Default::default(),
// });
// let collaborator_connection_id = if collaborator.is_host {
// ConnectionId(collaborator.connection_id as u32); left_project.host_user_id = collaborator.user_id;
// if collaborator_connection_id != connection_id { left_project.host_connection_id =
// left_project.connection_ids.push(collaborator_connection_id); ConnectionId(collaborator.connection_id as u32);
// } }
}
drop(collaborators);
// if collaborator.is_host { // Leave projects.
// left_project.host_user_id = collaborator.user_id; project_collaborator::Entity::delete_many()
// left_project.host_connection_id = .filter(project_collaborator::Column::ConnectionId.eq(connection_id.0))
// ConnectionId(collaborator.connection_id as u32); .exec(&tx)
// } .await?;
// }
// }
// sqlx::query(
// "
// DELETE FROM project_collaborators
// WHERE connection_id = $1
// ",
// )
// .bind(connection_id.0 as i32)
// .execute(&mut tx)
// .await?;
// // Unshare projects. // Unshare projects.
// sqlx::query( project::Entity::delete_many()
// " .filter(
// DELETE FROM projects project::Column::RoomId
// WHERE room_id = $1 AND host_connection_id = $2 .eq(room_id)
// ", .and(project::Column::HostConnectionId.eq(connection_id.0)),
// ) )
// .bind(room_id) .exec(&tx)
// .bind(connection_id.0 as i32) .await?;
// .execute(&mut tx)
// .await?;
// let room = self.get_room(room_id, &mut tx).await?; let room = self.get_room(room_id, &tx).await?;
// Ok(Some( Ok(Some(
// self.commit_room_transaction( self.commit_room_transaction(
// room_id, room_id,
// tx, tx,
// LeftRoom { LeftRoom {
// room, room,
// left_projects, left_projects,
// canceled_calls_to_user_ids, canceled_calls_to_user_ids,
// }, },
// ) )
// .await?, .await?,
// )) ))
// } else { } else {
// Ok(None) Ok(None)
// } }
}) })
.await .await
} }
@ -1259,46 +1250,48 @@ impl Database {
location: proto::ParticipantLocation, location: proto::ParticipantLocation,
) -> Result<RoomGuard<proto::Room>> { ) -> Result<RoomGuard<proto::Room>> {
self.transact(|tx| async { self.transact(|tx| async {
todo!() let mut tx = tx;
// let mut tx = tx; let location_kind;
// let location_kind; let location_project_id;
// let location_project_id; match location
// match location .variant
// .variant .as_ref()
// .as_ref() .ok_or_else(|| anyhow!("invalid location"))?
// .ok_or_else(|| anyhow!("invalid location"))? {
// { proto::participant_location::Variant::SharedProject(project) => {
// proto::participant_location::Variant::SharedProject(project) => { location_kind = 0;
// location_kind = 0; location_project_id = Some(ProjectId::from_proto(project.id));
// location_project_id = Some(ProjectId::from_proto(project.id)); }
// } proto::participant_location::Variant::UnsharedProject(_) => {
// proto::participant_location::Variant::UnsharedProject(_) => { location_kind = 1;
// location_kind = 1; location_project_id = None;
// location_project_id = None; }
// } proto::participant_location::Variant::External(_) => {
// proto::participant_location::Variant::External(_) => { location_kind = 2;
// location_kind = 2; location_project_id = None;
// location_project_id = None; }
// } }
// }
// sqlx::query( let result = room_participant::Entity::update_many()
// " .filter(
// UPDATE room_participants room_participant::Column::RoomId
// SET location_kind = $1, location_project_id = $2 .eq(room_id)
// WHERE room_id = $3 AND answering_connection_id = $4 .and(room_participant::Column::AnsweringConnectionId.eq(connection_id.0)),
// RETURNING 1 )
// ", .set(room_participant::ActiveModel {
// ) location_kind: ActiveValue::set(Some(location_kind)),
// .bind(location_kind) location_project_id: ActiveValue::set(location_project_id),
// .bind(location_project_id) ..Default::default()
// .bind(room_id) })
// .bind(connection_id.0 as i32) .exec(&tx)
// .fetch_one(&mut tx) .await?;
// .await?;
// let room = self.get_room(room_id, &mut tx).await?; if result.rows_affected == 1 {
// self.commit_room_transaction(room_id, tx, room).await let room = self.get_room(room_id, &mut tx).await?;
self.commit_room_transaction(room_id, tx, room).await
} else {
Err(anyhow!("could not update room participant location"))?
}
}) })
.await .await
} }