]> Untitled Git - lemmy.git/blobdiff - crates/db_views/src/comment_view.rs
Hide community v2 (#2055)
[lemmy.git] / crates / db_views / src / comment_view.rs
index e61752805c9cb05e643891cf817842ddca9490a7..4a7f96b6acece8cfe782dd869bc007d695654bf2 100644 (file)
@@ -1,16 +1,10 @@
-use diesel::{result::Error, *};
-use lemmy_db_queries::{
+use diesel::{dsl::*, result::Error, *};
+use lemmy_db_schema::{
   aggregates::comment_aggregates::CommentAggregates,
   functions::hot_rank,
   fuzzy_search,
   limit_and_offset,
-  ListingType,
-  MaybeOptional,
-  SortType,
-  ToSafe,
-  ViewToVec,
-};
-use lemmy_db_schema::{
+  newtypes::{CommentId, CommunityId, DbUrl, PersonId, PostId},
   schema::{
     comment,
     comment_aggregates,
@@ -33,15 +27,13 @@ use lemmy_db_schema::{
     person_block::PersonBlock,
     post::Post,
   },
-  CommentId,
-  CommunityId,
-  DbUrl,
-  PersonId,
-  PostId,
+  traits::{MaybeOptional, ToSafe, ViewToVec},
+  ListingType,
+  SortType,
 };
-use serde::Serialize;
+use serde::{Deserialize, Serialize};
 
-#[derive(Debug, PartialEq, Serialize, Clone)]
+#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
 pub struct CommentView {
   pub comment: Comment,
   pub creator: PersonSafe,
@@ -106,7 +98,12 @@ impl CommentView {
         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(
@@ -353,7 +350,12 @@ impl<'a> CommentQueryBuilder<'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(
@@ -446,10 +448,25 @@ impl<'a> CommentQueryBuilder<'a> {
     };
 
     if let Some(listing_type) = self.listing_type {
-      query = match listing_type {
-        ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
-        ListingType::Local => query.filter(community::local.eq(true)),
-        _ => query,
+      match listing_type {
+        ListingType::Subscribed => {
+          query = query.filter(community_follower::person_id.is_not_null())
+        } // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
+        ListingType::Local => {
+          query = query.filter(community::local.eq(true)).filter(
+            community::hidden
+              .eq(false)
+              .or(community_follower::person_id.eq(person_id_join)),
+          )
+        }
+        ListingType::All => {
+          query = query.filter(
+            community::hidden
+              .eq(false)
+              .or(community_follower::person_id.eq(person_id_join)),
+          )
+        }
+        ListingType::Community => {}
       };
     }
 
@@ -526,19 +543,11 @@ impl ViewToVec for CommentView {
 #[cfg(test)]
 mod tests {
   use crate::comment_view::*;
-  use lemmy_db_queries::{
+  use lemmy_db_schema::{
     aggregates::comment_aggregates::CommentAggregates,
     establish_unpooled_connection,
-    Blockable,
-    Crud,
-    Likeable,
-  };
-  use lemmy_db_schema::source::{
-    comment::*,
-    community::*,
-    person::*,
-    person_block::PersonBlockForm,
-    post::*,
+    source::{comment::*, community::*, person::*, person_block::PersonBlockForm, post::*},
+    traits::{Blockable, Crud, Likeable},
   };
   use serial_test::serial;
 
@@ -662,6 +671,7 @@ mod tests {
         inbox_url: inserted_person.inbox_url.to_owned(),
         shared_inbox_url: None,
         matrix_user_id: None,
+        ban_expires: None,
       },
       recipient: None,
       post: Post {
@@ -698,6 +708,7 @@ mod tests {
         description: None,
         updated: None,
         banner: None,
+        hidden: false,
         published: inserted_community.published,
       },
       counts: CommentAggregates {