@@ -199,6 +199,9 @@ pub struct MasNewUser {
199
199
pub created_at : DateTime < Utc > ,
200
200
pub locked_at : Option < DateTime < Utc > > ,
201
201
pub can_request_admin : bool ,
202
+ /// Whether the user was a Synapse guest.
203
+ /// Although MAS doesn't support guest access, it's still useful to track for the future.
204
+ pub is_guest : bool ,
202
205
}
203
206
204
207
pub struct MasNewUserPassword {
@@ -563,52 +566,65 @@ impl<'conn> MasWriter<'conn> {
563
566
#[ allow( clippy:: missing_panics_doc) ] // not a real panic
564
567
#[ tracing:: instrument( skip_all, level = Level :: DEBUG ) ]
565
568
pub fn write_users ( & mut self , users : Vec < MasNewUser > ) -> BoxFuture < ' _ , Result < ( ) , Error > > {
566
- self . writer_pool . spawn_with_connection ( move |conn| Box :: pin ( async move {
567
- // `UNNEST` is a fast way to do bulk inserts, as it lets us send multiple rows in one statement
568
- // without having to change the statement SQL thus altering the query plan.
569
- // See <https://github.com/launchbadge/sqlx/blob/main/FAQ.md#how-can-i-bind-an-array-to-a-values-clause-how-can-i-do-bulk-inserts>.
570
- // In the future we could consider using sqlx's support for `PgCopyIn` / the `COPY FROM STDIN` statement,
571
- // which is allegedly the best for insert performance, but is less simple to encode.
572
- if users. is_empty ( ) {
573
- return Ok ( ( ) ) ;
574
- }
575
-
576
- let mut user_ids: Vec < Uuid > = Vec :: with_capacity ( users. len ( ) ) ;
577
- let mut usernames: Vec < String > = Vec :: with_capacity ( users. len ( ) ) ;
578
- let mut created_ats: Vec < DateTime < Utc > > = Vec :: with_capacity ( users. len ( ) ) ;
579
- let mut locked_ats: Vec < Option < DateTime < Utc > > > = Vec :: with_capacity ( users. len ( ) ) ;
580
- let mut can_request_admins: Vec < bool > = Vec :: with_capacity ( users. len ( ) ) ;
581
- for MasNewUser {
582
- user_id,
583
- username,
584
- created_at,
585
- locked_at,
586
- can_request_admin,
587
- } in users
588
- {
589
- user_ids. push ( user_id) ;
590
- usernames. push ( username) ;
591
- created_ats. push ( created_at) ;
592
- locked_ats. push ( locked_at) ;
593
- can_request_admins. push ( can_request_admin) ;
594
- }
569
+ self . writer_pool
570
+ . spawn_with_connection ( move |conn| {
571
+ Box :: pin ( async move {
572
+ // `UNNEST` is a fast way to do bulk inserts, as it lets us send multiple rows in one statement
573
+ // without having to change the statement SQL thus altering the query plan.
574
+ // See <https://github.com/launchbadge/sqlx/blob/main/FAQ.md#how-can-i-bind-an-array-to-a-values-clause-how-can-i-do-bulk-inserts>.
575
+ // In the future we could consider using sqlx's support for `PgCopyIn` / the `COPY FROM STDIN` statement,
576
+ // which is allegedly the best for insert performance, but is less simple to encode.
577
+ let mut user_ids: Vec < Uuid > = Vec :: with_capacity ( users. len ( ) ) ;
578
+ let mut usernames: Vec < String > = Vec :: with_capacity ( users. len ( ) ) ;
579
+ let mut created_ats: Vec < DateTime < Utc > > = Vec :: with_capacity ( users. len ( ) ) ;
580
+ let mut locked_ats: Vec < Option < DateTime < Utc > > > =
581
+ Vec :: with_capacity ( users. len ( ) ) ;
582
+ let mut can_request_admins: Vec < bool > = Vec :: with_capacity ( users. len ( ) ) ;
583
+ let mut is_guests: Vec < bool > = Vec :: with_capacity ( users. len ( ) ) ;
584
+ for MasNewUser {
585
+ user_id,
586
+ username,
587
+ created_at,
588
+ locked_at,
589
+ can_request_admin,
590
+ is_guest,
591
+ } in users
592
+ {
593
+ user_ids. push ( user_id) ;
594
+ usernames. push ( username) ;
595
+ created_ats. push ( created_at) ;
596
+ locked_ats. push ( locked_at) ;
597
+ can_request_admins. push ( can_request_admin) ;
598
+ is_guests. push ( is_guest) ;
599
+ }
595
600
596
- sqlx:: query!(
597
- r#"
598
- INSERT INTO syn2mas__users
599
- (user_id, username, created_at, locked_at, can_request_admin)
600
- SELECT * FROM UNNEST($1::UUID[], $2::TEXT[], $3::TIMESTAMP WITH TIME ZONE[], $4::TIMESTAMP WITH TIME ZONE[], $5::BOOL[])
601
- "# ,
602
- & user_ids[ ..] ,
603
- & usernames[ ..] ,
604
- & created_ats[ ..] ,
605
- // We need to override the typing for arrays of optionals (sqlx limitation)
606
- & locked_ats[ ..] as & [ Option <DateTime <Utc >>] ,
607
- & can_request_admins[ ..] ,
608
- ) . execute ( & mut * conn) . await . into_database ( "writing users to MAS" ) ?;
601
+ sqlx:: query!(
602
+ r#"
603
+ INSERT INTO syn2mas__users (
604
+ user_id, username,
605
+ created_at, locked_at,
606
+ can_request_admin, is_guest)
607
+ SELECT * FROM UNNEST(
608
+ $1::UUID[], $2::TEXT[],
609
+ $3::TIMESTAMP WITH TIME ZONE[], $4::TIMESTAMP WITH TIME ZONE[],
610
+ $5::BOOL[], $6::BOOL[])
611
+ "# ,
612
+ & user_ids[ ..] ,
613
+ & usernames[ ..] ,
614
+ & created_ats[ ..] ,
615
+ // We need to override the typing for arrays of optionals (sqlx limitation)
616
+ & locked_ats[ ..] as & [ Option <DateTime <Utc >>] ,
617
+ & can_request_admins[ ..] ,
618
+ & is_guests[ ..] ,
619
+ )
620
+ . execute ( & mut * conn)
621
+ . await
622
+ . into_database ( "writing users to MAS" ) ?;
609
623
610
- Ok ( ( ) )
611
- } ) ) . boxed ( )
624
+ Ok ( ( ) )
625
+ } )
626
+ } )
627
+ . boxed ( )
612
628
}
613
629
614
630
/// Write a batch of user passwords to the database.
@@ -1197,6 +1213,7 @@ mod test {
1197
1213
created_at: DateTime :: default ( ) ,
1198
1214
locked_at: None ,
1199
1215
can_request_admin: false ,
1216
+ is_guest: false ,
1200
1217
} ] )
1201
1218
. await
1202
1219
. expect ( "failed to write user" ) ;
@@ -1221,6 +1238,7 @@ mod test {
1221
1238
created_at: DateTime :: default ( ) ,
1222
1239
locked_at: None ,
1223
1240
can_request_admin: false ,
1241
+ is_guest: false ,
1224
1242
} ] )
1225
1243
. await
1226
1244
. expect ( "failed to write user" ) ;
@@ -1252,6 +1270,7 @@ mod test {
1252
1270
created_at: DateTime :: default ( ) ,
1253
1271
locked_at: None ,
1254
1272
can_request_admin: false ,
1273
+ is_guest: false ,
1255
1274
} ] )
1256
1275
. await
1257
1276
. expect ( "failed to write user" ) ;
@@ -1285,6 +1304,7 @@ mod test {
1285
1304
created_at: DateTime :: default ( ) ,
1286
1305
locked_at: None ,
1287
1306
can_request_admin: false ,
1307
+ is_guest: false ,
1288
1308
} ] )
1289
1309
. await
1290
1310
. expect ( "failed to write user" ) ;
@@ -1319,6 +1339,7 @@ mod test {
1319
1339
created_at: DateTime :: default ( ) ,
1320
1340
locked_at: None ,
1321
1341
can_request_admin: false ,
1342
+ is_guest: false ,
1322
1343
} ] )
1323
1344
. await
1324
1345
. expect ( "failed to write user" ) ;
@@ -1352,6 +1373,7 @@ mod test {
1352
1373
created_at: DateTime :: default ( ) ,
1353
1374
locked_at: None ,
1354
1375
can_request_admin: false ,
1376
+ is_guest: false ,
1355
1377
} ] )
1356
1378
. await
1357
1379
. expect ( "failed to write user" ) ;
@@ -1389,6 +1411,7 @@ mod test {
1389
1411
created_at: DateTime :: default ( ) ,
1390
1412
locked_at: None ,
1391
1413
can_request_admin: false ,
1414
+ is_guest: false ,
1392
1415
} ] )
1393
1416
. await
1394
1417
. expect ( "failed to write user" ) ;
@@ -1437,6 +1460,7 @@ mod test {
1437
1460
created_at: DateTime :: default ( ) ,
1438
1461
locked_at: None ,
1439
1462
can_request_admin: false ,
1463
+ is_guest: false ,
1440
1464
} ] )
1441
1465
. await
1442
1466
. expect ( "failed to write user" ) ;
0 commit comments