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,
38 pub banner: Option<String>,
41 #[derive(Insertable, AsChangeset, Clone, Debug)]
42 #[table_name = "user_"]
45 pub preferred_username: Option<String>,
46 pub password_encrypted: String,
49 pub email: Option<String>,
50 pub avatar: Option<Option<String>>,
51 pub updated: Option<chrono::NaiveDateTime>,
54 pub default_sort_type: i16,
55 pub default_listing_type: i16,
57 pub show_avatars: bool,
58 pub send_notifications_to_email: bool,
59 pub matrix_user_id: Option<String>,
61 pub bio: Option<String>,
63 pub private_key: Option<String>,
64 pub public_key: Option<String>,
65 pub last_refreshed_at: Option<chrono::NaiveDateTime>,
66 pub banner: Option<Option<String>>,
69 impl Crud<UserForm> for User_ {
70 fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
71 user_.find(user_id).first::<Self>(conn)
73 fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
74 diesel::delete(user_.find(user_id)).execute(conn)
76 fn create(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
77 insert_into(user_).values(form).get_result::<Self>(conn)
79 fn update(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result<Self, Error> {
80 diesel::update(user_.find(user_id))
82 .get_result::<Self>(conn)
87 pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
88 let mut edited_user = form.clone();
90 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
91 edited_user.password_encrypted = password_hash;
93 Self::create(&conn, &edited_user)
96 // TODO do more individual updates like these
97 pub fn update_password(
101 ) -> Result<Self, Error> {
102 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
104 diesel::update(user_.find(user_id))
106 password_encrypted.eq(password_hash),
107 updated.eq(naive_now()),
109 .get_result::<Self>(conn)
112 pub fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
113 user_.filter(name.eq(from_user_name)).first::<Self>(conn)
116 pub fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<Self, Error> {
117 diesel::update(user_.find(user_id))
118 .set(admin.eq(added))
119 .get_result::<Self>(conn)
122 pub fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<Self, Error> {
123 diesel::update(user_.find(user_id))
125 .get_result::<Self>(conn)
128 pub fn read_from_actor_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
129 use crate::schema::user_::dsl::*;
130 user_.filter(actor_id.eq(object_id)).first::<Self>(conn)
133 pub fn find_by_email_or_username(
135 username_or_email: &str,
136 ) -> Result<Self, Error> {
137 if is_email_regex(username_or_email) {
138 Self::find_by_email(conn, username_or_email)
140 Self::find_by_username(conn, username_or_email)
144 pub fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
145 user_.filter(name.eq(username)).first::<User_>(conn)
148 pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
149 user_.filter(email.eq(from_email)).first::<User_>(conn)
152 pub fn get_profile_url(&self, hostname: &str) -> String {
153 format!("https://{}/u/{}", hostname, self.name)
159 use crate::{tests::establish_unpooled_connection, user::*, ListingType, SortType};
163 let conn = establish_unpooled_connection();
165 let new_user = UserForm {
166 name: "thommy".into(),
167 preferred_username: None,
168 password_encrypted: "nope".into(),
170 matrix_user_id: None,
177 theme: "darkly".into(),
178 default_sort_type: SortType::Hot as i16,
179 default_listing_type: ListingType::Subscribed as i16,
180 lang: "browser".into(),
182 send_notifications_to_email: false,
183 actor_id: "changeme_9826382637".into(),
188 last_refreshed_at: None,
191 let inserted_user = User_::create(&conn, &new_user).unwrap();
193 let expected_user = User_ {
194 id: inserted_user.id,
195 name: "thommy".into(),
196 preferred_username: None,
197 password_encrypted: "nope".into(),
199 matrix_user_id: None,
204 published: inserted_user.published,
207 theme: "darkly".into(),
208 default_sort_type: SortType::Hot as i16,
209 default_listing_type: ListingType::Subscribed as i16,
210 lang: "browser".into(),
212 send_notifications_to_email: false,
213 actor_id: inserted_user.actor_id.to_owned(),
218 last_refreshed_at: inserted_user.published,
221 let read_user = User_::read(&conn, inserted_user.id).unwrap();
222 let updated_user = User_::update(&conn, inserted_user.id, &new_user).unwrap();
223 let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
225 assert_eq!(expected_user, read_user);
226 assert_eq!(expected_user, inserted_user);
227 assert_eq!(expected_user, updated_user);
228 assert_eq!(1, num_deleted);