4 schema::{user_, user_::dsl::*},
7 use bcrypt::{hash, DEFAULT_COST};
8 use diesel::{dsl::*, result::Error, *};
9 use serde::{Deserialize, Serialize};
11 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
12 #[table_name = "user_"]
16 pub preferred_username: Option<String>,
17 pub password_encrypted: String,
18 pub email: Option<String>,
19 pub avatar: Option<String>,
22 pub published: chrono::NaiveDateTime,
23 pub updated: Option<chrono::NaiveDateTime>,
26 pub default_sort_type: i16,
27 pub default_listing_type: i16,
29 pub show_avatars: bool,
30 pub send_notifications_to_email: bool,
31 pub matrix_user_id: Option<String>,
33 pub bio: Option<String>,
35 pub private_key: Option<String>,
36 pub public_key: Option<String>,
37 pub last_refreshed_at: chrono::NaiveDateTime,
40 #[derive(Insertable, AsChangeset, Clone, Debug)]
41 #[table_name = "user_"]
44 pub preferred_username: Option<String>,
45 pub password_encrypted: String,
48 pub email: Option<String>,
49 pub avatar: Option<String>,
50 pub updated: Option<chrono::NaiveDateTime>,
53 pub default_sort_type: i16,
54 pub default_listing_type: i16,
56 pub show_avatars: bool,
57 pub send_notifications_to_email: bool,
58 pub matrix_user_id: Option<String>,
60 pub bio: Option<String>,
62 pub private_key: Option<String>,
63 pub public_key: Option<String>,
64 pub last_refreshed_at: Option<chrono::NaiveDateTime>,
67 impl Crud<UserForm> for User_ {
68 fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
69 user_.find(user_id).first::<Self>(conn)
71 fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
72 diesel::delete(user_.find(user_id)).execute(conn)
74 fn create(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
75 insert_into(user_).values(form).get_result::<Self>(conn)
77 fn update(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result<Self, Error> {
78 diesel::update(user_.find(user_id))
80 .get_result::<Self>(conn)
85 pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
86 let mut edited_user = form.clone();
88 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
89 edited_user.password_encrypted = password_hash;
91 Self::create(&conn, &edited_user)
94 // TODO do more individual updates like these
95 pub fn update_password(
99 ) -> Result<Self, Error> {
100 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
102 diesel::update(user_.find(user_id))
104 password_encrypted.eq(password_hash),
105 updated.eq(naive_now()),
107 .get_result::<Self>(conn)
110 pub fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
111 user_.filter(name.eq(from_user_name)).first::<Self>(conn)
114 pub fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<Self, Error> {
115 diesel::update(user_.find(user_id))
116 .set(admin.eq(added))
117 .get_result::<Self>(conn)
120 pub fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<Self, Error> {
121 diesel::update(user_.find(user_id))
123 .get_result::<Self>(conn)
126 pub fn read_from_actor_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
127 use crate::schema::user_::dsl::*;
128 user_.filter(actor_id.eq(object_id)).first::<Self>(conn)
131 pub fn find_by_email_or_username(
133 username_or_email: &str,
134 ) -> Result<Self, Error> {
135 if is_email_regex(username_or_email) {
136 Self::find_by_email(conn, username_or_email)
138 Self::find_by_username(conn, username_or_email)
142 pub fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
143 user_.filter(name.eq(username)).first::<User_>(conn)
146 pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
147 user_.filter(email.eq(from_email)).first::<User_>(conn)
150 pub fn get_profile_url(&self, hostname: &str) -> String {
151 format!("https://{}/u/{}", hostname, self.name)
157 use crate::{tests::establish_unpooled_connection, user::*, ListingType, SortType};
161 let conn = establish_unpooled_connection();
163 let new_user = UserForm {
164 name: "thommy".into(),
165 preferred_username: None,
166 password_encrypted: "nope".into(),
168 matrix_user_id: None,
174 theme: "darkly".into(),
175 default_sort_type: SortType::Hot as i16,
176 default_listing_type: ListingType::Subscribed as i16,
177 lang: "browser".into(),
179 send_notifications_to_email: false,
180 actor_id: "changeme_9826382637".into(),
185 last_refreshed_at: None,
188 let inserted_user = User_::create(&conn, &new_user).unwrap();
190 let expected_user = User_ {
191 id: inserted_user.id,
192 name: "thommy".into(),
193 preferred_username: None,
194 password_encrypted: "nope".into(),
196 matrix_user_id: None,
200 published: inserted_user.published,
203 theme: "darkly".into(),
204 default_sort_type: SortType::Hot as i16,
205 default_listing_type: ListingType::Subscribed as i16,
206 lang: "browser".into(),
208 send_notifications_to_email: false,
209 actor_id: inserted_user.actor_id.to_owned(),
214 last_refreshed_at: inserted_user.published,
217 let read_user = User_::read(&conn, inserted_user.id).unwrap();
218 let updated_user = User_::update(&conn, inserted_user.id, &new_user).unwrap();
219 let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
221 assert_eq!(expected_user, read_user);
222 assert_eq!(expected_user, inserted_user);
223 assert_eq!(expected_user, updated_user);
224 assert_eq!(1, num_deleted);