]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/local_user.rs
Moving settings to Database. (#2492)
[lemmy.git] / crates / db_schema / src / impls / local_user.rs
1 use crate::{
2   newtypes::LocalUserId,
3   schema::local_user::dsl::*,
4   source::{
5     actor_language::{LocalUserLanguage, SiteLanguage},
6     local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
7   },
8   traits::Crud,
9   utils::naive_now,
10 };
11 use bcrypt::{hash, DEFAULT_COST};
12 use diesel::{dsl::*, result::Error, *};
13
14 mod safe_settings_type {
15   use crate::{
16     schema::local_user::columns::*,
17     source::local_user::LocalUser,
18     traits::ToSafeSettings,
19   };
20
21   type Columns = (
22     id,
23     person_id,
24     email,
25     show_nsfw,
26     theme,
27     default_sort_type,
28     default_listing_type,
29     interface_language,
30     show_avatars,
31     send_notifications_to_email,
32     validator_time,
33     show_bot_accounts,
34     show_scores,
35     show_read_posts,
36     show_new_post_notifs,
37     email_verified,
38     accepted_application,
39   );
40
41   impl ToSafeSettings for LocalUser {
42     type SafeSettingsColumns = Columns;
43
44     /// Includes everything but the hashed password
45     fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
46       (
47         id,
48         person_id,
49         email,
50         show_nsfw,
51         theme,
52         default_sort_type,
53         default_listing_type,
54         interface_language,
55         show_avatars,
56         send_notifications_to_email,
57         validator_time,
58         show_bot_accounts,
59         show_scores,
60         show_read_posts,
61         show_new_post_notifs,
62         email_verified,
63         accepted_application,
64       )
65     }
66   }
67 }
68
69 impl LocalUser {
70   pub fn update_password(
71     conn: &mut PgConnection,
72     local_user_id: LocalUserId,
73     new_password: &str,
74   ) -> Result<Self, Error> {
75     let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
76
77     diesel::update(local_user.find(local_user_id))
78       .set((
79         password_encrypted.eq(password_hash),
80         validator_time.eq(naive_now()),
81       ))
82       .get_result::<Self>(conn)
83   }
84
85   pub fn set_all_users_email_verified(conn: &mut PgConnection) -> Result<Vec<Self>, Error> {
86     diesel::update(local_user)
87       .set(email_verified.eq(true))
88       .get_results::<Self>(conn)
89   }
90
91   pub fn set_all_users_registration_applications_accepted(
92     conn: &mut PgConnection,
93   ) -> Result<Vec<Self>, Error> {
94     diesel::update(local_user)
95       .set(accepted_application.eq(true))
96       .get_results::<Self>(conn)
97   }
98 }
99
100 impl Crud for LocalUser {
101   type InsertForm = LocalUserInsertForm;
102   type UpdateForm = LocalUserUpdateForm;
103   type IdType = LocalUserId;
104   fn read(conn: &mut PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
105     local_user.find(local_user_id).first::<Self>(conn)
106   }
107   fn delete(conn: &mut PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
108     diesel::delete(local_user.find(local_user_id)).execute(conn)
109   }
110   fn create(conn: &mut PgConnection, form: &Self::InsertForm) -> Result<Self, Error> {
111     let mut form_with_encrypted_password = form.clone();
112     let password_hash =
113       hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
114     form_with_encrypted_password.password_encrypted = password_hash;
115
116     let local_user_ = insert_into(local_user)
117       .values(form_with_encrypted_password)
118       .get_result::<Self>(conn)?;
119
120     let site_languages = SiteLanguage::read_local(conn);
121     if let Ok(langs) = site_languages {
122       // if site exists, init user with site languages
123       LocalUserLanguage::update(conn, langs, local_user_.id)?;
124     } else {
125       // otherwise, init with all languages (this only happens during tests and
126       // for first admin user, which is created before site)
127       LocalUserLanguage::update(conn, vec![], local_user_.id)?;
128     }
129
130     Ok(local_user_)
131   }
132   fn update(
133     conn: &mut PgConnection,
134     local_user_id: LocalUserId,
135     form: &Self::UpdateForm,
136   ) -> Result<Self, Error> {
137     diesel::update(local_user.find(local_user_id))
138       .set(form)
139       .get_result::<Self>(conn)
140   }
141 }