1 use crate::structs::PersonView;
8 PgTextExpressionMethods,
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
13 aggregates::structs::PersonAggregates,
16 schema::{person, person_aggregates},
17 source::person::Person,
19 utils::{fuzzy_search, get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
23 type PersonViewTuple = (Person, PersonAggregates);
32 ) -> Queries<impl ReadFn<'a, PersonView, PersonId>, impl ListFn<'a, PersonView, ListMode>> {
33 let all_joins = |query: person::BoxedQuery<'a, Pg>| {
35 .inner_join(person_aggregates::table)
36 .select((person::all_columns, person_aggregates::all_columns))
39 let read = move |mut conn: DbConn<'a>, person_id: PersonId| async move {
40 all_joins(person::table.find(person_id).into_boxed())
41 .first::<PersonViewTuple>(&mut conn)
45 let list = move |mut conn: DbConn<'a>, mode: ListMode| async move {
46 let mut query = all_joins(person::table.into_boxed());
50 .filter(person::admin.eq(true))
51 .filter(person::deleted.eq(false))
52 .order_by(person::published);
57 person::banned.eq(true).and(
60 .or(person::ban_expires.gt(now)),
63 .filter(person::deleted.eq(false));
65 ListMode::Query(options) => {
66 if let Some(search_term) = options.search_term {
67 let searcher = fuzzy_search(&search_term);
69 .filter(person::name.ilike(searcher.clone()))
70 .or_filter(person::display_name.ilike(searcher));
73 query = match options.sort.unwrap_or(PersonSortType::CommentScore) {
74 PersonSortType::New => query.order_by(person::published.desc()),
75 PersonSortType::Old => query.order_by(person::published.asc()),
76 PersonSortType::MostComments => query.order_by(person_aggregates::comment_count.desc()),
77 PersonSortType::CommentScore => query.order_by(person_aggregates::comment_score.desc()),
78 PersonSortType::PostScore => query.order_by(person_aggregates::post_score.desc()),
79 PersonSortType::PostCount => query.order_by(person_aggregates::post_count.desc()),
82 let (limit, offset) = limit_and_offset(options.page, options.limit)?;
83 query = query.limit(limit).offset(offset);
86 query.load::<PersonViewTuple>(&mut conn).await
89 Queries::new(read, list)
93 pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
94 queries().read(pool, person_id).await
97 pub async fn is_admin(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<bool, Error> {
98 use schema::person::dsl::{admin, id, person};
99 let conn = &mut get_conn(pool).await?;
100 let is_admin = person
101 .filter(id.eq(person_id))
108 pub async fn admins(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
109 queries().list(pool, ListMode::Admins).await
112 pub async fn banned(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
113 queries().list(pool, ListMode::Banned).await
118 pub struct PersonQuery {
119 pub sort: Option<PersonSortType>,
120 pub search_term: Option<String>,
121 pub page: Option<i64>,
122 pub limit: Option<i64>,
126 pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PersonView>, Error> {
127 queries().list(pool, ListMode::Query(self)).await
131 impl JoinView for PersonView {
132 type JoinTuple = PersonViewTuple;
133 fn from_tuple(a: Self::JoinTuple) -> Self {