]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/local_user.rs
Adding a setting to show / hide scores. Fixes #1503
[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_scores,
28   );
29
30   impl ToSafeSettings for LocalUser {
31     type SafeSettingsColumns = Columns;
32
33     /// Includes everything but the hashed password
34     fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
35       (
36         id,
37         person_id,
38         email,
39         show_nsfw,
40         theme,
41         default_sort_type,
42         default_listing_type,
43         lang,
44         show_avatars,
45         send_notifications_to_email,
46         validator_time,
47         show_scores,
48       )
49     }
50   }
51 }
52
53 pub trait LocalUser_ {
54   fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
55   fn update_password(
56     conn: &PgConnection,
57     local_user_id: LocalUserId,
58     new_password: &str,
59   ) -> Result<LocalUser, Error>;
60 }
61
62 impl LocalUser_ for LocalUser {
63   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   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<LocalUserForm, LocalUserId> for LocalUser {
89   fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
90     local_user.find(local_user_id).first::<Self>(conn)
91   }
92   fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
93     diesel::delete(local_user.find(local_user_id)).execute(conn)
94   }
95   fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
96     insert_into(local_user)
97       .values(form)
98       .get_result::<Self>(conn)
99   }
100   fn update(
101     conn: &PgConnection,
102     local_user_id: LocalUserId,
103     form: &LocalUserForm,
104   ) -> Result<Self, Error> {
105     diesel::update(local_user.find(local_user_id))
106       .set(form)
107       .get_result::<Self>(conn)
108   }
109 }