]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/local_user.rs
Add cargo feature for building lemmy_api_common with mininum deps (#2243)
[lemmy.git] / crates / db_schema / src / impls / local_user.rs
1 use crate::{
2   newtypes::LocalUserId,
3   schema::local_user::dsl::*,
4   source::local_user::{LocalUser, LocalUserForm},
5   traits::Crud,
6   utils::naive_now,
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     email_verified,
35     accepted_application,
36   );
37
38   impl ToSafeSettings for LocalUser {
39     type SafeSettingsColumns = Columns;
40
41     /// Includes everything but the hashed password
42     fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
43       (
44         id,
45         person_id,
46         email,
47         show_nsfw,
48         theme,
49         default_sort_type,
50         default_listing_type,
51         lang,
52         show_avatars,
53         send_notifications_to_email,
54         validator_time,
55         show_bot_accounts,
56         show_scores,
57         show_read_posts,
58         show_new_post_notifs,
59         email_verified,
60         accepted_application,
61       )
62     }
63   }
64 }
65
66 impl LocalUser {
67   pub fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
68     let mut edited_user = form.clone();
69     let password_hash = form
70       .password_encrypted
71       .as_ref()
72       .map(|p| hash(p, DEFAULT_COST).expect("Couldn't hash password"));
73     edited_user.password_encrypted = password_hash;
74
75     Self::create(conn, &edited_user)
76   }
77
78   pub 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   pub fn set_all_users_email_verified(conn: &PgConnection) -> Result<Vec<Self>, Error> {
94     diesel::update(local_user)
95       .set(email_verified.eq(true))
96       .get_results::<Self>(conn)
97   }
98
99   pub fn set_all_users_registration_applications_accepted(
100     conn: &PgConnection,
101   ) -> Result<Vec<Self>, Error> {
102     diesel::update(local_user)
103       .set(accepted_application.eq(true))
104       .get_results::<Self>(conn)
105   }
106 }
107
108 impl Crud for LocalUser {
109   type Form = LocalUserForm;
110   type IdType = LocalUserId;
111   fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
112     local_user.find(local_user_id).first::<Self>(conn)
113   }
114   fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
115     diesel::delete(local_user.find(local_user_id)).execute(conn)
116   }
117   fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
118     insert_into(local_user)
119       .values(form)
120       .get_result::<Self>(conn)
121   }
122   fn update(
123     conn: &PgConnection,
124     local_user_id: LocalUserId,
125     form: &LocalUserForm,
126   ) -> Result<Self, Error> {
127     diesel::update(local_user.find(local_user_id))
128       .set(form)
129       .get_result::<Self>(conn)
130   }
131 }