From 76a47acacd489a5e05ab63f07c935a9241fb52fb Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 1 Nov 2019 23:43:21 -0700 Subject: [PATCH] Running cargo fmt. --- server/src/api/mod.rs | 4 +- server/src/api/user.rs | 12 ++--- server/src/db/mod.rs | 2 +- server/src/db/password_reset_request.rs | 31 +++++++---- server/src/db/user.rs | 20 ++++--- server/src/lib.rs | 70 ++++++++++++++----------- server/src/schema.rs | 52 +++++++++--------- 7 files changed, 101 insertions(+), 90 deletions(-) diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index 6ccdef1a3..6b1a6f6e1 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -5,13 +5,13 @@ use crate::db::community::*; use crate::db::community_view::*; use crate::db::moderator::*; use crate::db::moderator_views::*; +use crate::db::password_reset_request::*; use crate::db::post::*; use crate::db::post_view::*; use crate::db::user::*; use crate::db::user_mention::*; use crate::db::user_mention_view::*; use crate::db::user_view::*; -use crate::db::password_reset_request::*; use crate::db::*; use crate::{extract_usernames, has_slurs, naive_from_unix, naive_now, remove_slurs, Settings}; use failure::Error; @@ -62,7 +62,7 @@ pub enum UserOperation { TransferCommunity, TransferSite, DeleteAccount, - PasswordReset, + PasswordReset, PasswordChange, } diff --git a/server/src/api/user.rs b/server/src/api/user.rs index ee4070c5a..50d3feb71 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -1,7 +1,7 @@ use super::*; +use crate::{generate_random_string, send_email}; use bcrypt::verify; use std::str::FromStr; -use crate::{generate_random_string,send_email}; #[derive(Serialize, Deserialize, Debug)] pub struct Login { @@ -140,7 +140,6 @@ pub struct DeleteAccount { auth: String, } - #[derive(Serialize, Deserialize)] pub struct PasswordReset { email: String, @@ -840,7 +839,7 @@ impl Perform for Oper { // Generate a random token let token = generate_random_string(); - + // Insert the row PasswordResetRequest::create_token(&conn, user.id, &token)?; @@ -852,12 +851,7 @@ impl Perform for Oper { let html = &format!("

Password Reset Request for {}


