]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/local_user.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_schema / src / impls / local_user.rs
1 use crate::{
2   newtypes::LocalUserId,
3   schema::local_user::dsl::*,
4   source::{
5     local_user::{LocalUser, LocalUserForm},
6     local_user_language::LocalUserLanguage,
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 register(conn: &mut PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
71     let mut edited_user = form.clone();
72     let password_hash = form
73       .password_encrypted
74       .as_ref()
75       .map(|p| hash(p, DEFAULT_COST).expect("Couldn't hash password"));
76     edited_user.password_encrypted = password_hash;
77
78     Self::create(conn, &edited_user)
79   }
80
81   pub fn update_password(
82     conn: &mut PgConnection,
83     local_user_id: LocalUserId,
84     new_password: &str,
85   ) -> Result<Self, Error> {
86     let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
87
88     diesel::update(local_user.find(local_user_id))
89       .set((
90         password_encrypted.eq(password_hash),
91         validator_time.eq(naive_now()),
92       ))
93       .get_result::<Self>(conn)
94   }
95
96   pub fn set_all_users_email_verified(conn: &mut PgConnection) -> Result<Vec<Self>, Error> {
97     diesel::update(local_user)
98       .set(email_verified.eq(true))
99       .get_results::<Self>(conn)
100   }
101
102   pub fn set_all_users_registration_applications_accepted(
103     conn: &mut PgConnection,
104   ) -> Result<Vec<Self>, Error> {
105     diesel::update(local_user)
106       .set(accepted_application.eq(true))
107       .get_results::<Self>(conn)
108   }
109 }
110
111 impl Crud for LocalUser {
112   type Form = LocalUserForm;
113   type IdType = LocalUserId;
114   fn read(conn: &mut PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
115     local_user.find(local_user_id).first::<Self>(conn)
116   }
117   fn delete(conn: &mut PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
118     diesel::delete(local_user.find(local_user_id)).execute(conn)
119   }
120   fn create(conn: &mut PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
121     let local_user_ = insert_into(local_user)
122       .values(form)
123       .get_result::<Self>(conn)?;
124     // initialize with all languages
125     LocalUserLanguage::update_user_languages(conn, None, local_user_.id)?;
126     Ok(local_user_)
127   }
128   fn update(
129     conn: &mut PgConnection,
130     local_user_id: LocalUserId,
131     form: &LocalUserForm,
132   ) -> Result<Self, Error> {
133     diesel::update(local_user.find(local_user_id))
134       .set(form)
135       .get_result::<Self>(conn)
136   }
137 }