@@ -525,29 +525,41 @@ pub(crate) async fn get(
525
525
// form, but this lead to poor UX. This is why we do
526
526
// it ahead of time here.
527
527
//:tchap:
528
- let mut maybe_existing_user =
529
- repo. user ( ) . find_by_username ( & localpart) . await ?;
530
- //if not found by username, check by email
528
+ let template = provider
529
+ . claims_imports
530
+ . email
531
+ . template
532
+ . as_deref ( )
533
+ . unwrap_or ( DEFAULT_EMAIL_TEMPLATE ) ;
534
+
535
+ let maybe_email = render_attribute_template (
536
+ & env,
537
+ template,
538
+ & context,
539
+ provider. claims_imports . email . is_required ( ) ,
540
+ ) ;
541
+
542
+ let mut maybe_existing_user = if let Ok ( Some ( email) ) = maybe_email {
543
+ tchap:: search_user_by_email ( & mut repo, & email, & tchap_config) . await ?
544
+ } else {
545
+ None
546
+ } ;
547
+
531
548
if maybe_existing_user. is_none ( ) {
532
549
let template = provider
533
550
. claims_imports
534
- . email
551
+ . localpart
535
552
. template
536
553
. as_deref ( )
537
- . unwrap_or ( DEFAULT_EMAIL_TEMPLATE ) ;
538
-
539
- let maybe_email = render_attribute_template (
540
- & env,
541
- template,
542
- & context,
543
- provider. claims_imports . email . is_required ( ) ,
544
- ) ;
545
-
546
- if let Ok ( Some ( email) ) = maybe_email {
547
- maybe_existing_user =
548
- tchap:: search_user_by_email ( & mut repo, & email, & tchap_config)
549
- . await ?;
550
- }
554
+ . unwrap_or ( DEFAULT_LOCALPART_TEMPLATE ) ;
555
+
556
+ let Some ( localpart) =
557
+ render_attribute_template ( & env, template, & context, true ) ?
558
+ else {
559
+ // This should never be the case at this point
560
+ return Err ( RouteError :: InvalidFormAction ) ;
561
+ } ;
562
+ maybe_existing_user = repo. user ( ) . find_by_username ( & localpart) . await ?;
551
563
}
552
564
//:tchap: end
553
565
let is_available = homeserver
@@ -768,41 +780,40 @@ pub(crate) async fn post(
768
780
return Err ( RouteError :: InvalidFormAction ) ;
769
781
}
770
782
783
+ //:tchap:
771
784
let template = provider
772
785
. claims_imports
773
- . localpart
786
+ . email
774
787
. template
775
788
. as_deref ( )
776
- . unwrap_or ( DEFAULT_LOCALPART_TEMPLATE ) ;
789
+ . unwrap_or ( DEFAULT_EMAIL_TEMPLATE ) ;
777
790
778
- let Some ( localpart) = render_attribute_template ( & env, template, & context, true ) ? else {
779
- // This should never be the case at this point
780
- return Err ( RouteError :: InvalidFormAction ) ;
781
- } ;
782
-
783
- //:tchap:
784
- let mut maybe_user = repo. user ( ) . find_by_username ( & localpart) . await ?;
791
+ let maybe_email = render_attribute_template (
792
+ & env,
793
+ template,
794
+ & context,
795
+ provider. claims_imports . email . is_required ( ) ,
796
+ ) ;
785
797
786
- //if not found by username, check by email
798
+ let mut maybe_user = if let Ok ( Some ( email) ) = maybe_email {
799
+ tchap:: search_user_by_email ( & mut repo, & email, & tchap_config) . await ?
800
+ } else {
801
+ None
802
+ } ;
787
803
if maybe_user. is_none ( ) {
788
804
let template = provider
789
805
. claims_imports
790
- . email
806
+ . localpart
791
807
. template
792
808
. as_deref ( )
793
- . unwrap_or ( DEFAULT_EMAIL_TEMPLATE ) ;
794
-
795
- let maybe_email = render_attribute_template (
796
- & env,
797
- template,
798
- & context,
799
- provider. claims_imports . email . is_required ( ) ,
800
- ) ;
809
+ . unwrap_or ( DEFAULT_LOCALPART_TEMPLATE ) ;
801
810
802
- if let Ok ( Some ( email) ) = maybe_email {
803
- maybe_user =
804
- tchap:: search_user_by_email ( & mut repo, & email, & tchap_config) . await ?;
805
- }
811
+ let Some ( localpart) = render_attribute_template ( & env, template, & context, true ) ?
812
+ else {
813
+ // This should never be the case at this point
814
+ return Err ( RouteError :: InvalidFormAction ) ;
815
+ } ;
816
+ maybe_user = repo. user ( ) . find_by_username ( & localpart) . await ?;
806
817
}
807
818
808
819
let Some ( user) = maybe_user else {
0 commit comments