db.rs

   1mod access_token;
   2mod contact;
   3mod language_server;
   4mod project;
   5mod project_collaborator;
   6mod room;
   7mod room_participant;
   8mod signup;
   9#[cfg(test)]
  10mod tests;
  11mod user;
  12mod worktree;
  13mod worktree_diagnostic_summary;
  14mod worktree_entry;
  15
  16use crate::{Error, Result};
  17use anyhow::anyhow;
  18use collections::{BTreeMap, HashMap, HashSet};
  19pub use contact::Contact;
  20use dashmap::DashMap;
  21use futures::StreamExt;
  22use hyper::StatusCode;
  23use rpc::{proto, ConnectionId};
  24pub use sea_orm::ConnectOptions;
  25use sea_orm::{
  26    entity::prelude::*, ActiveValue, ConnectionTrait, DatabaseConnection, DatabaseTransaction,
  27    DbErr, FromQueryResult, IntoActiveModel, IsolationLevel, JoinType, QueryOrder, QuerySelect,
  28    Statement, TransactionTrait,
  29};
  30use sea_query::{Alias, Expr, OnConflict, Query};
  31use serde::{Deserialize, Serialize};
  32pub use signup::{Invite, NewSignup, WaitlistSummary};
  33use sqlx::migrate::{Migrate, Migration, MigrationSource};
  34use sqlx::Connection;
  35use std::ops::{Deref, DerefMut};
  36use std::path::Path;
  37use std::time::Duration;
  38use std::{future::Future, marker::PhantomData, rc::Rc, sync::Arc};
  39use tokio::sync::{Mutex, OwnedMutexGuard};
  40pub use user::Model as User;
  41
  42pub struct Database {
  43    options: ConnectOptions,
  44    pool: DatabaseConnection,
  45    rooms: DashMap<RoomId, Arc<Mutex<()>>>,
  46    #[cfg(test)]
  47    background: Option<std::sync::Arc<gpui::executor::Background>>,
  48    #[cfg(test)]
  49    runtime: Option<tokio::runtime::Runtime>,
  50    epoch: Uuid,
  51}
  52
  53impl Database {
  54    pub async fn new(options: ConnectOptions) -> Result<Self> {
  55        Ok(Self {
  56            options: options.clone(),
  57            pool: sea_orm::Database::connect(options).await?,
  58            rooms: DashMap::with_capacity(16384),
  59            #[cfg(test)]
  60            background: None,
  61            #[cfg(test)]
  62            runtime: None,
  63            epoch: Uuid::new_v4(),
  64        })
  65    }
  66
  67    pub async fn migrate(
  68        &self,
  69        migrations_path: &Path,
  70        ignore_checksum_mismatch: bool,
  71    ) -> anyhow::Result<Vec<(Migration, Duration)>> {
  72        let migrations = MigrationSource::resolve(migrations_path)
  73            .await
  74            .map_err(|err| anyhow!("failed to load migrations: {err:?}"))?;
  75
  76        let mut connection = sqlx::AnyConnection::connect(self.options.get_url()).await?;
  77
  78        connection.ensure_migrations_table().await?;
  79        let applied_migrations: HashMap<_, _> = connection
  80            .list_applied_migrations()
  81            .await?
  82            .into_iter()
  83            .map(|m| (m.version, m))
  84            .collect();
  85
  86        let mut new_migrations = Vec::new();
  87        for migration in migrations {
  88            match applied_migrations.get(&migration.version) {
  89                Some(applied_migration) => {
  90                    if migration.checksum != applied_migration.checksum && !ignore_checksum_mismatch
  91                    {
  92                        Err(anyhow!(
  93                            "checksum mismatch for applied migration {}",
  94                            migration.description
  95                        ))?;
  96                    }
  97                }
  98                None => {
  99                    let elapsed = connection.apply(&migration).await?;
 100                    new_migrations.push((migration, elapsed));
 101                }
 102            }
 103        }
 104
 105        Ok(new_migrations)
 106    }
 107
 108    pub async fn clear_stale_data(&self) -> Result<()> {
 109        self.transaction(|tx| async move {
 110            project_collaborator::Entity::delete_many()
 111                .filter(project_collaborator::Column::ConnectionEpoch.ne(self.epoch))
 112                .exec(&*tx)
 113                .await?;
 114            room_participant::Entity::delete_many()
 115                .filter(
 116                    room_participant::Column::AnsweringConnectionEpoch
 117                        .ne(self.epoch)
 118                        .or(room_participant::Column::CallingConnectionEpoch.ne(self.epoch)),
 119                )
 120                .exec(&*tx)
 121                .await?;
 122            project::Entity::delete_many()
 123                .filter(project::Column::HostConnectionEpoch.ne(self.epoch))
 124                .exec(&*tx)
 125                .await?;
 126            room::Entity::delete_many()
 127                .filter(
 128                    room::Column::Id.not_in_subquery(
 129                        Query::select()
 130                            .column(room_participant::Column::RoomId)
 131                            .from(room_participant::Entity)
 132                            .distinct()
 133                            .to_owned(),
 134                    ),
 135                )
 136                .exec(&*tx)
 137                .await?;
 138            Ok(())
 139        })
 140        .await
 141    }
 142
 143    // users
 144
 145    pub async fn create_user(
 146        &self,
 147        email_address: &str,
 148        admin: bool,
 149        params: NewUserParams,
 150    ) -> Result<NewUserResult> {
 151        self.transaction(|tx| async {
 152            let tx = tx;
 153            let user = user::Entity::insert(user::ActiveModel {
 154                email_address: ActiveValue::set(Some(email_address.into())),
 155                github_login: ActiveValue::set(params.github_login.clone()),
 156                github_user_id: ActiveValue::set(Some(params.github_user_id)),
 157                admin: ActiveValue::set(admin),
 158                metrics_id: ActiveValue::set(Uuid::new_v4()),
 159                ..Default::default()
 160            })
 161            .on_conflict(
 162                OnConflict::column(user::Column::GithubLogin)
 163                    .update_column(user::Column::GithubLogin)
 164                    .to_owned(),
 165            )
 166            .exec_with_returning(&*tx)
 167            .await?;
 168
 169            Ok(NewUserResult {
 170                user_id: user.id,
 171                metrics_id: user.metrics_id.to_string(),
 172                signup_device_id: None,
 173                inviting_user_id: None,
 174            })
 175        })
 176        .await
 177    }
 178
 179    pub async fn get_user_by_id(&self, id: UserId) -> Result<Option<user::Model>> {
 180        self.transaction(|tx| async move { Ok(user::Entity::find_by_id(id).one(&*tx).await?) })
 181            .await
 182    }
 183
 184    pub async fn get_users_by_ids(&self, ids: Vec<UserId>) -> Result<Vec<user::Model>> {
 185        self.transaction(|tx| async {
 186            let tx = tx;
 187            Ok(user::Entity::find()
 188                .filter(user::Column::Id.is_in(ids.iter().copied()))
 189                .all(&*tx)
 190                .await?)
 191        })
 192        .await
 193    }
 194
 195    pub async fn get_user_by_github_account(
 196        &self,
 197        github_login: &str,
 198        github_user_id: Option<i32>,
 199    ) -> Result<Option<User>> {
 200        self.transaction(|tx| async move {
 201            let tx = &*tx;
 202            if let Some(github_user_id) = github_user_id {
 203                if let Some(user_by_github_user_id) = user::Entity::find()
 204                    .filter(user::Column::GithubUserId.eq(github_user_id))
 205                    .one(tx)
 206                    .await?
 207                {
 208                    let mut user_by_github_user_id = user_by_github_user_id.into_active_model();
 209                    user_by_github_user_id.github_login = ActiveValue::set(github_login.into());
 210                    Ok(Some(user_by_github_user_id.update(tx).await?))
 211                } else if let Some(user_by_github_login) = user::Entity::find()
 212                    .filter(user::Column::GithubLogin.eq(github_login))
 213                    .one(tx)
 214                    .await?
 215                {
 216                    let mut user_by_github_login = user_by_github_login.into_active_model();
 217                    user_by_github_login.github_user_id = ActiveValue::set(Some(github_user_id));
 218                    Ok(Some(user_by_github_login.update(tx).await?))
 219                } else {
 220                    Ok(None)
 221                }
 222            } else {
 223                Ok(user::Entity::find()
 224                    .filter(user::Column::GithubLogin.eq(github_login))
 225                    .one(tx)
 226                    .await?)
 227            }
 228        })
 229        .await
 230    }
 231
 232    pub async fn get_all_users(&self, page: u32, limit: u32) -> Result<Vec<User>> {
 233        self.transaction(|tx| async move {
 234            Ok(user::Entity::find()
 235                .order_by_asc(user::Column::GithubLogin)
 236                .limit(limit as u64)
 237                .offset(page as u64 * limit as u64)
 238                .all(&*tx)
 239                .await?)
 240        })
 241        .await
 242    }
 243
 244    pub async fn get_users_with_no_invites(
 245        &self,
 246        invited_by_another_user: bool,
 247    ) -> Result<Vec<User>> {
 248        self.transaction(|tx| async move {
 249            Ok(user::Entity::find()
 250                .filter(
 251                    user::Column::InviteCount
 252                        .eq(0)
 253                        .and(if invited_by_another_user {
 254                            user::Column::InviterId.is_not_null()
 255                        } else {
 256                            user::Column::InviterId.is_null()
 257                        }),
 258                )
 259                .all(&*tx)
 260                .await?)
 261        })
 262        .await
 263    }
 264
 265    pub async fn get_user_metrics_id(&self, id: UserId) -> Result<String> {
 266        #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
 267        enum QueryAs {
 268            MetricsId,
 269        }
 270
 271        self.transaction(|tx| async move {
 272            let metrics_id: Uuid = user::Entity::find_by_id(id)
 273                .select_only()
 274                .column(user::Column::MetricsId)
 275                .into_values::<_, QueryAs>()
 276                .one(&*tx)
 277                .await?
 278                .ok_or_else(|| anyhow!("could not find user"))?;
 279            Ok(metrics_id.to_string())
 280        })
 281        .await
 282    }
 283
 284    pub async fn set_user_is_admin(&self, id: UserId, is_admin: bool) -> Result<()> {
 285        self.transaction(|tx| async move {
 286            user::Entity::update_many()
 287                .filter(user::Column::Id.eq(id))
 288                .set(user::ActiveModel {
 289                    admin: ActiveValue::set(is_admin),
 290                    ..Default::default()
 291                })
 292                .exec(&*tx)
 293                .await?;
 294            Ok(())
 295        })
 296        .await
 297    }
 298
 299    pub async fn set_user_connected_once(&self, id: UserId, connected_once: bool) -> Result<()> {
 300        self.transaction(|tx| async move {
 301            user::Entity::update_many()
 302                .filter(user::Column::Id.eq(id))
 303                .set(user::ActiveModel {
 304                    connected_once: ActiveValue::set(connected_once),
 305                    ..Default::default()
 306                })
 307                .exec(&*tx)
 308                .await?;
 309            Ok(())
 310        })
 311        .await
 312    }
 313
 314    pub async fn destroy_user(&self, id: UserId) -> Result<()> {
 315        self.transaction(|tx| async move {
 316            access_token::Entity::delete_many()
 317                .filter(access_token::Column::UserId.eq(id))
 318                .exec(&*tx)
 319                .await?;
 320            user::Entity::delete_by_id(id).exec(&*tx).await?;
 321            Ok(())
 322        })
 323        .await
 324    }
 325
 326    // contacts
 327
 328    pub async fn get_contacts(&self, user_id: UserId) -> Result<Vec<Contact>> {
 329        #[derive(Debug, FromQueryResult)]
 330        struct ContactWithUserBusyStatuses {
 331            user_id_a: UserId,
 332            user_id_b: UserId,
 333            a_to_b: bool,
 334            accepted: bool,
 335            should_notify: bool,
 336            user_a_busy: bool,
 337            user_b_busy: bool,
 338        }
 339
 340        self.transaction(|tx| async move {
 341            let user_a_participant = Alias::new("user_a_participant");
 342            let user_b_participant = Alias::new("user_b_participant");
 343            let mut db_contacts = contact::Entity::find()
 344                .column_as(
 345                    Expr::tbl(user_a_participant.clone(), room_participant::Column::Id)
 346                        .is_not_null(),
 347                    "user_a_busy",
 348                )
 349                .column_as(
 350                    Expr::tbl(user_b_participant.clone(), room_participant::Column::Id)
 351                        .is_not_null(),
 352                    "user_b_busy",
 353                )
 354                .filter(
 355                    contact::Column::UserIdA
 356                        .eq(user_id)
 357                        .or(contact::Column::UserIdB.eq(user_id)),
 358                )
 359                .join_as(
 360                    JoinType::LeftJoin,
 361                    contact::Relation::UserARoomParticipant.def(),
 362                    user_a_participant,
 363                )
 364                .join_as(
 365                    JoinType::LeftJoin,
 366                    contact::Relation::UserBRoomParticipant.def(),
 367                    user_b_participant,
 368                )
 369                .into_model::<ContactWithUserBusyStatuses>()
 370                .stream(&*tx)
 371                .await?;
 372
 373            let mut contacts = Vec::new();
 374            while let Some(db_contact) = db_contacts.next().await {
 375                let db_contact = db_contact?;
 376                if db_contact.user_id_a == user_id {
 377                    if db_contact.accepted {
 378                        contacts.push(Contact::Accepted {
 379                            user_id: db_contact.user_id_b,
 380                            should_notify: db_contact.should_notify && db_contact.a_to_b,
 381                            busy: db_contact.user_b_busy,
 382                        });
 383                    } else if db_contact.a_to_b {
 384                        contacts.push(Contact::Outgoing {
 385                            user_id: db_contact.user_id_b,
 386                        })
 387                    } else {
 388                        contacts.push(Contact::Incoming {
 389                            user_id: db_contact.user_id_b,
 390                            should_notify: db_contact.should_notify,
 391                        });
 392                    }
 393                } else if db_contact.accepted {
 394                    contacts.push(Contact::Accepted {
 395                        user_id: db_contact.user_id_a,
 396                        should_notify: db_contact.should_notify && !db_contact.a_to_b,
 397                        busy: db_contact.user_a_busy,
 398                    });
 399                } else if db_contact.a_to_b {
 400                    contacts.push(Contact::Incoming {
 401                        user_id: db_contact.user_id_a,
 402                        should_notify: db_contact.should_notify,
 403                    });
 404                } else {
 405                    contacts.push(Contact::Outgoing {
 406                        user_id: db_contact.user_id_a,
 407                    });
 408                }
 409            }
 410
 411            contacts.sort_unstable_by_key(|contact| contact.user_id());
 412
 413            Ok(contacts)
 414        })
 415        .await
 416    }
 417
 418    pub async fn is_user_busy(&self, user_id: UserId) -> Result<bool> {
 419        self.transaction(|tx| async move {
 420            let participant = room_participant::Entity::find()
 421                .filter(room_participant::Column::UserId.eq(user_id))
 422                .one(&*tx)
 423                .await?;
 424            Ok(participant.is_some())
 425        })
 426        .await
 427    }
 428
 429    pub async fn has_contact(&self, user_id_1: UserId, user_id_2: UserId) -> Result<bool> {
 430        self.transaction(|tx| async move {
 431            let (id_a, id_b) = if user_id_1 < user_id_2 {
 432                (user_id_1, user_id_2)
 433            } else {
 434                (user_id_2, user_id_1)
 435            };
 436
 437            Ok(contact::Entity::find()
 438                .filter(
 439                    contact::Column::UserIdA
 440                        .eq(id_a)
 441                        .and(contact::Column::UserIdB.eq(id_b))
 442                        .and(contact::Column::Accepted.eq(true)),
 443                )
 444                .one(&*tx)
 445                .await?
 446                .is_some())
 447        })
 448        .await
 449    }
 450
 451    pub async fn send_contact_request(&self, sender_id: UserId, receiver_id: UserId) -> Result<()> {
 452        self.transaction(|tx| async move {
 453            let (id_a, id_b, a_to_b) = if sender_id < receiver_id {
 454                (sender_id, receiver_id, true)
 455            } else {
 456                (receiver_id, sender_id, false)
 457            };
 458
 459            let rows_affected = contact::Entity::insert(contact::ActiveModel {
 460                user_id_a: ActiveValue::set(id_a),
 461                user_id_b: ActiveValue::set(id_b),
 462                a_to_b: ActiveValue::set(a_to_b),
 463                accepted: ActiveValue::set(false),
 464                should_notify: ActiveValue::set(true),
 465                ..Default::default()
 466            })
 467            .on_conflict(
 468                OnConflict::columns([contact::Column::UserIdA, contact::Column::UserIdB])
 469                    .values([
 470                        (contact::Column::Accepted, true.into()),
 471                        (contact::Column::ShouldNotify, false.into()),
 472                    ])
 473                    .action_and_where(
 474                        contact::Column::Accepted.eq(false).and(
 475                            contact::Column::AToB
 476                                .eq(a_to_b)
 477                                .and(contact::Column::UserIdA.eq(id_b))
 478                                .or(contact::Column::AToB
 479                                    .ne(a_to_b)
 480                                    .and(contact::Column::UserIdA.eq(id_a))),
 481                        ),
 482                    )
 483                    .to_owned(),
 484            )
 485            .exec_without_returning(&*tx)
 486            .await?;
 487
 488            if rows_affected == 1 {
 489                Ok(())
 490            } else {
 491                Err(anyhow!("contact already requested"))?
 492            }
 493        })
 494        .await
 495    }
 496
 497    pub async fn remove_contact(&self, requester_id: UserId, responder_id: UserId) -> Result<()> {
 498        self.transaction(|tx| async move {
 499            let (id_a, id_b) = if responder_id < requester_id {
 500                (responder_id, requester_id)
 501            } else {
 502                (requester_id, responder_id)
 503            };
 504
 505            let result = contact::Entity::delete_many()
 506                .filter(
 507                    contact::Column::UserIdA
 508                        .eq(id_a)
 509                        .and(contact::Column::UserIdB.eq(id_b)),
 510                )
 511                .exec(&*tx)
 512                .await?;
 513
 514            if result.rows_affected == 1 {
 515                Ok(())
 516            } else {
 517                Err(anyhow!("no such contact"))?
 518            }
 519        })
 520        .await
 521    }
 522
 523    pub async fn dismiss_contact_notification(
 524        &self,
 525        user_id: UserId,
 526        contact_user_id: UserId,
 527    ) -> Result<()> {
 528        self.transaction(|tx| async move {
 529            let (id_a, id_b, a_to_b) = if user_id < contact_user_id {
 530                (user_id, contact_user_id, true)
 531            } else {
 532                (contact_user_id, user_id, false)
 533            };
 534
 535            let result = contact::Entity::update_many()
 536                .set(contact::ActiveModel {
 537                    should_notify: ActiveValue::set(false),
 538                    ..Default::default()
 539                })
 540                .filter(
 541                    contact::Column::UserIdA
 542                        .eq(id_a)
 543                        .and(contact::Column::UserIdB.eq(id_b))
 544                        .and(
 545                            contact::Column::AToB
 546                                .eq(a_to_b)
 547                                .and(contact::Column::Accepted.eq(true))
 548                                .or(contact::Column::AToB
 549                                    .ne(a_to_b)
 550                                    .and(contact::Column::Accepted.eq(false))),
 551                        ),
 552                )
 553                .exec(&*tx)
 554                .await?;
 555            if result.rows_affected == 0 {
 556                Err(anyhow!("no such contact request"))?
 557            } else {
 558                Ok(())
 559            }
 560        })
 561        .await
 562    }
 563
 564    pub async fn respond_to_contact_request(
 565        &self,
 566        responder_id: UserId,
 567        requester_id: UserId,
 568        accept: bool,
 569    ) -> Result<()> {
 570        self.transaction(|tx| async move {
 571            let (id_a, id_b, a_to_b) = if responder_id < requester_id {
 572                (responder_id, requester_id, false)
 573            } else {
 574                (requester_id, responder_id, true)
 575            };
 576            let rows_affected = if accept {
 577                let result = contact::Entity::update_many()
 578                    .set(contact::ActiveModel {
 579                        accepted: ActiveValue::set(true),
 580                        should_notify: ActiveValue::set(true),
 581                        ..Default::default()
 582                    })
 583                    .filter(
 584                        contact::Column::UserIdA
 585                            .eq(id_a)
 586                            .and(contact::Column::UserIdB.eq(id_b))
 587                            .and(contact::Column::AToB.eq(a_to_b)),
 588                    )
 589                    .exec(&*tx)
 590                    .await?;
 591                result.rows_affected
 592            } else {
 593                let result = contact::Entity::delete_many()
 594                    .filter(
 595                        contact::Column::UserIdA
 596                            .eq(id_a)
 597                            .and(contact::Column::UserIdB.eq(id_b))
 598                            .and(contact::Column::AToB.eq(a_to_b))
 599                            .and(contact::Column::Accepted.eq(false)),
 600                    )
 601                    .exec(&*tx)
 602                    .await?;
 603
 604                result.rows_affected
 605            };
 606
 607            if rows_affected == 1 {
 608                Ok(())
 609            } else {
 610                Err(anyhow!("no such contact request"))?
 611            }
 612        })
 613        .await
 614    }
 615
 616    pub fn fuzzy_like_string(string: &str) -> String {
 617        let mut result = String::with_capacity(string.len() * 2 + 1);
 618        for c in string.chars() {
 619            if c.is_alphanumeric() {
 620                result.push('%');
 621                result.push(c);
 622            }
 623        }
 624        result.push('%');
 625        result
 626    }
 627
 628    pub async fn fuzzy_search_users(&self, name_query: &str, limit: u32) -> Result<Vec<User>> {
 629        self.transaction(|tx| async {
 630            let tx = tx;
 631            let like_string = Self::fuzzy_like_string(name_query);
 632            let query = "
 633                SELECT users.*
 634                FROM users
 635                WHERE github_login ILIKE $1
 636                ORDER BY github_login <-> $2
 637                LIMIT $3
 638            ";
 639
 640            Ok(user::Entity::find()
 641                .from_raw_sql(Statement::from_sql_and_values(
 642                    self.pool.get_database_backend(),
 643                    query.into(),
 644                    vec![like_string.into(), name_query.into(), limit.into()],
 645                ))
 646                .all(&*tx)
 647                .await?)
 648        })
 649        .await
 650    }
 651
 652    // signups
 653
 654    pub async fn create_signup(&self, signup: &NewSignup) -> Result<()> {
 655        self.transaction(|tx| async move {
 656            signup::Entity::insert(signup::ActiveModel {
 657                email_address: ActiveValue::set(signup.email_address.clone()),
 658                email_confirmation_code: ActiveValue::set(random_email_confirmation_code()),
 659                email_confirmation_sent: ActiveValue::set(false),
 660                platform_mac: ActiveValue::set(signup.platform_mac),
 661                platform_windows: ActiveValue::set(signup.platform_windows),
 662                platform_linux: ActiveValue::set(signup.platform_linux),
 663                platform_unknown: ActiveValue::set(false),
 664                editor_features: ActiveValue::set(Some(signup.editor_features.clone())),
 665                programming_languages: ActiveValue::set(Some(signup.programming_languages.clone())),
 666                device_id: ActiveValue::set(signup.device_id.clone()),
 667                added_to_mailing_list: ActiveValue::set(signup.added_to_mailing_list),
 668                ..Default::default()
 669            })
 670            .on_conflict(
 671                OnConflict::column(signup::Column::EmailAddress)
 672                    .update_columns([
 673                        signup::Column::PlatformMac,
 674                        signup::Column::PlatformWindows,
 675                        signup::Column::PlatformLinux,
 676                        signup::Column::EditorFeatures,
 677                        signup::Column::ProgrammingLanguages,
 678                        signup::Column::DeviceId,
 679                        signup::Column::AddedToMailingList,
 680                    ])
 681                    .to_owned(),
 682            )
 683            .exec(&*tx)
 684            .await?;
 685            Ok(())
 686        })
 687        .await
 688    }
 689
 690    pub async fn get_signup(&self, email_address: &str) -> Result<signup::Model> {
 691        self.transaction(|tx| async move {
 692            let signup = signup::Entity::find()
 693                .filter(signup::Column::EmailAddress.eq(email_address))
 694                .one(&*tx)
 695                .await?
 696                .ok_or_else(|| {
 697                    anyhow!("signup with email address {} doesn't exist", email_address)
 698                })?;
 699
 700            Ok(signup)
 701        })
 702        .await
 703    }
 704
 705    pub async fn get_waitlist_summary(&self) -> Result<WaitlistSummary> {
 706        self.transaction(|tx| async move {
 707            let query = "
 708                SELECT
 709                    COUNT(*) as count,
 710                    COALESCE(SUM(CASE WHEN platform_linux THEN 1 ELSE 0 END), 0) as linux_count,
 711                    COALESCE(SUM(CASE WHEN platform_mac THEN 1 ELSE 0 END), 0) as mac_count,
 712                    COALESCE(SUM(CASE WHEN platform_windows THEN 1 ELSE 0 END), 0) as windows_count,
 713                    COALESCE(SUM(CASE WHEN platform_unknown THEN 1 ELSE 0 END), 0) as unknown_count
 714                FROM (
 715                    SELECT *
 716                    FROM signups
 717                    WHERE
 718                        NOT email_confirmation_sent
 719                ) AS unsent
 720            ";
 721            Ok(
 722                WaitlistSummary::find_by_statement(Statement::from_sql_and_values(
 723                    self.pool.get_database_backend(),
 724                    query.into(),
 725                    vec![],
 726                ))
 727                .one(&*tx)
 728                .await?
 729                .ok_or_else(|| anyhow!("invalid result"))?,
 730            )
 731        })
 732        .await
 733    }
 734
 735    pub async fn record_sent_invites(&self, invites: &[Invite]) -> Result<()> {
 736        let emails = invites
 737            .iter()
 738            .map(|s| s.email_address.as_str())
 739            .collect::<Vec<_>>();
 740        self.transaction(|tx| async {
 741            let tx = tx;
 742            signup::Entity::update_many()
 743                .filter(signup::Column::EmailAddress.is_in(emails.iter().copied()))
 744                .set(signup::ActiveModel {
 745                    email_confirmation_sent: ActiveValue::set(true),
 746                    ..Default::default()
 747                })
 748                .exec(&*tx)
 749                .await?;
 750            Ok(())
 751        })
 752        .await
 753    }
 754
 755    pub async fn get_unsent_invites(&self, count: usize) -> Result<Vec<Invite>> {
 756        self.transaction(|tx| async move {
 757            Ok(signup::Entity::find()
 758                .select_only()
 759                .column(signup::Column::EmailAddress)
 760                .column(signup::Column::EmailConfirmationCode)
 761                .filter(
 762                    signup::Column::EmailConfirmationSent.eq(false).and(
 763                        signup::Column::PlatformMac
 764                            .eq(true)
 765                            .or(signup::Column::PlatformUnknown.eq(true)),
 766                    ),
 767                )
 768                .order_by_asc(signup::Column::CreatedAt)
 769                .limit(count as u64)
 770                .into_model()
 771                .all(&*tx)
 772                .await?)
 773        })
 774        .await
 775    }
 776
 777    // invite codes
 778
 779    pub async fn create_invite_from_code(
 780        &self,
 781        code: &str,
 782        email_address: &str,
 783        device_id: Option<&str>,
 784    ) -> Result<Invite> {
 785        self.transaction(|tx| async move {
 786            let existing_user = user::Entity::find()
 787                .filter(user::Column::EmailAddress.eq(email_address))
 788                .one(&*tx)
 789                .await?;
 790
 791            if existing_user.is_some() {
 792                Err(anyhow!("email address is already in use"))?;
 793            }
 794
 795            let inviting_user_with_invites = match user::Entity::find()
 796                .filter(
 797                    user::Column::InviteCode
 798                        .eq(code)
 799                        .and(user::Column::InviteCount.gt(0)),
 800                )
 801                .one(&*tx)
 802                .await?
 803            {
 804                Some(inviting_user) => inviting_user,
 805                None => {
 806                    return Err(Error::Http(
 807                        StatusCode::UNAUTHORIZED,
 808                        "unable to find an invite code with invites remaining".to_string(),
 809                    ))?
 810                }
 811            };
 812            user::Entity::update_many()
 813                .filter(
 814                    user::Column::Id
 815                        .eq(inviting_user_with_invites.id)
 816                        .and(user::Column::InviteCount.gt(0)),
 817                )
 818                .col_expr(
 819                    user::Column::InviteCount,
 820                    Expr::col(user::Column::InviteCount).sub(1),
 821                )
 822                .exec(&*tx)
 823                .await?;
 824
 825            let signup = signup::Entity::insert(signup::ActiveModel {
 826                email_address: ActiveValue::set(email_address.into()),
 827                email_confirmation_code: ActiveValue::set(random_email_confirmation_code()),
 828                email_confirmation_sent: ActiveValue::set(false),
 829                inviting_user_id: ActiveValue::set(Some(inviting_user_with_invites.id)),
 830                platform_linux: ActiveValue::set(false),
 831                platform_mac: ActiveValue::set(false),
 832                platform_windows: ActiveValue::set(false),
 833                platform_unknown: ActiveValue::set(true),
 834                device_id: ActiveValue::set(device_id.map(|device_id| device_id.into())),
 835                ..Default::default()
 836            })
 837            .on_conflict(
 838                OnConflict::column(signup::Column::EmailAddress)
 839                    .update_column(signup::Column::InvitingUserId)
 840                    .to_owned(),
 841            )
 842            .exec_with_returning(&*tx)
 843            .await?;
 844
 845            Ok(Invite {
 846                email_address: signup.email_address,
 847                email_confirmation_code: signup.email_confirmation_code,
 848            })
 849        })
 850        .await
 851    }
 852
 853    pub async fn create_user_from_invite(
 854        &self,
 855        invite: &Invite,
 856        user: NewUserParams,
 857    ) -> Result<Option<NewUserResult>> {
 858        self.transaction(|tx| async {
 859            let tx = tx;
 860            let signup = signup::Entity::find()
 861                .filter(
 862                    signup::Column::EmailAddress
 863                        .eq(invite.email_address.as_str())
 864                        .and(
 865                            signup::Column::EmailConfirmationCode
 866                                .eq(invite.email_confirmation_code.as_str()),
 867                        ),
 868                )
 869                .one(&*tx)
 870                .await?
 871                .ok_or_else(|| Error::Http(StatusCode::NOT_FOUND, "no such invite".to_string()))?;
 872
 873            if signup.user_id.is_some() {
 874                return Ok(None);
 875            }
 876
 877            let user = user::Entity::insert(user::ActiveModel {
 878                email_address: ActiveValue::set(Some(invite.email_address.clone())),
 879                github_login: ActiveValue::set(user.github_login.clone()),
 880                github_user_id: ActiveValue::set(Some(user.github_user_id)),
 881                admin: ActiveValue::set(false),
 882                invite_count: ActiveValue::set(user.invite_count),
 883                invite_code: ActiveValue::set(Some(random_invite_code())),
 884                metrics_id: ActiveValue::set(Uuid::new_v4()),
 885                ..Default::default()
 886            })
 887            .on_conflict(
 888                OnConflict::column(user::Column::GithubLogin)
 889                    .update_columns([
 890                        user::Column::EmailAddress,
 891                        user::Column::GithubUserId,
 892                        user::Column::Admin,
 893                    ])
 894                    .to_owned(),
 895            )
 896            .exec_with_returning(&*tx)
 897            .await?;
 898
 899            let mut signup = signup.into_active_model();
 900            signup.user_id = ActiveValue::set(Some(user.id));
 901            let signup = signup.update(&*tx).await?;
 902
 903            if let Some(inviting_user_id) = signup.inviting_user_id {
 904                let (user_id_a, user_id_b, a_to_b) = if inviting_user_id < user.id {
 905                    (inviting_user_id, user.id, true)
 906                } else {
 907                    (user.id, inviting_user_id, false)
 908                };
 909
 910                contact::Entity::insert(contact::ActiveModel {
 911                    user_id_a: ActiveValue::set(user_id_a),
 912                    user_id_b: ActiveValue::set(user_id_b),
 913                    a_to_b: ActiveValue::set(a_to_b),
 914                    should_notify: ActiveValue::set(true),
 915                    accepted: ActiveValue::set(true),
 916                    ..Default::default()
 917                })
 918                .on_conflict(OnConflict::new().do_nothing().to_owned())
 919                .exec_without_returning(&*tx)
 920                .await?;
 921            }
 922
 923            Ok(Some(NewUserResult {
 924                user_id: user.id,
 925                metrics_id: user.metrics_id.to_string(),
 926                inviting_user_id: signup.inviting_user_id,
 927                signup_device_id: signup.device_id,
 928            }))
 929        })
 930        .await
 931    }
 932
 933    pub async fn set_invite_count_for_user(&self, id: UserId, count: i32) -> Result<()> {
 934        self.transaction(|tx| async move {
 935            if count > 0 {
 936                user::Entity::update_many()
 937                    .filter(
 938                        user::Column::Id
 939                            .eq(id)
 940                            .and(user::Column::InviteCode.is_null()),
 941                    )
 942                    .set(user::ActiveModel {
 943                        invite_code: ActiveValue::set(Some(random_invite_code())),
 944                        ..Default::default()
 945                    })
 946                    .exec(&*tx)
 947                    .await?;
 948            }
 949
 950            user::Entity::update_many()
 951                .filter(user::Column::Id.eq(id))
 952                .set(user::ActiveModel {
 953                    invite_count: ActiveValue::set(count),
 954                    ..Default::default()
 955                })
 956                .exec(&*tx)
 957                .await?;
 958            Ok(())
 959        })
 960        .await
 961    }
 962
 963    pub async fn get_invite_code_for_user(&self, id: UserId) -> Result<Option<(String, i32)>> {
 964        self.transaction(|tx| async move {
 965            match user::Entity::find_by_id(id).one(&*tx).await? {
 966                Some(user) if user.invite_code.is_some() => {
 967                    Ok(Some((user.invite_code.unwrap(), user.invite_count)))
 968                }
 969                _ => Ok(None),
 970            }
 971        })
 972        .await
 973    }
 974
 975    pub async fn get_user_for_invite_code(&self, code: &str) -> Result<User> {
 976        self.transaction(|tx| async move {
 977            user::Entity::find()
 978                .filter(user::Column::InviteCode.eq(code))
 979                .one(&*tx)
 980                .await?
 981                .ok_or_else(|| {
 982                    Error::Http(
 983                        StatusCode::NOT_FOUND,
 984                        "that invite code does not exist".to_string(),
 985                    )
 986                })
 987        })
 988        .await
 989    }
 990
 991    // rooms
 992
 993    pub async fn incoming_call_for_user(
 994        &self,
 995        user_id: UserId,
 996    ) -> Result<Option<proto::IncomingCall>> {
 997        self.transaction(|tx| async move {
 998            let pending_participant = room_participant::Entity::find()
 999                .filter(
1000                    room_participant::Column::UserId
1001                        .eq(user_id)
1002                        .and(room_participant::Column::AnsweringConnectionId.is_null()),
1003                )
1004                .one(&*tx)
1005                .await?;
1006
1007            if let Some(pending_participant) = pending_participant {
1008                let room = self.get_room(pending_participant.room_id, &tx).await?;
1009                Ok(Self::build_incoming_call(&room, user_id))
1010            } else {
1011                Ok(None)
1012            }
1013        })
1014        .await
1015    }
1016
1017    pub async fn create_room(
1018        &self,
1019        user_id: UserId,
1020        connection_id: ConnectionId,
1021        live_kit_room: &str,
1022    ) -> Result<RoomGuard<proto::Room>> {
1023        self.room_transaction(|tx| async move {
1024            let room = room::ActiveModel {
1025                live_kit_room: ActiveValue::set(live_kit_room.into()),
1026                ..Default::default()
1027            }
1028            .insert(&*tx)
1029            .await?;
1030            let room_id = room.id;
1031
1032            room_participant::ActiveModel {
1033                room_id: ActiveValue::set(room_id),
1034                user_id: ActiveValue::set(user_id),
1035                answering_connection_id: ActiveValue::set(Some(connection_id.0 as i32)),
1036                answering_connection_epoch: ActiveValue::set(Some(self.epoch)),
1037                calling_user_id: ActiveValue::set(user_id),
1038                calling_connection_id: ActiveValue::set(connection_id.0 as i32),
1039                calling_connection_epoch: ActiveValue::set(self.epoch),
1040                ..Default::default()
1041            }
1042            .insert(&*tx)
1043            .await?;
1044
1045            let room = self.get_room(room_id, &tx).await?;
1046            Ok((room_id, room))
1047        })
1048        .await
1049    }
1050
1051    pub async fn call(
1052        &self,
1053        room_id: RoomId,
1054        calling_user_id: UserId,
1055        calling_connection_id: ConnectionId,
1056        called_user_id: UserId,
1057        initial_project_id: Option<ProjectId>,
1058    ) -> Result<RoomGuard<(proto::Room, proto::IncomingCall)>> {
1059        self.room_transaction(|tx| async move {
1060            room_participant::ActiveModel {
1061                room_id: ActiveValue::set(room_id),
1062                user_id: ActiveValue::set(called_user_id),
1063                calling_user_id: ActiveValue::set(calling_user_id),
1064                calling_connection_id: ActiveValue::set(calling_connection_id.0 as i32),
1065                calling_connection_epoch: ActiveValue::set(self.epoch),
1066                initial_project_id: ActiveValue::set(initial_project_id),
1067                ..Default::default()
1068            }
1069            .insert(&*tx)
1070            .await?;
1071
1072            let room = self.get_room(room_id, &tx).await?;
1073            let incoming_call = Self::build_incoming_call(&room, called_user_id)
1074                .ok_or_else(|| anyhow!("failed to build incoming call"))?;
1075            Ok((room_id, (room, incoming_call)))
1076        })
1077        .await
1078    }
1079
1080    pub async fn call_failed(
1081        &self,
1082        room_id: RoomId,
1083        called_user_id: UserId,
1084    ) -> Result<RoomGuard<proto::Room>> {
1085        self.room_transaction(|tx| async move {
1086            room_participant::Entity::delete_many()
1087                .filter(
1088                    room_participant::Column::RoomId
1089                        .eq(room_id)
1090                        .and(room_participant::Column::UserId.eq(called_user_id)),
1091                )
1092                .exec(&*tx)
1093                .await?;
1094            let room = self.get_room(room_id, &tx).await?;
1095            Ok((room_id, room))
1096        })
1097        .await
1098    }
1099
1100    pub async fn decline_call(
1101        &self,
1102        expected_room_id: Option<RoomId>,
1103        user_id: UserId,
1104    ) -> Result<RoomGuard<proto::Room>> {
1105        self.room_transaction(|tx| async move {
1106            let participant = room_participant::Entity::find()
1107                .filter(
1108                    room_participant::Column::UserId
1109                        .eq(user_id)
1110                        .and(room_participant::Column::AnsweringConnectionId.is_null()),
1111                )
1112                .one(&*tx)
1113                .await?
1114                .ok_or_else(|| anyhow!("could not decline call"))?;
1115            let room_id = participant.room_id;
1116
1117            if expected_room_id.map_or(false, |expected_room_id| expected_room_id != room_id) {
1118                return Err(anyhow!("declining call on unexpected room"))?;
1119            }
1120
1121            room_participant::Entity::delete(participant.into_active_model())
1122                .exec(&*tx)
1123                .await?;
1124
1125            let room = self.get_room(room_id, &tx).await?;
1126            Ok((room_id, room))
1127        })
1128        .await
1129    }
1130
1131    pub async fn cancel_call(
1132        &self,
1133        expected_room_id: Option<RoomId>,
1134        calling_connection_id: ConnectionId,
1135        called_user_id: UserId,
1136    ) -> Result<RoomGuard<proto::Room>> {
1137        self.room_transaction(|tx| async move {
1138            let participant = room_participant::Entity::find()
1139                .filter(
1140                    room_participant::Column::UserId
1141                        .eq(called_user_id)
1142                        .and(
1143                            room_participant::Column::CallingConnectionId
1144                                .eq(calling_connection_id.0 as i32),
1145                        )
1146                        .and(room_participant::Column::AnsweringConnectionId.is_null()),
1147                )
1148                .one(&*tx)
1149                .await?
1150                .ok_or_else(|| anyhow!("could not cancel call"))?;
1151            let room_id = participant.room_id;
1152            if expected_room_id.map_or(false, |expected_room_id| expected_room_id != room_id) {
1153                return Err(anyhow!("canceling call on unexpected room"))?;
1154            }
1155
1156            room_participant::Entity::delete(participant.into_active_model())
1157                .exec(&*tx)
1158                .await?;
1159
1160            let room = self.get_room(room_id, &tx).await?;
1161            Ok((room_id, room))
1162        })
1163        .await
1164    }
1165
1166    pub async fn join_room(
1167        &self,
1168        room_id: RoomId,
1169        user_id: UserId,
1170        connection_id: ConnectionId,
1171    ) -> Result<RoomGuard<proto::Room>> {
1172        self.room_transaction(|tx| async move {
1173            let result = room_participant::Entity::update_many()
1174                .filter(
1175                    room_participant::Column::RoomId
1176                        .eq(room_id)
1177                        .and(room_participant::Column::UserId.eq(user_id))
1178                        .and(room_participant::Column::AnsweringConnectionId.is_null()),
1179                )
1180                .set(room_participant::ActiveModel {
1181                    answering_connection_id: ActiveValue::set(Some(connection_id.0 as i32)),
1182                    answering_connection_epoch: ActiveValue::set(Some(self.epoch)),
1183                    ..Default::default()
1184                })
1185                .exec(&*tx)
1186                .await?;
1187            if result.rows_affected == 0 {
1188                Err(anyhow!("room does not exist or was already joined"))?
1189            } else {
1190                let room = self.get_room(room_id, &tx).await?;
1191                Ok((room_id, room))
1192            }
1193        })
1194        .await
1195    }
1196
1197    pub async fn leave_room(&self, connection_id: ConnectionId) -> Result<RoomGuard<LeftRoom>> {
1198        self.room_transaction(|tx| async move {
1199            let leaving_participant = room_participant::Entity::find()
1200                .filter(room_participant::Column::AnsweringConnectionId.eq(connection_id.0))
1201                .one(&*tx)
1202                .await?;
1203
1204            if let Some(leaving_participant) = leaving_participant {
1205                // Leave room.
1206                let room_id = leaving_participant.room_id;
1207                room_participant::Entity::delete_by_id(leaving_participant.id)
1208                    .exec(&*tx)
1209                    .await?;
1210
1211                // Cancel pending calls initiated by the leaving user.
1212                let called_participants = room_participant::Entity::find()
1213                    .filter(
1214                        room_participant::Column::CallingConnectionId
1215                            .eq(connection_id.0)
1216                            .and(room_participant::Column::AnsweringConnectionId.is_null()),
1217                    )
1218                    .all(&*tx)
1219                    .await?;
1220                room_participant::Entity::delete_many()
1221                    .filter(
1222                        room_participant::Column::Id
1223                            .is_in(called_participants.iter().map(|participant| participant.id)),
1224                    )
1225                    .exec(&*tx)
1226                    .await?;
1227                let canceled_calls_to_user_ids = called_participants
1228                    .into_iter()
1229                    .map(|participant| participant.user_id)
1230                    .collect();
1231
1232                // Detect left projects.
1233                #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
1234                enum QueryProjectIds {
1235                    ProjectId,
1236                }
1237                let project_ids: Vec<ProjectId> = project_collaborator::Entity::find()
1238                    .select_only()
1239                    .column_as(
1240                        project_collaborator::Column::ProjectId,
1241                        QueryProjectIds::ProjectId,
1242                    )
1243                    .filter(project_collaborator::Column::ConnectionId.eq(connection_id.0))
1244                    .into_values::<_, QueryProjectIds>()
1245                    .all(&*tx)
1246                    .await?;
1247                let mut left_projects = HashMap::default();
1248                let mut collaborators = project_collaborator::Entity::find()
1249                    .filter(project_collaborator::Column::ProjectId.is_in(project_ids))
1250                    .stream(&*tx)
1251                    .await?;
1252                while let Some(collaborator) = collaborators.next().await {
1253                    let collaborator = collaborator?;
1254                    let left_project =
1255                        left_projects
1256                            .entry(collaborator.project_id)
1257                            .or_insert(LeftProject {
1258                                id: collaborator.project_id,
1259                                host_user_id: Default::default(),
1260                                connection_ids: Default::default(),
1261                                host_connection_id: Default::default(),
1262                            });
1263
1264                    let collaborator_connection_id =
1265                        ConnectionId(collaborator.connection_id as u32);
1266                    if collaborator_connection_id != connection_id {
1267                        left_project.connection_ids.push(collaborator_connection_id);
1268                    }
1269
1270                    if collaborator.is_host {
1271                        left_project.host_user_id = collaborator.user_id;
1272                        left_project.host_connection_id =
1273                            ConnectionId(collaborator.connection_id as u32);
1274                    }
1275                }
1276                drop(collaborators);
1277
1278                // Leave projects.
1279                project_collaborator::Entity::delete_many()
1280                    .filter(project_collaborator::Column::ConnectionId.eq(connection_id.0))
1281                    .exec(&*tx)
1282                    .await?;
1283
1284                // Unshare projects.
1285                project::Entity::delete_many()
1286                    .filter(
1287                        project::Column::RoomId
1288                            .eq(room_id)
1289                            .and(project::Column::HostConnectionId.eq(connection_id.0)),
1290                    )
1291                    .exec(&*tx)
1292                    .await?;
1293
1294                let room = self.get_room(room_id, &tx).await?;
1295                if room.participants.is_empty() {
1296                    room::Entity::delete_by_id(room_id).exec(&*tx).await?;
1297                }
1298
1299                let left_room = LeftRoom {
1300                    room,
1301                    left_projects,
1302                    canceled_calls_to_user_ids,
1303                };
1304
1305                if left_room.room.participants.is_empty() {
1306                    self.rooms.remove(&room_id);
1307                }
1308
1309                Ok((room_id, left_room))
1310            } else {
1311                Err(anyhow!("could not leave room"))?
1312            }
1313        })
1314        .await
1315    }
1316
1317    pub async fn update_room_participant_location(
1318        &self,
1319        room_id: RoomId,
1320        connection_id: ConnectionId,
1321        location: proto::ParticipantLocation,
1322    ) -> Result<RoomGuard<proto::Room>> {
1323        self.room_transaction(|tx| async {
1324            let tx = tx;
1325            let location_kind;
1326            let location_project_id;
1327            match location
1328                .variant
1329                .as_ref()
1330                .ok_or_else(|| anyhow!("invalid location"))?
1331            {
1332                proto::participant_location::Variant::SharedProject(project) => {
1333                    location_kind = 0;
1334                    location_project_id = Some(ProjectId::from_proto(project.id));
1335                }
1336                proto::participant_location::Variant::UnsharedProject(_) => {
1337                    location_kind = 1;
1338                    location_project_id = None;
1339                }
1340                proto::participant_location::Variant::External(_) => {
1341                    location_kind = 2;
1342                    location_project_id = None;
1343                }
1344            }
1345
1346            let result = room_participant::Entity::update_many()
1347                .filter(
1348                    room_participant::Column::RoomId
1349                        .eq(room_id)
1350                        .and(room_participant::Column::AnsweringConnectionId.eq(connection_id.0)),
1351                )
1352                .set(room_participant::ActiveModel {
1353                    location_kind: ActiveValue::set(Some(location_kind)),
1354                    location_project_id: ActiveValue::set(location_project_id),
1355                    ..Default::default()
1356                })
1357                .exec(&*tx)
1358                .await?;
1359
1360            if result.rows_affected == 1 {
1361                let room = self.get_room(room_id, &tx).await?;
1362                Ok((room_id, room))
1363            } else {
1364                Err(anyhow!("could not update room participant location"))?
1365            }
1366        })
1367        .await
1368    }
1369
1370    fn build_incoming_call(
1371        room: &proto::Room,
1372        called_user_id: UserId,
1373    ) -> Option<proto::IncomingCall> {
1374        let pending_participant = room
1375            .pending_participants
1376            .iter()
1377            .find(|participant| participant.user_id == called_user_id.to_proto())?;
1378
1379        Some(proto::IncomingCall {
1380            room_id: room.id,
1381            calling_user_id: pending_participant.calling_user_id,
1382            participant_user_ids: room
1383                .participants
1384                .iter()
1385                .map(|participant| participant.user_id)
1386                .collect(),
1387            initial_project: room.participants.iter().find_map(|participant| {
1388                let initial_project_id = pending_participant.initial_project_id?;
1389                participant
1390                    .projects
1391                    .iter()
1392                    .find(|project| project.id == initial_project_id)
1393                    .cloned()
1394            }),
1395        })
1396    }
1397
1398    async fn get_room(&self, room_id: RoomId, tx: &DatabaseTransaction) -> Result<proto::Room> {
1399        let db_room = room::Entity::find_by_id(room_id)
1400            .one(tx)
1401            .await?
1402            .ok_or_else(|| anyhow!("could not find room"))?;
1403
1404        let mut db_participants = db_room
1405            .find_related(room_participant::Entity)
1406            .stream(tx)
1407            .await?;
1408        let mut participants = HashMap::default();
1409        let mut pending_participants = Vec::new();
1410        while let Some(db_participant) = db_participants.next().await {
1411            let db_participant = db_participant?;
1412            if let Some(answering_connection_id) = db_participant.answering_connection_id {
1413                let location = match (
1414                    db_participant.location_kind,
1415                    db_participant.location_project_id,
1416                ) {
1417                    (Some(0), Some(project_id)) => {
1418                        Some(proto::participant_location::Variant::SharedProject(
1419                            proto::participant_location::SharedProject {
1420                                id: project_id.to_proto(),
1421                            },
1422                        ))
1423                    }
1424                    (Some(1), _) => Some(proto::participant_location::Variant::UnsharedProject(
1425                        Default::default(),
1426                    )),
1427                    _ => Some(proto::participant_location::Variant::External(
1428                        Default::default(),
1429                    )),
1430                };
1431                participants.insert(
1432                    answering_connection_id,
1433                    proto::Participant {
1434                        user_id: db_participant.user_id.to_proto(),
1435                        peer_id: answering_connection_id as u32,
1436                        projects: Default::default(),
1437                        location: Some(proto::ParticipantLocation { variant: location }),
1438                    },
1439                );
1440            } else {
1441                pending_participants.push(proto::PendingParticipant {
1442                    user_id: db_participant.user_id.to_proto(),
1443                    calling_user_id: db_participant.calling_user_id.to_proto(),
1444                    initial_project_id: db_participant.initial_project_id.map(|id| id.to_proto()),
1445                });
1446            }
1447        }
1448        drop(db_participants);
1449
1450        let mut db_projects = db_room
1451            .find_related(project::Entity)
1452            .find_with_related(worktree::Entity)
1453            .stream(tx)
1454            .await?;
1455
1456        while let Some(row) = db_projects.next().await {
1457            let (db_project, db_worktree) = row?;
1458            if let Some(participant) = participants.get_mut(&db_project.host_connection_id) {
1459                let project = if let Some(project) = participant
1460                    .projects
1461                    .iter_mut()
1462                    .find(|project| project.id == db_project.id.to_proto())
1463                {
1464                    project
1465                } else {
1466                    participant.projects.push(proto::ParticipantProject {
1467                        id: db_project.id.to_proto(),
1468                        worktree_root_names: Default::default(),
1469                    });
1470                    participant.projects.last_mut().unwrap()
1471                };
1472
1473                if let Some(db_worktree) = db_worktree {
1474                    project.worktree_root_names.push(db_worktree.root_name);
1475                }
1476            }
1477        }
1478
1479        Ok(proto::Room {
1480            id: db_room.id.to_proto(),
1481            live_kit_room: db_room.live_kit_room,
1482            participants: participants.into_values().collect(),
1483            pending_participants,
1484        })
1485    }
1486
1487    // projects
1488
1489    pub async fn project_count_excluding_admins(&self) -> Result<usize> {
1490        #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
1491        enum QueryAs {
1492            Count,
1493        }
1494
1495        self.transaction(|tx| async move {
1496            Ok(project::Entity::find()
1497                .select_only()
1498                .column_as(project::Column::Id.count(), QueryAs::Count)
1499                .inner_join(user::Entity)
1500                .filter(user::Column::Admin.eq(false))
1501                .into_values::<_, QueryAs>()
1502                .one(&*tx)
1503                .await?
1504                .unwrap_or(0i64) as usize)
1505        })
1506        .await
1507    }
1508
1509    pub async fn share_project(
1510        &self,
1511        room_id: RoomId,
1512        connection_id: ConnectionId,
1513        worktrees: &[proto::WorktreeMetadata],
1514    ) -> Result<RoomGuard<(ProjectId, proto::Room)>> {
1515        self.room_transaction(|tx| async move {
1516            let participant = room_participant::Entity::find()
1517                .filter(room_participant::Column::AnsweringConnectionId.eq(connection_id.0))
1518                .one(&*tx)
1519                .await?
1520                .ok_or_else(|| anyhow!("could not find participant"))?;
1521            if participant.room_id != room_id {
1522                return Err(anyhow!("shared project on unexpected room"))?;
1523            }
1524
1525            let project = project::ActiveModel {
1526                room_id: ActiveValue::set(participant.room_id),
1527                host_user_id: ActiveValue::set(participant.user_id),
1528                host_connection_id: ActiveValue::set(connection_id.0 as i32),
1529                host_connection_epoch: ActiveValue::set(self.epoch),
1530                ..Default::default()
1531            }
1532            .insert(&*tx)
1533            .await?;
1534
1535            if !worktrees.is_empty() {
1536                worktree::Entity::insert_many(worktrees.iter().map(|worktree| {
1537                    worktree::ActiveModel {
1538                        id: ActiveValue::set(worktree.id as i64),
1539                        project_id: ActiveValue::set(project.id),
1540                        abs_path: ActiveValue::set(worktree.abs_path.clone()),
1541                        root_name: ActiveValue::set(worktree.root_name.clone()),
1542                        visible: ActiveValue::set(worktree.visible),
1543                        scan_id: ActiveValue::set(0),
1544                        is_complete: ActiveValue::set(false),
1545                    }
1546                }))
1547                .exec(&*tx)
1548                .await?;
1549            }
1550
1551            project_collaborator::ActiveModel {
1552                project_id: ActiveValue::set(project.id),
1553                connection_id: ActiveValue::set(connection_id.0 as i32),
1554                connection_epoch: ActiveValue::set(self.epoch),
1555                user_id: ActiveValue::set(participant.user_id),
1556                replica_id: ActiveValue::set(ReplicaId(0)),
1557                is_host: ActiveValue::set(true),
1558                ..Default::default()
1559            }
1560            .insert(&*tx)
1561            .await?;
1562
1563            let room = self.get_room(room_id, &tx).await?;
1564            Ok((room_id, (project.id, room)))
1565        })
1566        .await
1567    }
1568
1569    pub async fn unshare_project(
1570        &self,
1571        project_id: ProjectId,
1572        connection_id: ConnectionId,
1573    ) -> Result<RoomGuard<(proto::Room, Vec<ConnectionId>)>> {
1574        self.room_transaction(|tx| async move {
1575            let guest_connection_ids = self.project_guest_connection_ids(project_id, &tx).await?;
1576
1577            let project = project::Entity::find_by_id(project_id)
1578                .one(&*tx)
1579                .await?
1580                .ok_or_else(|| anyhow!("project not found"))?;
1581            if project.host_connection_id == connection_id.0 as i32 {
1582                let room_id = project.room_id;
1583                project::Entity::delete(project.into_active_model())
1584                    .exec(&*tx)
1585                    .await?;
1586                let room = self.get_room(room_id, &tx).await?;
1587                Ok((room_id, (room, guest_connection_ids)))
1588            } else {
1589                Err(anyhow!("cannot unshare a project hosted by another user"))?
1590            }
1591        })
1592        .await
1593    }
1594
1595    pub async fn update_project(
1596        &self,
1597        project_id: ProjectId,
1598        connection_id: ConnectionId,
1599        worktrees: &[proto::WorktreeMetadata],
1600    ) -> Result<RoomGuard<(proto::Room, Vec<ConnectionId>)>> {
1601        self.room_transaction(|tx| async move {
1602            let project = project::Entity::find_by_id(project_id)
1603                .filter(project::Column::HostConnectionId.eq(connection_id.0))
1604                .one(&*tx)
1605                .await?
1606                .ok_or_else(|| anyhow!("no such project"))?;
1607
1608            if !worktrees.is_empty() {
1609                worktree::Entity::insert_many(worktrees.iter().map(|worktree| {
1610                    worktree::ActiveModel {
1611                        id: ActiveValue::set(worktree.id as i64),
1612                        project_id: ActiveValue::set(project.id),
1613                        abs_path: ActiveValue::set(worktree.abs_path.clone()),
1614                        root_name: ActiveValue::set(worktree.root_name.clone()),
1615                        visible: ActiveValue::set(worktree.visible),
1616                        scan_id: ActiveValue::set(0),
1617                        is_complete: ActiveValue::set(false),
1618                    }
1619                }))
1620                .on_conflict(
1621                    OnConflict::columns([worktree::Column::ProjectId, worktree::Column::Id])
1622                        .update_column(worktree::Column::RootName)
1623                        .to_owned(),
1624                )
1625                .exec(&*tx)
1626                .await?;
1627            }
1628
1629            worktree::Entity::delete_many()
1630                .filter(
1631                    worktree::Column::ProjectId.eq(project.id).and(
1632                        worktree::Column::Id
1633                            .is_not_in(worktrees.iter().map(|worktree| worktree.id as i64)),
1634                    ),
1635                )
1636                .exec(&*tx)
1637                .await?;
1638
1639            let guest_connection_ids = self.project_guest_connection_ids(project.id, &tx).await?;
1640            let room = self.get_room(project.room_id, &tx).await?;
1641            Ok((project.room_id, (room, guest_connection_ids)))
1642        })
1643        .await
1644    }
1645
1646    pub async fn update_worktree(
1647        &self,
1648        update: &proto::UpdateWorktree,
1649        connection_id: ConnectionId,
1650    ) -> Result<RoomGuard<Vec<ConnectionId>>> {
1651        self.room_transaction(|tx| async move {
1652            let project_id = ProjectId::from_proto(update.project_id);
1653            let worktree_id = update.worktree_id as i64;
1654
1655            // Ensure the update comes from the host.
1656            let project = project::Entity::find_by_id(project_id)
1657                .filter(project::Column::HostConnectionId.eq(connection_id.0))
1658                .one(&*tx)
1659                .await?
1660                .ok_or_else(|| anyhow!("no such project"))?;
1661            let room_id = project.room_id;
1662
1663            // Update metadata.
1664            worktree::Entity::update(worktree::ActiveModel {
1665                id: ActiveValue::set(worktree_id),
1666                project_id: ActiveValue::set(project_id),
1667                root_name: ActiveValue::set(update.root_name.clone()),
1668                scan_id: ActiveValue::set(update.scan_id as i64),
1669                is_complete: ActiveValue::set(update.is_last_update),
1670                abs_path: ActiveValue::set(update.abs_path.clone()),
1671                ..Default::default()
1672            })
1673            .exec(&*tx)
1674            .await?;
1675
1676            if !update.updated_entries.is_empty() {
1677                worktree_entry::Entity::insert_many(update.updated_entries.iter().map(|entry| {
1678                    let mtime = entry.mtime.clone().unwrap_or_default();
1679                    worktree_entry::ActiveModel {
1680                        project_id: ActiveValue::set(project_id),
1681                        worktree_id: ActiveValue::set(worktree_id),
1682                        id: ActiveValue::set(entry.id as i64),
1683                        is_dir: ActiveValue::set(entry.is_dir),
1684                        path: ActiveValue::set(entry.path.clone()),
1685                        inode: ActiveValue::set(entry.inode as i64),
1686                        mtime_seconds: ActiveValue::set(mtime.seconds as i64),
1687                        mtime_nanos: ActiveValue::set(mtime.nanos as i32),
1688                        is_symlink: ActiveValue::set(entry.is_symlink),
1689                        is_ignored: ActiveValue::set(entry.is_ignored),
1690                    }
1691                }))
1692                .on_conflict(
1693                    OnConflict::columns([
1694                        worktree_entry::Column::ProjectId,
1695                        worktree_entry::Column::WorktreeId,
1696                        worktree_entry::Column::Id,
1697                    ])
1698                    .update_columns([
1699                        worktree_entry::Column::IsDir,
1700                        worktree_entry::Column::Path,
1701                        worktree_entry::Column::Inode,
1702                        worktree_entry::Column::MtimeSeconds,
1703                        worktree_entry::Column::MtimeNanos,
1704                        worktree_entry::Column::IsSymlink,
1705                        worktree_entry::Column::IsIgnored,
1706                    ])
1707                    .to_owned(),
1708                )
1709                .exec(&*tx)
1710                .await?;
1711            }
1712
1713            if !update.removed_entries.is_empty() {
1714                worktree_entry::Entity::delete_many()
1715                    .filter(
1716                        worktree_entry::Column::ProjectId
1717                            .eq(project_id)
1718                            .and(worktree_entry::Column::WorktreeId.eq(worktree_id))
1719                            .and(
1720                                worktree_entry::Column::Id
1721                                    .is_in(update.removed_entries.iter().map(|id| *id as i64)),
1722                            ),
1723                    )
1724                    .exec(&*tx)
1725                    .await?;
1726            }
1727
1728            let connection_ids = self.project_guest_connection_ids(project_id, &tx).await?;
1729            Ok((room_id, connection_ids))
1730        })
1731        .await
1732    }
1733
1734    pub async fn update_diagnostic_summary(
1735        &self,
1736        update: &proto::UpdateDiagnosticSummary,
1737        connection_id: ConnectionId,
1738    ) -> Result<RoomGuard<Vec<ConnectionId>>> {
1739        self.room_transaction(|tx| async move {
1740            let project_id = ProjectId::from_proto(update.project_id);
1741            let worktree_id = update.worktree_id as i64;
1742            let summary = update
1743                .summary
1744                .as_ref()
1745                .ok_or_else(|| anyhow!("invalid summary"))?;
1746
1747            // Ensure the update comes from the host.
1748            let project = project::Entity::find_by_id(project_id)
1749                .one(&*tx)
1750                .await?
1751                .ok_or_else(|| anyhow!("no such project"))?;
1752            if project.host_connection_id != connection_id.0 as i32 {
1753                return Err(anyhow!("can't update a project hosted by someone else"))?;
1754            }
1755
1756            // Update summary.
1757            worktree_diagnostic_summary::Entity::insert(worktree_diagnostic_summary::ActiveModel {
1758                project_id: ActiveValue::set(project_id),
1759                worktree_id: ActiveValue::set(worktree_id),
1760                path: ActiveValue::set(summary.path.clone()),
1761                language_server_id: ActiveValue::set(summary.language_server_id as i64),
1762                error_count: ActiveValue::set(summary.error_count as i32),
1763                warning_count: ActiveValue::set(summary.warning_count as i32),
1764                ..Default::default()
1765            })
1766            .on_conflict(
1767                OnConflict::columns([
1768                    worktree_diagnostic_summary::Column::ProjectId,
1769                    worktree_diagnostic_summary::Column::WorktreeId,
1770                    worktree_diagnostic_summary::Column::Path,
1771                ])
1772                .update_columns([
1773                    worktree_diagnostic_summary::Column::LanguageServerId,
1774                    worktree_diagnostic_summary::Column::ErrorCount,
1775                    worktree_diagnostic_summary::Column::WarningCount,
1776                ])
1777                .to_owned(),
1778            )
1779            .exec(&*tx)
1780            .await?;
1781
1782            let connection_ids = self.project_guest_connection_ids(project_id, &tx).await?;
1783            Ok((project.room_id, connection_ids))
1784        })
1785        .await
1786    }
1787
1788    pub async fn start_language_server(
1789        &self,
1790        update: &proto::StartLanguageServer,
1791        connection_id: ConnectionId,
1792    ) -> Result<RoomGuard<Vec<ConnectionId>>> {
1793        self.room_transaction(|tx| async move {
1794            let project_id = ProjectId::from_proto(update.project_id);
1795            let server = update
1796                .server
1797                .as_ref()
1798                .ok_or_else(|| anyhow!("invalid language server"))?;
1799
1800            // Ensure the update comes from the host.
1801            let project = project::Entity::find_by_id(project_id)
1802                .one(&*tx)
1803                .await?
1804                .ok_or_else(|| anyhow!("no such project"))?;
1805            if project.host_connection_id != connection_id.0 as i32 {
1806                return Err(anyhow!("can't update a project hosted by someone else"))?;
1807            }
1808
1809            // Add the newly-started language server.
1810            language_server::Entity::insert(language_server::ActiveModel {
1811                project_id: ActiveValue::set(project_id),
1812                id: ActiveValue::set(server.id as i64),
1813                name: ActiveValue::set(server.name.clone()),
1814                ..Default::default()
1815            })
1816            .on_conflict(
1817                OnConflict::columns([
1818                    language_server::Column::ProjectId,
1819                    language_server::Column::Id,
1820                ])
1821                .update_column(language_server::Column::Name)
1822                .to_owned(),
1823            )
1824            .exec(&*tx)
1825            .await?;
1826
1827            let connection_ids = self.project_guest_connection_ids(project_id, &tx).await?;
1828            Ok((project.room_id, connection_ids))
1829        })
1830        .await
1831    }
1832
1833    pub async fn join_project(
1834        &self,
1835        project_id: ProjectId,
1836        connection_id: ConnectionId,
1837    ) -> Result<RoomGuard<(Project, ReplicaId)>> {
1838        self.room_transaction(|tx| async move {
1839            let participant = room_participant::Entity::find()
1840                .filter(room_participant::Column::AnsweringConnectionId.eq(connection_id.0))
1841                .one(&*tx)
1842                .await?
1843                .ok_or_else(|| anyhow!("must join a room first"))?;
1844
1845            let project = project::Entity::find_by_id(project_id)
1846                .one(&*tx)
1847                .await?
1848                .ok_or_else(|| anyhow!("no such project"))?;
1849            if project.room_id != participant.room_id {
1850                return Err(anyhow!("no such project"))?;
1851            }
1852
1853            let mut collaborators = project
1854                .find_related(project_collaborator::Entity)
1855                .all(&*tx)
1856                .await?;
1857            let replica_ids = collaborators
1858                .iter()
1859                .map(|c| c.replica_id)
1860                .collect::<HashSet<_>>();
1861            let mut replica_id = ReplicaId(1);
1862            while replica_ids.contains(&replica_id) {
1863                replica_id.0 += 1;
1864            }
1865            let new_collaborator = project_collaborator::ActiveModel {
1866                project_id: ActiveValue::set(project_id),
1867                connection_id: ActiveValue::set(connection_id.0 as i32),
1868                connection_epoch: ActiveValue::set(self.epoch),
1869                user_id: ActiveValue::set(participant.user_id),
1870                replica_id: ActiveValue::set(replica_id),
1871                is_host: ActiveValue::set(false),
1872                ..Default::default()
1873            }
1874            .insert(&*tx)
1875            .await?;
1876            collaborators.push(new_collaborator);
1877
1878            let db_worktrees = project.find_related(worktree::Entity).all(&*tx).await?;
1879            let mut worktrees = db_worktrees
1880                .into_iter()
1881                .map(|db_worktree| {
1882                    (
1883                        db_worktree.id as u64,
1884                        Worktree {
1885                            id: db_worktree.id as u64,
1886                            abs_path: db_worktree.abs_path,
1887                            root_name: db_worktree.root_name,
1888                            visible: db_worktree.visible,
1889                            entries: Default::default(),
1890                            diagnostic_summaries: Default::default(),
1891                            scan_id: db_worktree.scan_id as u64,
1892                            is_complete: db_worktree.is_complete,
1893                        },
1894                    )
1895                })
1896                .collect::<BTreeMap<_, _>>();
1897
1898            // Populate worktree entries.
1899            {
1900                let mut db_entries = worktree_entry::Entity::find()
1901                    .filter(worktree_entry::Column::ProjectId.eq(project_id))
1902                    .stream(&*tx)
1903                    .await?;
1904                while let Some(db_entry) = db_entries.next().await {
1905                    let db_entry = db_entry?;
1906                    if let Some(worktree) = worktrees.get_mut(&(db_entry.worktree_id as u64)) {
1907                        worktree.entries.push(proto::Entry {
1908                            id: db_entry.id as u64,
1909                            is_dir: db_entry.is_dir,
1910                            path: db_entry.path,
1911                            inode: db_entry.inode as u64,
1912                            mtime: Some(proto::Timestamp {
1913                                seconds: db_entry.mtime_seconds as u64,
1914                                nanos: db_entry.mtime_nanos as u32,
1915                            }),
1916                            is_symlink: db_entry.is_symlink,
1917                            is_ignored: db_entry.is_ignored,
1918                        });
1919                    }
1920                }
1921            }
1922
1923            // Populate worktree diagnostic summaries.
1924            {
1925                let mut db_summaries = worktree_diagnostic_summary::Entity::find()
1926                    .filter(worktree_diagnostic_summary::Column::ProjectId.eq(project_id))
1927                    .stream(&*tx)
1928                    .await?;
1929                while let Some(db_summary) = db_summaries.next().await {
1930                    let db_summary = db_summary?;
1931                    if let Some(worktree) = worktrees.get_mut(&(db_summary.worktree_id as u64)) {
1932                        worktree
1933                            .diagnostic_summaries
1934                            .push(proto::DiagnosticSummary {
1935                                path: db_summary.path,
1936                                language_server_id: db_summary.language_server_id as u64,
1937                                error_count: db_summary.error_count as u32,
1938                                warning_count: db_summary.warning_count as u32,
1939                            });
1940                    }
1941                }
1942            }
1943
1944            // Populate language servers.
1945            let language_servers = project
1946                .find_related(language_server::Entity)
1947                .all(&*tx)
1948                .await?;
1949
1950            let room_id = project.room_id;
1951            let project = Project {
1952                collaborators,
1953                worktrees,
1954                language_servers: language_servers
1955                    .into_iter()
1956                    .map(|language_server| proto::LanguageServer {
1957                        id: language_server.id as u64,
1958                        name: language_server.name,
1959                    })
1960                    .collect(),
1961            };
1962            Ok((room_id, (project, replica_id as ReplicaId)))
1963        })
1964        .await
1965    }
1966
1967    pub async fn leave_project(
1968        &self,
1969        project_id: ProjectId,
1970        connection_id: ConnectionId,
1971    ) -> Result<RoomGuard<LeftProject>> {
1972        self.room_transaction(|tx| async move {
1973            let result = project_collaborator::Entity::delete_many()
1974                .filter(
1975                    project_collaborator::Column::ProjectId
1976                        .eq(project_id)
1977                        .and(project_collaborator::Column::ConnectionId.eq(connection_id.0)),
1978                )
1979                .exec(&*tx)
1980                .await?;
1981            if result.rows_affected == 0 {
1982                Err(anyhow!("not a collaborator on this project"))?;
1983            }
1984
1985            let project = project::Entity::find_by_id(project_id)
1986                .one(&*tx)
1987                .await?
1988                .ok_or_else(|| anyhow!("no such project"))?;
1989            let collaborators = project
1990                .find_related(project_collaborator::Entity)
1991                .all(&*tx)
1992                .await?;
1993            let connection_ids = collaborators
1994                .into_iter()
1995                .map(|collaborator| ConnectionId(collaborator.connection_id as u32))
1996                .collect();
1997
1998            let left_project = LeftProject {
1999                id: project_id,
2000                host_user_id: project.host_user_id,
2001                host_connection_id: ConnectionId(project.host_connection_id as u32),
2002                connection_ids,
2003            };
2004            Ok((project.room_id, left_project))
2005        })
2006        .await
2007    }
2008
2009    pub async fn project_collaborators(
2010        &self,
2011        project_id: ProjectId,
2012        connection_id: ConnectionId,
2013    ) -> Result<RoomGuard<Vec<project_collaborator::Model>>> {
2014        self.room_transaction(|tx| async move {
2015            let project = project::Entity::find_by_id(project_id)
2016                .one(&*tx)
2017                .await?
2018                .ok_or_else(|| anyhow!("no such project"))?;
2019            let collaborators = project_collaborator::Entity::find()
2020                .filter(project_collaborator::Column::ProjectId.eq(project_id))
2021                .all(&*tx)
2022                .await?;
2023
2024            if collaborators
2025                .iter()
2026                .any(|collaborator| collaborator.connection_id == connection_id.0 as i32)
2027            {
2028                Ok((project.room_id, collaborators))
2029            } else {
2030                Err(anyhow!("no such project"))?
2031            }
2032        })
2033        .await
2034    }
2035
2036    pub async fn project_connection_ids(
2037        &self,
2038        project_id: ProjectId,
2039        connection_id: ConnectionId,
2040    ) -> Result<RoomGuard<HashSet<ConnectionId>>> {
2041        self.room_transaction(|tx| async move {
2042            #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
2043            enum QueryAs {
2044                ConnectionId,
2045            }
2046
2047            let project = project::Entity::find_by_id(project_id)
2048                .one(&*tx)
2049                .await?
2050                .ok_or_else(|| anyhow!("no such project"))?;
2051            let mut db_connection_ids = project_collaborator::Entity::find()
2052                .select_only()
2053                .column_as(
2054                    project_collaborator::Column::ConnectionId,
2055                    QueryAs::ConnectionId,
2056                )
2057                .filter(project_collaborator::Column::ProjectId.eq(project_id))
2058                .into_values::<i32, QueryAs>()
2059                .stream(&*tx)
2060                .await?;
2061
2062            let mut connection_ids = HashSet::default();
2063            while let Some(connection_id) = db_connection_ids.next().await {
2064                connection_ids.insert(ConnectionId(connection_id? as u32));
2065            }
2066
2067            if connection_ids.contains(&connection_id) {
2068                Ok((project.room_id, connection_ids))
2069            } else {
2070                Err(anyhow!("no such project"))?
2071            }
2072        })
2073        .await
2074    }
2075
2076    async fn project_guest_connection_ids(
2077        &self,
2078        project_id: ProjectId,
2079        tx: &DatabaseTransaction,
2080    ) -> Result<Vec<ConnectionId>> {
2081        #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
2082        enum QueryAs {
2083            ConnectionId,
2084        }
2085
2086        let mut db_guest_connection_ids = project_collaborator::Entity::find()
2087            .select_only()
2088            .column_as(
2089                project_collaborator::Column::ConnectionId,
2090                QueryAs::ConnectionId,
2091            )
2092            .filter(
2093                project_collaborator::Column::ProjectId
2094                    .eq(project_id)
2095                    .and(project_collaborator::Column::IsHost.eq(false)),
2096            )
2097            .into_values::<i32, QueryAs>()
2098            .stream(tx)
2099            .await?;
2100
2101        let mut guest_connection_ids = Vec::new();
2102        while let Some(connection_id) = db_guest_connection_ids.next().await {
2103            guest_connection_ids.push(ConnectionId(connection_id? as u32));
2104        }
2105        Ok(guest_connection_ids)
2106    }
2107
2108    // access tokens
2109
2110    pub async fn create_access_token_hash(
2111        &self,
2112        user_id: UserId,
2113        access_token_hash: &str,
2114        max_access_token_count: usize,
2115    ) -> Result<()> {
2116        self.transaction(|tx| async {
2117            let tx = tx;
2118
2119            access_token::ActiveModel {
2120                user_id: ActiveValue::set(user_id),
2121                hash: ActiveValue::set(access_token_hash.into()),
2122                ..Default::default()
2123            }
2124            .insert(&*tx)
2125            .await?;
2126
2127            access_token::Entity::delete_many()
2128                .filter(
2129                    access_token::Column::Id.in_subquery(
2130                        Query::select()
2131                            .column(access_token::Column::Id)
2132                            .from(access_token::Entity)
2133                            .and_where(access_token::Column::UserId.eq(user_id))
2134                            .order_by(access_token::Column::Id, sea_orm::Order::Desc)
2135                            .limit(10000)
2136                            .offset(max_access_token_count as u64)
2137                            .to_owned(),
2138                    ),
2139                )
2140                .exec(&*tx)
2141                .await?;
2142            Ok(())
2143        })
2144        .await
2145    }
2146
2147    pub async fn get_access_token_hashes(&self, user_id: UserId) -> Result<Vec<String>> {
2148        #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
2149        enum QueryAs {
2150            Hash,
2151        }
2152
2153        self.transaction(|tx| async move {
2154            Ok(access_token::Entity::find()
2155                .select_only()
2156                .column(access_token::Column::Hash)
2157                .filter(access_token::Column::UserId.eq(user_id))
2158                .order_by_desc(access_token::Column::Id)
2159                .into_values::<_, QueryAs>()
2160                .all(&*tx)
2161                .await?)
2162        })
2163        .await
2164    }
2165
2166    async fn transaction<F, Fut, T>(&self, f: F) -> Result<T>
2167    where
2168        F: Send + Fn(TransactionHandle) -> Fut,
2169        Fut: Send + Future<Output = Result<T>>,
2170    {
2171        let body = async {
2172            loop {
2173                let (tx, result) = self.with_transaction(&f).await?;
2174                match result {
2175                    Ok(result) => {
2176                        match tx.commit().await.map_err(Into::into) {
2177                            Ok(()) => return Ok(result),
2178                            Err(error) => {
2179                                if is_serialization_error(&error) {
2180                                    // Retry (don't break the loop)
2181                                } else {
2182                                    return Err(error);
2183                                }
2184                            }
2185                        }
2186                    }
2187                    Err(error) => {
2188                        tx.rollback().await?;
2189                        if is_serialization_error(&error) {
2190                            // Retry (don't break the loop)
2191                        } else {
2192                            return Err(error);
2193                        }
2194                    }
2195                }
2196            }
2197        };
2198
2199        self.run(body).await
2200    }
2201
2202    async fn room_transaction<F, Fut, T>(&self, f: F) -> Result<RoomGuard<T>>
2203    where
2204        F: Send + Fn(TransactionHandle) -> Fut,
2205        Fut: Send + Future<Output = Result<(RoomId, T)>>,
2206    {
2207        let body = async {
2208            loop {
2209                let (tx, result) = self.with_transaction(&f).await?;
2210                match result {
2211                    Ok((room_id, data)) => {
2212                        let lock = self.rooms.entry(room_id).or_default().clone();
2213                        let _guard = lock.lock_owned().await;
2214                        match tx.commit().await.map_err(Into::into) {
2215                            Ok(()) => {
2216                                return Ok(RoomGuard {
2217                                    data,
2218                                    _guard,
2219                                    _not_send: PhantomData,
2220                                });
2221                            }
2222                            Err(error) => {
2223                                if is_serialization_error(&error) {
2224                                    // Retry (don't break the loop)
2225                                } else {
2226                                    return Err(error);
2227                                }
2228                            }
2229                        }
2230                    }
2231                    Err(error) => {
2232                        tx.rollback().await?;
2233                        if is_serialization_error(&error) {
2234                            // Retry (don't break the loop)
2235                        } else {
2236                            return Err(error);
2237                        }
2238                    }
2239                }
2240            }
2241        };
2242
2243        self.run(body).await
2244    }
2245
2246    async fn with_transaction<F, Fut, T>(&self, f: &F) -> Result<(DatabaseTransaction, Result<T>)>
2247    where
2248        F: Send + Fn(TransactionHandle) -> Fut,
2249        Fut: Send + Future<Output = Result<T>>,
2250    {
2251        let tx = self
2252            .pool
2253            .begin_with_config(Some(IsolationLevel::Serializable), None)
2254            .await?;
2255
2256        let mut tx = Arc::new(Some(tx));
2257        let result = f(TransactionHandle(tx.clone())).await;
2258        let Some(tx) = Arc::get_mut(&mut tx).and_then(|tx| tx.take()) else {
2259            return Err(anyhow!("couldn't complete transaction because it's still in use"))?;
2260        };
2261
2262        Ok((tx, result))
2263    }
2264
2265    async fn run<F, T>(&self, future: F) -> T
2266    where
2267        F: Future<Output = T>,
2268    {
2269        #[cfg(test)]
2270        {
2271            if let Some(background) = self.background.as_ref() {
2272                background.simulate_random_delay().await;
2273            }
2274
2275            self.runtime.as_ref().unwrap().block_on(future)
2276        }
2277
2278        #[cfg(not(test))]
2279        {
2280            future.await
2281        }
2282    }
2283}
2284
2285fn is_serialization_error(error: &Error) -> bool {
2286    const SERIALIZATION_FAILURE_CODE: &'static str = "40001";
2287    match error {
2288        Error::Database(
2289            DbErr::Exec(sea_orm::RuntimeErr::SqlxError(error))
2290            | DbErr::Query(sea_orm::RuntimeErr::SqlxError(error)),
2291        ) if error
2292            .as_database_error()
2293            .and_then(|error| error.code())
2294            .as_deref()
2295            == Some(SERIALIZATION_FAILURE_CODE) =>
2296        {
2297            true
2298        }
2299        _ => false,
2300    }
2301}
2302
2303struct TransactionHandle(Arc<Option<DatabaseTransaction>>);
2304
2305impl Deref for TransactionHandle {
2306    type Target = DatabaseTransaction;
2307
2308    fn deref(&self) -> &Self::Target {
2309        self.0.as_ref().as_ref().unwrap()
2310    }
2311}
2312
2313pub struct RoomGuard<T> {
2314    data: T,
2315    _guard: OwnedMutexGuard<()>,
2316    _not_send: PhantomData<Rc<()>>,
2317}
2318
2319impl<T> Deref for RoomGuard<T> {
2320    type Target = T;
2321
2322    fn deref(&self) -> &T {
2323        &self.data
2324    }
2325}
2326
2327impl<T> DerefMut for RoomGuard<T> {
2328    fn deref_mut(&mut self) -> &mut T {
2329        &mut self.data
2330    }
2331}
2332
2333#[derive(Debug, Serialize, Deserialize)]
2334pub struct NewUserParams {
2335    pub github_login: String,
2336    pub github_user_id: i32,
2337    pub invite_count: i32,
2338}
2339
2340#[derive(Debug)]
2341pub struct NewUserResult {
2342    pub user_id: UserId,
2343    pub metrics_id: String,
2344    pub inviting_user_id: Option<UserId>,
2345    pub signup_device_id: Option<String>,
2346}
2347
2348fn random_invite_code() -> String {
2349    nanoid::nanoid!(16)
2350}
2351
2352fn random_email_confirmation_code() -> String {
2353    nanoid::nanoid!(64)
2354}
2355
2356macro_rules! id_type {
2357    ($name:ident) => {
2358        #[derive(
2359            Clone,
2360            Copy,
2361            Debug,
2362            Default,
2363            PartialEq,
2364            Eq,
2365            PartialOrd,
2366            Ord,
2367            Hash,
2368            Serialize,
2369            Deserialize,
2370        )]
2371        #[serde(transparent)]
2372        pub struct $name(pub i32);
2373
2374        impl $name {
2375            #[allow(unused)]
2376            pub const MAX: Self = Self(i32::MAX);
2377
2378            #[allow(unused)]
2379            pub fn from_proto(value: u64) -> Self {
2380                Self(value as i32)
2381            }
2382
2383            #[allow(unused)]
2384            pub fn to_proto(self) -> u64 {
2385                self.0 as u64
2386            }
2387        }
2388
2389        impl std::fmt::Display for $name {
2390            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
2391                self.0.fmt(f)
2392            }
2393        }
2394
2395        impl From<$name> for sea_query::Value {
2396            fn from(value: $name) -> Self {
2397                sea_query::Value::Int(Some(value.0))
2398            }
2399        }
2400
2401        impl sea_orm::TryGetable for $name {
2402            fn try_get(
2403                res: &sea_orm::QueryResult,
2404                pre: &str,
2405                col: &str,
2406            ) -> Result<Self, sea_orm::TryGetError> {
2407                Ok(Self(i32::try_get(res, pre, col)?))
2408            }
2409        }
2410
2411        impl sea_query::ValueType for $name {
2412            fn try_from(v: Value) -> Result<Self, sea_query::ValueTypeErr> {
2413                match v {
2414                    Value::TinyInt(Some(int)) => {
2415                        Ok(Self(int.try_into().map_err(|_| sea_query::ValueTypeErr)?))
2416                    }
2417                    Value::SmallInt(Some(int)) => {
2418                        Ok(Self(int.try_into().map_err(|_| sea_query::ValueTypeErr)?))
2419                    }
2420                    Value::Int(Some(int)) => {
2421                        Ok(Self(int.try_into().map_err(|_| sea_query::ValueTypeErr)?))
2422                    }
2423                    Value::BigInt(Some(int)) => {
2424                        Ok(Self(int.try_into().map_err(|_| sea_query::ValueTypeErr)?))
2425                    }
2426                    Value::TinyUnsigned(Some(int)) => {
2427                        Ok(Self(int.try_into().map_err(|_| sea_query::ValueTypeErr)?))
2428                    }
2429                    Value::SmallUnsigned(Some(int)) => {
2430                        Ok(Self(int.try_into().map_err(|_| sea_query::ValueTypeErr)?))
2431                    }
2432                    Value::Unsigned(Some(int)) => {
2433                        Ok(Self(int.try_into().map_err(|_| sea_query::ValueTypeErr)?))
2434                    }
2435                    Value::BigUnsigned(Some(int)) => {
2436                        Ok(Self(int.try_into().map_err(|_| sea_query::ValueTypeErr)?))
2437                    }
2438                    _ => Err(sea_query::ValueTypeErr),
2439                }
2440            }
2441
2442            fn type_name() -> String {
2443                stringify!($name).into()
2444            }
2445
2446            fn array_type() -> sea_query::ArrayType {
2447                sea_query::ArrayType::Int
2448            }
2449
2450            fn column_type() -> sea_query::ColumnType {
2451                sea_query::ColumnType::Integer(None)
2452            }
2453        }
2454
2455        impl sea_orm::TryFromU64 for $name {
2456            fn try_from_u64(n: u64) -> Result<Self, DbErr> {
2457                Ok(Self(n.try_into().map_err(|_| {
2458                    DbErr::ConvertFromU64(concat!(
2459                        "error converting ",
2460                        stringify!($name),
2461                        " to u64"
2462                    ))
2463                })?))
2464            }
2465        }
2466
2467        impl sea_query::Nullable for $name {
2468            fn null() -> Value {
2469                Value::Int(None)
2470            }
2471        }
2472    };
2473}
2474
2475id_type!(AccessTokenId);
2476id_type!(ContactId);
2477id_type!(RoomId);
2478id_type!(RoomParticipantId);
2479id_type!(ProjectId);
2480id_type!(ProjectCollaboratorId);
2481id_type!(ReplicaId);
2482id_type!(SignupId);
2483id_type!(UserId);
2484
2485pub struct LeftRoom {
2486    pub room: proto::Room,
2487    pub left_projects: HashMap<ProjectId, LeftProject>,
2488    pub canceled_calls_to_user_ids: Vec<UserId>,
2489}
2490
2491pub struct Project {
2492    pub collaborators: Vec<project_collaborator::Model>,
2493    pub worktrees: BTreeMap<u64, Worktree>,
2494    pub language_servers: Vec<proto::LanguageServer>,
2495}
2496
2497pub struct LeftProject {
2498    pub id: ProjectId,
2499    pub host_user_id: UserId,
2500    pub host_connection_id: ConnectionId,
2501    pub connection_ids: Vec<ConnectionId>,
2502}
2503
2504pub struct Worktree {
2505    pub id: u64,
2506    pub abs_path: String,
2507    pub root_name: String,
2508    pub visible: bool,
2509    pub entries: Vec<proto::Entry>,
2510    pub diagnostic_summaries: Vec<proto::DiagnosticSummary>,
2511    pub scan_id: u64,
2512    pub is_complete: bool,
2513}
2514
2515#[cfg(test)]
2516pub use test::*;
2517
2518#[cfg(test)]
2519mod test {
2520    use super::*;
2521    use gpui::executor::Background;
2522    use lazy_static::lazy_static;
2523    use parking_lot::Mutex;
2524    use rand::prelude::*;
2525    use sea_orm::ConnectionTrait;
2526    use sqlx::migrate::MigrateDatabase;
2527    use std::sync::Arc;
2528
2529    pub struct TestDb {
2530        pub db: Option<Arc<Database>>,
2531        pub connection: Option<sqlx::AnyConnection>,
2532    }
2533
2534    impl TestDb {
2535        pub fn sqlite(background: Arc<Background>) -> Self {
2536            let url = format!("sqlite::memory:");
2537            let runtime = tokio::runtime::Builder::new_current_thread()
2538                .enable_io()
2539                .enable_time()
2540                .build()
2541                .unwrap();
2542
2543            let mut db = runtime.block_on(async {
2544                let mut options = ConnectOptions::new(url);
2545                options.max_connections(5);
2546                let db = Database::new(options).await.unwrap();
2547                let sql = include_str!(concat!(
2548                    env!("CARGO_MANIFEST_DIR"),
2549                    "/migrations.sqlite/20221109000000_test_schema.sql"
2550                ));
2551                db.pool
2552                    .execute(sea_orm::Statement::from_string(
2553                        db.pool.get_database_backend(),
2554                        sql.into(),
2555                    ))
2556                    .await
2557                    .unwrap();
2558                db
2559            });
2560
2561            db.background = Some(background);
2562            db.runtime = Some(runtime);
2563
2564            Self {
2565                db: Some(Arc::new(db)),
2566                connection: None,
2567            }
2568        }
2569
2570        pub fn postgres(background: Arc<Background>) -> Self {
2571            lazy_static! {
2572                static ref LOCK: Mutex<()> = Mutex::new(());
2573            }
2574
2575            let _guard = LOCK.lock();
2576            let mut rng = StdRng::from_entropy();
2577            let url = format!(
2578                "postgres://postgres@localhost/zed-test-{}",
2579                rng.gen::<u128>()
2580            );
2581            let runtime = tokio::runtime::Builder::new_current_thread()
2582                .enable_io()
2583                .enable_time()
2584                .build()
2585                .unwrap();
2586
2587            let mut db = runtime.block_on(async {
2588                sqlx::Postgres::create_database(&url)
2589                    .await
2590                    .expect("failed to create test db");
2591                let mut options = ConnectOptions::new(url);
2592                options
2593                    .max_connections(5)
2594                    .idle_timeout(Duration::from_secs(0));
2595                let db = Database::new(options).await.unwrap();
2596                let migrations_path = concat!(env!("CARGO_MANIFEST_DIR"), "/migrations");
2597                db.migrate(Path::new(migrations_path), false).await.unwrap();
2598                db
2599            });
2600
2601            db.background = Some(background);
2602            db.runtime = Some(runtime);
2603
2604            Self {
2605                db: Some(Arc::new(db)),
2606                connection: None,
2607            }
2608        }
2609
2610        pub fn db(&self) -> &Arc<Database> {
2611            self.db.as_ref().unwrap()
2612        }
2613    }
2614
2615    impl Drop for TestDb {
2616        fn drop(&mut self) {
2617            let db = self.db.take().unwrap();
2618            if let sea_orm::DatabaseBackend::Postgres = db.pool.get_database_backend() {
2619                db.runtime.as_ref().unwrap().block_on(async {
2620                    use util::ResultExt;
2621                    let query = "
2622                        SELECT pg_terminate_backend(pg_stat_activity.pid)
2623                        FROM pg_stat_activity
2624                        WHERE
2625                            pg_stat_activity.datname = current_database() AND
2626                            pid <> pg_backend_pid();
2627                    ";
2628                    db.pool
2629                        .execute(sea_orm::Statement::from_string(
2630                            db.pool.get_database_backend(),
2631                            query.into(),
2632                        ))
2633                        .await
2634                        .log_err();
2635                    sqlx::Postgres::drop_database(db.options.get_url())
2636                        .await
2637                        .log_err();
2638                })
2639            }
2640        }
2641    }
2642}