]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/local_user.rs
b711c82aa03638c9fc678722279ad076b4fcca71
[lemmy.git] / crates / db_queries / src / source / local_user.rs
1 use crate::Crud;
2 use bcrypt::{hash, DEFAULT_COST};
3 use diesel::{dsl::*, result::Error, *};
4 use lemmy_db_schema::{
5   naive_now,
6   schema::local_user::dsl::*,
7   source::local_user::{LocalUser, LocalUserForm},
8   LocalUserId,
9 };
10
11 mod safe_settings_type {
12   use crate::ToSafeSettings;
13   use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
14
15   type Columns = (
16     id,
17     person_id,
18     email,
19     show_nsfw,
20     theme,
21     default_sort_type,
22     default_listing_type,
23     lang,
24     show_avatars,
25     send_notifications_to_email,
26     validator_time,
27     show_bot_accounts,
28     show_scores,
29     show_read_posts,
30     show_new_post_notifs,
31   );
32
33   impl ToSafeSettings for LocalUser {
34     type SafeSettingsColumns = Columns;
35
36     /// Includes everything but the hashed password
37     fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
38       (
39         id,
40         person_id,
41         email,
42         show_nsfw,
43         theme,
44         default_sort_type,
45         default_listing_type,
46         lang,
47         show_avatars,
48         send_notifications_to_email,
49         validator_time,
50         show_bot_accounts,
51         show_scores,
52         show_read_posts,
53         show_new_post_notifs,
54       )
55     }
56   }
57 }
58
59 pub trait LocalUser_ {
60   fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
61   fn update_password(
62     conn: &PgConnection,
63     local_user_id: LocalUserId,
64     new_password: &str,
65   ) -> Result<LocalUser, Error>;
66 }
67
68 impl LocalUser_ for LocalUser {
69   fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
70     let mut edited_user = form.clone();
71     let password_hash =
72       hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
73     edited_user.password_encrypted = password_hash;
74
75     Self::create(conn, &edited_user)
76   }
77
78   fn update_password(
79     conn: &PgConnection,
80     local_user_id: LocalUserId,
81     new_password: &str,
82   ) -> Result<Self, Error> {
83     let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
84
85     diesel::update(local_user.find(local_user_id))
86       .set((
87         password_encrypted.eq(password_hash),
88         validator_time.eq(naive_now()),
89       ))
90       .get_result::<Self>(conn)
91   }
92 }
93
94 impl Crud<LocalUserForm, LocalUserId> for LocalUser {
95   fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
96     local_user.find(local_user_id).first::<Self>(conn)
97   }
98   fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
99     diesel::delete(local_user.find(local_user_id)).execute(conn)
100   }
101   fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
102     insert_into(local_user)
103       .values(form)
104       .get_result::<Self>(conn)
105   }
106   fn update(
107     conn: &PgConnection,
108     local_user_id: LocalUserId,
109     form: &LocalUserForm,
110   ) -> Result<Self, Error> {
111     diesel::update(local_user.find(local_user_id))
112       .set(form)
113       .get_result::<Self>(conn)
114   }
115 }