]> 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 497dec6d62ba197d2a554b781ed6c366496332f9..52089ec57d7289f4c738939b4194a6da5ab45827 100644 (file)
@@ -1,12 +1,8 @@
-use diesel::{result::Error, *};
-use lemmy_db_queries::{
+use diesel::{dsl::*, result::Error, *};
+use lemmy_db_schema::{
   aggregates::comment_aggregates::CommentAggregates,
   limit_and_offset,
-  MaybeOptional,
-  ToSafe,
-  ViewToVec,
-};
-use lemmy_db_schema::{
+  newtypes::{CommentReportId, CommunityId, PersonId},
   schema::{
     comment,
     comment_aggregates,
@@ -27,13 +23,11 @@ use lemmy_db_schema::{
     person::{Person, PersonAlias1, PersonAlias2, PersonSafe, PersonSafeAlias1, PersonSafeAlias2},
     post::Post,
   },
-  CommentReportId,
-  CommunityId,
-  PersonId,
+  traits::{MaybeOptional, ToSafe, ViewToVec},
 };
-use serde::Serialize;
+use serde::{Deserialize, Serialize};
 
-#[derive(Debug, PartialEq, Serialize, Clone)]
+#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
 pub struct CommentReportView {
   pub comment_report: CommentReport,
   pub comment: Comment,
@@ -86,7 +80,7 @@ impl CommentReportView {
       .inner_join(post::table.on(comment::post_id.eq(post::id)))
       .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(post::creator_id.eq(person_alias_1::id)))
+      .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
       .inner_join(
         comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
       )
@@ -94,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(
@@ -141,15 +140,11 @@ impl CommentReportView {
     })
   }
 
-  /// returns the current unresolved post report count for the supplied community ids
-  ///
-  /// * `community_ids` - a Vec<i32> of community_ids to get a count for
-  /// TODO this eq_any is a bad way to do this, would be better to join to communitymoderator
-  /// TODO FIX THIS NOW
-  /// for a person id
+  /// Returns the current unresolved post report count for the communities you mod
   pub fn get_report_count(
     conn: &PgConnection,
     my_person_id: PersonId,
+    admin: bool,
     community_id: Option<CommunityId>,
   ) -> Result<i64, Error> {
     use diesel::dsl::*;
@@ -157,14 +152,6 @@ impl CommentReportView {
     let mut query = comment_report::table
       .inner_join(comment::table)
       .inner_join(post::table.on(comment::post_id.eq(post::id)))
-      // Test this join
-      .inner_join(
-        community_moderator::table.on(
-          community_moderator::community_id
-            .eq(post::community_id)
-            .and(community_moderator::person_id.eq(my_person_id)),
-        ),
-      )
       .filter(comment_report::resolved.eq(false))
       .into_boxed();
 
@@ -172,13 +159,28 @@ impl CommentReportView {
       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)
+    }
   }
 }
 
 pub struct CommentReportQueryBuilder<'a> {
   conn: &'a PgConnection,
   my_person_id: PersonId,
+  admin: bool,
   community_id: Option<CommunityId>,
   page: Option<i64>,
   limit: Option<i64>,
@@ -186,10 +188,11 @@ pub struct CommentReportQueryBuilder<'a> {
 }
 
 impl<'a> CommentReportQueryBuilder<'a> {
-  pub fn create(conn: &'a PgConnection, my_person_id: PersonId) -> Self {
+  pub fn create(conn: &'a PgConnection, my_person_id: PersonId, admin: bool) -> Self {
     CommentReportQueryBuilder {
       conn,
       my_person_id,
+      admin,
       community_id: None,
       page: None,
       limit: None,
@@ -223,15 +226,7 @@ impl<'a> CommentReportQueryBuilder<'a> {
       .inner_join(post::table.on(comment::post_id.eq(post::id)))
       .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(post::creator_id.eq(person_alias_1::id)))
-      // Test this join
-      .inner_join(
-        community_moderator::table.on(
-          community_moderator::community_id
-            .eq(post::community_id)
-            .and(community_moderator::person_id.eq(self.my_person_id)),
-        ),
-      )
+      .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
       .inner_join(
         comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
       )
@@ -239,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(
@@ -276,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))
   }
@@ -310,14 +324,12 @@ impl ViewToVec for CommentReportView {
 #[cfg(test)]
 mod tests {
   use crate::comment_report_view::{CommentReportQueryBuilder, CommentReportView};
-  use lemmy_db_queries::{
+  use lemmy_db_schema::{
     aggregates::comment_aggregates::CommentAggregates,
     establish_unpooled_connection,
-    Crud,
-    Joinable,
-    Reportable,
+    source::{comment::*, comment_report::*, community::*, person::*, post::*},
+    traits::{Crud, Joinable, Reportable},
   };
-  use lemmy_db_schema::source::{comment::*, comment_report::*, community::*, person::*, post::*};
   use serial_test::serial;
 
   #[test]
@@ -442,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,
@@ -461,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 {
@@ -497,23 +511,25 @@ 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
-    let reports = CommentReportQueryBuilder::create(&conn, inserted_timmy.id)
+    let reports = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
       .list()
       .unwrap();
 
     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()
       ]
     );
 
     // Make sure the counts are correct
-    let report_count = CommentReportView::get_report_count(&conn, inserted_timmy.id, None).unwrap();
+    let report_count =
+      CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
     assert_eq!(2, report_count);
 
     // Try to resolve the report
@@ -551,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!(
@@ -560,14 +577,14 @@ mod tests {
 
     // Do a batch read of timmys reports
     // It should only show saras, which is unresolved
-    let reports_after_resolve = CommentReportQueryBuilder::create(&conn, inserted_timmy.id)
+    let reports_after_resolve = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
       .list()
       .unwrap();
     assert_eq!(reports_after_resolve[0], expected_sara_report_view);
 
     // Make sure the counts are correct
     let report_count_after_resolved =
-      CommentReportView::get_report_count(&conn, inserted_timmy.id, None).unwrap();
+      CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
     assert_eq!(1, report_count_after_resolved);
 
     Person::delete(&conn, inserted_timmy.id).unwrap();