]> Untitled Git - lemmy.git/blobdiff - crates/db_views_actor/src/person_view.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / db_views_actor / src / person_view.rs
index 307f02484c08490c0860249d7e003ccead398b11..3aee145c9dada5ed84cece62b6e1d7c83c40afa7 100644 (file)
@@ -1,42 +1,69 @@
-use crate::structs::PersonViewSafe;
-use diesel::{dsl::*, result::Error, *};
+use crate::structs::PersonView;
+use diesel::{
+  dsl::{now, IntervalDsl},
+  result::Error,
+  BoolExpressionMethods,
+  ExpressionMethods,
+  PgTextExpressionMethods,
+  QueryDsl,
+};
+use diesel_async::RunQueryDsl;
 use lemmy_db_schema::{
   aggregates::structs::PersonAggregates,
   newtypes::PersonId,
+  schema,
   schema::{person, person_aggregates},
-  source::person::{Person, PersonSafe},
-  traits::{MaybeOptional, ToSafe, ViewToVec},
-  utils::{fuzzy_search, limit_and_offset},
+  source::person::Person,
+  traits::JoinView,
+  utils::{fuzzy_search, get_conn, limit_and_offset, DbPool},
   SortType,
 };
+use std::iter::Iterator;
+use typed_builder::TypedBuilder;
 
-type PersonViewSafeTuple = (PersonSafe, PersonAggregates);
+type PersonViewTuple = (Person, PersonAggregates);
 
