@@ -13,20 +13,19 @@ use crate::views::{EncodablePrivateUser, EncodablePublicUser};
1313#[ derive( Clone , Debug , PartialEq , Eq , Queryable , Identifiable , AsChangeset , Associations ) ]
1414pub struct User {
1515 pub id : i32 ,
16- pub email : Option < String > ,
1716 pub gh_access_token : String ,
1817 pub gh_login : String ,
1918 pub name : Option < String > ,
2019 pub gh_avatar : Option < String > ,
2120 pub gh_id : i32 ,
2221}
2322
23+ /// Represents a new user record insertible to the `users` table
2424#[ derive( Insertable , Debug , Default ) ]
2525#[ table_name = "users" ]
2626pub struct NewUser < ' a > {
2727 pub gh_id : i32 ,
2828 pub gh_login : & ' a str ,
29- pub email : Option < & ' a str > ,
3029 pub name : Option < & ' a str > ,
3130 pub gh_avatar : Option < & ' a str > ,
3231 pub gh_access_token : Cow < ' a , str > ,
@@ -36,23 +35,25 @@ impl<'a> NewUser<'a> {
3635 pub fn new (
3736 gh_id : i32 ,
3837 gh_login : & ' a str ,
39- email : Option < & ' a str > ,
4038 name : Option < & ' a str > ,
4139 gh_avatar : Option < & ' a str > ,
4240 gh_access_token : & ' a str ,
4341 ) -> Self {
4442 NewUser {
4543 gh_id,
4644 gh_login,
47- email,
4845 name,
4946 gh_avatar,
5047 gh_access_token : Cow :: Borrowed ( gh_access_token) ,
5148 }
5249 }
5350
5451 /// Inserts the user into the database, or updates an existing one.
55- pub fn create_or_update ( & self , conn : & PgConnection ) -> QueryResult < User > {
52+ pub fn create_or_update (
53+ & self ,
54+ email : Option < & ' a str > ,
55+ conn : & PgConnection ,
56+ ) -> QueryResult < User > {
5657 use crate :: schema:: users:: dsl:: * ;
5758 use diesel:: dsl:: sql;
5859 use diesel:: insert_into;
@@ -81,7 +82,7 @@ impl<'a> NewUser<'a> {
8182 . get_result :: < User > ( conn) ?;
8283
8384 // To send the user an account verification email...
84- if let Some ( user_email) = user . email . as_ref ( ) {
85+ if let Some ( user_email) = email {
8586 let new_email = NewEmail {
8687 user_id : user. id ,
8788 email : user_email,
@@ -168,6 +169,8 @@ impl User {
168169 Ok ( best)
169170 }
170171
172+ /// Queries the database for the verified emails
173+ /// belonging to a given user
171174 pub fn verified_email ( & self , conn : & PgConnection ) -> CargoResult < Option < String > > {
172175 Ok ( Email :: belonging_to ( self )
173176 . select ( emails:: email)
@@ -179,19 +182,21 @@ impl User {
179182 /// Converts this `User` model into an `EncodablePrivateUser` for JSON serialization.
180183 pub fn encodable_private (
181184 self ,
185+ email : Option < String > ,
186+
182187 email_verified : bool ,
183188 email_verification_sent : bool ,
184- ) -> EncodablePrivateUser {
189+ ) -> CargoResult < EncodablePrivateUser > {
185190 let User {
186191 id,
187- email,
188192 name,
189193 gh_login,
190194 gh_avatar,
191195 ..
192196 } = self ;
193197 let url = format ! ( "https://github.com/{}" , gh_login) ;
194- EncodablePrivateUser {
198+
199+ Ok ( EncodablePrivateUser {
195200 id,
196201 email,
197202 email_verified,
@@ -200,7 +205,15 @@ impl User {
200205 login : gh_login,
201206 name,
202207 url : Some ( url) ,
203- }
208+ } )
209+ }
210+
211+ /// Queries for the email belonging to a particular user
212+ pub fn email ( & self , conn : & PgConnection ) -> CargoResult < Option < String > > {
213+ Ok ( Email :: belonging_to ( self )
214+ . select ( emails:: email)
215+ . first :: < String > ( & * conn)
216+ . optional ( ) ?)
204217 }
205218
206219 /// Converts this`User` model into an `EncodablePublicUser` for JSON serialization.
0 commit comments