]> Untitled Git - lemmy.git/blobdiff - crates/db_views/src/post_report_view.rs
Adding temporary bans. Fixes #1423 (#1999)
[lemmy.git] / crates / db_views / src / post_report_view.rs
index a8604ea9cdf3ce3b3f65b4df1917b74b13a4a240..e7ba0308a2ad1583ed9e5cbdf9f54cb9fe51823f 100644 (file)
@@ -1,12 +1,8 @@
-use diesel::{result::Error, *};
-use lemmy_db_queries::{
+use diesel::{dsl::*, result::Error, *};
+use lemmy_db_schema::{
   aggregates::post_aggregates::PostAggregates,
   limit_and_offset,
-  MaybeOptional,
-  ToSafe,
-  ViewToVec,
-};
-use lemmy_db_schema::{
+  newtypes::{CommunityId, PersonId, PostReportId},
   schema::{
     community,
     community_moderator,
@@ -25,9 +21,7 @@ use lemmy_db_schema::{
     post::Post,
     post_report::PostReport,
   },
-  CommunityId,
-  PersonId,
-  PostReportId,
+  traits::{MaybeOptional, ToSafe, ViewToVec},
 };
 use serde::{Deserialize, Serialize};
 
@@ -85,7 +79,12 @@ impl PostReportView {
         community_person_ban::table.on(
           post::community_id
             .eq(community_person_ban::community_id)
-            .and(community_person_ban::person_id.eq(post::creator_id)),
+            .and(community_person_ban::person_id.eq(post::creator_id))
+            .and(
+              community_person_ban::expires
+                .is_null()
+                .or(community_person_ban::expires.gt(now)),
+            ),
         ),
       )
       .left_join(
@@ -137,23 +136,28 @@ impl PostReportView {
     use diesel::dsl::*;
     let mut query = post_report::table
       .inner_join(post::table)
-      // Test this join
-      .inner_join(
-        community_moderator::table.on(community_moderator::community_id.eq(post::community_id)),
-      )
       .filter(post_report::resolved.eq(false))
       .into_boxed();
 
-    // If its not an admin, get only the ones you mod
-    if !admin {
-      query = query.filter(community_moderator::person_id.eq(my_person_id));
-    }
-
     if let Some(community_id) = community_id {
       query = query.filter(post::community_id.eq(community_id))
     }
 
-    query.select(count(post_report::id)).first::<i64>(conn)
+    // If its not an admin, get only the ones you mod
+    if !admin {
+      query
+        .inner_join(
+          community_moderator::table.on(
+            community_moderator::community_id
+              .eq(post::community_id)
+              .and(community_moderator::person_id.eq(my_person_id)),
+          ),
+        )
+        .select(count(post_report::id))
+        .first::<i64>(conn)
+    } else {
+      query.select(count(post_report::id)).first::<i64>(conn)
+    }
   }
 }
 
@@ -206,14 +210,16 @@ impl<'a> PostReportQueryBuilder<'a> {
       .inner_join(community::table.on(post::community_id.eq(community::id)))
       .inner_join(person::table.on(post_report::creator_id.eq(person::id)))
       .inner_join(person_alias_1::table.on(post::creator_id.eq(person_alias_1::id)))
-      .inner_join(
-        community_moderator::table.on(community_moderator::community_id.eq(post::community_id)),
-      )
       .left_join(
         community_person_ban::table.on(
           post::community_id
             .eq(community_person_ban::community_id)
-            .and(community_person_ban::person_id.eq(post::creator_id)),
+            .and(community_person_ban::person_id.eq(post::creator_id))
+            .and(
+              community_person_ban::expires
+                .is_null()
+                .or(community_person_ban::expires.gt(now)),
+            ),
         ),
       )
       .left_join(
@@ -240,11 +246,6 @@ impl<'a> PostReportQueryBuilder<'a> {
       ))
       .into_boxed();
 
-    // If its not an admin, get only the ones you mod
-    if !self.admin {
-      query = query.filter(community_moderator::person_id.eq(self.my_person_id));
-    }
-
     if let Some(community_id) = self.community_id {
       query = query.filter(post::community_id.eq(community_id));
     }
@@ -255,11 +256,25 @@ impl<'a> PostReportQueryBuilder<'a> {
 
     let (limit, offset) = limit_and_offset(self.page, self.limit);
 
-    let res = query
-      .order_by(post_report::published.asc())
+    query = query
+      .order_by(post_report::published.desc())
       .limit(limit)
-      .offset(offset)
-      .load::<PostReportViewTuple>(self.conn)?;
+      .offset(offset);
+
+    // If its not an admin, get only the ones you mod
+    let res = if !self.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)),
+          ),
+        )
+        .load::<PostReportViewTuple>(self.conn)?
+    } else {
+      query.load::<PostReportViewTuple>(self.conn)?
+    };
 
     Ok(PostReportView::from_tuple_to_vec(res))
   }
@@ -288,18 +303,16 @@ impl ViewToVec for PostReportView {
 #[cfg(test)]
 mod tests {
   use crate::post_report_view::{PostReportQueryBuilder, PostReportView};
-  use lemmy_db_queries::{
+  use lemmy_db_schema::{
     aggregates::post_aggregates::PostAggregates,
     establish_unpooled_connection,
-    Crud,
-    Joinable,
-    Reportable,
-  };
-  use lemmy_db_schema::source::{
-    community::*,
-    person::*,
-    post::*,
-    post_report::{PostReport, PostReportForm},
+    source::{
+      community::*,
+      person::*,
+      post::*,
+      post_report::{PostReport, PostReportForm},
+    },
+    traits::{Crud, Joinable, Reportable},
   };
   use serial_test::serial;
 
@@ -419,6 +432,7 @@ mod tests {
         inbox_url: inserted_jessica.inbox_url.to_owned(),
         shared_inbox_url: None,
         matrix_user_id: None,
+        ban_expires: None,
       },
       post_creator: PersonSafeAlias1 {
         id: inserted_timmy.id,
@@ -438,6 +452,7 @@ mod tests {
         inbox_url: inserted_timmy.inbox_url.to_owned(),
         shared_inbox_url: None,
         matrix_user_id: None,
+        ban_expires: None,
       },
       creator_banned_from_community: false,
       my_vote: None,
@@ -479,6 +494,7 @@ mod tests {
       inbox_url: inserted_sara.inbox_url.to_owned(),
       shared_inbox_url: None,
       matrix_user_id: None,
+      ban_expires: None,
     };
 
     // Do a batch read of timmys reports
@@ -489,8 +505,8 @@ mod tests {
     assert_eq!(
       reports,
       [
-        expected_sara_report_view.to_owned(),
-        expected_jessica_report_view.to_owned()
+        expected_jessica_report_view.to_owned(),
+        expected_sara_report_view.to_owned()
       ]
     );
 
@@ -532,6 +548,7 @@ mod tests {
       inbox_url: inserted_timmy.inbox_url.to_owned(),
       shared_inbox_url: None,
       matrix_user_id: None,
+      ban_expires: None,
     });
 
     assert_eq!(