]> Untitled Git - lemmy.git/blobdiff - crates/db_views/src/post_report_view.rs
Cache & Optimize Woodpecker CI (#3450)
[lemmy.git] / crates / db_views / src / post_report_view.rs
index 19822c795ae426bb8b4754d358f58730a84977d3..50b35b1c258bbce2db25ac04c501937c7af90371 100644 (file)
@@ -30,7 +30,6 @@ use lemmy_db_schema::{
   traits::JoinView,
   utils::{get_conn, limit_and_offset, DbPool},
 };
-use typed_builder::TypedBuilder;
 
 type PostReportViewTuple = (
   PostReport,
@@ -49,7 +48,7 @@ impl PostReportView {
   ///
   /// * `report_id` - the report id to obtain
   pub async fn read(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     report_id: PostReportId,
     my_person_id: PersonId,
   ) -> Result<Self, Error> {
@@ -121,7 +120,7 @@ impl PostReportView {
 
   /// returns the current unresolved post report count for the communities you mod
   pub async fn get_report_count(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     my_person_id: PersonId,
     admin: bool,
     community_id: Option<CommunityId>,
@@ -159,24 +158,21 @@ impl PostReportView {
   }
 }
 
-#[derive(TypedBuilder)]
-#[builder(field_defaults(default))]
-pub struct PostReportQuery<'a> {
-  #[builder(!default)]
-  pool: &'a DbPool,
-  #[builder(!default)]
-  my_person_id: PersonId,
-  #[builder(!default)]
-  admin: bool,
-  community_id: Option<CommunityId>,
-  page: Option<i64>,
-  limit: Option<i64>,
-  unresolved_only: Option<bool>,
+#[derive(Default)]
+pub struct PostReportQuery {
+  pub community_id: Option<CommunityId>,
+  pub page: Option<i64>,
+  pub limit: Option<i64>,
+  pub unresolved_only: Option<bool>,
 }
 
-impl<'a> PostReportQuery<'a> {
-  pub async fn list(self) -> Result<Vec<PostReportView>, Error> {
-    let conn = &mut get_conn(self.pool).await?;
+impl PostReportQuery {
+  pub async fn list(
+    self,
+    pool: &mut DbPool<'_>,
+    my_person: &Person,
+  ) -> Result<Vec<PostReportView>, Error> {
+    let conn = &mut get_conn(pool).await?;
     let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
 
     let mut query = post_report::table
@@ -195,7 +191,7 @@ impl<'a> PostReportQuery<'a> {
         post_like::table.on(
           post::id
             .eq(post_like::post_id)
-            .and(post_like::person_id.eq(self.my_person_id)),
+            .and(post_like::person_id.eq(my_person.id)),
         ),
       )
       .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
@@ -231,13 +227,13 @@ impl<'a> PostReportQuery<'a> {
       .offset(offset);
 
     // If its not an admin, get only the ones you mod
-    let res = if !self.admin {
+    let res = if !my_person.admin {
       query
         .inner_join(
           community_moderator::table.on(
             community_moderator::community_id
               .eq(post::community_id)
-              .and(community_moderator::person_id.eq(self.my_person_id)),
+              .and(community_moderator::person_id.eq(my_person.id)),
           ),
         )
         .load::<PostReportViewTuple>(conn)
@@ -269,6 +265,9 @@ impl JoinView for PostReportView {
 
 #[cfg(test)]
 mod tests {
+  #![allow(clippy::unwrap_used)]
+  #![allow(clippy::indexing_slicing)]
+
   use crate::post_report_view::{PostReportQuery, PostReportView};
   use lemmy_db_schema::{
     aggregates::structs::PostAggregates,
@@ -288,6 +287,7 @@ mod tests {
   #[serial]
   async fn test_crud() {
     let pool = &build_db_pool_for_tests().await;
+    let pool = &mut pool.into();
 
     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
       .await
@@ -505,12 +505,8 @@ mod tests {
     };
 
     // Do a batch read of timmys reports
-    let reports = PostReportQuery::builder()
-      .pool(pool)
-      .my_person_id(inserted_timmy.id)
-      .admin(false)
-      .build()
-      .list()
+    let reports = PostReportQuery::default()
+      .list(pool, &inserted_timmy)
       .await
       .unwrap();
 
@@ -579,15 +575,13 @@ mod tests {
 
     // Do a batch read of timmys reports
     // It should only show saras, which is unresolved
-    let reports_after_resolve = PostReportQuery::builder()
-      .pool(pool)
-      .my_person_id(inserted_timmy.id)
-      .admin(false)
-      .unresolved_only(Some(true))
-      .build()
-      .list()
-      .await
-      .unwrap();
+    let reports_after_resolve = PostReportQuery {
+      unresolved_only: (Some(true)),
+      ..Default::default()
+    }
+    .list(pool, &inserted_timmy)
+    .await
+    .unwrap();
     assert_eq!(reports_after_resolve[0], expected_sara_report_view);
 
     // Make sure the counts are correct