lemmy/crates/db_views_actor/src/person_view.rs

142 lines
4.5 KiB
Rust
Raw Normal View History

use crate::structs::PersonView;
2022-11-09 10:05:00 +00:00
use diesel::{
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::{
aggregates::structs::PersonAggregates,
2021-10-16 13:33:38 +00:00
newtypes::PersonId,
schema::{person, person_aggregates},
source::person::Person,
traits::JoinView,
2023-06-30 03:41:09 +00:00
utils::{fuzzy_search, limit_and_offset, DbConn},
SortType,
2021-03-10 22:33:55 +00:00
};
2022-11-09 10:05:00 +00:00
use std::iter::Iterator;
use typed_builder::TypedBuilder;
2021-03-10 22:33:55 +00:00
type PersonViewTuple = (Person, PersonAggregates);
2021-03-10 22:33:55 +00:00
impl PersonView {
2023-06-30 03:41:09 +00:00
pub async fn read(conn: &mut DbConn, person_id: PersonId) -> Result<Self, Error> {
let res = person::table
.find(person_id)
2021-03-10 22:33:55 +00:00
.inner_join(person_aggregates::table)
.select((person::all_columns, person_aggregates::all_columns))
.first::<PersonViewTuple>(conn)
2022-11-09 10:05:00 +00:00
.await?;
Ok(Self::from_tuple(res))
2021-03-10 22:33:55 +00:00
}
2023-06-30 03:41:09 +00:00
pub async fn admins(conn: &mut DbConn) -> Result<Vec<Self>, Error> {
2021-03-10 22:33:55 +00:00
let admins = person::table
.inner_join(person_aggregates::table)
.select((person::all_columns, person_aggregates::all_columns))
.filter(person::admin.eq(true))
.filter(person::deleted.eq(false))
2021-03-10 22:33:55 +00:00
.order_by(person::published)
.load::<PersonViewTuple>(conn)
2022-11-09 10:05:00 +00:00
.await?;
2021-03-10 22:33:55 +00:00
Ok(admins.into_iter().map(Self::from_tuple).collect())
2021-03-10 22:33:55 +00:00
}
2023-06-30 03:41:09 +00:00
pub async fn banned(conn: &mut DbConn) -> Result<Vec<Self>, Error> {
2021-03-10 22:33:55 +00:00
let banned = person::table
.inner_join(person_aggregates::table)
.select((person::all_columns, person_aggregates::all_columns))
.filter(
person::banned.eq(true).and(
person::ban_expires
.is_null()
.or(person::ban_expires.gt(now)),
),
)
.filter(person::deleted.eq(false))
.load::<PersonViewTuple>(conn)
2022-11-09 10:05:00 +00:00
.await?;
2021-03-10 22:33:55 +00:00
Ok(banned.into_iter().map(Self::from_tuple).collect())
2021-03-10 22:33:55 +00:00
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct PersonQuery<'a> {
#[builder(!default)]
2023-06-30 03:41:09 +00:00
conn: &'a mut DbConn,
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> {
pub async fn list(self) -> Result<Vec<PersonView>, Error> {
2023-06-30 03:41:09 +00:00
let conn = self.conn;
2021-03-10 22:33:55 +00:00
let mut query = person::table
.inner_join(person_aggregates::table)
.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 {
let searcher = fuzzy_search(&search_term);
query = query
.filter(person::name.ilike(searcher.clone()))
.or_filter(person::display_name.ilike(searcher));
2021-03-10 22:33:55 +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()),
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()),
SortType::TopHour => query
.filter(person::published.gt(now - 1.hours()))
.order_by(person_aggregates::comment_score.desc()),
SortType::TopSixHour => query
.filter(person::published.gt(now - 6.hours()))
.order_by(person_aggregates::comment_score.desc()),
SortType::TopTwelveHour => query
.filter(person::published.gt(now - 12.hours()))
.order_by(person_aggregates::comment_score.desc()),
2021-03-10 22:33:55 +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);
let res = query.load::<PersonViewTuple>(conn).await?;
2021-03-10 22:33:55 +00:00
Ok(res.into_iter().map(PersonView::from_tuple).collect())
2021-03-10 22:33:55 +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
}
}