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