Running cargo fmt.
This commit is contained in:
parent
68e4b61808
commit
3350599901
7 changed files with 101 additions and 90 deletions
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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<PasswordResetResponse> for Oper<PasswordReset> {
|
|||
|
||||
// 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<PasswordResetResponse> for Oper<PasswordReset> {
|
|||
let html = &format!("<h1>Password Reset Request for {}</h1><br><a href={}/password_change/{}>Click here to reset your password</a>", 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 {
|
||||
|
|
|
@ -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<T> {
|
||||
fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
|
||||
|
|
|
@ -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<PasswordResetRequestForm> for PasswordResetRequest {
|
||||
fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
|
||||
use crate::schema::password_reset_request::dsl::*;
|
||||
password_reset_request.find(password_reset_request_id).first::<Self>(conn)
|
||||
password_reset_request
|
||||
.find(password_reset_request_id)
|
||||
.first::<Self>(conn)
|
||||
}
|
||||
fn delete(conn: &PgConnection, password_reset_request_id: i32) -> Result<usize, Error> {
|
||||
diesel::delete(password_reset_request.find(password_reset_request_id)).execute(conn)
|
||||
}
|
||||
fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result<Self, Error> {
|
||||
insert_into(password_reset_request).values(form).get_result::<Self>(conn)
|
||||
insert_into(password_reset_request)
|
||||
.values(form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
fn update(conn: &PgConnection, password_reset_request_id: i32, form: &PasswordResetRequestForm) -> Result<Self, Error> {
|
||||
fn update(
|
||||
conn: &PgConnection,
|
||||
password_reset_request_id: i32,
|
||||
form: &PasswordResetRequestForm,
|
||||
) -> Result<Self, Error> {
|
||||
diesel::update(password_reset_request.find(password_reset_request_id))
|
||||
.set(form)
|
||||
.get_result::<Self>(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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,7 +69,11 @@ impl User_ {
|
|||
Self::create(&conn, &edited_user)
|
||||
}
|
||||
|
||||
pub fn update_password(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result<Self, Error> {
|
||||
pub fn update_password(
|
||||
conn: &PgConnection,
|
||||
user_id: i32,
|
||||
form: &UserForm,
|
||||
) -> Result<Self, Error> {
|
||||
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::<User_>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_by_email(
|
||||
conn: &PgConnection,
|
||||
from_email: &str,
|
||||
) -> Result<Self, Error> {
|
||||
user_
|
||||
.filter(email.eq(from_email))
|
||||
.first::<User_>(conn)
|
||||
}
|
||||
|
||||
pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<Self, Error> {
|
||||
user_.filter(email.eq(from_email)).first::<User_>(conn)
|
||||
}
|
||||
|
||||
pub fn find_by_jwt(conn: &PgConnection, jwt: &str) -> Result<Self, Error> {
|
||||
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() {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue