Skip to content

Commit 521beb7

Browse files
committed
remove uses of email field on User
1 parent a898c38 commit 521beb7

File tree

10 files changed

+36
-31
lines changed

10 files changed

+36
-31
lines changed

migrations/20140924113530_dumped_migration_1/up.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ CREATE TABLE users (
33
email VARCHAR NOT NULL UNIQUE,
44
gh_access_token VARCHAR NOT NULL,
55
api_token VARCHAR NOT NULL
6-
);
6+
);
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Not reversible
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Your SQL goes here
2+
ALTER TABLE users DROP COLUMN email CASCADE;

src/controllers/user/me.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ pub fn me(req: &mut dyn Request) -> CargoResult<Response> {
3838

3939
let verified = verified.unwrap_or(false);
4040
let verification_sent = verified || verification_sent;
41-
let user = User { email, ..user };
41+
let user = User { ..user };
4242

4343
Ok(req.json(&EncodableMe {
44-
user: user.encodable_private(verified, verification_sent),
44+
user: user.encodable_private(email, verified, verification_sent)?,
4545
}))
4646
}
4747

@@ -91,8 +91,7 @@ pub fn updates(req: &mut dyn Request) -> CargoResult<Response> {
9191
/// Handles the `PUT /user/:user_id` route.
9292
pub fn update_user(req: &mut dyn Request) -> CargoResult<Response> {
9393
use self::emails::user_id;
94-
use self::users::dsl::{email, gh_login, users};
95-
use diesel::{insert_into, update};
94+
use diesel::insert_into;
9695

9796
let mut body = String::new();
9897
req.body().read_to_string(&mut body)?;
@@ -130,10 +129,6 @@ pub fn update_user(req: &mut dyn Request) -> CargoResult<Response> {
130129
}
131130

132131
conn.transaction::<_, Box<dyn CargoError>, _>(|| {
133-
update(users.filter(gh_login.eq(&user.gh_login)))
134-
.set(email.eq(user_email))
135-
.execute(&*conn)?;
136-
137132
let new_email = NewEmail {
138133
user_id: user.id,
139134
email: user_email,

src/controllers/user/session.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,11 @@ impl GithubUser {
118118
NewUser::new(
119119
self.id,
120120
&self.login,
121-
self.email.as_ref().map(|s| &s[..]),
122121
self.name.as_ref().map(|s| &s[..]),
123122
self.avatar_url.as_ref().map(|s| &s[..]),
124123
access_token,
125124
)
126-
.create_or_update(conn)
125+
.create_or_update(self.email.as_ref().map(|s| &s[..]), conn)
127126
.map_err(Into::into)
128127
.or_else(|e: Box<dyn CargoError>| {
129128
// If we're in read only mode, we can't update their details

src/models/user.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@ use crate::views::{EncodablePrivateUser, EncodablePublicUser};
1313
#[derive(Clone, Debug, PartialEq, Eq, Queryable, Identifiable, AsChangeset, Associations)]
1414
pub 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"]
2626
pub 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.

src/publish_rate_limit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ mod tests {
325325
gh_login,
326326
..NewUser::default()
327327
}
328-
.create_or_update(conn)?;
328+
.create_or_update(None, conn)?;
329329
Ok(user.id)
330330
}
331331

src/schema.rs

-6
Original file line numberDiff line numberDiff line change
@@ -745,12 +745,6 @@ table! {
745745
///
746746
/// (Automatically generated by Diesel.)
747747
id -> Int4,
748-
/// The `email` column of the `users` table.
749-
///
750-
/// Its SQL type is `Nullable<Varchar>`.
751-
///
752-
/// (Automatically generated by Diesel.)
753-
email -> Nullable<Varchar>,
754748
/// The `gh_access_token` column of the `users` table.
755749
///
756750
/// Its SQL type is `Varchar`.

src/tasks/update_downloads.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ mod test {
9393
}
9494

9595
fn user(conn: &PgConnection) -> User {
96-
NewUser::new(2, "login", None, None, None, "access_token")
97-
.create_or_update(conn)
96+
NewUser::new(2, "login", None, None, "access_token")
97+
.create_or_update(None, conn)
9898
.unwrap()
9999
}
100100

src/views.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ pub struct EncodableMe {
161161
pub struct EncodablePrivateUser {
162162
pub id: i32,
163163
pub login: String,
164-
pub email: Option<String>,
165164
pub email_verified: bool,
166165
pub email_verification_sent: bool,
167166
pub name: Option<String>,
167+
pub email: Option<String>,
168168
pub avatar: Option<String>,
169169
pub url: Option<String>,
170170
}

0 commit comments

Comments
 (0)