@@ -13,20 +13,19 @@ use crate::views::{EncodablePrivateUser, EncodablePublicUser};
13
13
#[ derive( Clone , Debug , PartialEq , Eq , Queryable , Identifiable , AsChangeset , Associations ) ]
14
14
pub struct User {
15
15
pub id : i32 ,
16
- pub email : Option < String > ,
17
16
pub gh_access_token : String ,
18
17
pub gh_login : String ,
19
18
pub name : Option < String > ,
20
19
pub gh_avatar : Option < String > ,
21
20
pub gh_id : i32 ,
22
21
}
23
22
23
+ /// Represents a new user record insertible to the `users` table
24
24
#[ derive( Insertable , Debug , Default ) ]
25
25
#[ table_name = "users" ]
26
26
pub struct NewUser < ' a > {
27
27
pub gh_id : i32 ,
28
28
pub gh_login : & ' a str ,
29
- pub email : Option < & ' a str > ,
30
29
pub name : Option < & ' a str > ,
31
30
pub gh_avatar : Option < & ' a str > ,
32
31
pub gh_access_token : Cow < ' a , str > ,
@@ -36,23 +35,25 @@ impl<'a> NewUser<'a> {
36
35
pub fn new (
37
36
gh_id : i32 ,
38
37
gh_login : & ' a str ,
39
- email : Option < & ' a str > ,
40
38
name : Option < & ' a str > ,
41
39
gh_avatar : Option < & ' a str > ,
42
40
gh_access_token : & ' a str ,
43
41
) -> Self {
44
42
NewUser {
45
43
gh_id,
46
44
gh_login,
47
- email,
48
45
name,
49
46
gh_avatar,
50
47
gh_access_token : Cow :: Borrowed ( gh_access_token) ,
51
48
}
52
49
}
53
50
54
51
/// 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 > {
56
57
use crate :: schema:: users:: dsl:: * ;
57
58
use diesel:: dsl:: sql;
58
59
use diesel:: insert_into;
@@ -81,7 +82,7 @@ impl<'a> NewUser<'a> {
81
82
. get_result :: < User > ( conn) ?;
82
83
83
84
// 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 {
85
86
let new_email = NewEmail {
86
87
user_id : user. id ,
87
88
email : user_email,
@@ -168,6 +169,8 @@ impl User {
168
169
Ok ( best)
169
170
}
170
171
172
+ /// Queries the database for the verified emails
173
+ /// belonging to a given user
171
174
pub fn verified_email ( & self , conn : & PgConnection ) -> CargoResult < Option < String > > {
172
175
Ok ( Email :: belonging_to ( self )
173
176
. select ( emails:: email)
@@ -179,19 +182,21 @@ impl User {
179
182
/// Converts this `User` model into an `EncodablePrivateUser` for JSON serialization.
180
183
pub fn encodable_private (
181
184
self ,
185
+ email : Option < String > ,
186
+
182
187
email_verified : bool ,
183
188
email_verification_sent : bool ,
184
- ) -> EncodablePrivateUser {
189
+ ) -> CargoResult < EncodablePrivateUser > {
185
190
let User {
186
191
id,
187
- email,
188
192
name,
189
193
gh_login,
190
194
gh_avatar,
191
195
..
192
196
} = self ;
193
197
let url = format ! ( "https://github.com/{}" , gh_login) ;
194
- EncodablePrivateUser {
198
+
199
+ Ok ( EncodablePrivateUser {
195
200
id,
196
201
email,
197
202
email_verified,
@@ -200,7 +205,15 @@ impl User {
200
205
login : gh_login,
201
206
name,
202
207
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 ( ) ?)
204
217
}
205
218
206
219
/// Converts this`User` model into an `EncodablePublicUser` for JSON serialization.
0 commit comments