4 schema::{user_, user_::dsl::*},
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(Insertable, AsChangeset, Clone)]
107 #[table_name = "user_"]
108 pub struct UserForm {
110 pub preferred_username: Option<Option<String>>,
111 pub password_encrypted: String,
113 pub banned: Option<bool>,
114 pub email: Option<Option<String>>,
115 pub avatar: Option<Option<String>>,
116 pub published: Option<chrono::NaiveDateTime>,
117 pub updated: Option<chrono::NaiveDateTime>,
120 pub default_sort_type: i16,
121 pub default_listing_type: i16,
123 pub show_avatars: bool,
124 pub send_notifications_to_email: bool,
125 pub matrix_user_id: Option<Option<String>>,
126 pub actor_id: Option<String>,
127 pub bio: Option<Option<String>>,
129 pub private_key: Option<String>,
130 pub public_key: Option<String>,
131 pub last_refreshed_at: Option<chrono::NaiveDateTime>,
132 pub banner: Option<Option<String>>,
135 impl Crud<UserForm> for User_ {
136 fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
138 .filter(deleted.eq(false))
142 fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
143 diesel::delete(user_.find(user_id)).execute(conn)
145 fn create(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
146 insert_into(user_).values(form).get_result::<Self>(conn)
148 fn update(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result<Self, Error> {
149 diesel::update(user_.find(user_id))
151 .get_result::<Self>(conn)
155 impl ApubObject<UserForm> for User_ {
156 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
157 use crate::schema::user_::dsl::*;
159 .filter(deleted.eq(false))
160 .filter(actor_id.eq(object_id))
164 fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result<User_, Error> {
167 .on_conflict(actor_id)
170 .get_result::<Self>(conn)
175 pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
176 let mut edited_user = form.clone();
178 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
179 edited_user.password_encrypted = password_hash;
181 Self::create(&conn, &edited_user)
184 // TODO do more individual updates like these
185 pub fn update_password(
189 ) -> Result<Self, Error> {
190 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
192 diesel::update(user_.find(user_id))
194 password_encrypted.eq(password_hash),
195 updated.eq(naive_now()),
197 .get_result::<Self>(conn)
200 pub fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
202 .filter(local.eq(true))
203 .filter(deleted.eq(false))
204 .filter(name.eq(from_user_name))
208 pub fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<Self, Error> {
209 diesel::update(user_.find(user_id))
210 .set(admin.eq(added))
211 .get_result::<Self>(conn)
214 pub fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<Self, Error> {
215 diesel::update(user_.find(user_id))
217 .get_result::<Self>(conn)
220 pub fn find_by_email_or_username(
222 username_or_email: &str,
223 ) -> Result<Self, Error> {
224 if is_email_regex(username_or_email) {
225 Self::find_by_email(conn, username_or_email)
227 Self::find_by_username(conn, username_or_email)
231 pub fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
233 .filter(deleted.eq(false))
234 .filter(local.eq(true))
235 .filter(name.ilike(username))
236 .first::<User_>(conn)
239 pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
241 .filter(deleted.eq(false))
242 .filter(local.eq(true))
243 .filter(email.eq(from_email))
244 .first::<User_>(conn)
247 pub fn get_profile_url(&self, hostname: &str) -> String {
250 Settings::get().get_protocol_string(),
256 pub fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
257 diesel::update(user_.find(user_id))
258 .set((last_refreshed_at.eq(naive_now()),))
259 .get_result::<Self>(conn)
262 pub fn delete_account(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
263 diesel::update(user_.find(user_id))
265 preferred_username.eq::<Option<String>>(None),
266 email.eq::<Option<String>>(None),
267 matrix_user_id.eq::<Option<String>>(None),
268 bio.eq::<Option<String>>(None),
270 updated.eq(naive_now()),
272 .get_result::<Self>(conn)
278 use crate::{source::user::*, tests::establish_unpooled_connection, ListingType, SortType};
282 let conn = establish_unpooled_connection();
284 let new_user = UserForm {
285 name: "thommy".into(),
286 preferred_username: None,
287 password_encrypted: "nope".into(),
289 matrix_user_id: None,
297 theme: "browser".into(),
298 default_sort_type: SortType::Hot as i16,
299 default_listing_type: ListingType::Subscribed as i16,
300 lang: "browser".into(),
302 send_notifications_to_email: false,
308 last_refreshed_at: None,
311 let inserted_user = User_::create(&conn, &new_user).unwrap();
313 let expected_user = User_ {
314 id: inserted_user.id,
315 name: "thommy".into(),
316 preferred_username: None,
317 password_encrypted: "nope".into(),
319 matrix_user_id: None,
324 published: inserted_user.published,
327 theme: "browser".into(),
328 default_sort_type: SortType::Hot as i16,
329 default_listing_type: ListingType::Subscribed as i16,
330 lang: "browser".into(),
332 send_notifications_to_email: false,
333 actor_id: inserted_user.actor_id.to_owned(),
338 last_refreshed_at: inserted_user.published,
342 let read_user = User_::read(&conn, inserted_user.id).unwrap();
343 let updated_user = User_::update(&conn, inserted_user.id, &new_user).unwrap();
344 let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
346 assert_eq!(expected_user, read_user);
347 assert_eq!(expected_user, inserted_user);
348 assert_eq!(expected_user, updated_user);
349 assert_eq!(1, num_deleted);