2023-03-01 17:19:46 +00:00
|
|
|
use crate::structs::PersonView;
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel::{
|
2023-07-28 08:36:50 +00:00
|
|
|
pg::Pg,
|
2022-11-09 10:05:00 +00:00
|
|
|
result::Error,
|
|
|
|
BoolExpressionMethods,
|
|
|
|
ExpressionMethods,
|
2023-08-24 15:27:00 +00:00
|
|
|
NullableExpressionMethods,
|
2022-11-09 10:05:00 +00:00
|
|
|
PgTextExpressionMethods,
|
|
|
|
QueryDsl,
|
|
|
|
};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
newtypes::PersonId,
|
2023-07-06 12:44:26 +00:00
|
|
|
schema,
|
2023-08-24 09:40:08 +00:00
|
|
|
schema::{local_user, person, person_aggregates},
|
2023-08-24 15:27:00 +00:00
|
|
|
utils::{fuzzy_search, get_conn, limit_and_offset, now, DbConn, DbPool, ListFn, Queries, ReadFn},
|
2023-10-16 23:37:28 +00:00
|
|
|
SortType,
|
2021-03-10 22:33:55 +00:00
|
|
|
};
|
2023-10-16 23:37:28 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use strum_macros::{Display, EnumString};
|
2021-03-10 22:33:55 +00:00
|
|
|
|
2023-07-28 08:36:50 +00:00
|
|
|
enum ListMode {
|
|
|
|
Admins,
|
|
|
|
Banned,
|
|
|
|
Query(PersonQuery),
|
|
|
|
}
|
|
|
|
|
2023-10-16 23:37:28 +00: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 08:36:50 +00:00
|
|
|
fn queries<'a>(
|
|
|
|
) -> Queries<impl ReadFn<'a, PersonView, PersonId>, impl ListFn<'a, PersonView, ListMode>> {
|
|
|
|
let all_joins = |query: person::BoxedQuery<'a, Pg>| {
|
|
|
|
query
|
2021-03-10 22:33:55 +00:00
|
|
|
.inner_join(person_aggregates::table)
|
2023-08-24 09:40:08 +00:00
|
|
|
.left_join(local_user::table)
|
2023-03-01 17:19:46 +00:00
|
|
|
.select((person::all_columns, person_aggregates::all_columns))
|
2023-07-28 08:36:50 +00: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 13:26:10 +00:00
|
|
|
.first::<PersonView>(&mut conn)
|
2023-07-28 08:36:50 +00: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-08-24 09:40:08 +00:00
|
|
|
.filter(local_user::admin.eq(true))
|
2023-07-28 08:36:50 +00: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 15:27:00 +00:00
|
|
|
.or(person::ban_expires.gt(now().nullable())),
|
2023-07-28 08:36:50 +00: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 23:37:28 +00:00
|
|
|
let sort = options.sort.map(post_to_person_sort_type);
|
|
|
|
query = match sort.unwrap_or(PersonSortType::CommentScore) {
|
2023-07-28 08:36:50 +00: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);
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 13:26:10 +00:00
|
|
|
query.load::<PersonView>(&mut conn).await
|
2023-07-28 08:36:50 +00: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 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn is_admin(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<bool, Error> {
|
2023-08-24 09:40:08 +00:00
|
|
|
use schema::{
|
|
|
|
local_user::dsl::admin,
|
|
|
|
person::dsl::{id, person},
|
|
|
|
};
|
2023-07-06 12:44:26 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
let is_admin = person
|
2023-08-24 09:40:08 +00:00
|
|
|
.inner_join(local_user::table)
|
2023-07-06 12:44:26 +00:00
|
|
|
.filter(id.eq(person_id))
|
|
|
|
.select(admin)
|
|
|
|
.first::<bool>(conn)
|
|
|
|
.await?;
|
|
|
|
Ok(is_admin)
|
|
|
|
}
|
2021-03-10 22:33:55 +00:00
|
|
|
|
2023-07-28 08:36:50 +00:00
|
|
|
pub async fn admins(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
|
|
|
|
queries().list(pool, ListMode::Admins).await
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn banned(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
|
2023-07-28 08:36:50 +00:00
|
|
|
queries().list(pool, ListMode::Banned).await
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:20:25 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct PersonQuery {
|
2023-10-16 23:37:28 +00:00
|
|
|
pub sort: Option<SortType>,
|
2023-07-17 10:20:25 +00:00
|
|
|
pub search_term: Option<String>,
|
|
|
|
pub page: Option<i64>,
|
|
|
|
pub limit: Option<i64>,
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 10:20:25 +00:00
|
|
|
impl PersonQuery {
|
|
|
|
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PersonView>, Error> {
|
2023-07-28 08:36:50 +00:00
|
|
|
queries().list(pool, ListMode::Query(self)).await
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
}
|