2020-05-16 10:04:08 -04:00
|
|
|
use crate::{
|
2020-07-20 15:32:15 -04:00
|
|
|
is_email_regex,
|
2020-05-16 10:04:08 -04:00
|
|
|
naive_now,
|
|
|
|
schema::{user_, user_::dsl::*},
|
2020-07-10 14:15:41 -04:00
|
|
|
Crud,
|
2020-05-16 10:04:08 -04:00
|
|
|
};
|
2019-09-07 11:35:05 -04:00
|
|
|
use bcrypt::{hash, DEFAULT_COST};
|
2020-05-16 10:04:08 -04:00
|
|
|
use diesel::{dsl::*, result::Error, *};
|
2020-09-25 11:33:00 -04:00
|
|
|
use lemmy_utils::settings::Settings;
|
2020-09-14 11:29:50 -04:00
|
|
|
use serde::Serialize;
|
2019-02-28 01:02:55 -05:00
|
|
|
|
2020-09-03 15:45:12 -04:00
|
|
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
2019-09-07 11:35:05 -04:00
|
|
|
#[table_name = "user_"]
|
2019-02-28 01:02:55 -05:00
|
|
|
pub struct User_ {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
2019-03-04 11:39:07 -05:00
|
|
|
pub preferred_username: Option<String>,
|
2019-02-28 01:02:55 -05:00
|
|
|
pub password_encrypted: String,
|
|
|
|
pub email: Option<String>,
|
2019-12-29 15:39:48 -05:00
|
|
|
pub avatar: Option<String>,
|
2019-04-16 19:04:23 -04:00
|
|
|
pub admin: bool,
|
|
|
|
pub banned: bool,
|
2019-03-04 22:52:09 -05:00
|
|
|
pub published: chrono::NaiveDateTime,
|
2019-08-13 22:52:43 -04:00
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
|
|
|
pub show_nsfw: bool,
|
2019-10-15 15:21:27 -04:00
|
|
|
pub theme: String,
|
2019-10-21 00:21:54 -04:00
|
|
|
pub default_sort_type: i16,
|
|
|
|
pub default_listing_type: i16,
|
2019-12-09 03:24:53 -05:00
|
|
|
pub lang: String,
|
2020-01-02 16:55:54 -05:00
|
|
|
pub show_avatars: bool,
|
|
|
|
pub send_notifications_to_email: bool,
|
2020-01-22 16:35:29 -05:00
|
|
|
pub matrix_user_id: Option<String>,
|
2020-04-03 00:12:05 -04:00
|
|
|
pub actor_id: String,
|
|
|
|
pub bio: Option<String>,
|
|
|
|
pub local: bool,
|
|
|
|
pub private_key: Option<String>,
|
|
|
|
pub public_key: Option<String>,
|
|
|
|
pub last_refreshed_at: chrono::NaiveDateTime,
|
2020-08-05 12:03:46 -04:00
|
|
|
pub banner: Option<String>,
|
2020-11-27 16:00:18 -05:00
|
|
|
pub deleted: bool,
|
2019-02-28 01:02:55 -05:00
|
|
|
}
|
|
|
|
|
2020-12-04 23:18:30 -05:00
|
|
|
/// A safe representation of user, without the sensitive info
|
|
|
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
|
|
|
#[table_name = "user_"]
|
|
|
|
pub struct UserSafe {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
|
|
|
pub preferred_username: Option<String>,
|
|
|
|
pub avatar: Option<String>,
|
|
|
|
pub admin: bool,
|
|
|
|
pub banned: bool,
|
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
|
|
|
pub matrix_user_id: Option<String>,
|
|
|
|
pub actor_id: String,
|
|
|
|
pub bio: Option<String>,
|
|
|
|
pub local: bool,
|
|
|
|
pub banner: Option<String>,
|
|
|
|
pub deleted: bool,
|
|
|
|
}
|
|
|
|
|
2020-09-03 15:45:12 -04:00
|
|
|
#[derive(Insertable, AsChangeset, Clone)]
|
2019-09-07 11:35:05 -04:00
|
|
|
#[table_name = "user_"]
|
2019-03-22 21:42:57 -04:00
|
|
|
pub struct UserForm {
|
2019-09-07 11:35:05 -04:00
|
|
|
pub name: String,
|
2020-09-25 11:16:49 -04:00
|
|
|
pub preferred_username: Option<Option<String>>,
|
2019-09-07 11:35:05 -04:00
|
|
|
pub password_encrypted: String,
|
|
|
|
pub admin: bool,
|
2020-11-09 09:29:36 -05:00
|
|
|
pub banned: Option<bool>,
|
2020-08-08 22:36:29 -04:00
|
|
|
pub email: Option<Option<String>>,
|
2020-08-05 12:03:46 -04:00
|
|
|
pub avatar: Option<Option<String>>,
|
2020-09-18 07:04:12 -04:00
|
|
|
pub published: Option<chrono::NaiveDateTime>,
|
2019-09-07 11:35:05 -04:00
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
|
|
|
pub show_nsfw: bool,
|
2019-10-15 15:21:27 -04:00
|
|
|
pub theme: String,
|
2019-10-21 00:21:54 -04:00
|
|
|
pub default_sort_type: i16,
|
|
|
|
pub default_listing_type: i16,
|
2019-12-09 03:24:53 -05:00
|
|
|
pub lang: String,
|
2020-01-02 16:55:54 -05:00
|
|
|
pub show_avatars: bool,
|
|
|
|
pub send_notifications_to_email: bool,
|
2020-09-25 11:16:49 -04:00
|
|
|
pub matrix_user_id: Option<Option<String>>,
|
2020-08-31 09:48:02 -04:00
|
|
|
pub actor_id: Option<String>,
|
2020-09-25 11:16:49 -04:00
|
|
|
pub bio: Option<Option<String>>,
|
2020-04-03 00:12:05 -04:00
|
|
|
pub local: bool,
|
|
|
|
pub private_key: Option<String>,
|
|
|
|
pub public_key: Option<String>,
|
|
|
|
pub last_refreshed_at: Option<chrono::NaiveDateTime>,
|
2020-08-05 12:03:46 -04:00
|
|
|
pub banner: Option<Option<String>>,
|
2019-02-28 01:02:55 -05:00
|
|
|
}
|
|
|
|
|
2019-03-22 21:42:57 -04:00
|
|
|
impl Crud<UserForm> for User_ {
|
|
|
|
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
|
2020-11-27 16:00:18 -05:00
|
|
|
user_
|
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.find(user_id)
|
|
|
|
.first::<Self>(conn)
|
2019-02-28 01:02:55 -05:00
|
|
|
}
|
2019-03-22 21:42:57 -04:00
|
|
|
fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
|
2019-09-07 11:35:05 -04:00
|
|
|
diesel::delete(user_.find(user_id)).execute(conn)
|
2019-02-28 01:02:55 -05:00
|
|
|
}
|
2019-03-22 21:42:57 -04:00
|
|
|
fn create(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
|
2019-09-07 11:35:05 -04:00
|
|
|
insert_into(user_).values(form).get_result::<Self>(conn)
|
2019-03-04 11:39:07 -05:00
|
|
|
}
|
2019-03-22 21:42:57 -04:00
|
|
|
fn update(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result<Self, Error> {
|
2019-04-16 19:04:23 -04:00
|
|
|
diesel::update(user_.find(user_id))
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl User_ {
|
|
|
|
pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
|
2019-03-04 11:39:07 -05:00
|
|
|
let mut edited_user = form.clone();
|
2019-09-07 11:35:05 -04:00
|
|
|
let password_hash =
|
|
|
|
hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
|
2019-03-22 21:42:57 -04:00
|
|
|
edited_user.password_encrypted = password_hash;
|
2019-04-16 19:04:23 -04:00
|
|
|
|
|
|
|
Self::create(&conn, &edited_user)
|
2019-03-22 21:42:57 -04:00
|
|
|
}
|
2019-10-29 23:35:39 -04:00
|
|
|
|
2020-04-03 00:12:05 -04:00
|
|
|
// TODO do more individual updates like these
|
2019-11-02 02:43:21 -04:00
|
|
|
pub fn update_password(
|
|
|
|
conn: &PgConnection,
|
|
|
|
user_id: i32,
|
2020-01-01 15:46:14 -05:00
|
|
|
new_password: &str,
|
2019-11-02 02:43:21 -04:00
|
|
|
) -> Result<Self, Error> {
|
2020-01-01 15:46:14 -05:00
|
|
|
let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
|
2019-10-29 23:35:39 -04:00
|
|
|
|
2020-01-01 15:46:14 -05:00
|
|
|
diesel::update(user_.find(user_id))
|
2020-04-03 20:04:57 -04:00
|
|
|
.set((
|
|
|
|
password_encrypted.eq(password_hash),
|
|
|
|
updated.eq(naive_now()),
|
|
|
|
))
|
2020-01-01 15:46:14 -05:00
|
|
|
.get_result::<Self>(conn)
|
2019-10-29 23:35:39 -04:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:04:36 -04:00
|
|
|
pub fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
|
2020-10-08 11:10:10 -04:00
|
|
|
user_
|
|
|
|
.filter(local.eq(true))
|
2020-11-27 16:00:18 -05:00
|
|
|
.filter(deleted.eq(false))
|
2020-10-08 11:10:10 -04:00
|
|
|
.filter(name.eq(from_user_name))
|
|
|
|
.first::<Self>(conn)
|
2019-04-25 17:52:18 -04:00
|
|
|
}
|
2020-04-03 20:04:57 -04:00
|
|
|
|
|
|
|
pub fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<Self, Error> {
|
|
|
|
diesel::update(user_.find(user_id))
|
|
|
|
.set(admin.eq(added))
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<Self, Error> {
|
|
|
|
diesel::update(user_.find(user_id))
|
|
|
|
.set(banned.eq(ban))
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
2020-04-07 17:02:32 -04:00
|
|
|
|
2020-04-24 10:04:36 -04:00
|
|
|
pub fn read_from_actor_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
|
2020-04-07 17:02:32 -04:00
|
|
|
use crate::schema::user_::dsl::*;
|
2020-11-27 16:00:18 -05:00
|
|
|
user_
|
|
|
|
.filter(deleted.eq(false))
|
|
|
|
.filter(actor_id.eq(object_id))
|
|
|
|
.first::<Self>(conn)
|
2020-04-07 17:02:32 -04:00
|
|
|
}
|
2019-03-22 21:42:57 -04:00
|
|
|
|
2020-07-20 15:32:15 -04:00
|
|
|
pub fn find_by_email_or_username(
|
|
|
|
conn: &PgConnection,
|
|
|
|
username_or_email: &str,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
if is_email_regex(username_or_email) {
|
|
|
|
Self::find_by_email(conn, username_or_email)
|
|
|
|
} else {
|
|
|
|
Self::find_by_username(conn, username_or_email)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 14:15:41 -04:00
|
|
|
pub fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
|
2020-10-02 08:16:40 -04:00
|
|
|
user_
|
2020-11-27 16:00:18 -05:00
|
|
|
.filter(deleted.eq(false))
|
2020-10-08 11:10:10 -04:00
|
|
|
.filter(local.eq(true))
|
2020-10-02 08:16:40 -04:00
|
|
|
.filter(name.ilike(username))
|
|
|
|
.first::<User_>(conn)
|
2019-12-17 19:59:47 -05:00
|
|
|
}
|
|
|
|
|
2020-07-10 14:15:41 -04:00
|
|
|
pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
|
2020-10-02 08:16:40 -04:00
|
|
|
user_
|
2020-11-27 16:00:18 -05:00
|
|
|
.filter(deleted.eq(false))
|
2020-10-08 11:10:10 -04:00
|
|
|
.filter(local.eq(true))
|
2020-10-02 08:16:40 -04:00
|
|
|
.filter(email.eq(from_email))
|
|
|
|
.first::<User_>(conn)
|
2019-12-17 19:59:47 -05:00
|
|
|
}
|
|
|
|
|
2020-07-10 14:15:41 -04:00
|
|
|
pub fn get_profile_url(&self, hostname: &str) -> String {
|
2020-09-25 11:33:00 -04:00
|
|
|
format!(
|
|
|
|
"{}://{}/u/{}",
|
|
|
|
Settings::get().get_protocol_string(),
|
|
|
|
hostname,
|
|
|
|
self.name
|
|
|
|
)
|
2019-03-04 11:39:07 -05:00
|
|
|
}
|
2020-08-31 09:48:02 -04:00
|
|
|
|
|
|
|
pub fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result<User_, Error> {
|
|
|
|
insert_into(user_)
|
|
|
|
.values(user_form)
|
|
|
|
.on_conflict(actor_id)
|
|
|
|
.do_update()
|
|
|
|
.set(user_form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
2020-11-27 16:00:18 -05:00
|
|
|
|
|
|
|
pub fn delete_account(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
|
|
|
|
diesel::update(user_.find(user_id))
|
|
|
|
.set((
|
|
|
|
preferred_username.eq::<Option<String>>(None),
|
|
|
|
email.eq::<Option<String>>(None),
|
|
|
|
matrix_user_id.eq::<Option<String>>(None),
|
|
|
|
bio.eq::<Option<String>>(None),
|
|
|
|
deleted.eq(true),
|
|
|
|
updated.eq(naive_now()),
|
|
|
|
))
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
2020-12-04 23:18:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mod safe_type {
|
|
|
|
use crate::{schema::user_::columns::*, user::User_, ToSafe};
|
|
|
|
type Columns = (
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
preferred_username,
|
|
|
|
avatar,
|
|
|
|
admin,
|
|
|
|
banned,
|
|
|
|
published,
|
|
|
|
updated,
|
|
|
|
matrix_user_id,
|
|
|
|
actor_id,
|
|
|
|
bio,
|
|
|
|
local,
|
|
|
|
banner,
|
|
|
|
deleted,
|
|
|
|
);
|
2020-12-02 14:32:47 -05:00
|
|
|
|
2020-12-04 23:18:30 -05:00
|
|
|
impl ToSafe for User_ {
|
|
|
|
type SafeColumns = Columns;
|
|
|
|
fn safe_columns_tuple() -> Self::SafeColumns {
|
|
|
|
(
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
preferred_username,
|
|
|
|
avatar,
|
|
|
|
admin,
|
|
|
|
banned,
|
|
|
|
published,
|
|
|
|
updated,
|
|
|
|
matrix_user_id,
|
|
|
|
actor_id,
|
|
|
|
bio,
|
|
|
|
local,
|
|
|
|
banner,
|
|
|
|
deleted,
|
|
|
|
)
|
2020-12-02 14:32:47 -05:00
|
|
|
}
|
|
|
|
}
|
2019-02-28 01:02:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-07-10 14:15:41 -04:00
|
|
|
use crate::{tests::establish_unpooled_connection, user::*, ListingType, SortType};
|
2019-10-29 23:35:39 -04:00
|
|
|
|
2019-09-07 11:35:05 -04:00
|
|
|
#[test]
|
2019-02-28 01:02:55 -05:00
|
|
|
fn test_crud() {
|
2020-01-12 10:31:51 -05:00
|
|
|
let conn = establish_unpooled_connection();
|
2019-09-07 11:35:05 -04:00
|
|
|
|
2019-03-04 11:39:07 -05:00
|
|
|
let new_user = UserForm {
|
2019-04-03 19:01:20 -04:00
|
|
|
name: "thommy".into(),
|
2019-03-04 11:39:07 -05:00
|
|
|
preferred_username: None,
|
2019-02-28 01:02:55 -05:00
|
|
|
password_encrypted: "nope".into(),
|
2019-03-04 22:52:09 -05:00
|
|
|
email: None,
|
2020-01-22 16:35:29 -05:00
|
|
|
matrix_user_id: None,
|
2019-12-29 15:39:48 -05:00
|
|
|
avatar: None,
|
2020-08-05 12:03:46 -04:00
|
|
|
banner: None,
|
2019-04-16 19:04:23 -04:00
|
|
|
admin: false,
|
2020-11-10 11:11:08 -05:00
|
|
|
banned: Some(false),
|
2020-09-18 07:04:12 -04:00
|
|
|
published: None,
|
2019-08-13 22:52:43 -04:00
|
|
|
updated: None,
|
|
|
|
show_nsfw: false,
|
2020-10-03 12:44:25 -04:00
|
|
|
theme: "browser".into(),
|
2019-10-21 00:21:54 -04:00
|
|
|
default_sort_type: SortType::Hot as i16,
|
|
|
|
default_listing_type: ListingType::Subscribed as i16,
|
2019-12-09 03:24:53 -05:00
|
|
|
lang: "browser".into(),
|
2020-01-02 16:55:54 -05:00
|
|
|
show_avatars: true,
|
|
|
|
send_notifications_to_email: false,
|
2020-08-31 09:48:02 -04:00
|
|
|
actor_id: None,
|
2020-04-03 00:12:05 -04:00
|
|
|
bio: None,
|
|
|
|
local: true,
|
|
|
|
private_key: None,
|
|
|
|
public_key: None,
|
|
|
|
last_refreshed_at: None,
|
2019-02-28 01:02:55 -05:00
|
|
|
};
|
|
|
|
|
2019-03-22 21:42:57 -04:00
|
|
|
let inserted_user = User_::create(&conn, &new_user).unwrap();
|
2019-02-28 01:02:55 -05:00
|
|
|
|
|
|
|
let expected_user = User_ {
|
|
|
|
id: inserted_user.id,
|
2019-04-03 19:01:20 -04:00
|
|
|
name: "thommy".into(),
|
2019-03-04 11:39:07 -05:00
|
|
|
preferred_username: None,
|
2019-04-16 19:04:23 -04:00
|
|
|
password_encrypted: "nope".into(),
|
2019-02-28 01:02:55 -05:00
|
|
|
email: None,
|
2020-01-22 16:35:29 -05:00
|
|
|
matrix_user_id: None,
|
2019-12-29 15:39:48 -05:00
|
|
|
avatar: None,
|
2020-08-05 12:03:46 -04:00
|
|
|
banner: None,
|
2019-04-16 19:04:23 -04:00
|
|
|
admin: false,
|
|
|
|
banned: false,
|
2019-03-04 22:52:09 -05:00
|
|
|
published: inserted_user.published,
|
2019-08-13 22:52:43 -04:00
|
|
|
updated: None,
|
|
|
|
show_nsfw: false,
|
2020-10-03 12:44:25 -04:00
|
|
|
theme: "browser".into(),
|
2019-10-21 00:21:54 -04:00
|
|
|
default_sort_type: SortType::Hot as i16,
|
|
|
|
default_listing_type: ListingType::Subscribed as i16,
|
2019-12-09 03:24:53 -05:00
|
|
|
lang: "browser".into(),
|
2020-01-02 16:55:54 -05:00
|
|
|
show_avatars: true,
|
|
|
|
send_notifications_to_email: false,
|
2020-07-19 13:12:42 -04:00
|
|
|
actor_id: inserted_user.actor_id.to_owned(),
|
2020-04-03 00:12:05 -04:00
|
|
|
bio: None,
|
|
|
|
local: true,
|
|
|
|
private_key: None,
|
|
|
|
public_key: None,
|
|
|
|
last_refreshed_at: inserted_user.published,
|
2020-12-02 14:32:47 -05:00
|
|
|
deleted: false,
|
2019-02-28 01:02:55 -05:00
|
|
|
};
|
2019-09-07 11:35:05 -04:00
|
|
|
|
2019-03-22 21:42:57 -04:00
|
|
|
let read_user = User_::read(&conn, inserted_user.id).unwrap();
|
|
|
|
let updated_user = User_::update(&conn, inserted_user.id, &new_user).unwrap();
|
|
|
|
let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
|
2019-03-04 11:39:07 -05:00
|
|
|
|
2019-04-16 19:04:23 -04:00
|
|
|
assert_eq!(expected_user, read_user);
|
|
|
|
assert_eq!(expected_user, inserted_user);
|
|
|
|
assert_eq!(expected_user, updated_user);
|
2019-02-28 01:02:55 -05:00
|
|
|
assert_eq!(1, num_deleted);
|
|
|
|
}
|
|
|
|
}
|