Make project id optional when following - server only
This commit is contained in:
parent
83455028b0
commit
c71566e7f5
10 changed files with 204 additions and 55 deletions
|
@ -738,7 +738,7 @@ impl Database {
|
|||
Condition::any()
|
||||
.add(
|
||||
Condition::all()
|
||||
.add(follower::Column::ProjectId.eq(project_id))
|
||||
.add(follower::Column::ProjectId.eq(Some(project_id)))
|
||||
.add(
|
||||
follower::Column::LeaderConnectionServerId
|
||||
.eq(connection.owner_id),
|
||||
|
@ -747,7 +747,7 @@ impl Database {
|
|||
)
|
||||
.add(
|
||||
Condition::all()
|
||||
.add(follower::Column::ProjectId.eq(project_id))
|
||||
.add(follower::Column::ProjectId.eq(Some(project_id)))
|
||||
.add(
|
||||
follower::Column::FollowerConnectionServerId
|
||||
.eq(connection.owner_id),
|
||||
|
@ -862,13 +862,95 @@ impl Database {
|
|||
.await
|
||||
}
|
||||
|
||||
pub async fn check_can_follow(
|
||||
&self,
|
||||
room_id: RoomId,
|
||||
project_id: Option<ProjectId>,
|
||||
leader_id: ConnectionId,
|
||||
follower_id: ConnectionId,
|
||||
) -> Result<()> {
|
||||
let mut found_leader = false;
|
||||
let mut found_follower = false;
|
||||
self.transaction(|tx| async move {
|
||||
if let Some(project_id) = project_id {
|
||||
let mut rows = project_collaborator::Entity::find()
|
||||
.filter(project_collaborator::Column::ProjectId.eq(project_id))
|
||||
.stream(&*tx)
|
||||
.await?;
|
||||
while let Some(row) = rows.next().await {
|
||||
let row = row?;
|
||||
let connection = row.connection();
|
||||
if connection == leader_id {
|
||||
found_leader = true;
|
||||
} else if connection == follower_id {
|
||||
found_follower = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let mut rows = room_participant::Entity::find()
|
||||
.filter(room_participant::Column::RoomId.eq(room_id))
|
||||
.stream(&*tx)
|
||||
.await?;
|
||||
while let Some(row) = rows.next().await {
|
||||
let row = row?;
|
||||
if let Some(connection) = row.answering_connection() {
|
||||
if connection == leader_id {
|
||||
found_leader = true;
|
||||
} else if connection == follower_id {
|
||||
found_follower = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !found_leader || !found_follower {
|
||||
Err(anyhow!("not a room participant"))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn check_can_unfollow(
|
||||
&self,
|
||||
room_id: RoomId,
|
||||
project_id: Option<ProjectId>,
|
||||
leader_id: ConnectionId,
|
||||
follower_id: ConnectionId,
|
||||
) -> Result<()> {
|
||||
self.transaction(|tx| async move {
|
||||
follower::Entity::find()
|
||||
.filter(
|
||||
Condition::all()
|
||||
.add(follower::Column::RoomId.eq(room_id))
|
||||
.add(follower::Column::ProjectId.eq(project_id))
|
||||
.add(follower::Column::LeaderConnectionId.eq(leader_id.id as i32))
|
||||
.add(follower::Column::FollowerConnectionId.eq(follower_id.id as i32))
|
||||
.add(
|
||||
follower::Column::LeaderConnectionServerId
|
||||
.eq(leader_id.owner_id as i32),
|
||||
)
|
||||
.add(
|
||||
follower::Column::FollowerConnectionServerId
|
||||
.eq(follower_id.owner_id as i32),
|
||||
),
|
||||
)
|
||||
.one(&*tx)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow!("not a follower"))?;
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn follow(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
room_id: RoomId,
|
||||
project_id: Option<ProjectId>,
|
||||
leader_connection: ConnectionId,
|
||||
follower_connection: ConnectionId,
|
||||
) -> Result<RoomGuard<proto::Room>> {
|
||||
let room_id = self.room_id_for_project(project_id).await?;
|
||||
self.room_transaction(room_id, |tx| async move {
|
||||
follower::ActiveModel {
|
||||
room_id: ActiveValue::set(room_id),
|
||||
|
@ -894,15 +976,16 @@ impl Database {
|
|||
|
||||
pub async fn unfollow(
|
||||
&self,
|
||||
project_id: ProjectId,
|
||||
room_id: RoomId,
|
||||
project_id: Option<ProjectId>,
|
||||
leader_connection: ConnectionId,
|
||||
follower_connection: ConnectionId,
|
||||
) -> Result<RoomGuard<proto::Room>> {
|
||||
let room_id = self.room_id_for_project(project_id).await?;
|
||||
self.room_transaction(room_id, |tx| async move {
|
||||
follower::Entity::delete_many()
|
||||
.filter(
|
||||
Condition::all()
|
||||
.add(follower::Column::RoomId.eq(room_id))
|
||||
.add(follower::Column::ProjectId.eq(project_id))
|
||||
.add(
|
||||
follower::Column::LeaderConnectionServerId
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue