use diesel::{dsl::*, result::Error, *};
-use lemmy_db_queries::{
+use lemmy_db_schema::{
aggregates::person_aggregates::PersonAggregates,
fuzzy_search,
limit_and_offset,
- MaybeOptional,
+ newtypes::PersonId,
+ schema::{person, person_aggregates},
+ source::person::{Person, PersonSafe},
+ traits::{MaybeOptional, ToSafe, ViewToVec},
SortType,
- ToSafe,
- ViewToVec,
-};
-use lemmy_db_schema::{
- schema::{person, person_aggregates, local_user},
- source::person::{PersonSafe, Person},
};
-use serde::Serialize;
+use serde::{Deserialize, Serialize};
-#[derive(Debug, Serialize, Clone)]
+#[derive(Debug, Serialize, Deserialize, 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> {
+ pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
let (person, counts) = person::table
- .find(id)
+ .find(person_id)
.inner_join(person_aggregates::table)
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
.first::<PersonViewSafeTuple>(conn)?;
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))
+ .filter(person::admin.eq(true))
.order_by(person::published)
.load::<PersonViewSafeTuple>(conn)?;
let banned = person::table
.inner_join(person_aggregates::table)
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
- .filter(person::banned.eq(true))
+ .filter(
+ person::banned.eq(true).and(
+ person::ban_expires
+ .is_null()
+ .or(person::ban_expires.gt(now)),
+ ),
+ )
.load::<PersonViewSafeTuple>(conn)?;
Ok(Self::from_tuple_to_vec(banned))
pub struct PersonQueryBuilder<'a> {
conn: &'a PgConnection,
- sort: &'a SortType,
+ sort: Option<SortType>,
search_term: Option<String>,
page: Option<i64>,
limit: Option<i64>,
PersonQueryBuilder {
conn,
search_term: None,
- sort: &SortType::Hot,
+ sort: None,
page: None,
limit: None,
}
}
- pub fn sort(mut self, sort: &'a SortType) -> Self {
- self.sort = sort;
+ pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
+ self.sort = sort.get_optional();
self
}
query = query.filter(person::name.ilike(fuzzy_search(&search_term)));
}
- query = match self.sort {
+ query = match self.sort.unwrap_or(SortType::Hot) {
SortType::Hot => query
.order_by(person_aggregates::comment_score.desc())
.then_order_by(person::published.desc()),