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