]> Untitled Git - lemmy.git/blobdiff - crates/db_views/src/comment_report_view.rs
Adding temporary bans. Fixes #1423 (#1999)
[lemmy.git] / crates / db_views / src / comment_report_view.rs
index 5b39a5eb50f2c8dfbc3fa2bbe3bd46a5a777c57a..52089ec57d7289f4c738939b4194a6da5ab45827 100644 (file)
@@ -1,4 +1,4 @@
-use diesel::{result::Error, *};
+use diesel::{dsl::*, result::Error, *};
 use lemmy_db_schema::{
   aggregates::comment_aggregates::CommentAggregates,
   limit_and_offset,
@@ -88,7 +88,12 @@ impl CommentReportView {
         community_person_ban::table.on(
           community::id
             .eq(community_person_ban::community_id)
-            .and(community_person_ban::person_id.eq(comment::creator_id)),
+            .and(community_person_ban::person_id.eq(comment::creator_id))
+            .and(
+              community_person_ban::expires
+                .is_null()
+                .or(community_person_ban::expires.gt(now)),
+            ),
         ),
       )
       .left_join(
@@ -147,22 +152,28 @@ impl CommentReportView {
     let mut query = comment_report::table
       .inner_join(comment::table)
       .inner_join(post::table.on(comment::post_id.eq(post::id)))
-      .inner_join(
-        community_moderator::table.on(community_moderator::community_id.eq(post::community_id)),
-      )
       .filter(comment_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(comment_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(comment_report::id))
+        .first::<i64>(conn)
+    } else {
+      query.select(count(comment_report::id)).first::<i64>(conn)
+    }
   }
 }
 
@@ -216,10 +227,6 @@ impl<'a> CommentReportQueryBuilder<'a> {
       .inner_join(community::table.on(post::community_id.eq(community::id)))
       .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
       .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
-      // Test this join
-      .inner_join(
-        community_moderator::table.on(community_moderator::community_id.eq(post::community_id)),
-      )
       .inner_join(
         comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
       )
@@ -227,7 +234,12 @@ impl<'a> CommentReportQueryBuilder<'a> {
         community_person_ban::table.on(
           community::id
             .eq(community_person_ban::community_id)
-            .and(community_person_ban::person_id.eq(comment::creator_id)),
+            .and(community_person_ban::person_id.eq(comment::creator_id))
+            .and(
+              community_person_ban::expires
+                .is_null()
+                .or(community_person_ban::expires.gt(now)),
+            ),
         ),
       )
       .left_join(
@@ -254,11 +266,6 @@ impl<'a> CommentReportQueryBuilder<'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));
     }
@@ -269,11 +276,25 @@ impl<'a> CommentReportQueryBuilder<'a> {
 
     let (limit, offset) = limit_and_offset(self.page, self.limit);
 
-    let res = query
-      .order_by(comment_report::published.asc())
+    query = query
+      .order_by(comment_report::published.desc())
       .limit(limit)
-      .offset(offset)
-      .load::<CommentReportViewTuple>(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::<CommentReportViewTuple>(self.conn)?
+    } else {
+      query.load::<CommentReportViewTuple>(self.conn)?
+    };
 
     Ok(CommentReportView::from_tuple_to_vec(res))
   }
@@ -433,6 +454,7 @@ mod tests {
         inbox_url: inserted_jessica.inbox_url.to_owned(),
         shared_inbox_url: None,
         matrix_user_id: None,
+        ban_expires: None,
       },
       comment_creator: PersonSafeAlias1 {
         id: inserted_timmy.id,
@@ -452,6 +474,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,
       counts: CommentAggregates {
@@ -488,6 +511,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
@@ -498,8 +522,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()
       ]
     );
 
@@ -543,6 +567,7 @@ mod tests {
       inbox_url: inserted_timmy.inbox_url.to_owned(),
       shared_inbox_url: None,
       matrix_user_id: None,
+      ban_expires: None,
     });
 
     assert_eq!(