]> Untitled Git - lemmy.git/blobdiff - crates/db_views/src/post_view.rs
Reduce amount of columns selected (#3755)
[lemmy.git] / crates / db_views / src / post_view.rs
index 9f6d0735f107ad2cc369d67025f04d4572c2e879..5fcb73f69da09a24fdb2c14554037ecfd4730ad1 100644 (file)
@@ -34,15 +34,15 @@ use lemmy_db_schema::{
     post_saved,
   },
   source::{
-    community::{Community, CommunityFollower, CommunityPersonBan},
+    community::{Community, CommunityFollower},
     person::Person,
-    person_block::PersonBlock,
-    post::{Post, PostRead, PostSaved},
+    post::Post,
   },
   traits::JoinView,
   utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
   ListingType,
   SortType,
+  SubscribedType,
 };
 use tracing::debug;
 
@@ -50,12 +50,12 @@ type PostViewTuple = (
   Post,
   Person,
   Community,
-  Option<CommunityPersonBan>,
+  bool,
   PostAggregates,
-  Option<CommunityFollower>,
-  Option<PostSaved>,
-  Option<PostRead>,
-  Option<PersonBlock>,
+  SubscribedType,
+  bool,
+  bool,
+  bool,
   Option<i16>,
   i64,
 );
@@ -136,12 +136,12 @@ fn queries<'a>() -> Queries<
     post::all_columns,
     person::all_columns,
     community::all_columns,
-    community_person_ban::all_columns.nullable(),
+    community_person_ban::id.nullable().is_not_null(),
     post_aggregates::all_columns,
-    community_follower::all_columns.nullable(),
-    post_saved::all_columns.nullable(),
-    post_read::all_columns.nullable(),
-    person_block::all_columns.nullable(),
+    CommunityFollower::select_subscribed_type(),
+    post_saved::id.nullable().is_not_null(),
+    post_read::id.nullable().is_not_null(),
+    person_block::id.nullable().is_not_null(),
     post_like::score.nullable(),
     coalesce(
       post_aggregates::comments.nullable() - person_post_aggregates::read_comments.nullable(),
@@ -212,7 +212,6 @@ fn queries<'a>() -> Queries<
       )
       .select(selection);
 
-    let is_profile_view = options.is_profile_view.unwrap_or(false);
     let is_creator = options.creator_id == options.local_user.map(|l| l.person.id);
     // only show deleted posts to creator
     if is_creator {
@@ -223,7 +222,7 @@ fn queries<'a>() -> Queries<
 
     let is_admin = options.local_user.map(|l| l.person.admin).unwrap_or(false);
     // only show removed posts to admin when viewing user profile
-    if !(is_profile_view && is_admin) {
+    if !(options.is_profile_view && is_admin) {
       query = query
         .filter(community::removed.eq(false))
         .filter(post::removed.eq(false));
@@ -243,9 +242,7 @@ fn queries<'a>() -> Queries<
 
     if let Some(listing_type) = options.listing_type {
       match listing_type {
-        ListingType::Subscribed => {
-          query = query.filter(community_follower::person_id.is_not_null())
-        }
+        ListingType::Subscribed => query = query.filter(community_follower::pending.is_not_null()),
         ListingType::Local => {
           query = query.filter(community::local.eq(true)).filter(
             community::hidden
@@ -295,7 +292,7 @@ fn queries<'a>() -> Queries<
     };
 
     if options.saved_only.unwrap_or(false) {
-      query = query.filter(post_saved::post_id.is_not_null());
+      query = query.filter(post_saved::id.is_not_null());
     }
 
     if options.moderator_view.unwrap_or(false) {
@@ -308,7 +305,16 @@ fn queries<'a>() -> Queries<
       .map(|l| l.local_user.show_read_posts)
       .unwrap_or(true)
     {
-      query = query.filter(post_read::post_id.is_null());
+      // Do not hide read posts when it is a user profile view
+      if !options.is_profile_view {
+        query = query.filter(post_read::post_id.is_null());
+      }
+    }
+
+    if options.liked_only.unwrap_or_default() {
+      query = query.filter(post_like::score.eq(1));
+    } else if options.disliked_only.unwrap_or_default() {
+      query = query.filter(post_like::score.eq(-1));
     }
 
     if options.local_user.is_some() {
@@ -424,8 +430,10 @@ pub struct PostQuery<'a> {
   pub search_term: Option<String>,
   pub url_search: Option<String>,
   pub saved_only: Option<bool>,
+  pub liked_only: Option<bool>,
+  pub disliked_only: Option<bool>,
   pub moderator_view: Option<bool>,
-  pub is_profile_view: Option<bool>,
+  pub is_profile_view: bool,
   pub page: Option<i64>,
   pub limit: Option<i64>,
 }
@@ -443,12 +451,12 @@ impl JoinView for PostView {
       post: a.0,
       creator: a.1,
       community: a.2,
-      creator_banned_from_community: a.3.is_some(),
+      creator_banned_from_community: a.3,
       counts: a.4,
-      subscribed: CommunityFollower::to_subscribed_type(&a.5),
-      saved: a.6.is_some(),
-      read: a.7.is_some(),
-      creator_blocked: a.8.is_some(),
+      subscribed: a.5,
+      saved: a.6,
+      read: a.7,
+      creator_blocked: a.8,
       my_vote: a.9,
       unread_comments: a.10,
     }
@@ -794,6 +802,28 @@ mod tests {
 
     assert_eq!(expected_post_with_upvote, read_post_listing[0]);
 
+    let read_liked_post_listing = PostQuery {
+      community_id: (Some(data.inserted_community.id)),
+      local_user: (Some(&data.local_user_view)),
+      liked_only: (Some(true)),
+      ..Default::default()
+    }
+    .list(pool)
+    .await
+    .unwrap();
+    assert_eq!(read_post_listing, read_liked_post_listing);
+
+    let read_disliked_post_listing = PostQuery {
+      community_id: (Some(data.inserted_community.id)),
+      local_user: (Some(&data.local_user_view)),
+      disliked_only: (Some(true)),
+      ..Default::default()
+    }
+    .list(pool)
+    .await
+    .unwrap();
+    assert!(read_disliked_post_listing.is_empty());
+
     let like_removed =
       PostLike::remove(pool, data.local_user_view.person.id, data.inserted_post.id)
         .await
@@ -916,7 +946,7 @@ mod tests {
     let post_listings_is_admin = PostQuery {
       sort: Some(SortType::New),
       local_user: Some(&data.local_user_view),
-      is_profile_view: Some(true),
+      is_profile_view: true,
       ..Default::default()
     }
     .list(pool)