2022-05-03 17:44:13 +00:00
|
|
|
use crate::structs::PersonViewSafe;
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel::{
|
2022-11-19 04:33:54 +00:00
|
|
|
dsl::{now, IntervalDsl},
|
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,
|
2021-03-22 14:28:00 +00:00
|
|
|
schema::{person, person_aggregates},
|
2021-03-11 04:43:11 +00:00
|
|
|
source::person::{Person, PersonSafe},
|
2022-08-04 19:30:17 +00:00
|
|
|
traits::{ToSafe, ViewToVec},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{fuzzy_search, get_conn, limit_and_offset, DbPool},
|
2022-05-06 20:55:07 +00:00
|
|
|
SortType,
|
2021-03-10 22:33:55 +00:00
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use std::iter::Iterator;
|
2022-08-04 19:30:17 +00:00
|
|
|
use typed_builder::TypedBuilder;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
|
|
|
type PersonViewSafeTuple = (PersonSafe, PersonAggregates);
|
|
|
|
|
|
|
|
impl PersonViewSafe {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn read(pool: &DbPool, person_id: PersonId) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
let (person, counts) = 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)
|
|
|
|
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
|
2022-11-09 10:05:00 +00:00
|
|
|
.first::<PersonViewSafeTuple>(conn)
|
|
|
|
.await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
Ok(Self { person, counts })
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn admins(pool: &DbPool) -> Result<Vec<Self>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
let admins = person::table
|
|
|
|
.inner_join(person_aggregates::table)
|
|
|
|
.select((Person::safe_columns_tuple(), 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)
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<PersonViewSafeTuple>(conn)
|
|
|
|
.await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
|
|
|
Ok(Self::from_tuple_to_vec(admins))
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn banned(pool: &DbPool) -> Result<Vec<Self>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
let banned = person::table
|
|
|
|
.inner_join(person_aggregates::table)
|
|
|
|
.select((Person::safe_columns_tuple(), 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))
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<PersonViewSafeTuple>(conn)
|
|
|
|
.await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
|
|
|
Ok(Self::from_tuple_to_vec(banned))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:30:17 +00:00
|
|
|
#[derive(TypedBuilder)]
|
|
|
|
#[builder(field_defaults(default))]
|
|
|
|
pub struct PersonQuery<'a> {
|
|
|
|
#[builder(!default)]
|
2022-11-09 10:05:00 +00:00
|
|
|
pool: &'a DbPool,
|
2021-04-15 03:37:51 +00:00
|
|
|
sort: Option<SortType>,
|
2021-03-10 22:33:55 +00:00
|
|
|
search_term: Option<String>,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:30:17 +00:00
|
|
|
impl<'a> PersonQuery<'a> {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn list(self) -> Result<Vec<PersonViewSafe>, Error> {
|
|
|
|
let conn = &mut get_conn(self.pool).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
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 {
|
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
|
|
|
}
|
|
|
|
|
2021-04-15 03:37:51 +00:00
|
|
|
query = match self.sort.unwrap_or(SortType::Hot) {
|
2023-02-24 04:33:54 +00:00
|
|
|
SortType::New | SortType::NewComments => query.order_by(person::published.desc()),
|
2022-07-30 03:55:59 +00:00
|
|
|
SortType::Old => query.order_by(person::published.asc()),
|
2023-02-24 04:33:54 +00:00
|
|
|
SortType::Hot | SortType::Active | SortType::TopAll => {
|
|
|
|
query.order_by(person_aggregates::comment_score.desc())
|
|
|
|
}
|
|
|
|
SortType::MostComments => query.order_by(person_aggregates::comment_count.desc()),
|
2021-03-10 22:33:55 +00:00
|
|
|
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()),
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let res = query.load::<PersonViewSafeTuple>(conn).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
|
|
|
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
|
2022-08-04 19:30:17 +00:00
|
|
|
.into_iter()
|
2021-03-10 22:33:55 +00:00
|
|
|
.map(|a| Self {
|
2022-08-04 19:30:17 +00:00
|
|
|
person: a.0,
|
|
|
|
counts: a.1,
|
2021-03-10 22:33:55 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<Self>>()
|
|
|
|
}
|
|
|
|
}
|