-impl PersonViewSafe {
-  pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
-    let (person, counts) = person::table
+impl PersonView {
+  pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
+    let conn = &mut get_conn(pool).await?;
+    let res = person::table
       .find(person_id)
       .inner_join(person_aggregates::table)
-      .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
-      .first::<PersonViewSafeTuple>(conn)?;
-    Ok(Self { person, counts })
+      .select((person::all_columns, person_aggregates::all_columns))
+      .first::<PersonViewTuple>(conn)
+      .await?;
+    Ok(Self::from_tuple(res))
   }
 
-  pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
+  pub async fn is_admin(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<bool, Error> {
+    use schema::person::dsl::{admin, id, person};
+    let conn = &mut get_conn(pool).await?;
+    let is_admin = person
+      .filter(id.eq(person_id))
+      .select(admin)
+      .first::<bool>(conn)
+      .await?;
+    Ok(is_admin)
+  }
+  pub async fn admins(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
+    let conn = &mut get_conn(pool).await?;
     let admins = person::table
       .inner_join(person_aggregates::table)
-      .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
+      .select((person::all_columns, person_aggregates::all_columns))
       .filter(person::admin.eq(true))
+      .filter(person::deleted.eq(false))
       .order_by(person::published)
-      .load::<PersonViewSafeTuple>(conn)?;
+      .load::<PersonViewTuple>(conn)
+      .await?;
 
-    Ok(Self::from_tuple_to_vec(admins))
+    Ok(admins.into_iter().map(Self::from_tuple).collect())
   }
 
-  pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
+  pub async fn banned(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
+    let conn = &mut get_conn(pool).await?;
     let banned = person::table
       .inner_join(person_aggregates::table)
-      .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
+      .select((person::all_columns, person_aggregates::all_columns))
       .filter(
         person::banned.eq(true).and(
           person::ban_expires
@@ -44,72 +71,47 @@ impl PersonViewSafe {
             .or(person::ban_expires.gt(now)),
         ),
       )
-      .load::<PersonViewSafeTuple>(conn)?;
+      .filter(person::deleted.eq(false))
+      .load::<PersonViewTuple>(conn)
+      .await?;
 
-    Ok(Self::from_tuple_to_vec(banned))
+    Ok(banned.into_iter().map(Self::from_tuple).collect())
   }
 }
 
-pub struct PersonQueryBuilder<'a> {
-  conn: &'a PgConnection,
+#[derive(TypedBuilder)]
+#[builder(field_defaults(default))]
+pub struct PersonQuery<'a, 'b: 'a> {
+  #[builder(!default)]
+  pool: &'a mut DbPool<'b>,
   sort: Option<SortType>,
   search_term: Option<String>,
   page: Option<i64>,
   limit: Option<i64>,
 }
 
-impl<'a> PersonQueryBuilder<'a> {
-  pub fn create(conn: &'a PgConnection) -> Self {
-    PersonQueryBuilder {
-      conn,
-      search_term: None,
-      sort: None,
-      page: None,
-      limit: None,
-    }
-  }
-
-  pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
-    self.sort = sort.get_optional();
-    self
-  }
-
-  pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
-    self.search_term = search_term.get_optional();
-    self
-  }
-
-  pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
-    self.page = page.get_optional();
-    self
-  }
-
-  pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
-    self.limit = limit.get_optional();
-    self
-  }
-
-  pub fn list(self) -> Result<Vec<PersonViewSafe>, Error> {
+impl<'a, 'b: 'a> PersonQuery<'a, 'b> {
+  pub async fn list(self) -> Result<Vec<PersonView>, Error> {
+    let conn = &mut get_conn(self.pool).await?;
     let mut query = person::table
       .inner_join(person_aggregates::table)
-      .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
+      .select((person::all_columns, person_aggregates::all_columns))
       .into_boxed();
 
     if let Some(search_term) = self.search_term {
-      query = query.filter(person::name.ilike(fuzzy_search(&search_term)));
+      let searcher = fuzzy_search(&search_term);
+      query = query
+        .filter(person::name.ilike(searcher.clone()))
+        .or_filter(person::display_name.ilike(searcher));
     }
 
     query = match self.sort.unwrap_or(SortType::Hot) {
-      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::New | SortType::NewComments => query.order_by(person::published.desc()),
+      SortType::Old => query.order_by(person::published.asc()),
+      SortType::Hot | SortType::Active | SortType::TopAll => {
+        query.order_by(person_aggregates::comment_score.desc())
       }
-      SortType::TopAll => query.order_by(person_aggregates::comment_score.desc()),
+      SortType::MostComments => query.order_by(person_aggregates::comment_count.desc()),
       SortType::TopYear => query
         .filter(person::published.gt(now - 1.years()))
         .order_by(person_aggregates::comment_score.desc()),
@@ -122,26 +124,41 @@ impl<'a> PersonQueryBuilder<'a> {
       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()),
+      SortType::TopThreeMonths => query
+        .filter(person::published.gt(now - 3.months()))
+        .order_by(person_aggregates::comment_score.desc()),
+      SortType::TopSixMonths => query
+        .filter(person::published.gt(now - 6.months()))
+        .order_by(person_aggregates::comment_score.desc()),
+      SortType::TopNineMonths => query
+        .filter(person::published.gt(now - 9.months()))
+        .order_by(person_aggregates::comment_score.desc()),
     };
 
     let (limit, offset) = limit_and_offset(self.page, self.limit)?;
     query = query.limit(limit).offset(offset);
 
-    let res = query.load::<PersonViewSafeTuple>(self.conn)?;
+    let res = query.load::<PersonViewTuple>(conn).await?;
 
-    Ok(PersonViewSafe::from_tuple_to_vec(res))
+    Ok(res.into_iter().map(PersonView::from_tuple).collect())
   }
 }
 
-impl ViewToVec for PersonViewSafe {
-  type DbTuple = PersonViewSafeTuple;
-  fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
-    items
-      .iter()
-      .map(|a| Self {
-        person: a.0.to_owned(),
-        counts: a.1.to_owned(),
-      })
-      .collect::<Vec<Self>>()
+impl JoinView for PersonView {
+  type JoinTuple = PersonViewTuple;
+  fn from_tuple(a: Self::JoinTuple) -> Self {
+    Self {
+      person: a.0,
+      counts: a.1,
+    }
   }
 }