@@ -43,17 +43,44 @@ struct MailConfirmation {
4343 pub name : String ,
4444}
4545
46+ #[ derive( Debug , Clone ) ]
47+ struct UserName {
48+ pub name : String ,
49+ }
50+
51+ impl UserName {
52+ /// Throws error if email address is already taken
53+ pub fn check_used ( name : & str , store : & impl Storelike ) -> AtomicResult < Self > {
54+ let mut drive_url = store
55+ . get_self_url ( )
56+ . ok_or ( "No self url, cant check name" ) ?
57+ . clone ( ) ;
58+ drive_url. set_subdomain ( Some ( name) ) ?;
59+
60+ match store. get_resource ( & drive_url. to_string ( ) ) {
61+ Ok ( _) => Err ( "Name already used" . into ( ) ) ,
62+ Err ( _) => Ok ( Self { name : name. into ( ) } ) ,
63+ }
64+ }
65+ }
66+
67+ impl std:: fmt:: Display for UserName {
68+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
69+ write ! ( f, "{}" , self . name)
70+ }
71+ }
72+
4673#[ tracing:: instrument( skip( store) ) ]
4774pub fn handle_register_name_and_email (
4875 url : url:: Url ,
4976 store : & Db ,
5077 for_agent : Option < & str > ,
5178) -> AtomicResult < Resource > {
52- let mut name_option = None ;
79+ let mut name_option: Option < UserName > = None ;
5380 let mut email_option: Option < EmailAddress > = None ;
5481 for ( k, v) in url. query_pairs ( ) {
5582 match k. as_ref ( ) {
56- "name" | urls:: NAME => name_option = Some ( v . to_string ( ) ) ,
83+ "name" | urls:: NAME => name_option = Some ( UserName :: check_used ( & v , store ) ? ) ,
5784 "email" => email_option = Some ( EmailAddress :: new ( v. to_string ( ) ) ?) ,
5885 _ => { }
5986 }
@@ -64,13 +91,14 @@ pub fn handle_register_name_and_email(
6491 } ;
6592
6693 let name = name_option. ok_or ( "No name provided" ) ?;
94+ let _validate_name = Value :: new ( & name. to_string ( ) , & crate :: datatype:: DataType :: Slug ) ?;
6795 let email = email_option. ok_or ( "No email provided" ) ?. check_used ( store) ?;
6896
6997 // send the user an e-mail to confirm sign up
7098 let store_clone = store. clone ( ) ;
7199 let confirmation_token_struct = MailConfirmation {
72100 email : email. clone ( ) ,
73- name : name. clone ( ) ,
101+ name : name. to_string ( ) ,
74102 } ;
75103 let token = crate :: token:: sign_claim ( store, confirmation_token_struct) ?;
76104 let mut confirm_url = store
0 commit comments