lemmy/crates/db_schema/src/impls/local_user.rs

132 lines
3.3 KiB
Rust
Raw Normal View History

2021-10-16 13:33:38 +00:00
use crate::{
naive_now,
2021-10-16 13:33:38 +00:00
newtypes::LocalUserId,
2021-03-11 04:43:11 +00:00
schema::local_user::dsl::*,
2021-03-11 22:47:44 +00:00
source::local_user::{LocalUser, LocalUserForm},
2021-10-16 13:33:38 +00:00
traits::Crud,
2021-03-11 04:43:11 +00:00
};
2021-10-16 13:33:38 +00:00
use bcrypt::{hash, DEFAULT_COST};
use diesel::{dsl::*, result::Error, *};
2021-03-10 22:33:55 +00:00
2021-02-26 13:49:58 +00:00
mod safe_settings_type {
2021-10-16 13:33:38 +00:00
use crate::{
schema::local_user::columns::*,
source::local_user::LocalUser,
traits::ToSafeSettings,
};
2021-02-26 13:49:58 +00:00
type Columns = (
id,
2021-03-10 22:33:55 +00:00
person_id,
2021-02-26 13:49:58 +00:00
email,
show_nsfw,
theme,
default_sort_type,
default_listing_type,
lang,
show_avatars,
send_notifications_to_email,
validator_time,
show_bot_accounts,
show_scores,
show_read_posts,
show_new_post_notifs,
email_verified,
accepted_application,
2021-02-26 13:49:58 +00:00
);
2021-03-10 22:33:55 +00:00
impl ToSafeSettings for LocalUser {
2021-02-26 13:49:58 +00:00
type SafeSettingsColumns = Columns;
2021-03-11 04:43:11 +00:00
2021-03-10 22:33:55 +00:00
/// Includes everything but the hashed password
2021-02-26 13:49:58 +00:00
fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
(
id,
2021-03-10 22:33:55 +00:00
person_id,
2021-02-26 13:49:58 +00:00
email,
show_nsfw,
theme,
default_sort_type,
default_listing_type,
lang,
show_avatars,
send_notifications_to_email,
validator_time,
show_bot_accounts,
show_scores,
show_read_posts,
show_new_post_notifs,
email_verified,
accepted_application,
2021-02-26 13:49:58 +00:00
)
}
}
}
2021-10-16 13:33:38 +00:00
impl LocalUser {
pub fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
2021-03-10 22:33:55 +00:00
let mut edited_user = form.clone();
let password_hash = form
.password_encrypted
.as_ref()
.map(|p| hash(p, DEFAULT_COST).expect("Couldn't hash password"));
2021-03-10 22:33:55 +00:00
edited_user.password_encrypted = password_hash;
2021-07-05 16:07:26 +00:00
Self::create(conn, &edited_user)
2021-03-10 22:33:55 +00:00
}
2021-10-16 13:33:38 +00:00
pub fn update_password(
2021-03-11 04:43:11 +00:00
conn: &PgConnection,
local_user_id: LocalUserId,
2021-03-11 04:43:11 +00:00
new_password: &str,
) -> Result<Self, Error> {
2021-03-10 22:33:55 +00:00
let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
diesel::update(local_user.find(local_user_id))
.set((
password_encrypted.eq(password_hash),
validator_time.eq(naive_now()),
))
2021-03-10 22:33:55 +00:00
.get_result::<Self>(conn)
}
2021-12-12 18:14:30 +00:00
pub fn set_all_users_email_verified(conn: &PgConnection) -> Result<Vec<Self>, Error> {
diesel::update(local_user)
.set(email_verified.eq(true))
.get_results::<Self>(conn)
}
pub fn set_all_users_registration_applications_accepted(
conn: &PgConnection,
) -> Result<Vec<Self>, Error> {
diesel::update(local_user)
.set(accepted_application.eq(true))
.get_results::<Self>(conn)
}
2021-03-10 22:33:55 +00:00
}
impl Crud for LocalUser {
type Form = LocalUserForm;
type IdType = LocalUserId;
fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
2021-03-11 04:43:11 +00:00
local_user.find(local_user_id).first::<Self>(conn)
2021-03-10 22:33:55 +00:00
}
fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
2021-03-10 22:33:55 +00:00
diesel::delete(local_user.find(local_user_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
2021-03-11 04:43:11 +00:00
insert_into(local_user)
.values(form)
.get_result::<Self>(conn)
2021-03-10 22:33:55 +00:00
}
fn update(
conn: &PgConnection,
local_user_id: LocalUserId,
form: &LocalUserForm,
) -> Result<Self, Error> {
2021-03-10 22:33:55 +00:00
diesel::update(local_user.find(local_user_id))
.set(form)
.get_result::<Self>(conn)
}
}