2023-03-01 17:19:46 +00:00
|
|
|
use crate::structs::PersonView;
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel::{
|
2023-07-26 17:07:05 +00:00
|
|
|
dsl::now,
|
2022-11-09 10:05:00 +00:00
|
|
|
result::Error,
|
|
|
|
BoolExpressionMethods,
|
|
|
|
ExpressionMethods,
|
|
|
|
PgTextExpressionMethods,
|
|
|
|
QueryDsl,
|
|
|
|
};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-05-03 17:44:13 +00:00
|
|
|
aggregates::structs::PersonAggregates,
|
2021-10-16 13:33:38 +00:00
|
|
|
newtypes::PersonId,
|
2023-07-06 12:44:26 +00:00
|
|
|
schema,
|
2021-03-22 14:28:00 +00:00
|
|
|
schema::{person, person_aggregates},
|
2023-03-01 17:19:46 +00:00
|
|
|
source::person::Person,
|
|
|
|
traits::JoinView,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{fuzzy_search, get_conn, limit_and_offset, DbPool},
|
2023-07-26 17:07:05 +00:00
|
|
|
PersonSortType,
|
2021-03-10 22:33:55 +00:00
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use std::iter::Iterator;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
type PersonViewTuple = (Person, PersonAggregates);
|
2021-03-10 22:33:55 +00:00
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
impl PersonView {
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2023-03-01 17:19:46 +00:00
|
|
|
let res = person::table
|
2021-03-18 20:25:21 +00:00
|
|
|
.find(person_id)
|
2021-03-10 22:33:55 +00:00
|
|
|
.inner_join(person_aggregates::table)
|
2023-03-01 17:19:46 +00:00
|
|
|
.select((person::all_columns, person_aggregates::all_columns))
|
|
|
|
.first::<PersonViewTuple>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await?;
|
2023-03-01 17:19:46 +00:00
|
|
|
Ok(Self::from_tuple(res))
|
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-07-06 12:44:26 +00:00
|
|
|
use schema::person::dsl::{admin, id, person};
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
let is_admin = person
|
|
|
|
.filter(id.eq(person_id))
|
|
|
|
.select(admin)
|
|
|
|
.first::<bool>(conn)
|
|
|
|
.await?;
|
|
|
|
Ok(is_admin)
|
|
|
|
}
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn admins(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
let admins = person::table
|
|
|
|
.inner_join(person_aggregates::table)
|
2023-03-01 17:19:46 +00:00
|
|
|
.select((person::all_columns, person_aggregates::all_columns))
|
2021-03-22 14:28:00 +00:00
|
|
|
.filter(person::admin.eq(true))
|
2022-09-28 20:54:32 +00:00
|
|
|
.filter(person::deleted.eq(false))
|
2021-03-10 22:33:55 +00:00
|
|
|
.order_by(person::published)
|
2023-03-01 17:19:46 +00:00
|
|
|
.load::<PersonViewTuple>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
Ok(admins.into_iter().map(Self::from_tuple).collect())
|
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> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
let banned = person::table
|
|
|
|
.inner_join(person_aggregates::table)
|
2023-03-01 17:19:46 +00:00
|
|
|
.select((person::all_columns, person_aggregates::all_columns))
|
2022-01-08 12:37:07 +00:00
|
|
|
.filter(
|
|
|
|
person::banned.eq(true).and(
|
|
|
|
person::ban_expires
|
|
|
|
.is_null()
|
|
|
|
.or(person::ban_expires.gt(now)),
|
|
|
|
),
|
|
|
|
)
|
2022-09-28 20:54:32 +00:00
|
|
|
.filter(person::deleted.eq(false))
|
2023-03-01 17:19:46 +00:00
|
|
|
.load::<PersonViewTuple>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
Ok(banned.into_iter().map(Self::from_tuple).collect())
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:20:25 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct PersonQuery {
|
2023-07-26 17:07:05 +00:00
|
|
|
pub sort: Option<PersonSortType>,
|
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> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
let mut query = person::table
|
|
|
|
.inner_join(person_aggregates::table)
|
2023-03-01 17:19:46 +00:00
|
|
|
.select((person::all_columns, person_aggregates::all_columns))
|
2021-03-10 22:33:55 +00:00
|
|
|
.into_boxed();
|
|
|
|
|
|
|
|
if let Some(search_term) = self.search_term {
|
2022-09-07 08:58:41 +00:00
|
|
|
let searcher = fuzzy_search(&search_term);
|
|
|
|
query = query
|
2022-11-19 04:33:54 +00:00
|
|
|
.filter(person::name.ilike(searcher.clone()))
|
2022-09-07 08:58:41 +00:00
|
|
|
.or_filter(person::display_name.ilike(searcher));
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 17:07:05 +00:00
|
|
|
query = match self.sort.unwrap_or(PersonSortType::CommentScore) {
|
|
|
|
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()),
|
2021-03-10 22:33:55 +00:00
|
|
|
};
|
|
|
|
|
2022-07-08 10:21:33 +00:00
|
|
|
let (limit, offset) = limit_and_offset(self.page, self.limit)?;
|
2021-03-10 22:33:55 +00:00
|
|
|
query = query.limit(limit).offset(offset);
|
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
let res = query.load::<PersonViewTuple>(conn).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
Ok(res.into_iter().map(PersonView::from_tuple).collect())
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:19:46 +00:00
|
|
|
impl JoinView for PersonView {
|
|
|
|
type JoinTuple = PersonViewTuple;
|
|
|
|
fn from_tuple(a: Self::JoinTuple) -> Self {
|
|
|
|
Self {
|
|
|
|
person: a.0,
|
|
|
|
counts: a.1,
|
|
|
|
}
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
}
|