lemmy/crates/db_views_actor/src/person_view.rs

126 lines
4 KiB
Rust
Raw Normal View History

use crate::structs::PersonViewSafe;
2021-03-10 22:33:55 +00:00
use diesel::{dsl::*, result::Error, *};
2021-10-16 13:33:38 +00:00
use lemmy_db_schema::{
aggregates::structs::PersonAggregates,
2021-10-16 13:33:38 +00:00
newtypes::PersonId,
schema::{person, person_aggregates},
2021-03-11 04:43:11 +00:00
source::person::{Person, PersonSafe},
traits::{ToSafe, ViewToVec},
utils::{fuzzy_search, limit_and_offset},
SortType,
2021-03-10 22:33:55 +00:00
};
use typed_builder::TypedBuilder;
2021-03-10 22:33:55 +00:00
type PersonViewSafeTuple = (PersonSafe, PersonAggregates);
impl PersonViewSafe {
pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
2021-03-10 22:33:55 +00:00
let (person, counts) = person::table
.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))
.first::<PersonViewSafeTuple>(conn)?;
Ok(Self { person, counts })
}
pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
let admins = person::table
.inner_join(person_aggregates::table)
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
.filter(person::admin.eq(true))
2021-03-10 22:33:55 +00:00
.order_by(person::published)
.load::<PersonViewSafeTuple>(conn)?;
Ok(Self::from_tuple_to_vec(admins))
}
pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
let banned = person::table
.inner_join(person_aggregates::table)
.select((Person::safe_columns_tuple(), person_aggregates::all_columns))
.filter(
person::banned.eq(true).and(
person::ban_expires
.is_null()
.or(person::ban_expires.gt(now)),
),
)
2021-03-10 22:33:55 +00:00
.load::<PersonViewSafeTuple>(conn)?;
Ok(Self::from_tuple_to_vec(banned))
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct PersonQuery<'a> {
#[builder(!default)]
2021-03-10 22:33:55 +00:00
conn: &'a PgConnection,
sort: Option<SortType>,
2021-03-10 22:33:55 +00:00
search_term: Option<String>,
page: Option<i64>,
limit: Option<i64>,
}
impl<'a> PersonQuery<'a> {
2021-03-10 22:33:55 +00:00
pub fn list(self) -> Result<Vec<PersonViewSafe>, Error> {
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 {
let searcher = fuzzy_search(&search_term);
query = query
.filter(person::name.ilike(searcher.to_owned()))
.or_filter(person::display_name.ilike(searcher));
2021-03-10 22:33:55 +00:00
}
query = match self.sort.unwrap_or(SortType::Hot) {
2021-03-10 22:33:55 +00:00
SortType::Hot => query
.order_by(person_aggregates::comment_score.desc())
.then_order_by(person::published.desc()),
SortType::Active => query
.order_by(person_aggregates::comment_score.desc())
.then_order_by(person::published.desc()),
SortType::New | SortType::MostComments | SortType::NewComments => {
query.order_by(person::published.desc())
}
SortType::Old => query.order_by(person::published.asc()),
2021-03-10 22:33:55 +00:00
SortType::TopAll => query.order_by(person_aggregates::comment_score.desc()),
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()),
};
let (limit, offset) = limit_and_offset(self.page, self.limit)?;
2021-03-10 22:33:55 +00:00
query = query.limit(limit).offset(offset);
let res = query.load::<PersonViewSafeTuple>(self.conn)?;
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
.into_iter()
2021-03-10 22:33:55 +00:00
.map(|a| Self {
person: a.0,
counts: a.1,
2021-03-10 22:33:55 +00:00
})
.collect::<Vec<Self>>()
}
}