4 schema::{user_, user_::dsl::*, user_alias_1},
8 use bcrypt::{hash, DEFAULT_COST};
9 use diesel::{dsl::*, result::Error, *};
10 use lemmy_utils::settings::Settings;
13 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
14 #[table_name = "user_"]
18 pub preferred_username: Option<String>,
19 pub password_encrypted: String,
20 pub email: Option<String>,
21 pub avatar: Option<String>,
24 pub published: chrono::NaiveDateTime,
25 pub updated: Option<chrono::NaiveDateTime>,
28 pub default_sort_type: i16,
29 pub default_listing_type: i16,
31 pub show_avatars: bool,
32 pub send_notifications_to_email: bool,
33 pub matrix_user_id: Option<String>,
35 pub bio: Option<String>,
37 pub private_key: Option<String>,
38 pub public_key: Option<String>,
39 pub last_refreshed_at: chrono::NaiveDateTime,
40 pub banner: Option<String>,
44 /// A safe representation of user, without the sensitive info
45 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
46 #[table_name = "user_"]
50 pub preferred_username: Option<String>,
51 pub avatar: Option<String>,
54 pub published: chrono::NaiveDateTime,
55 pub updated: Option<chrono::NaiveDateTime>,
56 pub matrix_user_id: Option<String>,
58 pub bio: Option<String>,
60 pub banner: Option<String>,
65 use crate::{schema::user_::columns::*, source::user::User_, ToSafe};
83 impl ToSafe for User_ {
84 type SafeColumns = Columns;
85 fn safe_columns_tuple() -> Self::SafeColumns {
106 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
107 #[table_name = "user_alias_1"]
108 pub struct UserAlias1 {
111 pub preferred_username: Option<String>,
112 pub password_encrypted: String,
113 pub email: Option<String>,
114 pub avatar: Option<String>,
117 pub published: chrono::NaiveDateTime,
118 pub updated: Option<chrono::NaiveDateTime>,
121 pub default_sort_type: i16,
122 pub default_listing_type: i16,
124 pub show_avatars: bool,
125 pub send_notifications_to_email: bool,
126 pub matrix_user_id: Option<String>,
127 pub actor_id: String,
128 pub bio: Option<String>,
130 pub private_key: Option<String>,
131 pub public_key: Option<String>,
132 pub last_refreshed_at: chrono::NaiveDateTime,
133 pub banner: Option<String>,
137 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
138 #[table_name = "user_alias_1"]
139 pub struct UserSafeAlias1 {
142 pub preferred_username: Option<String>,
143 pub avatar: Option<String>,
146 pub published: chrono::NaiveDateTime,
147 pub updated: Option<chrono::NaiveDateTime>,
148 pub matrix_user_id: Option<String>,
149 pub actor_id: String,
150 pub bio: Option<String>,
152 pub banner: Option<String>,
156 mod safe_type_alias {
157 use crate::{schema::user_alias_1::columns::*, source::user::UserAlias1, ToSafe};
175 impl ToSafe for UserAlias1 {
176 type SafeColumns = Columns;
177 fn safe_columns_tuple() -> Self::SafeColumns {
198 #[derive(Insertable, AsChangeset, Clone)]
199 #[table_name = "user_"]
200 pub struct UserForm {
202 pub preferred_username: Option<Option<String>>,
203 pub password_encrypted: String,
205 pub banned: Option<bool>,
206 pub email: Option<Option<String>>,
207 pub avatar: Option<Option<String>>,
208 pub published: Option<chrono::NaiveDateTime>,
209 pub updated: Option<chrono::NaiveDateTime>,
212 pub default_sort_type: i16,
213 pub default_listing_type: i16,
215 pub show_avatars: bool,
216 pub send_notifications_to_email: bool,
217 pub matrix_user_id: Option<Option<String>>,
218 pub actor_id: Option<String>,
219 pub bio: Option<Option<String>>,
221 pub private_key: Option<String>,
222 pub public_key: Option<String>,
223 pub last_refreshed_at: Option<chrono::NaiveDateTime>,
224 pub banner: Option<Option<String>>,
227 impl Crud<UserForm> for User_ {
228 fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
230 .filter(deleted.eq(false))
234 fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
235 diesel::delete(user_.find(user_id)).execute(conn)
237 fn create(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
238 insert_into(user_).values(form).get_result::<Self>(conn)
240 fn update(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result<Self, Error> {
241 diesel::update(user_.find(user_id))
243 .get_result::<Self>(conn)
247 impl ApubObject<UserForm> for User_ {
248 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
249 use crate::schema::user_::dsl::*;
251 .filter(deleted.eq(false))
252 .filter(actor_id.eq(object_id))
256 fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result<User_, Error> {
259 .on_conflict(actor_id)
262 .get_result::<Self>(conn)
267 pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
268 let mut edited_user = form.clone();
270 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
271 edited_user.password_encrypted = password_hash;
273 Self::create(&conn, &edited_user)
276 // TODO do more individual updates like these
277 pub fn update_password(
281 ) -> Result<Self, Error> {
282 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
284 diesel::update(user_.find(user_id))
286 password_encrypted.eq(password_hash),
287 updated.eq(naive_now()),
289 .get_result::<Self>(conn)
292 pub fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
294 .filter(local.eq(true))
295 .filter(deleted.eq(false))
296 .filter(name.eq(from_user_name))
300 pub fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<Self, Error> {
301 diesel::update(user_.find(user_id))
302 .set(admin.eq(added))
303 .get_result::<Self>(conn)
306 pub fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<Self, Error> {
307 diesel::update(user_.find(user_id))
309 .get_result::<Self>(conn)
312 pub fn find_by_email_or_username(
314 username_or_email: &str,
315 ) -> Result<Self, Error> {
316 if is_email_regex(username_or_email) {
317 Self::find_by_email(conn, username_or_email)
319 Self::find_by_username(conn, username_or_email)
323 pub fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
325 .filter(deleted.eq(false))
326 .filter(local.eq(true))
327 .filter(name.ilike(username))
328 .first::<User_>(conn)
331 pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
333 .filter(deleted.eq(false))
334 .filter(local.eq(true))
335 .filter(email.eq(from_email))
336 .first::<User_>(conn)
339 pub fn get_profile_url(&self, hostname: &str) -> String {
342 Settings::get().get_protocol_string(),
348 pub fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
349 diesel::update(user_.find(user_id))
350 .set((last_refreshed_at.eq(naive_now()),))
351 .get_result::<Self>(conn)
354 pub fn delete_account(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
355 diesel::update(user_.find(user_id))
357 preferred_username.eq::<Option<String>>(None),
358 email.eq::<Option<String>>(None),
359 matrix_user_id.eq::<Option<String>>(None),
360 bio.eq::<Option<String>>(None),
362 updated.eq(naive_now()),
364 .get_result::<Self>(conn)
370 use crate::{source::user::*, tests::establish_unpooled_connection, ListingType, SortType};
374 let conn = establish_unpooled_connection();
376 let new_user = UserForm {
377 name: "thommy".into(),
378 preferred_username: None,
379 password_encrypted: "nope".into(),
381 matrix_user_id: None,
389 theme: "browser".into(),
390 default_sort_type: SortType::Hot as i16,
391 default_listing_type: ListingType::Subscribed as i16,
392 lang: "browser".into(),
394 send_notifications_to_email: false,
400 last_refreshed_at: None,
403 let inserted_user = User_::create(&conn, &new_user).unwrap();
405 let expected_user = User_ {
406 id: inserted_user.id,
407 name: "thommy".into(),
408 preferred_username: None,
409 password_encrypted: "nope".into(),
411 matrix_user_id: None,
416 published: inserted_user.published,
419 theme: "browser".into(),
420 default_sort_type: SortType::Hot as i16,
421 default_listing_type: ListingType::Subscribed as i16,
422 lang: "browser".into(),
424 send_notifications_to_email: false,
425 actor_id: inserted_user.actor_id.to_owned(),
430 last_refreshed_at: inserted_user.published,
434 let read_user = User_::read(&conn, inserted_user.id).unwrap();
435 let updated_user = User_::update(&conn, inserted_user.id, &new_user).unwrap();
436 let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
438 assert_eq!(expected_user, read_user);
439 assert_eq!(expected_user, inserted_user);
440 assert_eq!(expected_user, updated_user);
441 assert_eq!(1, num_deleted);