@@ -322,6 +322,11 @@ impl Options {
322322 } => {
323323 let database_config = DatabaseConfig :: extract_or_default ( figment)
324324 . map_err ( anyhow:: Error :: from_boxed) ?;
325+ let matrix_config =
326+ MatrixConfig :: extract ( figment) . map_err ( anyhow:: Error :: from_boxed) ?;
327+ let http_client = mas_http:: reqwest_client ( ) ;
328+ let homeserver =
329+ homeserver_connection_from_config ( & matrix_config, http_client) . await ?;
325330 let mut conn = database_connection_from_config ( & database_config) . await ?;
326331 let txn = conn. begin ( ) . await ?;
327332 let mut repo = PgRepository :: from_conn ( txn) ;
@@ -338,6 +343,24 @@ impl Options {
338343 Device :: generate ( & mut rng)
339344 } ;
340345
346+ if let Err ( e) = homeserver
347+ . upsert_device ( & user. username , device. as_str ( ) , None )
348+ . await
349+ {
350+ error ! (
351+ error = & * e,
352+ "Could not create the device on the homeserver, aborting"
353+ ) ;
354+
355+ // Schedule a device sync job to remove the potential leftover device
356+ repo. queue_job ( )
357+ . schedule_job ( & mut rng, & clock, SyncDevicesJob :: new ( & user) )
358+ . await ?;
359+
360+ repo. into_inner ( ) . commit ( ) . await ?;
361+ return Ok ( ExitCode :: FAILURE ) ;
362+ }
363+
341364 let compat_session = repo
342365 . compat_session ( )
343366 . add ( & mut rng, & clock, & user, device, None , admin, None )
@@ -590,7 +613,8 @@ impl Options {
590613 MatrixConfig :: extract ( figment) . map_err ( anyhow:: Error :: from_boxed) ?;
591614
592615 let password_manager = password_manager_from_config ( & password_config) . await ?;
593- let homeserver = homeserver_connection_from_config ( & matrix_config, http_client) ;
616+ let homeserver =
617+ homeserver_connection_from_config ( & matrix_config, http_client) . await ?;
594618 let mut conn = database_connection_from_config ( & database_config) . await ?;
595619 let txn = conn. begin ( ) . await ?;
596620 let mut repo = PgRepository :: from_conn ( txn) ;
0 commit comments