rooms.rs

   1use anyhow::Context as _;
   2
   3use super::*;
   4
   5impl Database {
   6    /// Clears all room participants in rooms attached to a stale server.
   7    pub async fn clear_stale_room_participants(
   8        &self,
   9        room_id: RoomId,
  10        new_server_id: ServerId,
  11    ) -> Result<TransactionGuard<RefreshedRoom>> {
  12        self.room_transaction(room_id, |tx| async move {
  13            let stale_participant_filter = Condition::all()
  14                .add(room_participant::Column::RoomId.eq(room_id))
  15                .add(room_participant::Column::AnsweringConnectionId.is_not_null())
  16                .add(room_participant::Column::AnsweringConnectionServerId.ne(new_server_id));
  17
  18            let stale_participant_user_ids = room_participant::Entity::find()
  19                .filter(stale_participant_filter.clone())
  20                .all(&*tx)
  21                .await?
  22                .into_iter()
  23                .map(|participant| participant.user_id)
  24                .collect::<Vec<_>>();
  25
  26            // Delete participants who failed to reconnect and cancel their calls.
  27            let mut canceled_calls_to_user_ids = Vec::new();
  28            room_participant::Entity::delete_many()
  29                .filter(stale_participant_filter)
  30                .exec(&*tx)
  31                .await?;
  32            let called_participants = room_participant::Entity::find()
  33                .filter(
  34                    Condition::all()
  35                        .add(
  36                            room_participant::Column::CallingUserId
  37                                .is_in(stale_participant_user_ids.iter().copied()),
  38                        )
  39                        .add(room_participant::Column::AnsweringConnectionId.is_null()),
  40                )
  41                .all(&*tx)
  42                .await?;
  43            room_participant::Entity::delete_many()
  44                .filter(
  45                    room_participant::Column::Id
  46                        .is_in(called_participants.iter().map(|participant| participant.id)),
  47                )
  48                .exec(&*tx)
  49                .await?;
  50            canceled_calls_to_user_ids.extend(
  51                called_participants
  52                    .into_iter()
  53                    .map(|participant| participant.user_id),
  54            );
  55
  56            let (channel, room) = self.get_channel_room(room_id, &tx).await?;
  57            if channel.is_none() {
  58                // Delete the room if it becomes empty.
  59                if room.participants.is_empty() {
  60                    project::Entity::delete_many()
  61                        .filter(project::Column::RoomId.eq(room_id))
  62                        .exec(&*tx)
  63                        .await?;
  64                    room::Entity::delete_by_id(room_id).exec(&*tx).await?;
  65                }
  66            };
  67
  68            Ok(RefreshedRoom {
  69                room,
  70                channel,
  71                stale_participant_user_ids,
  72                canceled_calls_to_user_ids,
  73            })
  74        })
  75        .await
  76    }
  77
  78    /// Returns the incoming calls for user with the given ID.
  79    pub async fn incoming_call_for_user(
  80        &self,
  81        user_id: UserId,
  82    ) -> Result<Option<proto::IncomingCall>> {
  83        self.transaction(|tx| async move {
  84            let pending_participant = room_participant::Entity::find()
  85                .filter(
  86                    room_participant::Column::UserId
  87                        .eq(user_id)
  88                        .and(room_participant::Column::AnsweringConnectionId.is_null()),
  89                )
  90                .one(&*tx)
  91                .await?;
  92
  93            if let Some(pending_participant) = pending_participant {
  94                let room = self.get_room(pending_participant.room_id, &tx).await?;
  95                Ok(Self::build_incoming_call(&room, user_id))
  96            } else {
  97                Ok(None)
  98            }
  99        })
 100        .await
 101    }
 102
 103    /// Creates a new room.
 104    pub async fn create_room(
 105        &self,
 106        user_id: UserId,
 107        connection: ConnectionId,
 108        livekit_room: &str,
 109    ) -> Result<proto::Room> {
 110        self.transaction(|tx| async move {
 111            let room = room::ActiveModel {
 112                live_kit_room: ActiveValue::set(livekit_room.into()),
 113                ..Default::default()
 114            }
 115            .insert(&*tx)
 116            .await?;
 117            room_participant::ActiveModel {
 118                room_id: ActiveValue::set(room.id),
 119                user_id: ActiveValue::set(user_id),
 120                answering_connection_id: ActiveValue::set(Some(connection.id as i32)),
 121                answering_connection_server_id: ActiveValue::set(Some(ServerId(
 122                    connection.owner_id as i32,
 123                ))),
 124                answering_connection_lost: ActiveValue::set(false),
 125                calling_user_id: ActiveValue::set(user_id),
 126                calling_connection_id: ActiveValue::set(connection.id as i32),
 127                calling_connection_server_id: ActiveValue::set(Some(ServerId(
 128                    connection.owner_id as i32,
 129                ))),
 130                participant_index: ActiveValue::set(Some(0)),
 131                role: ActiveValue::set(Some(ChannelRole::Admin)),
 132
 133                id: ActiveValue::NotSet,
 134                location_kind: ActiveValue::NotSet,
 135                location_project_id: ActiveValue::NotSet,
 136                initial_project_id: ActiveValue::NotSet,
 137            }
 138            .insert(&*tx)
 139            .await?;
 140
 141            let room = self.get_room(room.id, &tx).await?;
 142            Ok(room)
 143        })
 144        .await
 145    }
 146
 147    pub async fn call(
 148        &self,
 149        room_id: RoomId,
 150        calling_user_id: UserId,
 151        calling_connection: ConnectionId,
 152        called_user_id: UserId,
 153        initial_project_id: Option<ProjectId>,
 154    ) -> Result<TransactionGuard<(proto::Room, proto::IncomingCall)>> {
 155        self.room_transaction(room_id, |tx| async move {
 156            let caller = room_participant::Entity::find()
 157                .filter(
 158                    room_participant::Column::UserId
 159                        .eq(calling_user_id)
 160                        .and(room_participant::Column::RoomId.eq(room_id)),
 161                )
 162                .one(&*tx)
 163                .await?
 164                .context("user is not in the room")?;
 165
 166            let called_user_role = match caller.role.unwrap_or(ChannelRole::Member) {
 167                ChannelRole::Admin | ChannelRole::Member => ChannelRole::Member,
 168                ChannelRole::Guest | ChannelRole::Talker => ChannelRole::Guest,
 169                ChannelRole::Banned => return Err(anyhow!("banned users cannot invite").into()),
 170            };
 171
 172            room_participant::ActiveModel {
 173                room_id: ActiveValue::set(room_id),
 174                user_id: ActiveValue::set(called_user_id),
 175                answering_connection_lost: ActiveValue::set(false),
 176                participant_index: ActiveValue::NotSet,
 177                calling_user_id: ActiveValue::set(calling_user_id),
 178                calling_connection_id: ActiveValue::set(calling_connection.id as i32),
 179                calling_connection_server_id: ActiveValue::set(Some(ServerId(
 180                    calling_connection.owner_id as i32,
 181                ))),
 182                initial_project_id: ActiveValue::set(initial_project_id),
 183                role: ActiveValue::set(Some(called_user_role)),
 184
 185                id: ActiveValue::NotSet,
 186                answering_connection_id: ActiveValue::NotSet,
 187                answering_connection_server_id: ActiveValue::NotSet,
 188                location_kind: ActiveValue::NotSet,
 189                location_project_id: ActiveValue::NotSet,
 190            }
 191            .insert(&*tx)
 192            .await?;
 193
 194            let room = self.get_room(room_id, &tx).await?;
 195            let incoming_call = Self::build_incoming_call(&room, called_user_id)
 196                .context("failed to build incoming call")?;
 197            Ok((room, incoming_call))
 198        })
 199        .await
 200    }
 201
 202    pub async fn call_failed(
 203        &self,
 204        room_id: RoomId,
 205        called_user_id: UserId,
 206    ) -> Result<TransactionGuard<proto::Room>> {
 207        self.room_transaction(room_id, |tx| async move {
 208            room_participant::Entity::delete_many()
 209                .filter(
 210                    room_participant::Column::RoomId
 211                        .eq(room_id)
 212                        .and(room_participant::Column::UserId.eq(called_user_id)),
 213                )
 214                .exec(&*tx)
 215                .await?;
 216            let room = self.get_room(room_id, &tx).await?;
 217            Ok(room)
 218        })
 219        .await
 220    }
 221
 222    pub async fn decline_call(
 223        &self,
 224        expected_room_id: Option<RoomId>,
 225        user_id: UserId,
 226    ) -> Result<Option<TransactionGuard<proto::Room>>> {
 227        self.optional_room_transaction(|tx| async move {
 228            let mut filter = Condition::all()
 229                .add(room_participant::Column::UserId.eq(user_id))
 230                .add(room_participant::Column::AnsweringConnectionId.is_null());
 231            if let Some(room_id) = expected_room_id {
 232                filter = filter.add(room_participant::Column::RoomId.eq(room_id));
 233            }
 234            let participant = room_participant::Entity::find()
 235                .filter(filter)
 236                .one(&*tx)
 237                .await?;
 238
 239            let participant = if let Some(participant) = participant {
 240                participant
 241            } else if expected_room_id.is_some() {
 242                return Err(anyhow!("could not find call to decline"))?;
 243            } else {
 244                return Ok(None);
 245            };
 246
 247            let room_id = participant.room_id;
 248            room_participant::Entity::delete(participant.into_active_model())
 249                .exec(&*tx)
 250                .await?;
 251
 252            let room = self.get_room(room_id, &tx).await?;
 253            Ok(Some((room_id, room)))
 254        })
 255        .await
 256    }
 257
 258    pub async fn cancel_call(
 259        &self,
 260        room_id: RoomId,
 261        calling_connection: ConnectionId,
 262        called_user_id: UserId,
 263    ) -> Result<TransactionGuard<proto::Room>> {
 264        self.room_transaction(room_id, |tx| async move {
 265            let participant = room_participant::Entity::find()
 266                .filter(
 267                    Condition::all()
 268                        .add(room_participant::Column::UserId.eq(called_user_id))
 269                        .add(room_participant::Column::RoomId.eq(room_id))
 270                        .add(
 271                            room_participant::Column::CallingConnectionId
 272                                .eq(calling_connection.id as i32),
 273                        )
 274                        .add(
 275                            room_participant::Column::CallingConnectionServerId
 276                                .eq(calling_connection.owner_id as i32),
 277                        )
 278                        .add(room_participant::Column::AnsweringConnectionId.is_null()),
 279                )
 280                .one(&*tx)
 281                .await?
 282                .context("no call to cancel")?;
 283
 284            room_participant::Entity::delete(participant.into_active_model())
 285                .exec(&*tx)
 286                .await?;
 287
 288            let room = self.get_room(room_id, &tx).await?;
 289            Ok(room)
 290        })
 291        .await
 292    }
 293
 294    pub async fn join_room(
 295        &self,
 296        room_id: RoomId,
 297        user_id: UserId,
 298        connection: ConnectionId,
 299    ) -> Result<TransactionGuard<JoinRoom>> {
 300        self.room_transaction(room_id, |tx| async move {
 301            #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
 302            enum QueryChannelId {
 303                ChannelId,
 304            }
 305
 306            let channel_id: Option<ChannelId> = room::Entity::find()
 307                .select_only()
 308                .column(room::Column::ChannelId)
 309                .filter(room::Column::Id.eq(room_id))
 310                .into_values::<_, QueryChannelId>()
 311                .one(&*tx)
 312                .await?
 313                .context("no such room")?;
 314
 315            if channel_id.is_some() {
 316                Err(anyhow!("tried to join channel call directly"))?
 317            }
 318
 319            let participant_index = self
 320                .get_next_participant_index_internal(room_id, &tx)
 321                .await?;
 322
 323            let result = room_participant::Entity::update_many()
 324                .filter(
 325                    Condition::all()
 326                        .add(room_participant::Column::RoomId.eq(room_id))
 327                        .add(room_participant::Column::UserId.eq(user_id))
 328                        .add(room_participant::Column::AnsweringConnectionId.is_null()),
 329                )
 330                .set(room_participant::ActiveModel {
 331                    participant_index: ActiveValue::Set(Some(participant_index)),
 332                    answering_connection_id: ActiveValue::set(Some(connection.id as i32)),
 333                    answering_connection_server_id: ActiveValue::set(Some(ServerId(
 334                        connection.owner_id as i32,
 335                    ))),
 336                    answering_connection_lost: ActiveValue::set(false),
 337                    ..Default::default()
 338                })
 339                .exec(&*tx)
 340                .await?;
 341            if result.rows_affected == 0 {
 342                Err(anyhow!("room does not exist or was already joined"))?;
 343            }
 344
 345            let room = self.get_room(room_id, &tx).await?;
 346            Ok(JoinRoom {
 347                room,
 348                channel: None,
 349            })
 350        })
 351        .await
 352    }
 353
 354    pub async fn stale_room_connection(&self, user_id: UserId) -> Result<Option<ConnectionId>> {
 355        self.transaction(|tx| async move {
 356            let participant = room_participant::Entity::find()
 357                .filter(room_participant::Column::UserId.eq(user_id))
 358                .one(&*tx)
 359                .await?;
 360            Ok(participant.and_then(|p| p.answering_connection()))
 361        })
 362        .await
 363    }
 364
 365    async fn get_next_participant_index_internal(
 366        &self,
 367        room_id: RoomId,
 368        tx: &DatabaseTransaction,
 369    ) -> Result<i32> {
 370        #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
 371        enum QueryParticipantIndices {
 372            ParticipantIndex,
 373        }
 374        let existing_participant_indices: Vec<i32> = room_participant::Entity::find()
 375            .filter(
 376                room_participant::Column::RoomId
 377                    .eq(room_id)
 378                    .and(room_participant::Column::ParticipantIndex.is_not_null()),
 379            )
 380            .select_only()
 381            .column(room_participant::Column::ParticipantIndex)
 382            .into_values::<_, QueryParticipantIndices>()
 383            .all(tx)
 384            .await?;
 385
 386        let mut participant_index = 0;
 387        while existing_participant_indices.contains(&participant_index) {
 388            participant_index += 1;
 389        }
 390
 391        Ok(participant_index)
 392    }
 393
 394    /// Returns the channel ID for the given room, if it has one.
 395    pub async fn channel_id_for_room(&self, room_id: RoomId) -> Result<Option<ChannelId>> {
 396        self.transaction(|tx| async move {
 397            let room: Option<room::Model> = room::Entity::find()
 398                .filter(room::Column::Id.eq(room_id))
 399                .one(&*tx)
 400                .await?;
 401
 402            Ok(room.and_then(|room| room.channel_id))
 403        })
 404        .await
 405    }
 406
 407    pub(crate) async fn join_channel_room_internal(
 408        &self,
 409        room_id: RoomId,
 410        user_id: UserId,
 411        connection: ConnectionId,
 412        role: ChannelRole,
 413        tx: &DatabaseTransaction,
 414    ) -> Result<JoinRoom> {
 415        let participant_index = self
 416            .get_next_participant_index_internal(room_id, tx)
 417            .await?;
 418
 419        // If someone has been invited into the room, accept the invite instead of inserting
 420        let result = room_participant::Entity::update_many()
 421            .filter(
 422                Condition::all()
 423                    .add(room_participant::Column::RoomId.eq(room_id))
 424                    .add(room_participant::Column::UserId.eq(user_id))
 425                    .add(room_participant::Column::AnsweringConnectionId.is_null()),
 426            )
 427            .set(room_participant::ActiveModel {
 428                participant_index: ActiveValue::Set(Some(participant_index)),
 429                answering_connection_id: ActiveValue::set(Some(connection.id as i32)),
 430                answering_connection_server_id: ActiveValue::set(Some(ServerId(
 431                    connection.owner_id as i32,
 432                ))),
 433                answering_connection_lost: ActiveValue::set(false),
 434                ..Default::default()
 435            })
 436            .exec(tx)
 437            .await?;
 438
 439        if result.rows_affected == 0 {
 440            room_participant::Entity::insert(room_participant::ActiveModel {
 441                room_id: ActiveValue::set(room_id),
 442                user_id: ActiveValue::set(user_id),
 443                answering_connection_id: ActiveValue::set(Some(connection.id as i32)),
 444                answering_connection_server_id: ActiveValue::set(Some(ServerId(
 445                    connection.owner_id as i32,
 446                ))),
 447                answering_connection_lost: ActiveValue::set(false),
 448                calling_user_id: ActiveValue::set(user_id),
 449                calling_connection_id: ActiveValue::set(connection.id as i32),
 450                calling_connection_server_id: ActiveValue::set(Some(ServerId(
 451                    connection.owner_id as i32,
 452                ))),
 453                participant_index: ActiveValue::Set(Some(participant_index)),
 454                role: ActiveValue::set(Some(role)),
 455                id: ActiveValue::NotSet,
 456                location_kind: ActiveValue::NotSet,
 457                location_project_id: ActiveValue::NotSet,
 458                initial_project_id: ActiveValue::NotSet,
 459            })
 460            .exec(tx)
 461            .await?;
 462        }
 463
 464        let (channel, room) = self.get_channel_room(room_id, tx).await?;
 465        let channel = channel.context("no channel for room")?;
 466        Ok(JoinRoom {
 467            room,
 468            channel: Some(channel),
 469        })
 470    }
 471
 472    pub async fn rejoin_room(
 473        &self,
 474        rejoin_room: proto::RejoinRoom,
 475        user_id: UserId,
 476        connection: ConnectionId,
 477    ) -> Result<TransactionGuard<RejoinedRoom>> {
 478        let room_id = RoomId::from_proto(rejoin_room.id);
 479        self.room_transaction(room_id, |tx| async {
 480            let tx = tx;
 481            let participant_update = room_participant::Entity::update_many()
 482                .filter(
 483                    Condition::all()
 484                        .add(room_participant::Column::RoomId.eq(room_id))
 485                        .add(room_participant::Column::UserId.eq(user_id))
 486                        .add(room_participant::Column::AnsweringConnectionId.is_not_null()),
 487                )
 488                .set(room_participant::ActiveModel {
 489                    answering_connection_id: ActiveValue::set(Some(connection.id as i32)),
 490                    answering_connection_server_id: ActiveValue::set(Some(ServerId(
 491                        connection.owner_id as i32,
 492                    ))),
 493                    answering_connection_lost: ActiveValue::set(false),
 494                    ..Default::default()
 495                })
 496                .exec(&*tx)
 497                .await?;
 498            if participant_update.rows_affected == 0 {
 499                return Err(anyhow!("room does not exist or was already joined"))?;
 500            }
 501
 502            let mut reshared_projects = Vec::new();
 503            for reshared_project in &rejoin_room.reshared_projects {
 504                let project_id = ProjectId::from_proto(reshared_project.project_id);
 505                let project = project::Entity::find_by_id(project_id)
 506                    .one(&*tx)
 507                    .await?
 508                    .context("project does not exist")?;
 509                if project.host_user_id != Some(user_id) {
 510                    return Err(anyhow!("no such project"))?;
 511                }
 512
 513                let mut collaborators = project
 514                    .find_related(project_collaborator::Entity)
 515                    .all(&*tx)
 516                    .await?;
 517                let host_ix = collaborators
 518                    .iter()
 519                    .position(|collaborator| {
 520                        collaborator.user_id == user_id && collaborator.is_host
 521                    })
 522                    .context("host not found among collaborators")?;
 523                let host = collaborators.swap_remove(host_ix);
 524                let old_connection_id = host.connection();
 525
 526                project::Entity::update(project::ActiveModel {
 527                    host_connection_id: ActiveValue::set(Some(connection.id as i32)),
 528                    host_connection_server_id: ActiveValue::set(Some(ServerId(
 529                        connection.owner_id as i32,
 530                    ))),
 531                    ..project.into_active_model()
 532                })
 533                .exec(&*tx)
 534                .await?;
 535                project_collaborator::Entity::update(project_collaborator::ActiveModel {
 536                    connection_id: ActiveValue::set(connection.id as i32),
 537                    connection_server_id: ActiveValue::set(ServerId(connection.owner_id as i32)),
 538                    ..host.into_active_model()
 539                })
 540                .exec(&*tx)
 541                .await?;
 542
 543                self.update_project_worktrees(project_id, &reshared_project.worktrees, &tx)
 544                    .await?;
 545
 546                reshared_projects.push(ResharedProject {
 547                    id: project_id,
 548                    old_connection_id,
 549                    collaborators: collaborators
 550                        .iter()
 551                        .map(|collaborator| ProjectCollaborator {
 552                            connection_id: collaborator.connection(),
 553                            user_id: collaborator.user_id,
 554                            replica_id: collaborator.replica_id,
 555                            is_host: collaborator.is_host,
 556                            committer_name: collaborator.committer_name.clone(),
 557                            committer_email: collaborator.committer_email.clone(),
 558                        })
 559                        .collect(),
 560                    worktrees: reshared_project.worktrees.clone(),
 561                });
 562            }
 563
 564            project::Entity::delete_many()
 565                .filter(
 566                    Condition::all()
 567                        .add(project::Column::RoomId.eq(room_id))
 568                        .add(project::Column::HostUserId.eq(user_id))
 569                        .add(
 570                            project::Column::Id
 571                                .is_not_in(reshared_projects.iter().map(|project| project.id)),
 572                        ),
 573                )
 574                .exec(&*tx)
 575                .await?;
 576
 577            let mut rejoined_projects = Vec::new();
 578            for rejoined_project in &rejoin_room.rejoined_projects {
 579                if let Some(rejoined_project) = self
 580                    .rejoin_project_internal(&tx, rejoined_project, user_id, connection)
 581                    .await?
 582                {
 583                    rejoined_projects.push(rejoined_project);
 584                }
 585            }
 586
 587            let (channel, room) = self.get_channel_room(room_id, &tx).await?;
 588
 589            Ok(RejoinedRoom {
 590                room,
 591                channel,
 592                rejoined_projects,
 593                reshared_projects,
 594            })
 595        })
 596        .await
 597    }
 598
 599    pub async fn rejoin_project_internal(
 600        &self,
 601        tx: &DatabaseTransaction,
 602        rejoined_project: &proto::RejoinProject,
 603        user_id: UserId,
 604        connection: ConnectionId,
 605    ) -> Result<Option<RejoinedProject>> {
 606        let project_id = ProjectId::from_proto(rejoined_project.id);
 607        let Some(project) = project::Entity::find_by_id(project_id).one(tx).await? else {
 608            return Ok(None);
 609        };
 610
 611        let mut worktrees = Vec::new();
 612        let db_worktrees = project.find_related(worktree::Entity).all(tx).await?;
 613        let db_repos = project
 614            .find_related(project_repository::Entity)
 615            .all(tx)
 616            .await?;
 617
 618        for db_worktree in db_worktrees {
 619            let mut worktree = RejoinedWorktree {
 620                id: db_worktree.id as u64,
 621                abs_path: db_worktree.abs_path,
 622                root_name: db_worktree.root_name,
 623                visible: db_worktree.visible,
 624                updated_entries: Default::default(),
 625                removed_entries: Default::default(),
 626                updated_repositories: Default::default(),
 627                removed_repositories: Default::default(),
 628                diagnostic_summaries: Default::default(),
 629                settings_files: Default::default(),
 630                scan_id: db_worktree.scan_id as u64,
 631                completed_scan_id: db_worktree.completed_scan_id as u64,
 632                root_repo_common_dir: db_worktree.root_repo_common_dir,
 633            };
 634
 635            let rejoined_worktree = rejoined_project
 636                .worktrees
 637                .iter()
 638                .find(|worktree| worktree.id == db_worktree.id as u64);
 639
 640            // File entries
 641            {
 642                let entry_filter = if let Some(rejoined_worktree) = rejoined_worktree {
 643                    worktree_entry::Column::ScanId.gt(rejoined_worktree.scan_id)
 644                } else {
 645                    worktree_entry::Column::IsDeleted.eq(false)
 646                };
 647
 648                let mut db_entries = worktree_entry::Entity::find()
 649                    .filter(
 650                        Condition::all()
 651                            .add(worktree_entry::Column::ProjectId.eq(project.id))
 652                            .add(worktree_entry::Column::WorktreeId.eq(worktree.id))
 653                            .add(entry_filter),
 654                    )
 655                    .stream(tx)
 656                    .await?;
 657
 658                while let Some(db_entry) = db_entries.next().await {
 659                    let db_entry = db_entry?;
 660                    if db_entry.is_deleted {
 661                        worktree.removed_entries.push(db_entry.id as u64);
 662                    } else {
 663                        worktree.updated_entries.push(proto::Entry {
 664                            id: db_entry.id as u64,
 665                            is_dir: db_entry.is_dir,
 666                            path: db_entry.path,
 667                            inode: db_entry.inode as u64,
 668                            mtime: Some(proto::Timestamp {
 669                                seconds: db_entry.mtime_seconds as u64,
 670                                nanos: db_entry.mtime_nanos as u32,
 671                            }),
 672                            canonical_path: db_entry.canonical_path,
 673                            is_ignored: db_entry.is_ignored,
 674                            is_external: db_entry.is_external,
 675                            is_hidden: db_entry.is_hidden,
 676                            // This is only used in the summarization backlog, so if it's None,
 677                            // that just means we won't be able to detect when to resummarize
 678                            // based on total number of backlogged bytes - instead, we'd go
 679                            // on number of files only. That shouldn't be a huge deal in practice.
 680                            size: None,
 681                            is_fifo: db_entry.is_fifo,
 682                        });
 683                    }
 684                }
 685            }
 686
 687            worktrees.push(worktree);
 688        }
 689
 690        let mut removed_repositories = Vec::new();
 691        let mut updated_repositories = Vec::new();
 692        for db_repo in db_repos {
 693            let rejoined_repository = rejoined_project
 694                .repositories
 695                .iter()
 696                .find(|repo| repo.id == db_repo.id as u64);
 697
 698            let repository_filter = if let Some(rejoined_repository) = rejoined_repository {
 699                project_repository::Column::ScanId.gt(rejoined_repository.scan_id)
 700            } else {
 701                project_repository::Column::IsDeleted.eq(false)
 702            };
 703
 704            let db_repositories = project_repository::Entity::find()
 705                .filter(
 706                    Condition::all()
 707                        .add(project_repository::Column::ProjectId.eq(project.id))
 708                        .add(repository_filter),
 709                )
 710                .all(tx)
 711                .await?;
 712
 713            for db_repository in db_repositories.into_iter() {
 714                if db_repository.is_deleted {
 715                    removed_repositories.push(db_repository.id as u64);
 716                } else {
 717                    let status_entry_filter = if let Some(rejoined_repository) = rejoined_repository
 718                    {
 719                        project_repository_statuses::Column::ScanId.gt(rejoined_repository.scan_id)
 720                    } else {
 721                        project_repository_statuses::Column::IsDeleted.eq(false)
 722                    };
 723
 724                    let mut db_statuses = project_repository_statuses::Entity::find()
 725                        .filter(
 726                            Condition::all()
 727                                .add(project_repository_statuses::Column::ProjectId.eq(project.id))
 728                                .add(
 729                                    project_repository_statuses::Column::RepositoryId
 730                                        .eq(db_repository.id),
 731                                )
 732                                .add(status_entry_filter),
 733                        )
 734                        .stream(tx)
 735                        .await?;
 736                    let mut removed_statuses = Vec::new();
 737                    let mut updated_statuses = Vec::new();
 738
 739                    while let Some(db_status) = db_statuses.next().await {
 740                        let db_status: project_repository_statuses::Model = db_status?;
 741                        if db_status.is_deleted {
 742                            removed_statuses.push(db_status.repo_path.clone());
 743                        } else {
 744                            updated_statuses.push(db_status_to_proto(db_status)?);
 745                        }
 746                    }
 747
 748                    let current_merge_conflicts = db_repository
 749                        .current_merge_conflicts
 750                        .as_ref()
 751                        .map(|conflicts| serde_json::from_str(conflicts))
 752                        .transpose()?
 753                        .unwrap_or_default();
 754
 755                    let branch_summary = db_repository
 756                        .branch_summary
 757                        .as_ref()
 758                        .map(|branch_summary| serde_json::from_str(branch_summary))
 759                        .transpose()?
 760                        .unwrap_or_default();
 761
 762                    let head_commit_details = db_repository
 763                        .head_commit_details
 764                        .as_ref()
 765                        .map(|head_commit_details| serde_json::from_str(head_commit_details))
 766                        .transpose()?
 767                        .unwrap_or_default();
 768
 769                    let entry_ids = serde_json::from_str(&db_repository.entry_ids)
 770                        .context("failed to deserialize repository's entry ids")?;
 771
 772                    if let Some(legacy_worktree_id) = db_repository.legacy_worktree_id {
 773                        if let Some(worktree) = worktrees
 774                            .iter_mut()
 775                            .find(|worktree| worktree.id as i64 == legacy_worktree_id)
 776                        {
 777                            worktree.updated_repositories.push(proto::RepositoryEntry {
 778                                repository_id: db_repository.id as u64,
 779                                updated_statuses,
 780                                removed_statuses,
 781                                current_merge_conflicts,
 782                                branch_summary,
 783                            });
 784                        }
 785                    } else {
 786                        updated_repositories.push(proto::UpdateRepository {
 787                            entry_ids,
 788                            updated_statuses,
 789                            removed_statuses,
 790                            current_merge_conflicts,
 791                            branch_summary,
 792                            head_commit_details,
 793                            project_id: project_id.to_proto(),
 794                            id: db_repository.id as u64,
 795                            abs_path: db_repository.abs_path.clone(),
 796                            scan_id: db_repository.scan_id as u64,
 797                            is_last_update: true,
 798                            merge_message: db_repository.merge_message,
 799                            stash_entries: Vec::new(),
 800                            remote_upstream_url: db_repository.remote_upstream_url.clone(),
 801                            remote_origin_url: db_repository.remote_origin_url.clone(),
 802                            original_repo_abs_path: Some(db_repository.abs_path),
 803                            linked_worktrees: db_repository
 804                                .linked_worktrees
 805                                .as_deref()
 806                                .and_then(|s| serde_json::from_str(s).ok())
 807                                .unwrap_or_default(),
 808                        });
 809                    }
 810                }
 811            }
 812        }
 813
 814        let language_servers = project
 815            .find_related(language_server::Entity)
 816            .all(tx)
 817            .await?
 818            .into_iter()
 819            .map(|language_server| LanguageServer {
 820                server: proto::LanguageServer {
 821                    id: language_server.id as u64,
 822                    name: language_server.name,
 823                    worktree_id: language_server.worktree_id.map(|id| id as u64),
 824                },
 825                capabilities: language_server.capabilities,
 826            })
 827            .collect::<Vec<_>>();
 828
 829        {
 830            let mut db_settings_files = worktree_settings_file::Entity::find()
 831                .filter(worktree_settings_file::Column::ProjectId.eq(project_id))
 832                .stream(tx)
 833                .await?;
 834            while let Some(db_settings_file) = db_settings_files.next().await {
 835                let db_settings_file = db_settings_file?;
 836                if let Some(worktree) = worktrees
 837                    .iter_mut()
 838                    .find(|w| w.id == db_settings_file.worktree_id as u64)
 839                {
 840                    worktree.settings_files.push(WorktreeSettingsFile {
 841                        path: db_settings_file.path,
 842                        content: db_settings_file.content,
 843                        kind: db_settings_file.kind,
 844                        outside_worktree: db_settings_file.outside_worktree,
 845                    });
 846                }
 847            }
 848        }
 849
 850        let mut collaborators = project
 851            .find_related(project_collaborator::Entity)
 852            .all(tx)
 853            .await?;
 854        let self_collaborator = if let Some(self_collaborator_ix) = collaborators
 855            .iter()
 856            .position(|collaborator| collaborator.user_id == user_id)
 857        {
 858            collaborators.swap_remove(self_collaborator_ix)
 859        } else {
 860            return Ok(None);
 861        };
 862        let old_connection_id = self_collaborator.connection();
 863        project_collaborator::Entity::update(project_collaborator::ActiveModel {
 864            connection_id: ActiveValue::set(connection.id as i32),
 865            connection_server_id: ActiveValue::set(ServerId(connection.owner_id as i32)),
 866            ..self_collaborator.into_active_model()
 867        })
 868        .exec(tx)
 869        .await?;
 870
 871        let collaborators = collaborators
 872            .into_iter()
 873            .map(|collaborator| ProjectCollaborator {
 874                connection_id: collaborator.connection(),
 875                user_id: collaborator.user_id,
 876                replica_id: collaborator.replica_id,
 877                is_host: collaborator.is_host,
 878                committer_name: collaborator.committer_name,
 879                committer_email: collaborator.committer_email,
 880            })
 881            .collect::<Vec<_>>();
 882
 883        Ok(Some(RejoinedProject {
 884            id: project_id,
 885            old_connection_id,
 886            collaborators,
 887            updated_repositories,
 888            removed_repositories,
 889            worktrees,
 890            language_servers,
 891        }))
 892    }
 893
 894    pub async fn leave_room(
 895        &self,
 896        connection: ConnectionId,
 897    ) -> Result<Option<TransactionGuard<LeftRoom>>> {
 898        self.optional_room_transaction(|tx| async move {
 899            let leaving_participant = room_participant::Entity::find()
 900                .filter(
 901                    Condition::all()
 902                        .add(
 903                            room_participant::Column::AnsweringConnectionId
 904                                .eq(connection.id as i32),
 905                        )
 906                        .add(
 907                            room_participant::Column::AnsweringConnectionServerId
 908                                .eq(connection.owner_id as i32),
 909                        ),
 910                )
 911                .one(&*tx)
 912                .await?;
 913
 914            if let Some(leaving_participant) = leaving_participant {
 915                // Leave room.
 916                let room_id = leaving_participant.room_id;
 917                room_participant::Entity::delete_by_id(leaving_participant.id)
 918                    .exec(&*tx)
 919                    .await?;
 920
 921                // Cancel pending calls initiated by the leaving user.
 922                let called_participants = room_participant::Entity::find()
 923                    .filter(
 924                        Condition::all()
 925                            .add(
 926                                room_participant::Column::CallingUserId
 927                                    .eq(leaving_participant.user_id),
 928                            )
 929                            .add(room_participant::Column::AnsweringConnectionId.is_null()),
 930                    )
 931                    .all(&*tx)
 932                    .await?;
 933                room_participant::Entity::delete_many()
 934                    .filter(
 935                        room_participant::Column::Id
 936                            .is_in(called_participants.iter().map(|participant| participant.id)),
 937                    )
 938                    .exec(&*tx)
 939                    .await?;
 940                let canceled_calls_to_user_ids = called_participants
 941                    .into_iter()
 942                    .map(|participant| participant.user_id)
 943                    .collect();
 944
 945                // Detect left projects.
 946                #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
 947                enum QueryProjectIds {
 948                    ProjectId,
 949                }
 950                let project_ids: Vec<ProjectId> = project_collaborator::Entity::find()
 951                    .select_only()
 952                    .column_as(
 953                        project_collaborator::Column::ProjectId,
 954                        QueryProjectIds::ProjectId,
 955                    )
 956                    .filter(
 957                        Condition::all()
 958                            .add(
 959                                project_collaborator::Column::ConnectionId.eq(connection.id as i32),
 960                            )
 961                            .add(
 962                                project_collaborator::Column::ConnectionServerId
 963                                    .eq(connection.owner_id as i32),
 964                            ),
 965                    )
 966                    .into_values::<_, QueryProjectIds>()
 967                    .all(&*tx)
 968                    .await?;
 969
 970                let mut left_projects = HashMap::default();
 971                let mut collaborators = project_collaborator::Entity::find()
 972                    .filter(project_collaborator::Column::ProjectId.is_in(project_ids))
 973                    .stream(&*tx)
 974                    .await?;
 975
 976                while let Some(collaborator) = collaborators.next().await {
 977                    let collaborator = collaborator?;
 978                    let left_project =
 979                        left_projects
 980                            .entry(collaborator.project_id)
 981                            .or_insert(LeftProject {
 982                                id: collaborator.project_id,
 983                                connection_ids: Default::default(),
 984                                should_unshare: false,
 985                            });
 986
 987                    let collaborator_connection_id = collaborator.connection();
 988                    if collaborator_connection_id != connection {
 989                        left_project.connection_ids.push(collaborator_connection_id);
 990                    }
 991
 992                    if collaborator.is_host && collaborator.connection() == connection {
 993                        left_project.should_unshare = true;
 994                    }
 995                }
 996                drop(collaborators);
 997
 998                // Leave projects.
 999                project_collaborator::Entity::delete_many()
1000                    .filter(
1001                        Condition::all()
1002                            .add(
1003                                project_collaborator::Column::ConnectionId.eq(connection.id as i32),
1004                            )
1005                            .add(
1006                                project_collaborator::Column::ConnectionServerId
1007                                    .eq(connection.owner_id as i32),
1008                            ),
1009                    )
1010                    .exec(&*tx)
1011                    .await?;
1012
1013                follower::Entity::delete_many()
1014                    .filter(
1015                        Condition::all()
1016                            .add(follower::Column::FollowerConnectionId.eq(connection.id as i32)),
1017                    )
1018                    .exec(&*tx)
1019                    .await?;
1020
1021                // Unshare projects.
1022                project::Entity::delete_many()
1023                    .filter(
1024                        Condition::all()
1025                            .add(project::Column::RoomId.eq(room_id))
1026                            .add(project::Column::HostConnectionId.eq(connection.id as i32))
1027                            .add(
1028                                project::Column::HostConnectionServerId
1029                                    .eq(connection.owner_id as i32),
1030                            ),
1031                    )
1032                    .exec(&*tx)
1033                    .await?;
1034
1035                let (channel, room) = self.get_channel_room(room_id, &tx).await?;
1036                let deleted = if room.participants.is_empty() {
1037                    let result = room::Entity::delete_by_id(room_id).exec(&*tx).await?;
1038                    result.rows_affected > 0
1039                } else {
1040                    false
1041                };
1042
1043                let left_room = LeftRoom {
1044                    room,
1045                    channel,
1046                    left_projects,
1047                    canceled_calls_to_user_ids,
1048                    deleted,
1049                };
1050
1051                if left_room.room.participants.is_empty() {
1052                    self.rooms.remove(&room_id);
1053                }
1054
1055                Ok(Some((room_id, left_room)))
1056            } else {
1057                Ok(None)
1058            }
1059        })
1060        .await
1061    }
1062
1063    /// Updates the location of a participant in the given room.
1064    pub async fn update_room_participant_location(
1065        &self,
1066        room_id: RoomId,
1067        connection: ConnectionId,
1068        location: proto::ParticipantLocation,
1069    ) -> Result<TransactionGuard<proto::Room>> {
1070        self.room_transaction(room_id, |tx| async {
1071            let tx = tx;
1072            let location_kind;
1073            let location_project_id;
1074            match location.variant.as_ref().context("invalid location")? {
1075                proto::participant_location::Variant::SharedProject(project) => {
1076                    location_kind = 0;
1077                    location_project_id = Some(ProjectId::from_proto(project.id));
1078                }
1079                proto::participant_location::Variant::UnsharedProject(_) => {
1080                    location_kind = 1;
1081                    location_project_id = None;
1082                }
1083                proto::participant_location::Variant::External(_) => {
1084                    location_kind = 2;
1085                    location_project_id = None;
1086                }
1087            }
1088
1089            let result = room_participant::Entity::update_many()
1090                .filter(
1091                    Condition::all()
1092                        .add(room_participant::Column::RoomId.eq(room_id))
1093                        .add(
1094                            room_participant::Column::AnsweringConnectionId
1095                                .eq(connection.id as i32),
1096                        )
1097                        .add(
1098                            room_participant::Column::AnsweringConnectionServerId
1099                                .eq(connection.owner_id as i32),
1100                        ),
1101                )
1102                .set(room_participant::ActiveModel {
1103                    location_kind: ActiveValue::set(Some(location_kind)),
1104                    location_project_id: ActiveValue::set(location_project_id),
1105                    ..Default::default()
1106                })
1107                .exec(&*tx)
1108                .await?;
1109
1110            if result.rows_affected == 1 {
1111                let room = self.get_room(room_id, &tx).await?;
1112                Ok(room)
1113            } else {
1114                Err(anyhow!("could not update room participant location"))?
1115            }
1116        })
1117        .await
1118    }
1119
1120    /// Sets the role of a participant in the given room.
1121    pub async fn set_room_participant_role(
1122        &self,
1123        admin_id: UserId,
1124        room_id: RoomId,
1125        user_id: UserId,
1126        role: ChannelRole,
1127    ) -> Result<TransactionGuard<proto::Room>> {
1128        self.room_transaction(room_id, |tx| async move {
1129            room_participant::Entity::find()
1130                .filter(
1131                    Condition::all()
1132                        .add(room_participant::Column::RoomId.eq(room_id))
1133                        .add(room_participant::Column::UserId.eq(admin_id))
1134                        .add(room_participant::Column::Role.eq(ChannelRole::Admin)),
1135                )
1136                .one(&*tx)
1137                .await?
1138                .context("only admins can set participant role")?;
1139
1140            if role.requires_cla() {
1141                self.check_user_has_signed_cla(user_id, room_id, &tx)
1142                    .await?;
1143            }
1144
1145            let result = room_participant::Entity::update_many()
1146                .filter(
1147                    Condition::all()
1148                        .add(room_participant::Column::RoomId.eq(room_id))
1149                        .add(room_participant::Column::UserId.eq(user_id)),
1150                )
1151                .set(room_participant::ActiveModel {
1152                    role: ActiveValue::set(Some(role)),
1153                    ..Default::default()
1154                })
1155                .exec(&*tx)
1156                .await?;
1157
1158            if result.rows_affected != 1 {
1159                Err(anyhow!("could not update room participant role"))?;
1160            }
1161            self.get_room(room_id, &tx).await
1162        })
1163        .await
1164    }
1165
1166    async fn check_user_has_signed_cla(
1167        &self,
1168        user_id: UserId,
1169        room_id: RoomId,
1170        tx: &DatabaseTransaction,
1171    ) -> Result<()> {
1172        let channel = room::Entity::find_by_id(room_id)
1173            .one(tx)
1174            .await?
1175            .context("could not find room")?
1176            .find_related(channel::Entity)
1177            .one(tx)
1178            .await?;
1179
1180        if let Some(channel) = channel {
1181            let requires_zed_cla = channel.requires_zed_cla
1182                || channel::Entity::find()
1183                    .filter(
1184                        channel::Column::Id
1185                            .is_in(channel.ancestors())
1186                            .and(channel::Column::RequiresZedCla.eq(true)),
1187                    )
1188                    .count(tx)
1189                    .await?
1190                    > 0;
1191            if requires_zed_cla
1192                && contributor::Entity::find()
1193                    .filter(contributor::Column::UserId.eq(user_id))
1194                    .one(tx)
1195                    .await?
1196                    .is_none()
1197            {
1198                Err(anyhow!("user has not signed the Zed CLA"))?;
1199            }
1200        }
1201        Ok(())
1202    }
1203
1204    pub async fn connection_lost(&self, connection: ConnectionId) -> Result<()> {
1205        self.transaction(|tx| async move {
1206            self.room_connection_lost(connection, &tx).await?;
1207            self.channel_buffer_connection_lost(connection, &tx).await?;
1208            Ok(())
1209        })
1210        .await
1211    }
1212
1213    pub async fn room_connection_lost(
1214        &self,
1215        connection: ConnectionId,
1216        tx: &DatabaseTransaction,
1217    ) -> Result<()> {
1218        let participant = room_participant::Entity::find()
1219            .filter(
1220                Condition::all()
1221                    .add(room_participant::Column::AnsweringConnectionId.eq(connection.id as i32))
1222                    .add(
1223                        room_participant::Column::AnsweringConnectionServerId
1224                            .eq(connection.owner_id as i32),
1225                    ),
1226            )
1227            .one(tx)
1228            .await?;
1229
1230        if let Some(participant) = participant {
1231            room_participant::Entity::update(room_participant::ActiveModel {
1232                answering_connection_lost: ActiveValue::set(true),
1233                ..participant.into_active_model()
1234            })
1235            .exec(tx)
1236            .await?;
1237        }
1238        Ok(())
1239    }
1240
1241    fn build_incoming_call(
1242        room: &proto::Room,
1243        called_user_id: UserId,
1244    ) -> Option<proto::IncomingCall> {
1245        let pending_participant = room
1246            .pending_participants
1247            .iter()
1248            .find(|participant| participant.user_id == called_user_id.to_proto())?;
1249
1250        Some(proto::IncomingCall {
1251            room_id: room.id,
1252            calling_user_id: pending_participant.calling_user_id,
1253            participant_user_ids: room
1254                .participants
1255                .iter()
1256                .map(|participant| participant.user_id)
1257                .collect(),
1258            initial_project: room.participants.iter().find_map(|participant| {
1259                let initial_project_id = pending_participant.initial_project_id?;
1260                participant
1261                    .projects
1262                    .iter()
1263                    .find(|project| project.id == initial_project_id)
1264                    .cloned()
1265            }),
1266        })
1267    }
1268
1269    pub async fn get_room(&self, room_id: RoomId, tx: &DatabaseTransaction) -> Result<proto::Room> {
1270        let (_, room) = self.get_channel_room(room_id, tx).await?;
1271        Ok(room)
1272    }
1273
1274    pub async fn room_connection_ids(
1275        &self,
1276        room_id: RoomId,
1277        connection_id: ConnectionId,
1278    ) -> Result<TransactionGuard<HashSet<ConnectionId>>> {
1279        self.room_transaction(room_id, |tx| async move {
1280            let mut participants = room_participant::Entity::find()
1281                .filter(room_participant::Column::RoomId.eq(room_id))
1282                .stream(&*tx)
1283                .await?;
1284
1285            let mut is_participant = false;
1286            let mut connection_ids = HashSet::default();
1287            while let Some(participant) = participants.next().await {
1288                let participant = participant?;
1289                if let Some(answering_connection) = participant.answering_connection() {
1290                    if answering_connection == connection_id {
1291                        is_participant = true;
1292                    } else {
1293                        connection_ids.insert(answering_connection);
1294                    }
1295                }
1296            }
1297
1298            if !is_participant {
1299                Err(anyhow!("not a room participant"))?;
1300            }
1301
1302            Ok(connection_ids)
1303        })
1304        .await
1305    }
1306
1307    async fn get_channel_room(
1308        &self,
1309        room_id: RoomId,
1310        tx: &DatabaseTransaction,
1311    ) -> Result<(Option<channel::Model>, proto::Room)> {
1312        let db_room = room::Entity::find_by_id(room_id)
1313            .one(tx)
1314            .await?
1315            .context("could not find room")?;
1316
1317        let mut db_participants = db_room
1318            .find_related(room_participant::Entity)
1319            .stream(tx)
1320            .await?;
1321        let mut participants = HashMap::default();
1322        let mut pending_participants = Vec::new();
1323        while let Some(db_participant) = db_participants.next().await {
1324            let db_participant = db_participant?;
1325            if let (
1326                Some(answering_connection_id),
1327                Some(answering_connection_server_id),
1328                Some(participant_index),
1329            ) = (
1330                db_participant.answering_connection_id,
1331                db_participant.answering_connection_server_id,
1332                db_participant.participant_index,
1333            ) {
1334                let location = match (
1335                    db_participant.location_kind,
1336                    db_participant.location_project_id,
1337                ) {
1338                    (Some(0), Some(project_id)) => {
1339                        Some(proto::participant_location::Variant::SharedProject(
1340                            proto::participant_location::SharedProject {
1341                                id: project_id.to_proto(),
1342                            },
1343                        ))
1344                    }
1345                    (Some(1), _) => Some(proto::participant_location::Variant::UnsharedProject(
1346                        Default::default(),
1347                    )),
1348                    _ => Some(proto::participant_location::Variant::External(
1349                        Default::default(),
1350                    )),
1351                };
1352
1353                let answering_connection = ConnectionId {
1354                    owner_id: answering_connection_server_id.0 as u32,
1355                    id: answering_connection_id as u32,
1356                };
1357                participants.insert(
1358                    answering_connection,
1359                    proto::Participant {
1360                        user_id: db_participant.user_id.to_proto(),
1361                        peer_id: Some(answering_connection.into()),
1362                        projects: Default::default(),
1363                        location: Some(proto::ParticipantLocation { variant: location }),
1364                        participant_index: participant_index as u32,
1365                        role: db_participant.role.unwrap_or(ChannelRole::Member).into(),
1366                    },
1367                );
1368            } else {
1369                pending_participants.push(proto::PendingParticipant {
1370                    user_id: db_participant.user_id.to_proto(),
1371                    calling_user_id: db_participant.calling_user_id.to_proto(),
1372                    initial_project_id: db_participant.initial_project_id.map(|id| id.to_proto()),
1373                });
1374            }
1375        }
1376        drop(db_participants);
1377
1378        let db_projects = db_room
1379            .find_related(project::Entity)
1380            .find_with_related(worktree::Entity)
1381            .all(tx)
1382            .await?;
1383
1384        for (db_project, db_worktrees) in db_projects {
1385            let host_connection = db_project.host_connection()?;
1386            if let Some(participant) = participants.get_mut(&host_connection) {
1387                participant.projects.push(proto::ParticipantProject {
1388                    id: db_project.id.to_proto(),
1389                    worktree_root_names: Default::default(),
1390                });
1391                let project = participant.projects.last_mut().unwrap();
1392
1393                for db_worktree in db_worktrees {
1394                    if db_worktree.visible {
1395                        project.worktree_root_names.push(db_worktree.root_name);
1396                    }
1397                }
1398            }
1399        }
1400
1401        let mut db_followers = db_room.find_related(follower::Entity).stream(tx).await?;
1402        let mut followers = Vec::new();
1403        while let Some(db_follower) = db_followers.next().await {
1404            let db_follower = db_follower?;
1405            followers.push(proto::Follower {
1406                leader_id: Some(db_follower.leader_connection().into()),
1407                follower_id: Some(db_follower.follower_connection().into()),
1408                project_id: db_follower.project_id.to_proto(),
1409            });
1410        }
1411        drop(db_followers);
1412
1413        let channel = if let Some(channel_id) = db_room.channel_id {
1414            Some(self.get_channel_internal(channel_id, tx).await?)
1415        } else {
1416            None
1417        };
1418
1419        Ok((
1420            channel,
1421            proto::Room {
1422                id: db_room.id.to_proto(),
1423                livekit_room: db_room.live_kit_room,
1424                participants: participants.into_values().collect(),
1425                pending_participants,
1426                followers,
1427            },
1428        ))
1429    }
1430}