Skip to content

User: Replace encodable_private() method #3173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/controllers/user/me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::models::{
CrateOwner, Email, Follow, NewEmail, OwnerKind, User, Version, VersionOwnerAction,
};
use crate::schema::{crate_owners, crates, emails, follows, users, versions};
use crate::views::{EncodableMe, EncodableVersion, OwnedCrate};
use crate::views::{EncodableMe, EncodablePrivateUser, EncodableVersion, OwnedCrate};

/// Handles the `GET /me` route.
pub fn me(req: &mut dyn RequestExt) -> EndpointResult {
Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn me(req: &mut dyn RequestExt) -> EndpointResult {
let verified = verified.unwrap_or(false);
let verification_sent = verified || verification_sent;
Ok(req.json(&EncodableMe {
user: user.encodable_private(email, verified, verification_sent),
user: EncodablePrivateUser::from(user, email, verified, verification_sent),
owned_crates,
}))
}
Expand Down
29 changes: 0 additions & 29 deletions src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::util::errors::AppResult;

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

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

/// Converts this `User` model into an `EncodablePrivateUser` for JSON serialization.
pub fn encodable_private(
self,
email: Option<String>,
email_verified: bool,
email_verification_sent: bool,
) -> EncodablePrivateUser {
let User {
id,
name,
gh_login,
gh_avatar,
..
} = self;
let url = format!("https://github.com/{}", gh_login);

EncodablePrivateUser {
id,
email,
email_verified,
email_verification_sent,
avatar: gh_avatar,
login: gh_login,
name,
url: Some(url),
}
}

/// Queries for the email belonging to a particular user
pub fn email(&self, conn: &PgConnection) -> AppResult<Option<String>> {
Ok(Email::belonging_to(self)
Expand Down
30 changes: 30 additions & 0 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,36 @@ pub struct EncodablePrivateUser {
pub url: Option<String>,
}

impl EncodablePrivateUser {
/// Converts this `User` model into an `EncodablePrivateUser` for JSON serialization.
pub fn from(
user: User,
email: Option<String>,
email_verified: bool,
email_verification_sent: bool,
) -> Self {
let User {
id,
name,
gh_login,
gh_avatar,
..
} = user;
let url = format!("https://github.com/{}", gh_login);

EncodablePrivateUser {
id,
email,
email_verified,
email_verification_sent,
avatar: gh_avatar,
login: gh_login,
name,
url: Some(url),
}
}
}

/// The serialization format for the `User` model.
/// Same as private user, except no email field
#[derive(Deserialize, Serialize, Debug)]
Expand Down