]> 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 43f99dfd42da4e04d3a10c6c955ad5c992617693..3aee145c9dada5ed84cece62b6e1d7c83c40afa7 100644 (file)
@@ -11,6 +11,7 @@ use diesel_async::RunQueryDsl;
 use lemmy_db_schema::{
   aggregates::structs::PersonAggregates,
   newtypes::PersonId,
+  schema,
   schema::{person, person_aggregates},
   source::person::Person,
   traits::JoinView,
@@ -23,7 +24,7 @@ use typed_builder::TypedBuilder;
 type PersonViewTuple = (Person, PersonAggregates);
 
 impl PersonView {
-  pub async fn read(pool: &DbPool, person_id: PersonId) -> Result<Self, Error> {
+  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)
@@ -34,7 +35,17 @@ impl PersonView {
     Ok(Self::from_tuple(res))
   }
 
-  pub async fn admins(pool: &DbPool) -> 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)
@@ -48,7 +59,7 @@ impl PersonView {
     Ok(admins.into_iter().map(Self::from_tuple).collect())
   }
 
-  pub async fn banned(pool: &DbPool) -> 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)
@@ -70,16 +81,16 @@ impl PersonView {
 
 #[derive(TypedBuilder)]
 #[builder(field_defaults(default))]
-pub struct PersonQuery<'a> {
+pub struct PersonQuery<'a, 'b: 'a> {
   #[builder(!default)]
-  pool: &'a DbPool,
+  pool: &'a mut DbPool<'b>,
   sort: Option<SortType>,
   search_term: Option<String>,
   page: Option<i64>,
   limit: Option<i64>,
 }
 
-impl<'a> PersonQuery<'a> {
+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
@@ -113,6 +124,24 @@ impl<'a> PersonQuery<'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)?;