mirror of https://github.com/LemmyNet/lemmy
Still continuing on....
parent
efc9047f87
commit
a869a2823b
@ -0,0 +1,76 @@
|
|||||||
|
use crate::{is_email_regex, ApubObject, Crud, ToSafeSettings};
|
||||||
|
mod safe_settings_type {
|
||||||
|
use crate::ToSafeSettings;
|
||||||
|
use lemmy_db_schema::{schema::user_::columns::*, source::user::User_};
|
||||||
|
|
||||||
|
type Columns = (
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
preferred_username,
|
||||||
|
email,
|
||||||
|
avatar,
|
||||||
|
admin,
|
||||||
|
banned,
|
||||||
|
published,
|
||||||
|
updated,
|
||||||
|
show_nsfw,
|
||||||
|
theme,
|
||||||
|
default_sort_type,
|
||||||
|
default_listing_type,
|
||||||
|
lang,
|
||||||
|
show_avatars,
|
||||||
|
send_notifications_to_email,
|
||||||
|
matrix_user_id,
|
||||||
|
actor_id,
|
||||||
|
bio,
|
||||||
|
local,
|
||||||
|
last_refreshed_at,
|
||||||
|
banner,
|
||||||
|
deleted,
|
||||||
|
);
|
||||||
|
|
||||||
|
impl ToSafeSettings for User_ {
|
||||||
|
type SafeSettingsColumns = Columns;
|
||||||
|
fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
|
||||||
|
(
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
preferred_username,
|
||||||
|
email,
|
||||||
|
avatar,
|
||||||
|
admin,
|
||||||
|
banned,
|
||||||
|
published,
|
||||||
|
updated,
|
||||||
|
show_nsfw,
|
||||||
|
theme,
|
||||||
|
default_sort_type,
|
||||||
|
default_listing_type,
|
||||||
|
lang,
|
||||||
|
show_avatars,
|
||||||
|
send_notifications_to_email,
|
||||||
|
matrix_user_id,
|
||||||
|
actor_id,
|
||||||
|
bio,
|
||||||
|
local,
|
||||||
|
last_refreshed_at,
|
||||||
|
banner,
|
||||||
|
deleted,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait UserSafeSettings_ {
|
||||||
|
fn read(conn: &PgConnection, user_id: i32) -> Result<UserSafeSettings, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UserSafeSettings_ for UserSafeSettings {
|
||||||
|
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
|
||||||
|
user_
|
||||||
|
.select(User_::safe_settings_columns_tuple())
|
||||||
|
.filter(deleted.eq(false))
|
||||||
|
.find(user_id)
|
||||||
|
.first::<Self>(conn)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,364 @@
|
|||||||
|
use crate::{is_email_regex, ApubObject, Crud, ToSafeSettings};
|
||||||
|
use bcrypt::{hash, DEFAULT_COST};
|
||||||
|
use diesel::{dsl::*, result::Error, *};
|
||||||
|
use lemmy_db_schema::{
|
||||||
|
naive_now,
|
||||||
|
schema::person::dsl::*,
|
||||||
|
source::person::{PersonForm, Person},
|
||||||
|
Url,
|
||||||
|
};
|
||||||
|
use lemmy_utils::settings::Settings;
|
||||||
|
|
||||||
|
mod safe_type {
|
||||||
|
use crate::ToSafe;
|
||||||
|
use lemmy_db_schema::{schema::person::columns::*, source::person::Person};
|
||||||
|
|
||||||
|
type Columns = (
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
preferred_username,
|
||||||
|
avatar,
|
||||||
|
banned,
|
||||||
|
published,
|
||||||
|
updated,
|
||||||
|
actor_id,
|
||||||
|
bio,
|
||||||
|
local,
|
||||||
|
last_refreshed_at,
|
||||||
|
banner,
|
||||||
|
deleted,
|
||||||
|
inbox_url,
|
||||||
|
shared_inbox_url,
|
||||||
|
);
|
||||||
|
|
||||||
|
impl ToSafe for Person {
|
||||||
|
type SafeColumns = Columns;
|
||||||
|
fn safe_columns_tuple() -> Self::SafeColumns {
|
||||||
|
(
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
preferred_username,
|
||||||
|
avatar,
|
||||||
|
banned,
|
||||||
|
published,
|
||||||
|
updated,
|
||||||
|
actor_id,
|
||||||
|
bio,
|
||||||
|
local,
|
||||||
|
last_refreshed_at,
|
||||||
|
banner,
|
||||||
|
deleted,
|
||||||
|
inbox_url,
|
||||||
|
shared_inbox_url,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod safe_type_alias_1 {
|
||||||
|
use crate::ToSafe;
|
||||||
|
use lemmy_db_schema::{schema::person_alias_1::columns::*, source::person::PersonAlias1};
|
||||||
|
|
||||||
|
type Columns = (
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
preferred_username,
|
||||||
|
avatar,
|
||||||
|
banned,
|
||||||
|
published,
|
||||||
|
updated,
|
||||||
|
actor_id,
|
||||||
|
bio,
|
||||||
|
local,
|
||||||
|
last_refreshed_at,
|
||||||
|
banner,
|
||||||
|
deleted,
|
||||||
|
inbox_url,
|
||||||
|
shared_inbox_url,
|
||||||
|
);
|
||||||
|
|
||||||
|
impl ToSafe for PersonAlias1 {
|
||||||
|
type SafeColumns = Columns;
|
||||||
|
fn safe_columns_tuple() -> Self::SafeColumns {
|
||||||
|
(
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
preferred_username,
|
||||||
|
avatar,
|
||||||
|
banned,
|
||||||
|
published,
|
||||||
|
updated,
|
||||||
|
actor_id,
|
||||||
|
bio,
|
||||||
|
local,
|
||||||
|
last_refreshed_at,
|
||||||
|
banner,
|
||||||
|
deleted,
|
||||||
|
inbox_url,
|
||||||
|
shared_inbox_url,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod safe_type_alias_2 {
|
||||||
|
use crate::ToSafe;
|
||||||
|
use lemmy_db_schema::{schema::person_alias_2::columns::*, source::person::PersonAlias2};
|
||||||
|
|
||||||
|
type Columns = (
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
preferred_username,
|
||||||
|
avatar,
|
||||||
|
banned,
|
||||||
|
published,
|
||||||
|
updated,
|
||||||
|
actor_id,
|
||||||
|
bio,
|
||||||
|
local,
|
||||||
|
last_refreshed_at,
|
||||||
|
banner,
|
||||||
|
deleted,
|
||||||
|
inbox_url,
|
||||||
|
shared_inbox_url,
|
||||||
|
);
|
||||||
|
|
||||||
|
impl ToSafe for PersonAlias2 {
|
||||||
|
type SafeColumns = Columns;
|
||||||
|
fn safe_columns_tuple() -> Self::SafeColumns {
|
||||||
|
(
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
preferred_username,
|
||||||
|
avatar,
|
||||||
|
banned,
|
||||||
|
published,
|
||||||
|
updated,
|
||||||
|
actor_id,
|
||||||
|
bio,
|
||||||
|
local,
|
||||||
|
last_refreshed_at,
|
||||||
|
banner,
|
||||||
|
deleted,
|
||||||
|
inbox_url,
|
||||||
|
shared_inbox_url,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Crud<PersonForm> for Person {
|
||||||
|
fn read(conn: &PgConnection, person_id: i32) -> Result<Self, Error> {
|
||||||
|
person
|
||||||
|
.filter(deleted.eq(false))
|
||||||
|
.find(person_id)
|
||||||
|
.first::<Self>(conn)
|
||||||
|
}
|
||||||
|
fn delete(conn: &PgConnection, person_id: i32) -> Result<usize, Error> {
|
||||||
|
diesel::delete(person.find(person_id)).execute(conn)
|
||||||
|
}
|
||||||
|
fn create(conn: &PgConnection, form: &PersonForm) -> Result<Self, Error> {
|
||||||
|
insert_into(person).values(form).get_result::<Self>(conn)
|
||||||
|
}
|
||||||
|
fn update(conn: &PgConnection, person_id: i32, form: &PersonForm) -> Result<Self, Error> {
|
||||||
|
diesel::update(person.find(person_id))
|
||||||
|
.set(form)
|
||||||
|
.get_result::<Self>(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ApubObject<PersonForm> for Person {
|
||||||
|
fn read_from_apub_id(conn: &PgConnection, object_id: &Url) -> Result<Self, Error> {
|
||||||
|
use lemmy_db_schema::schema::person::dsl::*;
|
||||||
|
person
|
||||||
|
.filter(deleted.eq(false))
|
||||||
|
.filter(actor_id.eq(object_id))
|
||||||
|
.first::<Self>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
|
||||||
|
insert_into(person)
|
||||||
|
.values(person_form)
|
||||||
|
.on_conflict(actor_id)
|
||||||
|
.do_update()
|
||||||
|
.set(person_form)
|
||||||
|
.get_result::<Self>(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Person_ {
|
||||||
|
fn register(conn: &PgConnection, form: &PersonForm) -> Result<Person, Error>;
|
||||||
|
fn update_password(conn: &PgConnection, person_id: i32, new_password: &str)
|
||||||
|
-> Result<Person, Error>;
|
||||||
|
fn read_from_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error>;
|
||||||
|
fn add_admin(conn: &PgConnection, person_id: i32, added: bool) -> Result<Person, Error>;
|
||||||
|
fn ban_person(conn: &PgConnection, person_id: i32, ban: bool) -> Result<Person, Error>;
|
||||||
|
fn find_by_email_or_name(
|
||||||
|
conn: &PgConnection,
|
||||||
|
name_or_email: &str,
|
||||||
|
) -> Result<Person, Error>;
|
||||||
|
fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>;
|
||||||
|
fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<Person, Error>;
|
||||||
|
fn get_profile_url(&self, hostname: &str) -> String;
|
||||||
|
fn mark_as_updated(conn: &PgConnection, person_id: i32) -> Result<Person, Error>;
|
||||||
|
fn delete_account(conn: &PgConnection, person_id: i32) -> Result<Person, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Person_ for Person {
|
||||||
|
fn register(conn: &PgConnection, form: &PersonForm) -> Result<Self, Error> {
|
||||||
|
let mut edited_person = form.clone();
|
||||||
|
let password_hash =
|
||||||
|
hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
|
||||||
|
edited_person.password_encrypted = password_hash;
|
||||||
|
|
||||||
|
Self::create(&conn, &edited_person)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO do more individual updates like these
|
||||||
|
fn update_password(conn: &PgConnection, person_id: i32, new_password: &str) -> Result<Self, Error> {
|
||||||
|
let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
|
||||||
|
|
||||||
|
diesel::update(person.find(person_id))
|
||||||
|
.set((
|
||||||
|
password_encrypted.eq(password_hash),
|
||||||
|
updated.eq(naive_now()),
|
||||||
|
))
|
||||||
|
.get_result::<Self>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_from_name(conn: &PgConnection, from_name: &str) -> Result<Self, Error> {
|
||||||
|
person
|
||||||
|
.filter(local.eq(true))
|
||||||
|
.filter(deleted.eq(false))
|
||||||
|
.filter(name.eq(from_name))
|
||||||
|
.first::<Self>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_admin(conn: &PgConnection, person_id: i32, added: bool) -> Result<Self, Error> {
|
||||||
|
diesel::update(person.find(person_id))
|
||||||
|
.set(admin.eq(added))
|
||||||
|
.get_result::<Self>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ban_person(conn: &PgConnection, person_id: i32, ban: bool) -> Result<Self, Error> {
|
||||||
|
diesel::update(person.find(person_id))
|
||||||
|
.set(banned.eq(ban))
|
||||||
|
.get_result::<Self>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_by_email_or_name(
|
||||||
|
conn: &PgConnection,
|
||||||
|
name_or_email: &str,
|
||||||
|
) -> Result<Self, Error> {
|
||||||
|
if is_email_regex(name_or_email) {
|
||||||
|
Self::find_by_email(conn, name_or_email)
|
||||||
|
} else {
|
||||||
|
Self::find_by_name(conn, name_or_email)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error> {
|
||||||
|
person
|
||||||
|
.filter(deleted.eq(false))
|
||||||
|
.filter(local.eq(true))
|
||||||
|
.filter(name.ilike(name))
|
||||||
|
.first::<Person>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<Person, Error> {
|
||||||
|
person
|
||||||
|
.filter(deleted.eq(false))
|
||||||
|
.filter(local.eq(true))
|
||||||
|
.filter(email.eq(from_email))
|
||||||
|
.first::<Person>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_profile_url(&self, hostname: &str) -> String {
|
||||||
|
format!(
|
||||||
|
"{}://{}/u/{}",
|
||||||
|
Settings::get().get_protocol_string(),
|
||||||
|
hostname,
|
||||||
|
self.name
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mark_as_updated(conn: &PgConnection, person_id: i32) -> Result<Person, Error> {
|
||||||
|
diesel::update(person.find(person_id))
|
||||||
|
.set((last_refreshed_at.eq(naive_now()),))
|
||||||
|
.get_result::<Self>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn delete_account(conn: &PgConnection, person_id: i32) -> Result<Person, Error> {
|
||||||
|
diesel::update(person.find(person_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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::{establish_unpooled_connection, source::person::*, ListingType, SortType};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_crud() {
|
||||||
|
let conn = establish_unpooled_connection();
|
||||||
|
|
||||||
|
let new_person = PersonForm {
|
||||||
|
name: "thommy".into(),
|
||||||
|
preferred_username: None,
|
||||||
|
avatar: None,
|
||||||
|
banner: None,
|
||||||
|
banned: Some(false),
|
||||||
|
deleted: false,
|
||||||
|
published: None,
|
||||||
|
updated: None,
|
||||||
|
actor_id: None,
|
||||||
|
bio: None,
|
||||||
|
local: true,
|
||||||
|
private_key: None,
|
||||||
|
public_key: None,
|
||||||
|
last_refreshed_at: None,
|
||||||
|
inbox_url: None,
|
||||||
|
shared_inbox_url: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let inserted_person = Person::create(&conn, &new_person).unwrap();
|
||||||
|
|
||||||
|
let expected_person = Person {
|
||||||
|
id: inserted_person.id,
|
||||||
|
name: "thommy".into(),
|
||||||
|
preferred_username: None,
|
||||||
|
avatar: None,
|
||||||
|
banner: None,
|
||||||
|
banned: false,
|
||||||
|
deleted: false,
|
||||||
|
published: inserted_person.published,
|
||||||
|
updated: None,
|
||||||
|
actor_id: inserted_person.actor_id.to_owned(),
|
||||||
|
bio: None,
|
||||||
|
local: true,
|
||||||
|
private_key: None,
|
||||||
|
public_key: None,
|
||||||
|
last_refreshed_at: inserted_person.published,
|
||||||
|
deleted: false,
|
||||||
|
inbox_url: inserted_person.inbox_url.to_owned(),
|
||||||
|
shared_inbox_url: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let read_person = Person::read(&conn, inserted_person.id).unwrap();
|
||||||
|
let updated_person = Person::update(&conn, inserted_person.id, &new_person).unwrap();
|
||||||
|
let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(expected_person, read_person);
|
||||||
|
assert_eq!(expected_person, inserted_person);
|
||||||
|
assert_eq!(expected_person, updated_person);
|
||||||
|
assert_eq!(1, num_deleted);
|
||||||
|
}
|
||||||
|
}
|
@ -1,458 +0,0 @@
|
|||||||
use crate::{is_email_regex, ApubObject, Crud, ToSafeSettings};
|
|
||||||
use bcrypt::{hash, DEFAULT_COST};
|
|
||||||
use diesel::{dsl::*, result::Error, *};
|
|
||||||
use lemmy_db_schema::{
|
|
||||||
naive_now,
|
|
||||||
schema::user_::dsl::*,
|
|
||||||
source::user::{UserForm, UserSafeSettings, User_},
|
|
||||||
Url,
|
|
||||||
};
|
|
||||||
use lemmy_utils::settings::Settings;
|
|
||||||
|
|
||||||
mod safe_type {
|
|
||||||
use crate::ToSafe;
|
|
||||||
use lemmy_db_schema::{schema::user_::columns::*, source::user::User_};
|
|
||||||
|
|
||||||
type Columns = (
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
preferred_username,
|
|
||||||
avatar,
|
|
||||||
admin,
|
|
||||||
banned,
|
|
||||||
published,
|
|
||||||
updated,
|
|
||||||
matrix_user_id,
|
|
||||||
actor_id,
|
|
||||||
bio,
|
|
||||||
local,
|
|
||||||
banner,
|
|
||||||
deleted,
|
|
||||||
inbox_url,
|
|
||||||
shared_inbox_url,
|
|
||||||
);
|
|
||||||
|
|
||||||
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,
|
|
||||||
inbox_url,
|
|
||||||
shared_inbox_url,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod safe_type_alias_1 {
|
|
||||||
use crate::ToSafe;
|
|
||||||
use lemmy_db_schema::{schema::user_alias_1::columns::*, source::user::UserAlias1};
|
|
||||||
|
|
||||||
type Columns = (
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
preferred_username,
|
|
||||||
avatar,
|
|
||||||
admin,
|
|
||||||
banned,
|
|
||||||
published,
|
|
||||||
updated,
|
|
||||||
matrix_user_id,
|
|
||||||
actor_id,
|
|
||||||
bio,
|
|
||||||
local,
|
|
||||||
banner,
|
|
||||||
deleted,
|
|
||||||
);
|
|
||||||
|
|
||||||
impl ToSafe for UserAlias1 {
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod safe_type_alias_2 {
|
|
||||||
use crate::ToSafe;
|
|
||||||
use lemmy_db_schema::{schema::user_alias_2::columns::*, source::user::UserAlias2};
|
|
||||||
|
|
||||||
type Columns = (
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
preferred_username,
|
|
||||||
avatar,
|
|
||||||
admin,
|
|
||||||
banned,
|
|
||||||
published,
|
|
||||||
updated,
|
|
||||||
matrix_user_id,
|
|
||||||
actor_id,
|
|
||||||
bio,
|
|
||||||
local,
|
|
||||||
banner,
|
|
||||||
deleted,
|
|
||||||
);
|
|
||||||
|
|
||||||
impl ToSafe for UserAlias2 {
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod safe_settings_type {
|
|
||||||
use crate::ToSafeSettings;
|
|
||||||
use lemmy_db_schema::{schema::user_::columns::*, source::user::User_};
|
|
||||||
|
|
||||||
type Columns = (
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
preferred_username,
|
|
||||||
email,
|
|
||||||
avatar,
|
|
||||||
admin,
|
|
||||||
banned,
|
|
||||||
published,
|
|
||||||
updated,
|
|
||||||
show_nsfw,
|
|
||||||
theme,
|
|
||||||
default_sort_type,
|
|
||||||
default_listing_type,
|
|
||||||
lang,
|
|
||||||
show_avatars,
|
|
||||||
send_notifications_to_email,
|
|
||||||
matrix_user_id,
|
|
||||||
actor_id,
|
|
||||||
bio,
|
|
||||||
local,
|
|
||||||
last_refreshed_at,
|
|
||||||
banner,
|
|
||||||
deleted,
|
|
||||||
);
|
|
||||||
|
|
||||||
impl ToSafeSettings for User_ {
|
|
||||||
type SafeSettingsColumns = Columns;
|
|
||||||
fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
|
|
||||||
(
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
preferred_username,
|
|
||||||
email,
|
|
||||||
avatar,
|
|
||||||
admin,
|
|
||||||
banned,
|
|
||||||
published,
|
|
||||||
updated,
|
|
||||||
show_nsfw,
|
|
||||||
theme,
|
|
||||||
default_sort_type,
|
|
||||||
default_listing_type,
|
|
||||||
lang,
|
|
||||||
show_avatars,
|
|
||||||
send_notifications_to_email,
|
|
||||||
matrix_user_id,
|
|
||||||
actor_id,
|
|
||||||
bio,
|
|
||||||
local,
|
|
||||||
last_refreshed_at,
|
|
||||||
banner,
|
|
||||||
deleted,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait UserSafeSettings_ {
|
|
||||||
fn read(conn: &PgConnection, user_id: i32) -> Result<UserSafeSettings, Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl UserSafeSettings_ for UserSafeSettings {
|
|
||||||
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
|
|
||||||
user_
|
|
||||||
.select(User_::safe_settings_columns_tuple())
|
|
||||||
.filter(deleted.eq(false))
|
|
||||||
.find(user_id)
|
|
||||||
.first::<Self>(conn)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Crud<UserForm> for User_ {
|
|
||||||
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
|
|
||||||
user_
|
|
||||||
.filter(deleted.eq(false))
|
|
||||||
.find(user_id)
|
|
||||||
.first::<Self>(conn)
|
|
||||||
}
|
|
||||||
fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
|
|
||||||
diesel::delete(user_.find(user_id)).execute(conn)
|
|
||||||
}
|
|
||||||
fn create(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
|
|
||||||
insert_into(user_).values(form).get_result::<Self>(conn)
|
|
||||||
}
|
|
||||||
fn update(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result<Self, Error> {
|
|
||||||
diesel::update(user_.find(user_id))
|
|
||||||
.set(form)
|
|
||||||
.get_result::<Self>(conn)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ApubObject<UserForm> for User_ {
|
|
||||||
fn read_from_apub_id(conn: &PgConnection, object_id: &Url) -> Result<Self, Error> {
|
|
||||||
use lemmy_db_schema::schema::user_::dsl::*;
|
|
||||||
user_
|
|
||||||
.filter(deleted.eq(false))
|
|
||||||
.filter(actor_id.eq(object_id))
|
|
||||||
.first::<Self>(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait User {
|
|
||||||
fn register(conn: &PgConnection, form: &UserForm) -> Result<User_, Error>;
|
|
||||||
fn update_password(conn: &PgConnection, user_id: i32, new_password: &str)
|
|
||||||
-> Result<User_, Error>;
|
|
||||||
fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<User_, Error>;
|
|
||||||
fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<User_, Error>;
|
|
||||||
fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<User_, Error>;
|
|
||||||
fn find_by_email_or_username(
|
|
||||||
conn: &PgConnection,
|
|
||||||
username_or_email: &str,
|
|
||||||
) -> Result<User_, Error>;
|
|
||||||
fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error>;
|
|
||||||
fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error>;
|
|
||||||
fn get_profile_url(&self, hostname: &str) -> String;
|
|
||||||
fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result<User_, Error>;
|
|
||||||
fn delete_account(conn: &PgConnection, user_id: i32) -> Result<User_, Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl User for User_ {
|
|
||||||
fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
|
|
||||||
let mut edited_user = form.clone();
|
|
||||||
let password_hash =
|
|
||||||
hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
|
|
||||||
edited_user.password_encrypted = password_hash;
|
|
||||||
|
|
||||||
Self::create(&conn, &edited_user)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO do more individual updates like these
|
|
||||||
fn update_password(conn: &PgConnection, user_id: i32, new_password: &str) -> Result<Self, Error> {
|
|
||||||
let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
|
|
||||||
|
|
||||||
diesel::update(user_.find(user_id))
|
|
||||||
.set((
|
|
||||||
password_encrypted.eq(password_hash),
|
|
||||||
updated.eq(naive_now()),
|
|
||||||
))
|
|
||||||
.get_result::<Self>(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
|
|
||||||
user_
|
|
||||||
.filter(local.eq(true))
|
|
||||||
.filter(deleted.eq(false))
|
|
||||||
.filter(name.eq(from_user_name))
|
|
||||||
.first::<Self>(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
|
|
||||||
user_
|
|
||||||
.filter(deleted.eq(false))
|
|
||||||
.filter(local.eq(true))
|
|
||||||
.filter(name.ilike(username))
|
|
||||||
.first::<User_>(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
|
|
||||||
user_
|
|
||||||
.filter(deleted.eq(false))
|
|
||||||
.filter(local.eq(true))
|
|
||||||
.filter(email.eq(from_email))
|
|
||||||
.first::<User_>(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_profile_url(&self, hostname: &str) -> String {
|
|
||||||
format!(
|
|
||||||
"{}://{}/u/{}",
|
|
||||||
Settings::get().get_protocol_string(),
|
|
||||||
hostname,
|
|
||||||
self.name
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
|
|
||||||
diesel::update(user_.find(user_id))
|
|
||||||
.set((last_refreshed_at.eq(naive_now()),))
|
|
||||||
.get_result::<Self>(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::{establish_unpooled_connection, source::user::*, ListingType, SortType};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_crud() {
|
|
||||||
let conn = establish_unpooled_connection();
|
|
||||||
|
|
||||||
let new_user = UserForm {
|
|
||||||
name: "thommy".into(),
|
|
||||||
preferred_username: None,
|
|
||||||
password_encrypted: "nope".into(),
|
|
||||||
email: None,
|
|
||||||
matrix_user_id: None,
|
|
||||||
avatar: None,
|
|
||||||
banner: None,
|
|
||||||
admin: false,
|
|
||||||
banned: Some(false),
|
|
||||||
published: None,
|
|
||||||
updated: None,
|
|
||||||
show_nsfw: false,
|
|
||||||
theme: "browser".into(),
|
|
||||||
default_sort_type: SortType::Hot as i16,
|
|
||||||
default_listing_type: ListingType::Subscribed as i16,
|
|
||||||
lang: "browser".into(),
|
|
||||||
show_avatars: true,
|
|
||||||
send_notifications_to_email: false,
|
|
||||||
actor_id: None,
|
|
||||||
bio: None,
|
|
||||||
local: true,
|
|
||||||
private_key: None,
|
|
||||||
public_key: None,
|
|
||||||
last_refreshed_at: None,
|
|
||||||
inbox_url: None,
|
|
||||||
shared_inbox_url: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let inserted_user = User_::create(&conn, &new_user).unwrap();
|
|
||||||
|
|
||||||
let expected_user = User_ {
|
|
||||||
id: inserted_user.id,
|
|
||||||
name: "thommy".into(),
|
|
||||||
preferred_username: None,
|
|
||||||
password_encrypted: "nope".into(),
|
|
||||||
email: None,
|
|
||||||
matrix_user_id: None,
|
|
||||||
avatar: None,
|
|
||||||
banner: None,
|
|
||||||
admin: false,
|
|
||||||
banned: false,
|
|
||||||
published: inserted_user.published,
|
|
||||||
updated: None,
|
|
||||||
show_nsfw: false,
|
|
||||||
theme: "browser".into(),
|
|
||||||
default_sort_type: SortType::Hot as i16,
|
|
||||||
default_listing_type: ListingType::Subscribed as i16,
|
|
||||||
lang: "browser".into(),
|
|
||||||
show_avatars: true,
|
|
||||||
send_notifications_to_email: false,
|
|
||||||
actor_id: inserted_user.actor_id.to_owned(),
|
|
||||||
bio: None,
|
|
||||||
local: true,
|
|
||||||
private_key: None,
|
|
||||||
public_key: None,
|
|
||||||
last_refreshed_at: inserted_user.published,
|
|
||||||
deleted: false,
|
|
||||||
inbox_url: inserted_user.inbox_url.to_owned(),
|
|
||||||
shared_inbox_url: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
assert_eq!(expected_user, read_user);
|
|
||||||
assert_eq!(expected_user, inserted_user);
|
|
||||||
assert_eq!(expected_user, updated_user);
|
|
||||||
assert_eq!(1, num_deleted);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,66 @@
|
|||||||
|
use crate::schema::local_user;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
||||||
|
#[table_name = "local_user"]
|
||||||
|
pub struct LocalUser {
|
||||||
|
pub id: i32,
|
||||||
|
pub person_id: i32,
|
||||||
|
pub password_encrypted: String,
|
||||||
|
pub email: Option<String>,
|
||||||
|
pub admin: bool,
|
||||||
|
pub show_nsfw: bool,
|
||||||
|
pub theme: String,
|
||||||
|
pub default_sort_type: i16,
|
||||||
|
pub default_listing_type: i16,
|
||||||
|
pub lang: String,
|
||||||
|
pub show_avatars: bool,
|
||||||
|
pub send_notifications_to_email: bool,
|
||||||
|
pub matrix_user_id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO redo these, check table defaults
|
||||||
|
#[derive(Insertable, AsChangeset, Clone)]
|
||||||
|
#[table_name = "local_user"]
|
||||||
|
pub struct LocalUserForm {
|
||||||
|
pub person_id: i32,
|
||||||
|
pub password_encrypted: String,
|
||||||
|
pub email: Option<Option<String>>,
|
||||||
|
pub admin: Option<bool>,
|
||||||
|
pub show_nsfw: Option<bool>,
|
||||||
|
pub theme: Option<String>,
|
||||||
|
pub default_sort_type: Option<i16>,
|
||||||
|
pub default_listing_type: Option<i16>,
|
||||||
|
pub lang: Option<String>,
|
||||||
|
pub show_avatars: Option<bool>,
|
||||||
|
pub send_notifications_to_email: Option<bool>,
|
||||||
|
pub matrix_user_id: Option<Option<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A safe local user view, without settings, password, or email
|
||||||
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
||||||
|
#[table_name = "local_user"]
|
||||||
|
pub struct LocalUserSafe {
|
||||||
|
pub id: i32,
|
||||||
|
pub person_id: i32,
|
||||||
|
pub admin: bool,
|
||||||
|
pub matrix_user_id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A local user view that removes password encrypted
|
||||||
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
||||||
|
#[table_name = "local_user"]
|
||||||
|
pub struct LocalUserSettings{
|
||||||
|
pub id: i32,
|
||||||
|
pub person_id: i32,
|
||||||
|
pub email: Option<String>,
|
||||||
|
pub admin: bool,
|
||||||
|
pub show_nsfw: bool,
|
||||||
|
pub theme: String,
|
||||||
|
pub default_sort_type: i16,
|
||||||
|
pub default_listing_type: i16,
|
||||||
|
pub lang: String,
|
||||||
|
pub show_avatars: bool,
|
||||||
|
pub send_notifications_to_email: bool,
|
||||||
|
pub matrix_user_id: Option<String>,
|
||||||
|
}
|
@ -0,0 +1,154 @@
|
|||||||
|
use crate::{
|
||||||
|
schema::{person, person_alias_1, person_alias_2},
|
||||||
|
Url,
|
||||||
|
};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
||||||
|
#[table_name = "person"]
|
||||||
|
pub struct Person {
|
||||||
|
pub id: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub preferred_username: Option<String>,
|
||||||
|
pub avatar: Option<String>,
|
||||||
|
pub banned: bool,
|
||||||
|
pub published: chrono::NaiveDateTime,
|
||||||
|
pub updated: Option<chrono::NaiveDateTime>,
|
||||||
|
pub actor_id: Url,
|
||||||
|
pub bio: Option<String>,
|
||||||
|
pub local: bool,
|
||||||
|
pub private_key: Option<String>,
|
||||||
|
pub public_key: Option<String>,
|
||||||
|
pub last_refreshed_at: chrono::NaiveDateTime,
|
||||||
|
pub banner: Option<String>,
|
||||||
|
pub deleted: bool,
|
||||||
|
pub inbox_url: Url,
|
||||||
|
pub shared_inbox_url: Option<Url>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A safe representation of user, without the sensitive info
|
||||||
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
||||||
|
#[table_name = "person"]
|
||||||
|
pub struct PersonSafe {
|
||||||
|
pub id: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub preferred_username: Option<String>,
|
||||||
|
pub avatar: Option<String>,
|
||||||
|
pub banned: bool,
|
||||||
|
pub published: chrono::NaiveDateTime,
|
||||||
|
pub updated: Option<chrono::NaiveDateTime>,
|
||||||
|
pub actor_id: Url,
|
||||||
|
pub bio: Option<String>,
|
||||||
|
pub local: bool,
|
||||||
|
pub last_refreshed_at: chrono::NaiveDateTime,
|
||||||
|
pub banner: Option<String>,
|
||||||
|
pub deleted: bool,
|
||||||
|
pub inbox_url: Url,
|
||||||
|
pub shared_inbox_url: Option<Url>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
||||||
|
#[table_name = "person_alias_1"]
|
||||||
|
pub struct PersonAlias1 {
|
||||||
|
pub id: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub preferred_username: Option<String>,
|
||||||
|
pub avatar: Option<String>,
|
||||||
|
pub banned: bool,
|
||||||
|
pub published: chrono::NaiveDateTime,
|
||||||
|
pub updated: Option<chrono::NaiveDateTime>,
|
||||||
|
pub actor_id: Url,
|
||||||
|
pub bio: Option<String>,
|
||||||
|
pub local: bool,
|
||||||
|
pub private_key: Option<String>,
|
||||||
|
pub public_key: Option<String>,
|
||||||
|
pub last_refreshed_at: chrono::NaiveDateTime,
|
||||||
|
pub banner: Option<String>,
|
||||||
|
pub deleted: bool,
|
||||||
|
pub inbox_url: Url,
|
||||||
|
pub shared_inbox_url: Option<Url>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
||||||
|
#[table_name = "person_alias_1"]
|
||||||
|
pub struct PersonSafeAlias1 {
|
||||||
|
pub id: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub preferred_username: Option<String>,
|
||||||
|
pub avatar: Option<String>,
|
||||||
|
pub banned: bool,
|
||||||
|
pub published: chrono::NaiveDateTime,
|
||||||
|
pub updated: Option<chrono::NaiveDateTime>,
|
||||||
|
pub actor_id: Url,
|
||||||
|
pub bio: Option<String>,
|
||||||
|
pub local: bool,
|
||||||
|
pub last_refreshed_at: chrono::NaiveDateTime,
|
||||||
|
pub banner: Option<String>,
|
||||||
|
pub deleted: bool,
|
||||||
|
pub inbox_url: Url,
|
||||||
|
pub shared_inbox_url: Option<Url>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
||||||
|
#[table_name = "person_alias_2"]
|
||||||
|
pub struct PersonAlias2 {
|
||||||
|
pub id: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub preferred_username: Option<String>,
|
||||||
|
pub avatar: Option<String>,
|
||||||
|
pub banned: bool,
|
||||||
|
pub published: chrono::NaiveDateTime,
|
||||||
|
pub updated: Option<chrono::NaiveDateTime>,
|
||||||
|
pub actor_id: Url,
|
||||||
|
pub bio: Option<String>,
|
||||||
|
pub local: bool,
|
||||||
|
pub private_key: Option<String>,
|
||||||
|
pub public_key: Option<String>,
|
||||||
|
pub last_refreshed_at: chrono::NaiveDateTime,
|
||||||
|
pub banner: Option<String>,
|
||||||
|
pub deleted: bool,
|
||||||
|
pub inbox_url: Url,
|
||||||
|
pub shared_inbox_url: Option<Url>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
||||||
|
#[table_name = "person_alias_1"]
|
||||||
|
pub struct PersonSafeAlias2 {
|
||||||
|
pub id: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub preferred_username: Option<String>,
|
||||||
|
pub avatar: Option<String>,
|
||||||
|
pub banned: bool,
|
||||||
|
pub published: chrono::NaiveDateTime,
|
||||||
|
pub updated: Option<chrono::NaiveDateTime>,
|
||||||
|
pub actor_id: Url,
|
||||||
|
pub bio: Option<String>,
|
||||||
|
pub local: bool,
|
||||||
|
pub last_refreshed_at: chrono::NaiveDateTime,
|
||||||
|
pub banner: Option<String>,
|
||||||
|
pub deleted: bool,
|
||||||
|
pub inbox_url: Url,
|
||||||
|
pub shared_inbox_url: Option<Url>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable, AsChangeset, Clone)]
|
||||||
|
#[table_name = "person"]
|
||||||
|
pub struct PersonForm {
|
||||||
|
pub name: String,
|
||||||
|
pub preferred_username: Option<Option<String>>,
|
||||||
|
pub avatar: Option<Option<String>>,
|
||||||
|
pub banned: Option<bool>,
|
||||||
|
pub published: Option<chrono::NaiveDateTime>,
|
||||||
|
pub updated: Option<chrono::NaiveDateTime>,
|
||||||
|
pub actor_id: Option<Url>,
|
||||||
|
pub bio: Option<Option<String>>,
|
||||||
|
pub local: Option<bool>,
|
||||||
|
pub private_key: Option<Option<String>>,
|
||||||
|
pub public_key: Option<Option<String>>,
|
||||||
|
pub last_refreshed_at: Option<chrono::NaiveDateTime>,
|
||||||
|
pub banner: Option<Option<String>>,
|
||||||
|
pub deleted: Option<bool>,
|
||||||
|
pub inbox_url: Option<Url>,
|
||||||
|
pub shared_inbox_url: Option<Option<Url>>,
|
||||||
|
}
|
@ -1,220 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
schema::{user_, user_alias_1, user_alias_2},
|
|
||||||
Url,
|
|
||||||
};
|
|
||||||
use serde::Serialize;
|
|
||||||
|
|
||||||
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
|
||||||
#[table_name = "user_"]
|
|
||||||
pub struct User_ {
|
|
||||||
pub id: i32, // person
|
|
||||||
pub name: String, // person
|
|
||||||
pub preferred_username: Option<String>, // person
|
|
||||||
pub password_encrypted: String, // local_user
|
|
||||||
pub email: Option<String>, // local_user
|
|
||||||
pub avatar: Option<String>, // person
|
|
||||||
pub admin: bool, // local_user
|
|
||||||
pub banned: bool, // person?
|
|
||||||
pub published: chrono::NaiveDateTime, // person
|
|
||||||
pub updated: Option<chrono::NaiveDateTime>, // person
|
|
||||||
pub show_nsfw: bool, // local_user
|
|
||||||
pub theme: String, // local_user
|
|
||||||
pub default_sort_type: i16, // local_user
|
|
||||||
pub default_listing_type: i16, // local_user
|
|
||||||
pub lang: String, // local_user
|
|
||||||
pub show_avatars: bool, // local_user
|
|
||||||
pub send_notifications_to_email: bool, // local_user
|
|
||||||
pub matrix_user_id: Option<String>, // local_user
|
|
||||||
pub actor_id: Url, // person
|
|
||||||
pub bio: Option<String>, // person
|
|
||||||
pub local: bool, // person
|
|
||||||
pub private_key: Option<String>, // person
|
|
||||||
pub public_key: Option<String>, // person
|
|
||||||
pub last_refreshed_at: chrono::NaiveDateTime, // person
|
|
||||||
pub banner: Option<String>, // person
|
|
||||||
pub deleted: bool, // person
|
|
||||||
pub inbox_url: Url, // person
|
|
||||||
pub shared_inbox_url: Option<Url>, // person
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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: Url,
|
|
||||||
pub bio: Option<String>,
|
|
||||||
pub local: bool,
|
|
||||||
pub banner: Option<String>,
|
|
||||||
pub deleted: bool,
|
|
||||||
pub inbox_url: Url,
|
|
||||||
pub shared_inbox_url: Option<Url>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A safe user view with only settings
|
|
||||||
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
|
||||||
#[table_name = "user_"]
|
|
||||||
pub struct UserSafeSettings {
|
|
||||||
pub id: i32,
|
|
||||||
pub name: String,
|
|
||||||
pub preferred_username: Option<String>,
|
|
||||||
pub email: Option<String>,
|
|
||||||
pub avatar: Option<String>,
|
|
||||||
pub admin: bool,
|
|
||||||
pub banned: bool,
|
|
||||||
pub published: chrono::NaiveDateTime,
|
|
||||||
pub updated: Option<chrono::NaiveDateTime>,
|
|
||||||
pub show_nsfw: bool,
|
|
||||||
pub theme: String,
|
|
||||||
pub default_sort_type: i16,
|
|
||||||
pub default_listing_type: i16,
|
|
||||||
pub lang: String,
|
|
||||||
pub show_avatars: bool,
|
|
||||||
pub send_notifications_to_email: bool,
|
|
||||||
pub matrix_user_id: Option<String>,
|
|
||||||
pub actor_id: Url,
|
|
||||||
pub bio: Option<String>,
|
|
||||||
pub local: bool,
|
|
||||||
pub last_refreshed_at: chrono::NaiveDateTime,
|
|
||||||
pub banner: Option<String>,
|
|
||||||
pub deleted: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
|
||||||
#[table_name = "user_alias_1"]
|
|
||||||
pub struct UserAlias1 {
|
|
||||||
pub id: i32,
|
|
||||||
pub name: String,
|
|
||||||
pub preferred_username: Option<String>,
|
|
||||||
pub password_encrypted: String,
|
|
||||||
pub email: Option<String>,
|
|
||||||
pub avatar: Option<String>,
|
|
||||||
pub admin: bool,
|
|
||||||
pub banned: bool,
|
|
||||||
pub published: chrono::NaiveDateTime,
|
|
||||||
pub updated: Option<chrono::NaiveDateTime>,
|
|
||||||
pub show_nsfw: bool,
|
|
||||||
pub theme: String,
|
|
||||||
pub default_sort_type: i16,
|
|
||||||
pub default_listing_type: i16,
|
|
||||||
pub lang: String,
|
|
||||||
pub show_avatars: bool,
|
|
||||||
pub send_notifications_to_email: bool,
|
|
||||||
pub matrix_user_id: Option<String>,
|
|
||||||
pub actor_id: Url,
|
|
||||||
pub bio: Option<String>,
|
|
||||||
pub local: bool,
|
|
||||||
pub private_key: Option<String>,
|
|
||||||
pub public_key: Option<String>,
|
|
||||||
pub last_refreshed_at: chrono::NaiveDateTime,
|
|
||||||
pub banner: Option<String>,
|
|
||||||
pub deleted: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
|
||||||
#[table_name = "user_alias_1"]
|
|
||||||
pub struct UserSafeAlias1 {
|
|
||||||
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: Url,
|
|
||||||
pub bio: Option<String>,
|
|
||||||
pub local: bool,
|
|
||||||
pub banner: Option<String>,
|
|
||||||
pub deleted: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
|
||||||
#[table_name = "user_alias_2"]
|
|
||||||
pub struct UserAlias2 {
|
|
||||||
pub id: i32,
|
|
||||||
pub name: String,
|
|
||||||
pub preferred_username: Option<String>,
|
|
||||||
pub password_encrypted: String,
|
|
||||||
pub email: Option<String>,
|
|
||||||
pub avatar: Option<String>,
|
|
||||||
pub admin: bool,
|
|
||||||
pub banned: bool,
|
|
||||||
pub published: chrono::NaiveDateTime,
|
|
||||||
pub updated: Option<chrono::NaiveDateTime>,
|
|
||||||
pub show_nsfw: bool,
|
|
||||||
pub theme: String,
|
|
||||||
pub default_sort_type: i16,
|
|
||||||
pub default_listing_type: i16,
|
|
||||||
pub lang: String,
|
|
||||||
pub show_avatars: bool,
|
|
||||||
pub send_notifications_to_email: bool,
|
|
||||||
pub matrix_user_id: Option<String>,
|
|
||||||
pub actor_id: Url,
|
|
||||||
pub bio: Option<String>,
|
|
||||||
pub local: bool,
|
|
||||||
pub private_key: Option<String>,
|
|
||||||
pub public_key: Option<String>,
|
|
||||||
pub last_refreshed_at: chrono::NaiveDateTime,
|
|
||||||
pub banner: Option<String>,
|
|
||||||
pub deleted: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
|
|
||||||
#[table_name = "user_alias_2"]
|
|
||||||
pub struct UserSafeAlias2 {
|
|
||||||
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: Url,
|
|
||||||
pub bio: Option<String>,
|
|
||||||
pub local: bool,
|
|
||||||
pub banner: Option<String>,
|
|
||||||
pub deleted: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Insertable, AsChangeset, Clone)]
|
|
||||||
#[table_name = "user_"]
|
|
||||||
pub struct UserForm {
|
|
||||||
pub name: String,
|
|
||||||
pub preferred_username: Option<Option<String>>,
|
|
||||||
pub password_encrypted: String,
|
|
||||||
pub admin: bool,
|
|
||||||
pub banned: Option<bool>,
|
|
||||||
pub email: Option<Option<String>>,
|
|
||||||
pub avatar: Option<Option<String>>,
|
|
||||||
pub published: Option<chrono::NaiveDateTime>,
|
|
||||||
pub updated: Option<chrono::NaiveDateTime>,
|
|
||||||
pub show_nsfw: bool,
|
|
||||||
pub theme: String,
|
|
||||||
pub default_sort_type: i16,
|
|
||||||
pub default_listing_type: i16,
|
|
||||||
pub lang: String,
|
|
||||||
pub show_avatars: bool,
|
|
||||||
pub send_notifications_to_email: bool,
|
|
||||||
pub matrix_user_id: Option<Option<String>>,
|
|
||||||
pub actor_id: Option<Url>,
|
|
||||||
pub bio: Option<Option<String>>,
|
|
||||||
pub local: bool,
|
|
||||||
pub private_key: Option<String>,
|
|
||||||
pub public_key: Option<String>,
|
|
||||||
pub last_refreshed_at: Option<chrono::NaiveDateTime>,
|
|
||||||
pub banner: Option<Option<String>>,
|
|
||||||
pub inbox_url: Option<Url>,
|
|
||||||
pub shared_inbox_url: Option<Option<Url>>,
|
|
||||||
}
|
|
Loading…
Reference in New Issue