]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/local_user.rs
Moving matrix_user_id to person table. #1438
[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   PersonId,
10 };
11
12 mod safe_settings_type {
13   use crate::ToSafeSettings;
14   use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
15
16   type Columns = (
17     id,
18     person_id,
19     email,
20     admin,
21     show_nsfw,
22     theme,
23     default_sort_type,
24     default_listing_type,
25     lang,
26     show_avatars,
27     send_notifications_to_email,
28     validator_time,
29   );
30
31   impl ToSafeSettings for LocalUser {
32     type SafeSettingsColumns = Columns;
33
34     /// Includes everything but the hashed password
35     fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
36       (
37         id,
38         person_id,
39         email,
40         admin,
41         show_nsfw,
42         theme,
43         default_sort_type,
44         default_listing_type,
45         lang,
46         show_avatars,
47         send_notifications_to_email,
48         validator_time,
49       )
50     }
51   }
52 }
53
54 pub trait LocalUser_ {
55   fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
56   fn update_password(
57     conn: &PgConnection,
58     local_user_id: LocalUserId,
59     new_password: &str,
60   ) -> Result<LocalUser, Error>;
61   fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<LocalUser, Error>;
62 }
63
64 impl LocalUser_ for LocalUser {
65   fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
66     let mut edited_user = form.clone();
67     let password_hash =
68       hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
69     edited_user.password_encrypted = password_hash;
70
71     Self::create(&conn, &edited_user)
72   }
73
74   fn update_password(
75     conn: &PgConnection,
76     local_user_id: LocalUserId,
77     new_password: &str,
78   ) -> Result<Self, Error> {
79     let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
80
81     diesel::update(local_user.find(local_user_id))
82       .set((
83         password_encrypted.eq(password_hash),
84         validator_time.eq(naive_now()),
85       ))
86       .get_result::<Self>(conn)
87   }
88
89   fn add_admin(conn: &PgConnection, for_person_id: PersonId, added: bool) -> Result<Self, Error> {
90     diesel::update(local_user.filter(person_id.eq(for_person_id)))
91       .set(admin.eq(added))
92       .get_result::<Self>(conn)
93   }
94 }
95
96 impl Crud<LocalUserForm, LocalUserId> for LocalUser {
97   fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
98     local_user.find(local_user_id).first::<Self>(conn)
99   }
100   fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
101     diesel::delete(local_user.find(local_user_id)).execute(conn)
102   }
103   fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
104     insert_into(local_user)
105       .values(form)
106       .get_result::<Self>(conn)
107   }
108   fn update(
109     conn: &PgConnection,
110     local_user_id: LocalUserId,
111     form: &LocalUserForm,
112   ) -> Result<Self, Error> {
113     diesel::update(local_user.find(local_user_id))
114       .set(form)
115       .get_result::<Self>(conn)
116   }
117 }