2023-03-01 10:19:46 -07:00
|
|
|
use crate::structs::PersonView;
|
2022-11-09 03:05:00 -07:00
|
|
|
use diesel::{
|
2023-07-28 02:36:50 -06:00
|
|
|
pg::Pg,
|
2022-11-09 03:05:00 -07:00
|
|
|
result::Error,
|
|
|
|
BoolExpressionMethods,
|
|
|
|
ExpressionMethods,
|
2023-08-24 09:27:00 -06:00
|
|
|
NullableExpressionMethods,
|
2022-11-09 03:05:00 -07:00
|
|
|
PgTextExpressionMethods,
|
|
|
|
QueryDsl,
|
|
|
|
};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2021-10-16 07:33:38 -06:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
newtypes::PersonId,
|
2023-08-24 03:40:08 -06:00
|
|
|
schema::{local_user, person, person_aggregates},
|
2023-12-12 10:09:52 -07:00
|
|
|
utils::{
|
|
|
|
functions::coalesce,
|
|
|
|
fuzzy_search,
|
|
|
|
limit_and_offset,
|
|
|
|
now,
|
|
|
|
DbConn,
|
|
|
|
DbPool,
|
|
|
|
ListFn,
|
|
|
|
Queries,
|
|
|
|
ReadFn,
|
|
|
|
},
|
2024-03-01 09:53:20 -07:00
|
|
|
ListingType,
|
2023-10-16 17:37:28 -06:00
|
|
|
SortType,
|
2021-03-10 15:33:55 -07:00
|
|
|
};
|
2023-10-16 17:37:28 -06:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use strum_macros::{Display, EnumString};
|
2021-03-10 15:33:55 -07:00
|
|
|
|
2023-07-28 02:36:50 -06:00
|
|
|
enum ListMode {
|
|
|
|
Admins,
|
|
|
|
Banned,
|
|
|
|
Query(PersonQuery),
|
|
|
|
}
|
|
|
|
|
2023-10-16 17:37:28 -06:00
|
|
|
#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy)]
|
|
|
|
/// The person sort types. Converted automatically from `SortType`
|
|
|
|
enum PersonSortType {
|
|
|
|
New,
|
|
|
|
Old,
|
|
|
|
MostComments,
|
|
|
|
CommentScore,
|
|
|
|
PostScore,
|
|
|
|
PostCount,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn post_to_person_sort_type(sort: SortType) -> PersonSortType {
|
|
|
|
match sort {
|
|
|
|
SortType::Active | SortType::Hot | SortType::Controversial => PersonSortType::CommentScore,
|
|
|
|
SortType::New | SortType::NewComments => PersonSortType::New,
|
|
|
|
SortType::MostComments => PersonSortType::MostComments,
|
|
|
|
SortType::Old => PersonSortType::Old,
|
|
|
|
_ => PersonSortType::CommentScore,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 02:36:50 -06:00
|
|
|
fn queries<'a>(
|
|
|
|
) -> Queries<impl ReadFn<'a, PersonView, PersonId>, impl ListFn<'a, PersonView, ListMode>> {
|
2023-11-21 09:20:24 -07:00
|
|
|
let all_joins = move |query: person::BoxedQuery<'a, Pg>| {
|
2023-07-28 02:36:50 -06:00
|
|
|
query
|
2021-03-10 15:33:55 -07:00
|
|
|
.inner_join(person_aggregates::table)
|
2023-12-12 10:09:52 -07:00
|
|
|
.left_join(local_user::table)
|
2023-10-20 08:01:29 -06:00
|
|
|
.filter(person::deleted.eq(false))
|
2023-11-21 09:20:24 -07:00
|
|
|
.select((
|
|
|
|
person::all_columns,
|
|
|
|
person_aggregates::all_columns,
|
2023-12-12 10:09:52 -07:00
|
|
|
coalesce(local_user::admin.nullable(), false),
|
2023-11-21 09:20:24 -07:00
|
|
|
))
|
2023-07-28 02:36:50 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let read = move |mut conn: DbConn<'a>, person_id: PersonId| async move {
|
|
|
|
all_joins(person::table.find(person_id).into_boxed())
|
2023-08-31 07:26:10 -06:00
|
|
|
.first::<PersonView>(&mut conn)
|
2023-07-28 02:36:50 -06:00
|
|
|
.await
|
|
|
|
};
|
|
|
|
|
|
|
|
let list = move |mut conn: DbConn<'a>, mode: ListMode| async move {
|
|
|
|
let mut query = all_joins(person::table.into_boxed());
|
|
|
|
match mode {
|
|
|
|
ListMode::Admins => {
|
|
|
|
query = query
|
2023-12-12 10:09:52 -07:00
|
|
|
.filter(local_user::admin.eq(true))
|
2023-07-28 02:36:50 -06:00
|
|
|
.filter(person::deleted.eq(false))
|
|
|
|
.order_by(person::published);
|
|
|
|
}
|
|
|
|
ListMode::Banned => {
|
|
|
|
query = query
|
|
|
|
.filter(
|
|
|
|
person::banned.eq(true).and(
|
|
|
|
person::ban_expires
|
|
|
|
.is_null()
|
2023-08-24 09:27:00 -06:00
|
|
|
.or(person::ban_expires.gt(now().nullable())),
|
2023-07-28 02:36:50 -06:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.filter(person::deleted.eq(false));
|
|
|
|
}
|
|
|
|
ListMode::Query(options) => {
|
|
|
|
if let Some(search_term) = options.search_term {
|
|
|
|
let searcher = fuzzy_search(&search_term);
|
|
|
|
query = query
|
|
|
|
.filter(person::name.ilike(searcher.clone()))
|
|
|
|
.or_filter(person::display_name.ilike(searcher));
|
|
|
|
}
|
|
|
|
|
2023-10-16 17:37:28 -06:00
|
|
|
let sort = options.sort.map(post_to_person_sort_type);
|
|
|
|
query = match sort.unwrap_or(PersonSortType::CommentScore) {
|
2023-07-28 02:36:50 -06:00
|
|
|
PersonSortType::New => query.order_by(person::published.desc()),
|
|
|
|
PersonSortType::Old => query.order_by(person::published.asc()),
|
|
|
|
PersonSortType::MostComments => query.order_by(person_aggregates::comment_count.desc()),
|
|
|
|
PersonSortType::CommentScore => query.order_by(person_aggregates::comment_score.desc()),
|
|
|
|
PersonSortType::PostScore => query.order_by(person_aggregates::post_score.desc()),
|
|
|
|
PersonSortType::PostCount => query.order_by(person_aggregates::post_count.desc()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let (limit, offset) = limit_and_offset(options.page, options.limit)?;
|
|
|
|
query = query.limit(limit).offset(offset);
|
2024-03-01 09:53:20 -07:00
|
|
|
|
|
|
|
if let Some(listing_type) = options.listing_type {
|
|
|
|
query = match listing_type {
|
|
|
|
// return nothing as its not possible to follow users
|
|
|
|
ListingType::Subscribed => query.limit(0),
|
|
|
|
ListingType::Local => query.filter(person::local.eq(true)),
|
|
|
|
_ => query,
|
|
|
|
};
|
|
|
|
}
|
2023-07-28 02:36:50 -06:00
|
|
|
}
|
|
|
|
}
|
2023-08-31 07:26:10 -06:00
|
|
|
query.load::<PersonView>(&mut conn).await
|
2023-07-28 02:36:50 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
Queries::new(read, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PersonView {
|
|
|
|
pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
|
|
|
|
queries().read(pool, person_id).await
|
2021-03-10 15:33:55 -07:00
|
|
|
}
|
|
|
|
|
2023-07-28 02:36:50 -06:00
|
|
|
pub async fn admins(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
|
|
|
|
queries().list(pool, ListMode::Admins).await
|
2021-03-10 15:33:55 -07:00
|
|
|
}
|
|
|
|
|
2023-07-11 07:09:59 -06:00
|
|
|
pub async fn banned(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
|
2023-07-28 02:36:50 -06:00
|
|
|
queries().list(pool, ListMode::Banned).await
|
2021-03-10 15:33:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 04:20:25 -06:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct PersonQuery {
|
2023-10-16 17:37:28 -06:00
|
|
|
pub sort: Option<SortType>,
|
2023-07-17 04:20:25 -06:00
|
|
|
pub search_term: Option<String>,
|
2024-03-01 09:53:20 -07:00
|
|
|
pub listing_type: Option<ListingType>,
|
2023-07-17 04:20:25 -06:00
|
|
|
pub page: Option<i64>,
|
|
|
|
pub limit: Option<i64>,
|
2021-03-10 15:33:55 -07:00
|
|
|
}
|
|
|
|
|
2023-07-17 04:20:25 -06:00
|
|
|
impl PersonQuery {
|
|
|
|
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PersonView>, Error> {
|
2023-07-28 02:36:50 -06:00
|
|
|
queries().list(pool, ListMode::Query(self)).await
|
2021-03-10 15:33:55 -07:00
|
|
|
}
|
|
|
|
}
|
2023-10-20 08:01:29 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#![allow(clippy::unwrap_used)]
|
|
|
|
#![allow(clippy::indexing_slicing)]
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use diesel::NotFound;
|
|
|
|
use lemmy_db_schema::{
|
2024-01-04 09:51:55 -07:00
|
|
|
assert_length,
|
2023-10-20 08:01:29 -06:00
|
|
|
source::{
|
|
|
|
instance::Instance,
|
|
|
|
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
|
|
|
|
person::{Person, PersonInsertForm, PersonUpdateForm},
|
|
|
|
},
|
|
|
|
traits::Crud,
|
|
|
|
utils::build_db_pool_for_tests,
|
|
|
|
};
|
2024-03-01 09:53:20 -07:00
|
|
|
use lemmy_utils::error::LemmyResult;
|
2024-01-04 02:47:18 -07:00
|
|
|
use pretty_assertions::assert_eq;
|
2023-10-20 08:01:29 -06:00
|
|
|
use serial_test::serial;
|
|
|
|
|
|
|
|
struct Data {
|
|
|
|
alice: Person,
|
|
|
|
alice_local_user: LocalUser,
|
|
|
|
bob: Person,
|
|
|
|
bob_local_user: LocalUser,
|
|
|
|
}
|
|
|
|
|
2024-03-01 09:53:20 -07:00
|
|
|
async fn init_data(pool: &mut DbPool<'_>) -> LemmyResult<Data> {
|
|
|
|
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
|
|
|
|
let alice_form = PersonInsertForm::builder()
|
|
|
|
.name("alice".to_string())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
2024-03-01 09:53:20 -07:00
|
|
|
.local(Some(true))
|
2023-10-20 08:01:29 -06:00
|
|
|
.build();
|
2024-03-01 09:53:20 -07:00
|
|
|
let alice = Person::create(pool, &alice_form).await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
let alice_local_user_form = LocalUserInsertForm::builder()
|
|
|
|
.person_id(alice.id)
|
|
|
|
.password_encrypted(String::new())
|
|
|
|
.build();
|
2024-03-01 09:53:20 -07:00
|
|
|
let alice_local_user = LocalUser::create(pool, &alice_local_user_form).await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
|
|
|
|
let bob_form = PersonInsertForm::builder()
|
|
|
|
.name("bob".to_string())
|
|
|
|
.bot_account(Some(true))
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(inserted_instance.id)
|
2024-03-01 09:53:20 -07:00
|
|
|
.local(Some(false))
|
2023-10-20 08:01:29 -06:00
|
|
|
.build();
|
2024-03-01 09:53:20 -07:00
|
|
|
let bob = Person::create(pool, &bob_form).await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
let bob_local_user_form = LocalUserInsertForm::builder()
|
|
|
|
.person_id(bob.id)
|
|
|
|
.password_encrypted(String::new())
|
|
|
|
.build();
|
2024-03-01 09:53:20 -07:00
|
|
|
let bob_local_user = LocalUser::create(pool, &bob_local_user_form).await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
|
2024-03-01 09:53:20 -07:00
|
|
|
Ok(Data {
|
2023-10-20 08:01:29 -06:00
|
|
|
alice,
|
|
|
|
alice_local_user,
|
|
|
|
bob,
|
|
|
|
bob_local_user,
|
2024-03-01 09:53:20 -07:00
|
|
|
})
|
2023-10-20 08:01:29 -06:00
|
|
|
}
|
|
|
|
|
2024-03-01 09:53:20 -07:00
|
|
|
async fn cleanup(data: Data, pool: &mut DbPool<'_>) -> LemmyResult<()> {
|
|
|
|
LocalUser::delete(pool, data.alice_local_user.id).await?;
|
|
|
|
LocalUser::delete(pool, data.bob_local_user.id).await?;
|
|
|
|
Person::delete(pool, data.alice.id).await?;
|
|
|
|
Person::delete(pool, data.bob.id).await?;
|
|
|
|
Instance::delete(pool, data.bob.instance_id).await?;
|
|
|
|
Ok(())
|
2023-10-20 08:01:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
2024-03-01 09:53:20 -07:00
|
|
|
async fn exclude_deleted() -> LemmyResult<()> {
|
2023-10-20 08:01:29 -06:00
|
|
|
let pool = &build_db_pool_for_tests().await;
|
|
|
|
let pool = &mut pool.into();
|
2024-03-01 09:53:20 -07:00
|
|
|
let data = init_data(pool).await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
|
|
|
|
Person::update(
|
|
|
|
pool,
|
|
|
|
data.alice.id,
|
|
|
|
&PersonUpdateForm {
|
|
|
|
deleted: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2024-03-01 09:53:20 -07:00
|
|
|
.await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
|
|
|
|
let read = PersonView::read(pool, data.alice.id).await;
|
|
|
|
assert_eq!(read.err(), Some(NotFound));
|
|
|
|
|
2023-11-21 09:20:24 -07:00
|
|
|
let list = PersonQuery {
|
|
|
|
sort: Some(SortType::New),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.list(pool)
|
2024-03-01 09:53:20 -07:00
|
|
|
.await?;
|
2024-01-04 09:51:55 -07:00
|
|
|
assert_length!(1, list);
|
2023-10-20 08:01:29 -06:00
|
|
|
assert_eq!(list[0].person.id, data.bob.id);
|
|
|
|
|
2024-03-01 09:53:20 -07:00
|
|
|
cleanup(data, pool).await
|
2023-10-20 08:01:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
2024-03-01 09:53:20 -07:00
|
|
|
async fn list_banned() -> LemmyResult<()> {
|
2023-10-20 08:01:29 -06:00
|
|
|
let pool = &build_db_pool_for_tests().await;
|
|
|
|
let pool = &mut pool.into();
|
2024-03-01 09:53:20 -07:00
|
|
|
let data = init_data(pool).await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
|
|
|
|
Person::update(
|
|
|
|
pool,
|
|
|
|
data.alice.id,
|
|
|
|
&PersonUpdateForm {
|
|
|
|
banned: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2024-03-01 09:53:20 -07:00
|
|
|
.await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
|
2024-03-01 09:53:20 -07:00
|
|
|
let list = PersonView::banned(pool).await?;
|
2024-01-04 09:51:55 -07:00
|
|
|
assert_length!(1, list);
|
2023-10-20 08:01:29 -06:00
|
|
|
assert_eq!(list[0].person.id, data.alice.id);
|
|
|
|
|
2024-03-01 09:53:20 -07:00
|
|
|
cleanup(data, pool).await
|
2023-10-20 08:01:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
2024-03-01 09:53:20 -07:00
|
|
|
async fn list_admins() -> LemmyResult<()> {
|
2023-10-20 08:01:29 -06:00
|
|
|
let pool = &build_db_pool_for_tests().await;
|
|
|
|
let pool = &mut pool.into();
|
2024-03-01 09:53:20 -07:00
|
|
|
let data = init_data(pool).await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
|
|
|
|
LocalUser::update(
|
|
|
|
pool,
|
|
|
|
data.alice_local_user.id,
|
|
|
|
&LocalUserUpdateForm {
|
|
|
|
admin: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2024-03-01 09:53:20 -07:00
|
|
|
.await?;
|
2023-10-20 08:01:29 -06:00
|
|
|
|
2024-03-01 09:53:20 -07:00
|
|
|
let list = PersonView::admins(pool).await?;
|
2024-01-04 09:51:55 -07:00
|
|
|
assert_length!(1, list);
|
2023-10-20 08:01:29 -06:00
|
|
|
assert_eq!(list[0].person.id, data.alice.id);
|
|
|
|
|
2024-03-01 09:53:20 -07:00
|
|
|
let is_admin = PersonView::read(pool, data.alice.id).await?.is_admin;
|
2023-10-20 08:01:29 -06:00
|
|
|
assert!(is_admin);
|
|
|
|
|
2024-03-01 09:53:20 -07:00
|
|
|
let is_admin = PersonView::read(pool, data.bob.id).await?.is_admin;
|
2023-10-20 08:01:29 -06:00
|
|
|
assert!(!is_admin);
|
|
|
|
|
2024-03-01 09:53:20 -07:00
|
|
|
cleanup(data, pool).await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
|
|
|
async fn listing_type() -> LemmyResult<()> {
|
|
|
|
let pool = &build_db_pool_for_tests().await;
|
|
|
|
let pool = &mut pool.into();
|
|
|
|
let data = init_data(pool).await?;
|
|
|
|
|
|
|
|
let list = PersonQuery {
|
|
|
|
listing_type: Some(ListingType::Local),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.list(pool)
|
|
|
|
.await?;
|
|
|
|
assert_length!(1, list);
|
|
|
|
assert_eq!(list[0].person.id, data.alice.id);
|
|
|
|
|
|
|
|
let list = PersonQuery {
|
|
|
|
listing_type: Some(ListingType::All),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.list(pool)
|
|
|
|
.await?;
|
|
|
|
assert_length!(2, list);
|
|
|
|
|
|
|
|
cleanup(data, pool).await
|
2023-10-20 08:01:29 -06:00
|
|
|
}
|
|
|
|
}
|