]> Untitled Git - lemmy.git/blobdiff - crates/db_views_actor/src/person_view.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_views_actor / src / person_view.rs
index 307f02484c08490c0860249d7e003ccead398b11..af0927a8519f0ae503ef1c180109ce4ffd57dae7 100644 (file)
@@ -5,15 +5,16 @@ use lemmy_db_schema::{
   newtypes::PersonId,
   schema::{person, person_aggregates},
   source::person::{Person, PersonSafe},
-  traits::{MaybeOptional, ToSafe, ViewToVec},
+  traits::{ToSafe, ViewToVec},
   utils::{fuzzy_search, limit_and_offset},
   SortType,
 };
+use typed_builder::TypedBuilder;
 
 type PersonViewSafeTuple = (PersonSafe, PersonAggregates);
 
 impl PersonViewSafe {
-  pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
+  pub fn read(conn: &mut PgConnection, person_id: PersonId) -> Result<Self, Error> {
     let (person, counts) = person::table
       .find(person_id)
       .inner_join(person_aggregates::table)
@@ -22,7 +23,7 @@ impl PersonViewSafe {
     Ok(Self { person, counts })
   }
 
-  pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
+  pub fn admins(conn: &mut PgConnection) -> Result<Vec<Self>, Error> {
     let admins = person::table
       .inner_join(person_aggregates::table)
       .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
@@ -33,7 +34,7 @@ impl PersonViewSafe {
     Ok(Self::from_tuple_to_vec(admins))
   }
 
-  pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
+  pub fn banned(conn: &mut PgConnection) -> Result<Vec<Self>, Error> {
     let banned = person::table
       .inner_join(person_aggregates::table)
       .select((Person::safe_columns_tuple(), person_aggregates::all_columns))
@@ -50,45 +51,18 @@ impl PersonViewSafe {
   }
 }
 
-pub struct PersonQueryBuilder<'a> {
-  conn: &'a PgConnection,
+#[derive(TypedBuilder)]
+#[builder(field_defaults(default))]
+pub struct PersonQuery<'a> {
+  #[builder(!default)]
+  conn: &'a mut PgConnection,
   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
-  }
-
+impl<'a> PersonQuery<'a> {
   pub fn list(self) -> Result<Vec<PersonViewSafe>, Error> {
     let mut query = person::table
       .inner_join(person_aggregates::table)
@@ -96,7 +70,10 @@ impl<'a> PersonQueryBuilder<'a> {
       .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.to_owned()))
+        .or_filter(person::display_name.ilike(searcher));
     }
 
     query = match self.sort.unwrap_or(SortType::Hot) {
@@ -109,6 +86,7 @@ impl<'a> PersonQueryBuilder<'a> {
       SortType::New | SortType::MostComments | SortType::NewComments => {
         query.order_by(person::published.desc())
       }
+      SortType::Old => query.order_by(person::published.asc()),
       SortType::TopAll => query.order_by(person_aggregates::comment_score.desc()),
       SortType::TopYear => query
         .filter(person::published.gt(now - 1.years()))
@@ -137,10 +115,10 @@ impl ViewToVec for PersonViewSafe {
   type DbTuple = PersonViewSafeTuple;
   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
     items
-      .iter()
+      .into_iter()
       .map(|a| Self {
-        person: a.0.to_owned(),
-        counts: a.1.to_owned(),
+        person: a.0,
+        counts: a.1,
       })
       .collect::<Vec<Self>>()
   }