Click here to reset your password", user.name, hostname, &token); match send_email(subject, user_email, &user.name, html) { Ok(_o) => _o, - Err(_e) => { - return Err(APIError::err( - &self.op, - &_e.to_string(), - ))? - } + Err(_e) => return Err(APIError::err(&self.op, &_e.to_string()))?, }; Ok(PasswordResetResponse { diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs index 8070041c6..09640e22c 100644 --- a/server/src/db/mod.rs +++ b/server/src/db/mod.rs @@ -11,13 +11,13 @@ pub mod community; pub mod community_view; pub mod moderator; pub mod moderator_views; +pub mod password_reset_request; pub mod post; pub mod post_view; pub mod user; pub mod user_mention; pub mod user_mention_view; pub mod user_view; -pub mod password_reset_request; pub trait Crud { fn create(conn: &PgConnection, form: &T) -> Result diff --git a/server/src/db/password_reset_request.rs b/server/src/db/password_reset_request.rs index 6720bd7d3..265945df2 100644 --- a/server/src/db/password_reset_request.rs +++ b/server/src/db/password_reset_request.rs @@ -1,8 +1,8 @@ use super::*; use crate::schema::password_reset_request; use crate::schema::password_reset_request::dsl::*; -use crypto::sha2::Sha256; use crypto::digest::Digest; +use crypto::sha2::Sha256; #[derive(Queryable, Identifiable, PartialEq, Debug)] #[table_name = "password_reset_request"] @@ -23,15 +23,23 @@ pub struct PasswordResetRequestForm { impl Crud for PasswordResetRequest { fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result { use crate::schema::password_reset_request::dsl::*; - password_reset_request.find(password_reset_request_id).first::(conn) + password_reset_request + .find(password_reset_request_id) + .first::(conn) } fn delete(conn: &PgConnection, password_reset_request_id: i32) -> Result { diesel::delete(password_reset_request.find(password_reset_request_id)).execute(conn) } fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result { - insert_into(password_reset_request).values(form).get_result::(conn) + insert_into(password_reset_request) + .values(form) + .get_result::(conn) } - fn update(conn: &PgConnection, password_reset_request_id: i32, form: &PasswordResetRequestForm) -> Result { + fn update( + conn: &PgConnection, + password_reset_request_id: i32, + form: &PasswordResetRequestForm, + ) -> Result { diesel::update(password_reset_request.find(password_reset_request_id)) .set(form) .get_result::(conn) @@ -64,8 +72,8 @@ impl PasswordResetRequest { #[cfg(test)] mod tests { - use super::*; use super::super::user::*; + use super::*; #[test] fn test_crud() { @@ -92,8 +100,9 @@ mod tests { user_id: inserted_user.id, token_encrypted: "no".into(), }; - - let inserted_password_reset_request = PasswordResetRequest::create(&conn, &new_password_reset_request).unwrap(); + + let inserted_password_reset_request = + PasswordResetRequest::create(&conn, &new_password_reset_request).unwrap(); let expected_password_reset_request = PasswordResetRequest { id: inserted_password_reset_request.id, @@ -102,11 +111,15 @@ mod tests { published: inserted_password_reset_request.published, }; - let read_password_reset_request = PasswordResetRequest::read(&conn, inserted_password_reset_request.id).unwrap(); + let read_password_reset_request = + PasswordResetRequest::read(&conn, inserted_password_reset_request.id).unwrap(); let num_deleted = User_::delete(&conn, inserted_user.id).unwrap(); assert_eq!(expected_password_reset_request, read_password_reset_request); - assert_eq!(expected_password_reset_request, inserted_password_reset_request); + assert_eq!( + expected_password_reset_request, + inserted_password_reset_request + ); assert_eq!(1, num_deleted); } } diff --git a/server/src/db/user.rs b/server/src/db/user.rs index da8e5dc2f..064bff39f 100644 --- a/server/src/db/user.rs +++ b/server/src/db/user.rs @@ -69,7 +69,11 @@ impl User_ { Self::create(&conn, &edited_user) } - pub fn update_password(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result { + pub fn update_password( + conn: &PgConnection, + user_id: i32, + form: &UserForm, + ) -> Result { let mut edited_user = form.clone(); let password_hash = hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password"); @@ -138,16 +142,10 @@ impl User_ { .first::(conn) } } - - pub fn find_by_email( - conn: &PgConnection, - from_email: &str, - ) -> Result { - user_ - .filter(email.eq(from_email)) - .first::(conn) - } + pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result { + user_.filter(email.eq(from_email)).first::(conn) + } pub fn find_by_jwt(conn: &PgConnection, jwt: &str) -> Result { let claims: Claims = Claims::decode(&jwt).expect("Invalid token").claims; @@ -157,8 +155,8 @@ impl User_ { #[cfg(test)] mod tests { - use super::*; use super::User_; + use super::*; #[test] fn test_crud() { diff --git a/server/src/lib.rs b/server/src/lib.rs index 653a6fefc..162d95786 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -11,16 +11,16 @@ pub extern crate actix; pub extern crate actix_web; pub extern crate bcrypt; pub extern crate chrono; +pub extern crate crypto; pub extern crate dotenv; pub extern crate jsonwebtoken; +pub extern crate lettre; +pub extern crate lettre_email; pub extern crate rand; pub extern crate regex; pub extern crate serde; pub extern crate serde_json; pub extern crate strum; -pub extern crate lettre; -pub extern crate lettre_email; -pub extern crate crypto; pub mod api; pub mod apub; @@ -30,15 +30,15 @@ pub mod websocket; use chrono::{DateTime, NaiveDateTime, Utc}; use dotenv::dotenv; -use regex::Regex; -use std::env; -use rand::{thread_rng, Rng}; -use rand::distributions::Alphanumeric; -use lettre::{SmtpClient, Transport}; -use lettre_email::{Email}; use lettre::smtp::authentication::{Credentials, Mechanism}; use lettre::smtp::extension::ClientId; use lettre::smtp::ConnectionReuseParameters; +use lettre::{SmtpClient, Transport}; +use lettre_email::Email; +use rand::distributions::Alphanumeric; +use rand::{thread_rng, Rng}; +use regex::Regex; +use std::env; pub struct Settings { db_url: String, @@ -63,18 +63,18 @@ pub struct EmailConfig { impl Settings { fn get() -> Self { dotenv().ok(); - - let email_config = if env::var("SMTP_SERVER").is_ok() && - !env::var("SMTP_SERVER").unwrap().eq("") { - Some(EmailConfig { - smtp_server: env::var("SMTP_SERVER").expect("SMTP_SERVER must be set"), - smtp_login: env::var("SMTP_LOGIN").expect("SMTP_LOGIN must be set"), - smtp_password: env::var("SMTP_PASSWORD").expect("SMTP_PASSWORD must be set"), - smtp_from_address: env::var("SMTP_FROM_ADDRESS").expect("SMTP_FROM_ADDRESS must be set") - }) - } else { - None - }; + + let email_config = + if env::var("SMTP_SERVER").is_ok() && !env::var("SMTP_SERVER").unwrap().eq("") { + Some(EmailConfig { + smtp_server: env::var("SMTP_SERVER").expect("SMTP_SERVER must be set"), + smtp_login: env::var("SMTP_LOGIN").expect("SMTP_LOGIN must be set"), + smtp_password: env::var("SMTP_PASSWORD").expect("SMTP_PASSWORD must be set"), + smtp_from_address: env::var("SMTP_FROM_ADDRESS").expect("SMTP_FROM_ADDRESS must be set"), + }) + } else { + None + }; Settings { db_url: env::var("DATABASE_URL").expect("DATABASE_URL must be set"), @@ -104,7 +104,7 @@ impl Settings { .unwrap_or("3600".to_string()) .parse() .unwrap(), - email_config: email_config, + email_config: email_config, } } fn api_endpoint(&self) -> String { @@ -151,29 +151,35 @@ pub fn extract_usernames(test: &str) -> Vec<&str> { } pub fn generate_random_string() -> String { - thread_rng() - .sample_iter(&Alphanumeric) - .take(30) - .collect() + thread_rng().sample_iter(&Alphanumeric).take(30).collect() } -pub fn send_email(subject: &str, to_email: &str, to_username: &str, html: &str) -> Result<(), String> { - +pub fn send_email( + subject: &str, + to_email: &str, + to_username: &str, + html: &str, +) -> Result<(), String> { let email_config = Settings::get().email_config.ok_or("no_email_setup")?; let email = Email::builder() .to((to_email, to_username)) - .from((email_config.smtp_login.to_owned(), email_config.smtp_from_address)) + .from(( + email_config.smtp_login.to_owned(), + email_config.smtp_from_address, + )) .subject(subject) .html(html) .build() .unwrap(); - let mut mailer = SmtpClient::new_simple(&email_config.smtp_server).unwrap() + let mut mailer = SmtpClient::new_simple(&email_config.smtp_server) + .unwrap() .hello_name(ClientId::Domain("localhost".to_string())) .credentials(Credentials::new( - email_config.smtp_login.to_owned(), - email_config.smtp_password.to_owned())) + email_config.smtp_login.to_owned(), + email_config.smtp_password.to_owned(), + )) .smtp_utf8(true) .authentication_mechanism(Mechanism::Plain) .connection_reuse(ConnectionReuseParameters::ReuseUnlimited) diff --git a/server/src/schema.rs b/server/src/schema.rs index 15e87bbee..5fc709536 100644 --- a/server/src/schema.rs +++ b/server/src/schema.rs @@ -329,30 +329,30 @@ joinable!(user_mention -> comment (comment_id)); joinable!(user_mention -> user_ (recipient_id)); allow_tables_to_appear_in_same_query!( - category, - comment, - comment_like, - comment_saved, - community, - community_follower, - community_moderator, - community_user_ban, - mod_add, - mod_add_community, - mod_ban, - mod_ban_from_community, - mod_lock_post, - mod_remove_comment, - mod_remove_community, - mod_remove_post, - mod_sticky_post, - password_reset_request, - post, - post_like, - post_read, - post_saved, - site, - user_, - user_ban, - user_mention, + category, + comment, + comment_like, + comment_saved, + community, + community_follower, + community_moderator, + community_user_ban, + mod_add, + mod_add_community, + mod_ban, + mod_ban_from_community, + mod_lock_post, + mod_remove_comment, + mod_remove_community, + mod_remove_post, + mod_sticky_post, + password_reset_request, + post, + post_like, + post_read, + post_saved, + site, + user_, + user_ban, + user_mention, );