mirror of https://github.com/LemmyNet/lemmy
commit
e25bcb35d7
@ -1 +1 @@
|
||||
0.9.9
|
||||
0.10.0-rc.5
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,121 @@
|
||||
use crate::Crud;
|
||||
use bcrypt::{hash, DEFAULT_COST};
|
||||
use diesel::{dsl::*, result::Error, *};
|
||||
use lemmy_db_schema::{
|
||||
schema::local_user::dsl::*,
|
||||
source::local_user::{LocalUser, LocalUserForm},
|
||||
};
|
||||
|
||||
mod safe_type {
|
||||
use crate::ToSafe;
|
||||
use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
|
||||
|
||||
type Columns = (id, person_id, admin, matrix_user_id);
|
||||
|
||||
impl ToSafe for LocalUser {
|
||||
type SafeColumns = Columns;
|
||||
fn safe_columns_tuple() -> Self::SafeColumns {
|
||||
(id, person_id, admin, matrix_user_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod safe_settings_type {
|
||||
use crate::ToSafeSettings;
|
||||
use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
|
||||
|
||||
type Columns = (
|
||||
id,
|
||||
person_id,
|
||||
email,
|
||||
admin,
|
||||
show_nsfw,
|
||||
theme,
|
||||
default_sort_type,
|
||||
default_listing_type,
|
||||
lang,
|
||||
show_avatars,
|
||||
send_notifications_to_email,
|
||||
matrix_user_id,
|
||||
);
|
||||
|
||||
impl ToSafeSettings for LocalUser {
|
||||
type SafeSettingsColumns = Columns;
|
||||
|
||||
/// Includes everything but the hashed password
|
||||
fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
|
||||
(
|
||||
id,
|
||||
person_id,
|
||||
email,
|
||||
admin,
|
||||
show_nsfw,
|
||||
theme,
|
||||
default_sort_type,
|
||||
default_listing_type,
|
||||
lang,
|
||||
show_avatars,
|
||||
send_notifications_to_email,
|
||||
matrix_user_id,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait LocalUser_ {
|
||||
fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
|
||||
fn update_password(
|
||||
conn: &PgConnection,
|
||||
local_user_id: i32,
|
||||
new_password: &str,
|
||||
) -> Result<LocalUser, Error>;
|
||||
fn add_admin(conn: &PgConnection, person_id: i32, added: bool) -> Result<LocalUser, Error>;
|
||||
}
|
||||
|
||||
impl LocalUser_ for LocalUser {
|
||||
fn register(conn: &PgConnection, form: &LocalUserForm) -> 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)
|
||||
}
|
||||
|
||||
fn update_password(
|
||||
conn: &PgConnection,
|
||||
local_user_id: i32,
|
||||
new_password: &str,
|
||||
) -> Result<Self, Error> {
|
||||
let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
|
||||
|
||||
diesel::update(local_user.find(local_user_id))
|
||||
.set((password_encrypted.eq(password_hash),))
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
|
||||
fn add_admin(conn: &PgConnection, for_person_id: i32, added: bool) -> Result<Self, Error> {
|
||||
diesel::update(local_user.filter(person_id.eq(for_person_id)))
|
||||
.set(admin.eq(added))
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl Crud<LocalUserForm> for LocalUser {
|
||||
fn read(conn: &PgConnection, local_user_id: i32) -> Result<Self, Error> {
|
||||
local_user.find(local_user_id).first::<Self>(conn)
|
||||
}
|
||||
fn delete(conn: &PgConnection, local_user_id: i32) -> Result<usize, Error> {
|
||||
diesel::delete(local_user.find(local_user_id)).execute(conn)
|
||||
}
|
||||
fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
|
||||
insert_into(local_user)
|
||||
.values(form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
fn update(conn: &PgConnection, local_user_id: i32, form: &LocalUserForm) -> Result<Self, Error> {
|
||||
diesel::update(local_user.find(local_user_id))
|
||||
.set(form)
|
||||
.get_result::<Self>(conn)
|
||||
}
|
||||
}
|
@ -0,0 +1,289 @@
|
||||
use crate::{ApubObject, Crud};
|
||||
use diesel::{dsl::*, result::Error, *};
|
||||
use lemmy_db_schema::{
|
||||
naive_now,
|
||||
schema::person::dsl::*,
|
||||
source::person::{Person, PersonForm},
|
||||
DbUrl,
|
||||
};
|
||||
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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: &DbUrl) -> 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 ban_person(conn: &PgConnection, person_id: i32, ban: bool) -> Result<Person, Error>;
|
||||
fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>;
|
||||
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 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_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> {
|
||||
person
|
||||
.filter(deleted.eq(false))
|
||||
.filter(local.eq(true))
|
||||
.filter(name.ilike(from_name))
|
||||
.first::<Person>(conn)
|
||||
}
|
||||
|
||||
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> {
|
||||
use lemmy_db_schema::schema::local_user;
|
||||
|
||||
// Set the local user info to none
|
||||
diesel::update(local_user::table.filter(local_user::person_id.eq(person_id)))
|
||||
.set((
|
||||
local_user::email.eq::<Option<String>>(None),
|
||||
local_user::matrix_user_id.eq::<Option<String>>(None),
|
||||
))
|
||||
.execute(conn)?;
|
||||
|
||||
diesel::update(person.find(person_id))
|
||||
.set((
|
||||
preferred_username.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::*};
|
||||
|
||||
#[test]
|
||||
fn test_crud() {
|
||||
let conn = establish_unpooled_connection();
|
||||
|
||||
let new_person = PersonForm {
|
||||
name: "holly".into(),
|
||||
preferred_username: None,
|
||||
avatar: None,
|
||||
banner: None,
|
||||
banned: None,
|
||||
deleted: None,
|
||||
published: None,
|
||||
updated: None,
|
||||
actor_id: None,
|
||||
bio: None,
|
||||
local: None,
|
||||
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: "holly".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,
|
||||
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,460 +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_},
|
||||
DbUrl,
|
||||
};
|
||||
use lemmy_utils::settings::structs::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: &DbUrl) -> 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};
|
||||
use serial_test::serial;
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
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,56 @@
|
||||
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 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,145 @@
|
||||
use diesel::{result::Error, *};
|
||||
use lemmy_db_queries::{aggregates::person_aggregates::PersonAggregates, ToSafe, ToSafeSettings};
|
||||
use lemmy_db_schema::{
|
||||
schema::{local_user, person, person_aggregates},
|
||||
source::{
|
||||
local_user::{LocalUser, LocalUserSettings},
|
||||
person::{Person, PersonSafe},
|
||||
},
|
||||
};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct LocalUserView {
|
||||
pub local_user: LocalUser,
|
||||
pub person: Person,
|
||||
pub counts: PersonAggregates,
|
||||
}
|
||||
|
||||
type LocalUserViewTuple = (LocalUser, Person, PersonAggregates);
|
||||
|
||||
impl LocalUserView {
|
||||
pub fn read(conn: &PgConnection, local_user_id: i32) -> Result<Self, Error> {
|
||||
let (local_user, person, counts) = local_user::table
|
||||
.find(local_user_id)
|
||||
.inner_join(person::table)
|
||||
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
||||
.select((
|
||||
local_user::all_columns,
|
||||
person::all_columns,
|
||||
person_aggregates::all_columns,
|
||||
))
|
||||
.first::<LocalUserViewTuple>(conn)?;
|
||||
Ok(Self {
|
||||
local_user,
|
||||
person,
|
||||
counts,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn read_person(conn: &PgConnection, person_id: i32) -> Result<Self, Error> {
|
||||
let (local_user, person, counts) = local_user::table
|
||||
.filter(person::id.eq(person_id))
|
||||
.inner_join(person::table)
|
||||
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
||||
.select((
|
||||
local_user::all_columns,
|
||||
person::all_columns,
|
||||
person_aggregates::all_columns,
|
||||
))
|
||||
.first::<LocalUserViewTuple>(conn)?;
|
||||
Ok(Self {
|
||||
local_user,
|
||||
person,
|
||||
counts,
|
||||
})
|
||||
}
|
||||
|
||||
// TODO check where this is used
|
||||
pub fn read_from_name(conn: &PgConnection, name: &str) -> Result<Self, Error> {
|
||||
let (local_user, person, counts) = local_user::table
|
||||
.filter(person::name.eq(name))
|
||||
.inner_join(person::table)
|
||||
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
||||
.select((
|
||||
local_user::all_columns,
|
||||
person::all_columns,
|
||||
person_aggregates::all_columns,
|
||||
))
|
||||
.first::<LocalUserViewTuple>(conn)?;
|
||||
Ok(Self {
|
||||
person,
|
||||
counts,
|
||||
local_user,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn find_by_email_or_name(conn: &PgConnection, name_or_email: &str) -> Result<Self, Error> {
|
||||
let (local_user, person, counts) = local_user::table
|
||||
.inner_join(person::table)
|
||||
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
||||
.filter(
|
||||
person::name
|
||||
.ilike(name_or_email)
|
||||
.or(local_user::email.ilike(name_or_email)),
|
||||
)
|
||||
.select((
|
||||
local_user::all_columns,
|
||||
person::all_columns,
|
||||
person_aggregates::all_columns,
|
||||
))
|
||||
.first::<LocalUserViewTuple>(conn)?;
|
||||
Ok(Self {
|
||||
person,
|
||||
counts,
|
||||
local_user,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<Self, Error> {
|
||||
let (local_user, person, counts) = local_user::table
|
||||
.inner_join(person::table)
|
||||
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
||||
.filter(local_user::email.eq(from_email))
|
||||
.select((
|
||||
local_user::all_columns,
|
||||
person::all_columns,
|
||||
person_aggregates::all_columns,
|
||||
))
|
||||
.first::<LocalUserViewTuple>(conn)?;
|
||||
Ok(Self {
|
||||
person,
|
||||
counts,
|
||||
local_user,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct LocalUserSettingsView {
|
||||
pub local_user: LocalUserSettings,
|
||||
pub person: PersonSafe,
|
||||
pub counts: PersonAggregates,
|
||||
}
|
||||
|
||||
type LocalUserSettingsViewTuple = (LocalUserSettings, PersonSafe, PersonAggregates);
|
||||
|
||||
impl LocalUserSettingsView {
|
||||
pub fn read(conn: &PgConnection, local_user_id: i32) -> Result<Self, Error> {
|
||||
let (local_user, person, counts) = local_user::table
|
||||
.find(local_user_id)
|
||||
.inner_join(person::table)
|
||||
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
|
||||
.select((
|
||||
LocalUser::safe_settings_columns_tuple(),
|
||||
Person::safe_columns_tuple(),
|
||||
person_aggregates::all_columns,
|
||||
))
|
||||
.first::<LocalUserSettingsViewTuple>(conn)?;
|
||||
Ok(Self {
|
||||
person,
|
||||
counts,
|
||||
local_user,
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
use diesel::{result::Error, *};
|
||||
use lemmy_db_queries::ToSafe;
|
||||
use lemmy_db_schema::{
|
||||
schema::{community, community_person_ban, person},
|
||||
source::{
|
||||
community::{Community, CommunitySafe},
|
||||
person::{Person, PersonSafe},
|
||||
},
|
||||
};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct CommunityPersonBanView {
|
||||
pub community: CommunitySafe,
|
||||
pub person: PersonSafe,
|
||||
}
|
||||
|
||||
impl CommunityPersonBanView {
|
||||
pub fn get(
|
||||
conn: &PgConnection,
|
||||
from_person_id: i32,
|
||||
from_community_id: i32,
|
||||
) -> Result<Self, Error> {
|
||||
let (community, person) = community_person_ban::table
|
||||
.inner_join(community::table)
|
||||
.inner_join(person::table)
|
||||
.select((
|
||||
Community::safe_columns_tuple(),
|
||||
Person::safe_columns_tuple(),
|
||||
))
|
||||
.filter(community_person_ban::community_id.eq(from_community_id))
|
||||
.filter(community_person_ban::person_id.eq(from_person_id))
|
||||
.order_by(community_person_ban::published)
|
||||
.first::<(CommunitySafe, PersonSafe)>(conn)?;
|
||||
|
||||
Ok(CommunityPersonBanView { community, person })
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
use diesel::{result::Error, *};
|
||||
use lemmy_db_queries::ToSafe;
|
||||
use lemmy_db_schema::{
|
||||
schema::{community, community_user_ban, user_},
|
||||
source::{
|
||||
community::{Community, CommunitySafe},
|
||||
user::{UserSafe, User_},
|
||||
},
|
||||
};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct CommunityUserBanView {
|
||||
pub community: CommunitySafe,
|
||||
pub user: UserSafe,
|
||||
}
|
||||
|
||||
impl CommunityUserBanView {
|
||||
pub fn get(
|
||||
conn: &PgConnection,
|
||||
from_user_id: i32,
|
||||
from_community_id: i32,
|
||||
) -> Result<Self, Error> {
|
||||
let (community, user) = community_user_ban::table
|
||||
.inner_join(community::table)
|
||||
.inner_join(user_::table)
|
||||
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
|
||||
.filter(community_user_ban::community_id.eq(from_community_id))
|
||||
.filter(community_user_ban::user_id.eq(from_user_id))
|
||||
.order_by(community_user_ban::published)
|
||||
.first::<(CommunitySafe, UserSafe)>(conn)?;
|
||||
|
||||
Ok(CommunityUserBanView { community, user })
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
pub mod community_follower_view;
|
||||
pub mod community_moderator_view;
|
||||
pub mod community_user_ban_view;
|
||||
pub mod community_person_ban_view;
|
||||
pub mod community_view;
|
||||
pub mod user_mention_view;
|
||||
pub mod user_view;
|
||||
pub mod person_mention_view;
|
||||
pub mod person_view;
|
||||
|
@ -0,0 +1,152 @@
|
||||
use diesel::{dsl::*, result::Error, *};
|
||||
use lemmy_db_queries::{
|
||||
aggregates::person_aggregates::PersonAggregates,
|
||||
fuzzy_search,
|
||||
limit_and_offset,
|
||||
MaybeOptional,
|
||||
SortType,
|
||||
ToSafe,
|
||||
ViewToVec,
|
||||
};
|
||||
use lemmy_db_schema::{
|
||||
schema::{local_user, person, person_aggregates},
|
||||
source::person::{Person, PersonSafe},
|
||||
};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct PersonViewSafe {
|
||||
pub person: PersonSafe,
|
||||
pub counts: PersonAggregates,
|
||||
}
|
||||
|
||||
type PersonViewSafeTuple = (PersonSafe, PersonAggregates);
|
||||
|
||||
impl PersonViewSafe {
|
||||
pub fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> {
|
||||
let (person, counts) = person::table
|
||||
.find(id)
|
||||
.inner_join(person_aggregates::table)
|
||||
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
|
||||
.first::<PersonViewSafeTuple>(conn)?;
|
||||
Ok(Self { person, counts })
|
||||
}
|
||||
|
||||
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
||||
let admins = person::table
|
||||
.inner_join(person_aggregates::table)
|
||||
.inner_join(local_user::table)
|
||||
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
|
||||
.filter(local_user::admin.eq(true))
|
||||
.order_by(person::published)
|
||||
.load::<PersonViewSafeTuple>(conn)?;
|
||||
|
||||
Ok(Self::from_tuple_to_vec(admins))
|
||||
}
|
||||
|
||||
pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
||||
let banned = person::table
|
||||
.inner_join(person_aggregates::table)
|
||||
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
|
||||
.filter(person::banned.eq(true))
|
||||
.load::<PersonViewSafeTuple>(conn)?;
|
||||
|
||||
Ok(Self::from_tuple_to_vec(banned))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PersonQueryBuilder<'a> {
|
||||
conn: &'a PgConnection,
|
||||
sort: &'a SortType,
|
||||
search_term: Option<String>,
|
||||
page: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
}
|
||||
|
||||
impl<'a> PersonQueryBuilder<'a> {
|
||||
pub fn create(conn: &'a PgConnection) -> Self {
|
||||
PersonQueryBuilder {
|
||||
conn,
|
||||
search_term: None,
|
||||
sort: &SortType::Hot,
|
||||
page: None,
|
||||
limit: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sort(mut self, sort: &'a SortType) -> Self {
|
||||
self.sort = sort;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
|
||||
self.search_term = search_term.get_optional();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
|
||||
self.page = page.get_optional();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
|
||||
self.limit = limit.get_optional();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn list(self) -> Result<Vec<PersonViewSafe>, Error> {
|
||||
let mut query = person::table
|
||||
.inner_join(person_aggregates::table)
|
||||
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
|
||||
.into_boxed();
|
||||
|
||||
if let Some(search_term) = self.search_term {
|
||||
query = query.filter(person::name.ilike(fuzzy_search(&search_term)));
|
||||
}
|
||||
|
||||
query = match self.sort {
|
||||
SortType::Hot => query
|
||||
.order_by(person_aggregates::comment_score.desc())
|
||||
.then_order_by(person::published.desc()),
|
||||
SortType::Active => query
|
||||
.order_by(person_aggregates::comment_score.desc())
|
||||
.then_order_by(person::published.desc()),
|
||||
SortType::New | SortType::MostComments | SortType::NewComments => {
|
||||
query.order_by(person::published.desc())
|
||||
}
|
||||
SortType::TopAll => query.order_by(person_aggregates::comment_score.desc()),
|
||||
SortType::TopYear => query
|
||||
.filter(person::published.gt(now - 1.years()))
|
||||
.order_by(person_aggregates::comment_score.desc()),
|
||||
SortType::TopMonth => query
|
||||
.filter(person::published.gt(now - 1.months()))
|
||||
.order_by(person_aggregates::comment_score.desc()),
|
||||
SortType::TopWeek => query
|
||||
.filter(person::published.gt(now - 1.weeks()))
|
||||
.order_by(person_aggregates::comment_score.desc()),
|
||||
SortType::TopDay => query
|
||||
.filter(person::published.gt(now - 1.days()))
|
||||
.order_by(person_aggregates::comment_score.desc()),
|
||||
};
|
||||
|
||||
let (limit, offset) = limit_and_offset(self.page, self.limit);
|
||||
query = query.limit(limit).offset(offset);
|
||||
|
||||
let res = query.load::<PersonViewSafeTuple>(self.conn)?;
|
||||
|
||||
Ok(PersonViewSafe::from_tuple_to_vec(res))
|
||||
}
|
||||
}
|
||||
|
||||
impl ViewToVec for PersonViewSafe {
|
||||
type DbTuple = PersonViewSafeTuple;
|
||||
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
|
||||
items
|
||||
.iter()
|
||||
.map(|a| Self {
|
||||
person: a.0.to_owned(),
|
||||
counts: a.1.to_owned(),
|
||||
})
|
||||
.collect::<Vec<Self>>()
|
||||
}
|
||||
}
|
@ -1,151 +0,0 @@
|
||||
use diesel::{dsl::*, result::Error, *};
|
||||
use lemmy_db_queries::{
|
||||
aggregates::user_aggregates::UserAggregates,
|
||||
fuzzy_search,
|
||||
limit_and_offset,
|
||||
MaybeOptional,
|
||||
SortType,
|
||||
ToSafe,
|
||||
ViewToVec,
|
||||
};
|
||||
use lemmy_db_schema::{
|
||||
schema::{user_, user_aggregates},
|
||||
source::user::{UserSafe, User_},
|
||||
};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct UserViewSafe {
|
||||
pub user: UserSafe,
|
||||
pub counts: UserAggregates,
|
||||
}
|
||||
|
||||
type UserViewSafeTuple = (UserSafe, UserAggregates);
|
||||
|
||||
impl UserViewSafe {
|
||||
pub fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> {
|
||||
let (user, counts) = user_::table
|
||||
.find(id)
|
||||
.inner_join(user_aggregates::table)
|
||||
.select((User_::safe_columns_tuple(), user_aggregates::all_columns))
|
||||
.first::<UserViewSafeTuple>(conn)?;
|
||||
Ok(Self { user, counts })
|
||||
}
|
||||
|
||||
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
||||
let admins = user_::table
|
||||
.inner_join(user_aggregates::table)
|
||||
.select((User_::safe_columns_tuple(), user_aggregates::all_columns))
|
||||
.filter(user_::admin.eq(true))
|
||||
.order_by(user_::published)
|
||||
.load::<UserViewSafeTuple>(conn)?;
|
||||
|
||||
Ok(Self::from_tuple_to_vec(admins))
|
||||
}
|
||||
|
||||
pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
||||
let banned = user_::table
|
||||
.inner_join(user_aggregates::table)
|
||||
.select((User_::safe_columns_tuple(), user_aggregates::all_columns))
|
||||
.filter(user_::banned.eq(true))
|
||||
.load::<UserViewSafeTuple>(conn)?;
|
||||
|
||||
Ok(Self::from_tuple_to_vec(banned))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UserQueryBuilder<'a> {
|
||||
conn: &'a PgConnection,
|
||||
sort: &'a SortType,
|
||||
search_term: Option<String>,
|
||||
page: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
}
|
||||
|
||||
impl<'a> UserQueryBuilder<'a> {
|
||||
pub fn create(conn: &'a PgConnection) -> Self {
|
||||
UserQueryBuilder {
|
||||
conn,
|
||||
search_term: None,
|
||||
sort: &SortType::Hot,
|
||||
page: None,
|
||||
limit: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sort(mut self, sort: &'a SortType) -> Self {
|
||||
self.sort = sort;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
|
||||
self.search_term = search_term.get_optional();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
|
||||
self.page = page.get_optional();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
|
||||
self.limit = limit.get_optional();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn list(self) -> Result<Vec<UserViewSafe>, Error> {
|
||||
let mut query = user_::table
|
||||
.inner_join(user_aggregates::table)
|
||||
.select((User_::safe_columns_tuple(), user_aggregates::all_columns))
|
||||
.into_boxed();
|
||||
|
||||
if let Some(search_term) = self.search_term {
|
||||
query = query.filter(user_::name.ilike(fuzzy_search(&search_term)));
|
||||
}
|
||||
|
||||
query = match self.sort {
|
||||
SortType::Hot => query
|
||||
.order_by(user_aggregates::comment_score.desc())
|
||||
.then_order_by(user_::published.desc()),
|
||||
SortType::Active => query
|
||||
.order_by(user_aggregates::comment_score.desc())
|
||||
.then_order_by(user_::published.desc()),
|
||||
SortType::New | SortType::MostComments | SortType::NewComments => {
|
||||
query.order_by(user_::published.desc())
|
||||
}
|
||||
SortType::TopAll => query.order_by(user_aggregates::comment_score.desc()),
|
||||
SortType::TopYear => query
|
||||
.filter(user_::published.gt(now - 1.years()))
|
||||
.order_by(user_aggregates::comment_score.desc()),
|
||||
SortType::TopMonth => query
|
||||
.filter(user_::published.gt(now - 1.months()))
|
||||
.order_by(user_aggregates::comment_score.desc()),
|
||||
SortType::TopWeek => query
|
||||
.filter(user_::published.gt(now - 1.weeks()))
|
||||
.order_by(user_aggregates::comment_score.desc()),
|
||||
SortType::TopDay => query
|
||||
.filter(user_::published.gt(now - 1.days()))
|
||||
.order_by(user_aggregates::comment_score.desc()),
|
||||
};
|
||||
|
||||
let (limit, offset) = limit_and_offset(self.page, self.limit);
|
||||
query = query.limit(limit).offset(offset);
|
||||
|
||||
let res = query.load::<UserViewSafeTuple>(self.conn)?;
|
||||
|
||||
Ok(UserViewSafe::from_tuple_to_vec(res))
|
||||
}
|
||||
}
|
||||
|
||||
impl ViewToVec for UserViewSafe {
|
||||
type DbTuple = UserViewSafeTuple;
|
||||
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
|
||||
items
|
||||
.iter()
|
||||
.map(|a| Self {
|
||||
user: a.0.to_owned(),
|
||||
counts: a.1.to_owned(),
|
||||
})
|
||||
.collect::<Vec<Self>>()
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue