Skip to content

Commit 3eb1ce6

Browse files
committed
Auto merge of #3173 - Turbo87:encodable-user, r=pietroalbini
User: Replace `encodable_private()` method This PR is similar to #3168 and brings us one step closer to removing the `models -> views` dependency by inverting the relationship for the `User` model and `EncodablePrivateUser` view. r? `@pietroalbini`
2 parents 1ec0fc9 + 58842e6 commit 3eb1ce6

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

src/controllers/user/me.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::models::{
1010
CrateOwner, Email, Follow, NewEmail, OwnerKind, User, Version, VersionOwnerAction,
1111
};
1212
use crate::schema::{crate_owners, crates, emails, follows, users, versions};
13-
use crate::views::{EncodableMe, EncodableVersion, OwnedCrate};
13+
use crate::views::{EncodableMe, EncodablePrivateUser, EncodableVersion, OwnedCrate};
1414

1515
/// Handles the `GET /me` route.
1616
pub fn me(req: &mut dyn RequestExt) -> EndpointResult {
@@ -46,7 +46,7 @@ pub fn me(req: &mut dyn RequestExt) -> EndpointResult {
4646
let verified = verified.unwrap_or(false);
4747
let verification_sent = verified || verification_sent;
4848
Ok(req.json(&EncodableMe {
49-
user: user.encodable_private(email, verified, verification_sent),
49+
user: EncodablePrivateUser::from(user, email, verified, verification_sent),
5050
owned_crates,
5151
}))
5252
}

src/models/user.rs

-29
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::util::errors::AppResult;
77

88
use crate::models::{ApiToken, Crate, CrateOwner, Email, NewEmail, Owner, OwnerKind, Rights};
99
use crate::schema::{crate_owners, emails, users};
10-
use crate::views::EncodablePrivateUser;
1110

1211
/// The model representing a row in the `users` database table.
1312
#[derive(Clone, Debug, PartialEq, Eq, Queryable, Identifiable, AsChangeset, Associations)]
@@ -168,34 +167,6 @@ impl User {
168167
.optional()?)
169168
}
170169

171-
/// Converts this `User` model into an `EncodablePrivateUser` for JSON serialization.
172-
pub fn encodable_private(
173-
self,
174-
email: Option<String>,
175-
email_verified: bool,
176-
email_verification_sent: bool,
177-
) -> EncodablePrivateUser {
178-
let User {
179-
id,
180-
name,
181-
gh_login,
182-
gh_avatar,
183-
..
184-
} = self;
185-
let url = format!("https://github.com/{}", gh_login);
186-
187-
EncodablePrivateUser {
188-
id,
189-
email,
190-
email_verified,
191-
email_verification_sent,
192-
avatar: gh_avatar,
193-
login: gh_login,
194-
name,
195-
url: Some(url),
196-
}
197-
}
198-
199170
/// Queries for the email belonging to a particular user
200171
pub fn email(&self, conn: &PgConnection) -> AppResult<Option<String>> {
201172
Ok(Email::belonging_to(self)

src/views.rs

+30
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,36 @@ pub struct EncodablePrivateUser {
352352
pub url: Option<String>,
353353
}
354354

355+
impl EncodablePrivateUser {
356+
/// Converts this `User` model into an `EncodablePrivateUser` for JSON serialization.
357+
pub fn from(
358+
user: User,
359+
email: Option<String>,
360+
email_verified: bool,
361+
email_verification_sent: bool,
362+
) -> Self {
363+
let User {
364+
id,
365+
name,
366+
gh_login,
367+
gh_avatar,
368+
..
369+
} = user;
370+
let url = format!("https://github.com/{}", gh_login);
371+
372+
EncodablePrivateUser {
373+
id,
374+
email,
375+
email_verified,
376+
email_verification_sent,
377+
avatar: gh_avatar,
378+
login: gh_login,
379+
name,
380+
url: Some(url),
381+
}
382+
}
383+
}
384+
355385
/// The serialization format for the `User` model.
356386
/// Same as private user, except no email field
357387
#[derive(Deserialize, Serialize, Debug)]

0 commit comments

Comments
 (0)