]> Untitled Git - lemmy.git/commitdiff
Getting rid of terrible boxedjoin types.
authorDessalines <tyhou13@gmx.com>
Wed, 16 Dec 2020 18:59:43 +0000 (13:59 -0500)
committerDessalines <tyhou13@gmx.com>
Wed, 16 Dec 2020 18:59:43 +0000 (13:59 -0500)
13 files changed:
lemmy_api/src/comment.rs
lemmy_api/src/community.rs
lemmy_api/src/post.rs
lemmy_api/src/site.rs
lemmy_api/src/user.rs
lemmy_db/src/views/comment_view.rs
lemmy_db/src/views/community_follower_view.rs
lemmy_db/src/views/community_moderator_view.rs
lemmy_db/src/views/community_view.rs
lemmy_db/src/views/post_view.rs
lemmy_db/src/views/user_mention_view.rs
lemmy_db/src/views/user_view.rs
src/routes/feeds.rs

index eed2cb701314ae1ef05d4e41798ebc3ff2af1c9b..b39444efd0a0e247be8b33851707053b99fe96b6 100644 (file)
@@ -669,11 +669,12 @@ impl Perform for GetComments {
     let page = data.page;
     let limit = data.limit;
     let comments = blocking(context.pool(), move |conn| {
-      CommentQueryBuilder::create(conn, user_id)
+      CommentQueryBuilder::create(conn)
         .listing_type(type_)
         .sort(&sort)
-        .for_community_id(community_id)
-        .for_community_name(community_name)
+        .community_id(community_id)
+        .community_name(community_name)
+        .my_user_id(user_id)
         .page(page)
         .limit(limit)
         .list()
index d35a4a6c15496a01fe7f7912401b8d58380a7931..04059a7c5b04ed9cbd5876f74ae6edce13bcdfe0 100644 (file)
@@ -438,9 +438,10 @@ impl Perform for ListCommunities {
     let page = data.page;
     let limit = data.limit;
     let communities = blocking(context.pool(), move |conn| {
-      CommunityQueryBuilder::create(conn, user_id)
+      CommunityQueryBuilder::create(conn)
         .sort(&sort)
         .show_nsfw(show_nsfw)
+        .my_user_id(user_id)
         .page(page)
         .limit(limit)
         .list()
@@ -591,9 +592,9 @@ impl Perform for BanFromCommunity {
       // Comments
       // Diesel doesn't allow updates with joins, so this has to be a loop
       let comments = blocking(context.pool(), move |conn| {
-        CommentQueryBuilder::create(conn, None)
-          .for_creator_id(banned_user_id)
-          .for_community_id(community_id)
+        CommentQueryBuilder::create(conn)
+          .creator_id(banned_user_id)
+          .community_id(community_id)
           .limit(std::i64::MAX)
           .list()
       })
index b0af10a4bfb151de6a7a8366665afe1a89da0caf..d998e7d977dc2b53d41a374d66bcb83bd71e722b 100644 (file)
@@ -181,8 +181,9 @@ impl Perform for GetPost {
 
     let id = data.id;
     let comments = blocking(context.pool(), move |conn| {
-      CommentQueryBuilder::create(conn, user_id)
-        .for_post_id(id)
+      CommentQueryBuilder::create(conn)
+        .my_user_id(user_id)
+        .post_id(id)
         .limit(9999)
         .list()
     })
@@ -247,12 +248,13 @@ impl Perform for GetPosts {
     let community_id = data.community_id;
     let community_name = data.community_name.to_owned();
     let posts = match blocking(context.pool(), move |conn| {
-      PostQueryBuilder::create(conn, user_id)
+      PostQueryBuilder::create(conn)
         .listing_type(&type_)
         .sort(&sort)
         .show_nsfw(show_nsfw)
-        .for_community_id(community_id)
-        .for_community_name(community_name)
+        .community_id(community_id)
+        .community_name(community_name)
+        .my_user_id(user_id)
         .page(page)
         .limit(limit)
         .list()
index 3c13b5a0673e28ae839cd752bacb468e33f343ac..e8dfaca3221466cf2cee812fcfb99a303d5300b6 100644 (file)
@@ -363,11 +363,12 @@ impl Perform for Search {
     match type_ {
       SearchType::Posts => {
         posts = blocking(context.pool(), move |conn| {
-          PostQueryBuilder::create(conn, user_id)
+          PostQueryBuilder::create(conn)
             .sort(&sort)
             .show_nsfw(true)
-            .for_community_id(community_id)
-            .for_community_name(community_name)
+            .community_id(community_id)
+            .community_name(community_name)
+            .my_user_id(user_id)
             .search_term(q)
             .page(page)
             .limit(limit)
@@ -377,9 +378,10 @@ impl Perform for Search {
       }
       SearchType::Comments => {
         comments = blocking(context.pool(), move |conn| {
-          CommentQueryBuilder::create(&conn, user_id)
+          CommentQueryBuilder::create(&conn)
             .sort(&sort)
             .search_term(q)
+            .my_user_id(user_id)
             .page(page)
             .limit(limit)
             .list()
@@ -388,7 +390,7 @@ impl Perform for Search {
       }
       SearchType::Communities => {
         communities = blocking(context.pool(), move |conn| {
-          CommunityQueryBuilder::create(conn, None)
+          CommunityQueryBuilder::create(conn)
             .sort(&sort)
             .search_term(q)
             .page(page)
@@ -410,11 +412,12 @@ impl Perform for Search {
       }
       SearchType::All => {
         posts = blocking(context.pool(), move |conn| {
-          PostQueryBuilder::create(conn, user_id)
+          PostQueryBuilder::create(conn)
             .sort(&sort)
             .show_nsfw(true)
-            .for_community_id(community_id)
-            .for_community_name(community_name)
+            .community_id(community_id)
+            .community_name(community_name)
+            .my_user_id(user_id)
             .search_term(q)
             .page(page)
             .limit(limit)
@@ -426,9 +429,10 @@ impl Perform for Search {
         let sort = SortType::from_str(&data.sort)?;
 
         comments = blocking(context.pool(), move |conn| {
-          CommentQueryBuilder::create(conn, user_id)
+          CommentQueryBuilder::create(conn)
             .sort(&sort)
             .search_term(q)
+            .my_user_id(user_id)
             .page(page)
             .limit(limit)
             .list()
@@ -439,7 +443,7 @@ impl Perform for Search {
         let sort = SortType::from_str(&data.sort)?;
 
         communities = blocking(context.pool(), move |conn| {
-          CommunityQueryBuilder::create(conn, None)
+          CommunityQueryBuilder::create(conn)
             .sort(&sort)
             .search_term(q)
             .page(page)
@@ -463,11 +467,11 @@ impl Perform for Search {
       }
       SearchType::Url => {
         posts = blocking(context.pool(), move |conn| {
-          PostQueryBuilder::create(conn, None)
+          PostQueryBuilder::create(conn)
             .sort(&sort)
             .show_nsfw(true)
-            .for_community_id(community_id)
-            .for_community_name(community_name)
+            .community_id(community_id)
+            .community_name(community_name)
             .url_search(q)
             .page(page)
             .limit(limit)
index ddd03d23bbd8de93b8eac3dfdaf12527a1683570..17e3fac655756865d488f9f1b195b2cda55b5410 100644 (file)
@@ -536,15 +536,17 @@ impl Perform for GetUserDetails {
     let community_id = data.community_id;
 
     let (posts, comments) = blocking(context.pool(), move |conn| {
-      let mut posts_query = PostQueryBuilder::create(conn, user_id)
+      let mut posts_query = PostQueryBuilder::create(conn)
         .sort(&sort)
         .show_nsfw(show_nsfw)
         .saved_only(saved_only)
-        .for_community_id(community_id)
+        .community_id(community_id)
+        .my_user_id(user_id)
         .page(page)
         .limit(limit);
 
-      let mut comments_query = CommentQueryBuilder::create(conn, user_id)
+      let mut comments_query = CommentQueryBuilder::create(conn)
+        .my_user_id(user_id)
         .sort(&sort)
         .saved_only(saved_only)
         .page(page)
@@ -553,8 +555,8 @@ impl Perform for GetUserDetails {
       // If its saved only, you don't care what creator it was
       // Or, if its not saved, then you only want it for that specific creator
       if !saved_only {
-        posts_query = posts_query.for_creator_id(user_details_id);
-        comments_query = comments_query.for_creator_id(user_details_id);
+        posts_query = posts_query.creator_id(user_details_id);
+        comments_query = comments_query.creator_id(user_details_id);
       }
 
       let posts = posts_query.list()?;
@@ -741,10 +743,11 @@ impl Perform for GetReplies {
     let unread_only = data.unread_only;
     let user_id = user.id;
     let replies = blocking(context.pool(), move |conn| {
-      CommentQueryBuilder::create(conn, Some(user_id))
+      CommentQueryBuilder::create(conn)
         .sort(&sort)
         .unread_only(unread_only)
-        .for_recipient_id(user_id)
+        .recipient_id(user_id)
+        .my_user_id(user_id)
         .page(page)
         .limit(limit)
         .list()
@@ -774,7 +777,9 @@ impl Perform for GetUserMentions {
     let unread_only = data.unread_only;
     let user_id = user.id;
     let mentions = blocking(context.pool(), move |conn| {
-      UserMentionQueryBuilder::create(conn, Some(user_id), user_id)
+      UserMentionQueryBuilder::create(conn)
+        .recipient_id(user_id)
+        .my_user_id(user_id)
         .sort(&sort)
         .unread_only(unread_only)
         .page(page)
@@ -841,8 +846,9 @@ impl Perform for MarkAllAsRead {
 
     let user_id = user.id;
     let replies = blocking(context.pool(), move |conn| {
-      CommentQueryBuilder::create(conn, Some(user_id))
-        .for_recipient_id(user_id)
+      CommentQueryBuilder::create(conn)
+        .my_user_id(user_id)
+        .recipient_id(user_id)
         .unread_only(true)
         .page(1)
         .limit(999)
index 129970aff3d23ad1cf0dcc815364f46b9d91c428..d85f654fb815e97d6e5d771c8a58325ff1e4b672 100644 (file)
@@ -147,275 +147,16 @@ impl CommentView {
   }
 }
 
-mod join_types {
-  use crate::schema::{
-    comment,
-    comment_aggregates,
-    comment_alias_1,
-    comment_like,
-    comment_saved,
-    community,
-    community_follower,
-    community_user_ban,
-    post,
-    user_,
-    user_alias_1,
-  };
-  use diesel::{
-    pg::Pg,
-    query_builder::BoxedSelectStatement,
-    query_source::joins::{Inner, Join, JoinOn, LeftOuter},
-    sql_types::*,
-  };
-
-  // /// TODO awful, but necessary because of the boxed join
-  pub(super) type BoxedCommentJoin<'a> = BoxedSelectStatement<
-    'a,
-    (
-      (
-        Integer,
-        Integer,
-        Integer,
-        Nullable<Integer>,
-        Text,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Bool,
-        Text,
-        Bool,
-      ),
-      (
-        Integer,
-        Text,
-        Nullable<Text>,
-        Nullable<Text>,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Nullable<Text>,
-        Text,
-        Nullable<Text>,
-        Bool,
-        Nullable<Text>,
-        Bool,
-      ),
-      Nullable<(
-        Integer,
-        Integer,
-        Integer,
-        Nullable<Integer>,
-        Text,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Bool,
-        Text,
-        Bool,
-      )>,
-      Nullable<(
-        Integer,
-        Text,
-        Nullable<Text>,
-        Nullable<Text>,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Nullable<Text>,
-        Text,
-        Nullable<Text>,
-        Bool,
-        Nullable<Text>,
-        Bool,
-      )>,
-      (
-        Integer,
-        Text,
-        Nullable<Text>,
-        Nullable<Text>,
-        Integer,
-        Integer,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Bool,
-        Bool,
-        Bool,
-        Nullable<Text>,
-        Nullable<Text>,
-        Nullable<Text>,
-        Nullable<Text>,
-        Text,
-        Bool,
-      ),
-      (
-        Integer,
-        Text,
-        Text,
-        Nullable<Text>,
-        Integer,
-        Integer,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Bool,
-        Bool,
-        Text,
-        Bool,
-        Nullable<Text>,
-        Nullable<Text>,
-      ),
-      (Integer, Integer, BigInt, BigInt, BigInt),
-      Nullable<(Integer, Integer, Integer, Timestamp)>,
-      Nullable<(Integer, Integer, Integer, Timestamp, Nullable<Bool>)>,
-      Nullable<(Integer, Integer, Integer, Timestamp)>,
-      Nullable<SmallInt>,
-    ),
-    JoinOn<
-      Join<
-        JoinOn<
-          Join<
-            JoinOn<
-              Join<
-                JoinOn<
-                  Join<
-                    JoinOn<
-                      Join<
-                        JoinOn<
-                          Join<
-                            JoinOn<
-                              Join<
-                                JoinOn<
-                                  Join<
-                                    JoinOn<
-                                      Join<
-                                        JoinOn<
-                                          Join<comment::table, user_::table, Inner>,
-                                          diesel::expression::operators::Eq<
-                                            diesel::expression::nullable::Nullable<
-                                              comment::columns::creator_id,
-                                            >,
-                                            diesel::expression::nullable::Nullable<
-                                              user_::columns::id,
-                                            >,
-                                          >,
-                                        >,
-                                        comment_alias_1::table,
-                                        LeftOuter,
-                                      >,
-                                      diesel::expression::operators::Eq<
-                                        diesel::expression::nullable::Nullable<
-                                          comment_alias_1::columns::id,
-                                        >,
-                                        comment::columns::parent_id,
-                                      >,
-                                    >,
-                                    user_alias_1::table,
-                                    LeftOuter,
-                                  >,
-                                  diesel::expression::operators::Eq<
-                                    user_alias_1::columns::id,
-                                    comment_alias_1::columns::creator_id,
-                                  >,
-                                >,
-                                post::table,
-                                Inner,
-                              >,
-                              diesel::expression::operators::Eq<
-                                diesel::expression::nullable::Nullable<comment::columns::post_id>,
-                                diesel::expression::nullable::Nullable<post::columns::id>,
-                              >,
-                            >,
-                            community::table,
-                            Inner,
-                          >,
-                          diesel::expression::operators::Eq<
-                            post::columns::community_id,
-                            community::columns::id,
-                          >,
-                        >,
-                        comment_aggregates::table,
-                        Inner,
-                      >,
-                      diesel::expression::operators::Eq<
-                        diesel::expression::nullable::Nullable<
-                          comment_aggregates::columns::comment_id,
-                        >,
-                        diesel::expression::nullable::Nullable<comment::columns::id>,
-                      >,
-                    >,
-                    community_user_ban::table,
-                    LeftOuter,
-                  >,
-                  diesel::expression::operators::And<
-                    diesel::expression::operators::Eq<
-                      community::columns::id,
-                      community_user_ban::columns::community_id,
-                    >,
-                    diesel::expression::operators::Eq<
-                      community_user_ban::columns::user_id,
-                      comment::columns::creator_id,
-                    >,
-                  >,
-                >,
-                community_follower::table,
-                LeftOuter,
-              >,
-              diesel::expression::operators::And<
-                diesel::expression::operators::Eq<
-                  post::columns::community_id,
-                  community_follower::columns::community_id,
-                >,
-                diesel::expression::operators::Eq<
-                  community_follower::columns::user_id,
-                  diesel::expression::bound::Bound<Integer, i32>,
-                >,
-              >,
-            >,
-            comment_saved::table,
-            LeftOuter,
-          >,
-          diesel::expression::operators::And<
-            diesel::expression::operators::Eq<
-              comment::columns::id,
-              comment_saved::columns::comment_id,
-            >,
-            diesel::expression::operators::Eq<
-              comment_saved::columns::user_id,
-              diesel::expression::bound::Bound<Integer, i32>,
-            >,
-          >,
-        >,
-        comment_like::table,
-        LeftOuter,
-      >,
-      diesel::expression::operators::And<
-        diesel::expression::operators::Eq<comment::columns::id, comment_like::columns::comment_id>,
-        diesel::expression::operators::Eq<
-          comment_like::columns::user_id,
-          diesel::expression::bound::Bound<Integer, i32>,
-        >,
-      >,
-    >,
-    Pg,
-  >;
-}
-
 pub struct CommentQueryBuilder<'a> {
   conn: &'a PgConnection,
-  query: join_types::BoxedCommentJoin<'a>,
   listing_type: ListingType,
   sort: &'a SortType,
-  for_community_id: Option<i32>,
-  for_community_name: Option<String>,
-  for_post_id: Option<i32>,
-  for_creator_id: Option<i32>,
-  for_recipient_id: Option<i32>,
+  community_id: Option<i32>,
+  community_name: Option<String>,
+  post_id: Option<i32>,
+  creator_id: Option<i32>,
+  recipient_id: Option<i32>,
+  my_user_id: Option<i32>,
   search_term: Option<String>,
   saved_only: bool,
   unread_only: bool,
@@ -424,71 +165,17 @@ pub struct CommentQueryBuilder<'a> {
 }
 
 impl<'a> CommentQueryBuilder<'a> {
-  pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self {
-    // The left join below will return None in this case
-    let user_id_join = my_user_id.unwrap_or(-1);
-
-    let query = comment::table
-      .inner_join(user_::table)
-      // recipient here
-      .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
-      .left_join(user_alias_1::table.on(user_alias_1::id.eq(comment_alias_1::creator_id)))
-      .inner_join(post::table)
-      .inner_join(community::table.on(post::community_id.eq(community::id)))
-      .inner_join(comment_aggregates::table)
-      .left_join(
-        community_user_ban::table.on(
-          community::id
-            .eq(community_user_ban::community_id)
-            .and(community_user_ban::user_id.eq(comment::creator_id)),
-        ),
-      )
-      .left_join(
-        community_follower::table.on(
-          post::community_id
-            .eq(community_follower::community_id)
-            .and(community_follower::user_id.eq(user_id_join)),
-        ),
-      )
-      .left_join(
-        comment_saved::table.on(
-          comment::id
-            .eq(comment_saved::comment_id)
-            .and(comment_saved::user_id.eq(user_id_join)),
-        ),
-      )
-      .left_join(
-        comment_like::table.on(
-          comment::id
-            .eq(comment_like::comment_id)
-            .and(comment_like::user_id.eq(user_id_join)),
-        ),
-      )
-      .select((
-        comment::all_columns,
-        User_::safe_columns_tuple(),
-        comment_alias_1::all_columns.nullable(),
-        UserAlias1::safe_columns_tuple().nullable(),
-        post::all_columns,
-        Community::safe_columns_tuple(),
-        comment_aggregates::all_columns,
-        community_user_ban::all_columns.nullable(),
-        community_follower::all_columns.nullable(),
-        comment_saved::all_columns.nullable(),
-        comment_like::score.nullable(),
-      ))
-      .into_boxed();
-
+  pub fn create(conn: &'a PgConnection) -> Self {
     CommentQueryBuilder {
       conn,
-      query,
       listing_type: ListingType::All,
       sort: &SortType::New,
-      for_community_id: None,
-      for_community_name: None,
-      for_post_id: None,
-      for_creator_id: None,
-      for_recipient_id: None,
+      community_id: None,
+      community_name: None,
+      post_id: None,
+      creator_id: None,
+      recipient_id: None,
+      my_user_id: None,
       search_term: None,
       saved_only: false,
       unread_only: false,
@@ -507,28 +194,33 @@ impl<'a> CommentQueryBuilder<'a> {
     self
   }
 
-  pub fn for_post_id<T: MaybeOptional<i32>>(mut self, for_post_id: T) -> Self {
-    self.for_post_id = for_post_id.get_optional();
+  pub fn post_id<T: MaybeOptional<i32>>(mut self, post_id: T) -> Self {
+    self.post_id = post_id.get_optional();
+    self
+  }
+
+  pub fn creator_id<T: MaybeOptional<i32>>(mut self, creator_id: T) -> Self {
+    self.creator_id = creator_id.get_optional();
     self
   }
 
-  pub fn for_creator_id<T: MaybeOptional<i32>>(mut self, for_creator_id: T) -> Self {
-    self.for_creator_id = for_creator_id.get_optional();
+  pub fn recipient_id<T: MaybeOptional<i32>>(mut self, recipient_id: T) -> Self {
+    self.recipient_id = recipient_id.get_optional();
     self
   }
 
-  pub fn for_recipient_id<T: MaybeOptional<i32>>(mut self, for_recipient_id: T) -> Self {
-    self.for_creator_id = for_recipient_id.get_optional();
+  pub fn community_id<T: MaybeOptional<i32>>(mut self, community_id: T) -> Self {
+    self.community_id = community_id.get_optional();
     self
   }
 
-  pub fn for_community_id<T: MaybeOptional<i32>>(mut self, for_community_id: T) -> Self {
-    self.for_community_id = for_community_id.get_optional();
+  pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
+    self.my_user_id = my_user_id.get_optional();
     self
   }
 
-  pub fn for_community_name<T: MaybeOptional<String>>(mut self, for_community_name: T) -> Self {
-    self.for_community_name = for_community_name.get_optional();
+  pub fn community_name<T: MaybeOptional<String>>(mut self, community_name: T) -> Self {
+    self.community_name = community_name.get_optional();
     self
   }
 
@@ -560,13 +252,65 @@ impl<'a> CommentQueryBuilder<'a> {
   pub fn list(self) -> Result<Vec<CommentView>, Error> {
     use diesel::dsl::*;
 
-    let mut query = self.query;
+    // The left join below will return None in this case
+    let user_id_join = self.my_user_id.unwrap_or(-1);
+
+    let mut query = comment::table
+      .inner_join(user_::table)
+      // recipient here
+      .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
+      .left_join(user_alias_1::table.on(user_alias_1::id.eq(comment_alias_1::creator_id)))
+      .inner_join(post::table)
+      .inner_join(community::table.on(post::community_id.eq(community::id)))
+      .inner_join(comment_aggregates::table)
+      .left_join(
+        community_user_ban::table.on(
+          community::id
+            .eq(community_user_ban::community_id)
+            .and(community_user_ban::user_id.eq(comment::creator_id)),
+        ),
+      )
+      .left_join(
+        community_follower::table.on(
+          post::community_id
+            .eq(community_follower::community_id)
+            .and(community_follower::user_id.eq(user_id_join)),
+        ),
+      )
+      .left_join(
+        comment_saved::table.on(
+          comment::id
+            .eq(comment_saved::comment_id)
+            .and(comment_saved::user_id.eq(user_id_join)),
+        ),
+      )
+      .left_join(
+        comment_like::table.on(
+          comment::id
+            .eq(comment_like::comment_id)
+            .and(comment_like::user_id.eq(user_id_join)),
+        ),
+      )
+      .select((
+        comment::all_columns,
+        User_::safe_columns_tuple(),
+        comment_alias_1::all_columns.nullable(),
+        UserAlias1::safe_columns_tuple().nullable(),
+        post::all_columns,
+        Community::safe_columns_tuple(),
+        comment_aggregates::all_columns,
+        community_user_ban::all_columns.nullable(),
+        community_follower::all_columns.nullable(),
+        comment_saved::all_columns.nullable(),
+        comment_like::score.nullable(),
+      ))
+      .into_boxed();
 
     // The replies
-    if let Some(for_recipient_id) = self.for_recipient_id {
+    if let Some(recipient_id) = self.recipient_id {
       query = query
         // TODO needs lots of testing
-        .filter(user_alias_1::id.eq(for_recipient_id))
+        .filter(user_alias_1::id.eq(recipient_id))
         .filter(comment::deleted.eq(false))
         .filter(comment::removed.eq(false));
     }
@@ -575,22 +319,22 @@ impl<'a> CommentQueryBuilder<'a> {
       query = query.filter(comment::read.eq(false));
     }
 
-    if let Some(for_creator_id) = self.for_creator_id {
-      query = query.filter(comment::creator_id.eq(for_creator_id));
+    if let Some(creator_id) = self.creator_id {
+      query = query.filter(comment::creator_id.eq(creator_id));
     };
 
-    if let Some(for_community_id) = self.for_community_id {
-      query = query.filter(post::community_id.eq(for_community_id));
+    if let Some(community_id) = self.community_id {
+      query = query.filter(post::community_id.eq(community_id));
     }
 
-    if let Some(for_community_name) = self.for_community_name {
+    if let Some(community_name) = self.community_name {
       query = query
-        .filter(community::name.eq(for_community_name))
+        .filter(community::name.eq(community_name))
         .filter(comment::local.eq(true));
     }
 
-    if let Some(for_post_id) = self.for_post_id {
-      query = query.filter(comment::post_id.eq(for_post_id));
+    if let Some(post_id) = self.post_id {
+      query = query.filter(comment::post_id.eq(post_id));
     };
 
     if let Some(search_term) = self.search_term {
@@ -861,13 +605,14 @@ mod tests {
     let mut expected_comment_view_with_user = expected_comment_view_no_user.to_owned();
     expected_comment_view_with_user.my_vote = Some(1);
 
-    let read_comment_views_no_user = CommentQueryBuilder::create(&conn, None)
-      .for_post_id(inserted_post.id)
+    let read_comment_views_no_user = CommentQueryBuilder::create(&conn)
+      .post_id(inserted_post.id)
       .list()
       .unwrap();
 
-    let read_comment_views_with_user = CommentQueryBuilder::create(&conn, Some(inserted_user.id))
-      .for_post_id(inserted_post.id)
+    let read_comment_views_with_user = CommentQueryBuilder::create(&conn)
+      .post_id(inserted_post.id)
+      .my_user_id(inserted_user.id)
       .list()
       .unwrap();
 
index 64adae3b767e07f74f42c8347cef3e0b2e1b9489..7de9cc3a4b92fea761b20f7041323280051ba92e 100644 (file)
@@ -19,24 +19,24 @@ pub struct CommunityFollowerView {
 type CommunityFollowerViewTuple = (CommunitySafe, UserSafe);
 
 impl CommunityFollowerView {
-  pub fn for_community(conn: &PgConnection, for_community_id: i32) -> Result<Vec<Self>, Error> {
+  pub fn for_community(conn: &PgConnection, community_id: i32) -> Result<Vec<Self>, Error> {
     let res = community_follower::table
       .inner_join(community::table)
       .inner_join(user_::table)
       .select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
-      .filter(community_follower::community_id.eq(for_community_id))
+      .filter(community_follower::community_id.eq(community_id))
       .order_by(community_follower::published)
       .load::<CommunityFollowerViewTuple>(conn)?;
 
     Ok(Self::to_vec(res))
   }
 
-  pub fn for_user(conn: &PgConnection, for_user_id: i32) -> Result<Vec<Self>, Error> {
+  pub fn for_user(conn: &PgConnection, user_id: i32) -> Result<Vec<Self>, Error> {
     let res = community_follower::table
       .inner_join(community::table)
       .inner_join(user_::table)
       .select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
-      .filter(community_follower::user_id.eq(for_user_id))
+      .filter(community_follower::user_id.eq(user_id))
       .order_by(community_follower::published)
       .load::<CommunityFollowerViewTuple>(conn)?;
 
index c98f072a110fd2b59f8b32ade3c9f079ae3b14e7..751e46232c715a697f1afa16c0bf66652d146f80 100644 (file)
@@ -19,24 +19,24 @@ pub struct CommunityModeratorView {
 type CommunityModeratorViewTuple = (CommunitySafe, UserSafe);
 
 impl CommunityModeratorView {
-  pub fn for_community(conn: &PgConnection, for_community_id: i32) -> Result<Vec<Self>, Error> {
+  pub fn for_community(conn: &PgConnection, community_id: i32) -> Result<Vec<Self>, Error> {
     let res = community_moderator::table
       .inner_join(community::table)
       .inner_join(user_::table)
       .select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
-      .filter(community_moderator::community_id.eq(for_community_id))
+      .filter(community_moderator::community_id.eq(community_id))
       .order_by(community_moderator::published)
       .load::<CommunityModeratorViewTuple>(conn)?;
 
     Ok(Self::to_vec(res))
   }
 
-  pub fn for_user(conn: &PgConnection, for_user_id: i32) -> Result<Vec<Self>, Error> {
+  pub fn for_user(conn: &PgConnection, user_id: i32) -> Result<Vec<Self>, Error> {
     let res = community_moderator::table
       .inner_join(community::table)
       .inner_join(user_::table)
       .select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
-      .filter(community_moderator::user_id.eq(for_user_id))
+      .filter(community_moderator::user_id.eq(user_id))
       .order_by(community_moderator::published)
       .load::<CommunityModeratorViewTuple>(conn)?;
 
index d4518f7f413df90ff87eeb2a267a63e14c45c73c..fcc707c0682b0ae178630e5bf742a73bdb9e640b 100644 (file)
@@ -74,107 +74,10 @@ impl CommunityView {
   }
 }
 
-mod join_types {
-  use crate::schema::{category, community, community_aggregates, community_follower, user_};
-  use diesel::{
-    pg::Pg,
-    query_builder::BoxedSelectStatement,
-    query_source::joins::{Inner, Join, JoinOn, LeftOuter},
-    sql_types::*,
-  };
-
-  /// TODO awful, but necessary because of the boxed join
-  pub(super) type BoxedCommunityJoin<'a> = BoxedSelectStatement<
-    'a,
-    (
-      (
-        Integer,
-        Text,
-        Text,
-        Nullable<Text>,
-        Integer,
-        Integer,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Bool,
-        Bool,
-        Text,
-        Bool,
-        Nullable<Text>,
-        Nullable<Text>,
-      ),
-      (
-        Integer,
-        Text,
-        Nullable<Text>,
-        Nullable<Text>,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Nullable<Text>,
-        Text,
-        Nullable<Text>,
-        Bool,
-        Nullable<Text>,
-        Bool,
-      ),
-      (Integer, Text),
-      (Integer, Integer, BigInt, BigInt, BigInt),
-      Nullable<(Integer, Integer, Integer, Timestamp, Nullable<Bool>)>,
-    ),
-    JoinOn<
-      Join<
-        JoinOn<
-          Join<
-            JoinOn<
-              Join<
-                JoinOn<
-                  Join<community::table, user_::table, Inner>,
-                  diesel::expression::operators::Eq<
-                    diesel::expression::nullable::Nullable<community::columns::creator_id>,
-                    diesel::expression::nullable::Nullable<user_::columns::id>,
-                  >,
-                >,
-                category::table,
-                Inner,
-              >,
-              diesel::expression::operators::Eq<
-                diesel::expression::nullable::Nullable<community::columns::category_id>,
-                diesel::expression::nullable::Nullable<category::columns::id>,
-              >,
-            >,
-            community_aggregates::table,
-            Inner,
-          >,
-          diesel::expression::operators::Eq<
-            diesel::expression::nullable::Nullable<community_aggregates::columns::community_id>,
-            diesel::expression::nullable::Nullable<community::columns::id>,
-          >,
-        >,
-        community_follower::table,
-        LeftOuter,
-      >,
-      diesel::expression::operators::And<
-        diesel::expression::operators::Eq<
-          community::columns::id,
-          community_follower::columns::community_id,
-        >,
-        diesel::expression::operators::Eq<
-          community_follower::columns::user_id,
-          diesel::expression::bound::Bound<diesel::sql_types::Integer, i32>,
-        >,
-      >,
-    >,
-    Pg,
-  >;
-}
-
 pub struct CommunityQueryBuilder<'a> {
   conn: &'a PgConnection,
-  query: join_types::BoxedCommunityJoin<'a>,
   sort: &'a SortType,
+  my_user_id: Option<i32>,
   show_nsfw: bool,
   search_term: Option<String>,
   page: Option<i64>,
@@ -182,33 +85,10 @@ pub struct CommunityQueryBuilder<'a> {
 }
 
 impl<'a> CommunityQueryBuilder<'a> {
-  pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self {
-    // The left join below will return None in this case
-    let user_id_join = my_user_id.unwrap_or(-1);
-
-    let query = community::table
-      .inner_join(user_::table)
-      .inner_join(category::table)
-      .inner_join(community_aggregates::table)
-      .left_join(
-        community_follower::table.on(
-          community::id
-            .eq(community_follower::community_id)
-            .and(community_follower::user_id.eq(user_id_join)),
-        ),
-      )
-      .select((
-        Community::safe_columns_tuple(),
-        User_::safe_columns_tuple(),
-        category::all_columns,
-        community_aggregates::all_columns,
-        community_follower::all_columns.nullable(),
-      ))
-      .into_boxed();
-
+  pub fn create(conn: &'a PgConnection) -> Self {
     CommunityQueryBuilder {
       conn,
-      query,
+      my_user_id: None,
       sort: &SortType::Hot,
       show_nsfw: true,
       search_term: None,
@@ -232,6 +112,11 @@ impl<'a> CommunityQueryBuilder<'a> {
     self
   }
 
+  pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
+    self.my_user_id = my_user_id.get_optional();
+    self
+  }
+
   pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
     self.page = page.get_optional();
     self
@@ -243,7 +128,28 @@ impl<'a> CommunityQueryBuilder<'a> {
   }
 
   pub fn list(self) -> Result<Vec<CommunityView>, Error> {
-    let mut query = self.query;
+    // The left join below will return None in this case
+    let user_id_join = self.my_user_id.unwrap_or(-1);
+
+    let mut query = community::table
+      .inner_join(user_::table)
+      .inner_join(category::table)
+      .inner_join(community_aggregates::table)
+      .left_join(
+        community_follower::table.on(
+          community::id
+            .eq(community_follower::community_id)
+            .and(community_follower::user_id.eq(user_id_join)),
+        ),
+      )
+      .select((
+        Community::safe_columns_tuple(),
+        User_::safe_columns_tuple(),
+        category::all_columns,
+        community_aggregates::all_columns,
+        community_follower::all_columns.nullable(),
+      ))
+      .into_boxed();
 
     if let Some(search_term) = self.search_term {
       let searcher = fuzzy_search(&search_term);
index 9791d0a8b508d2450b89c6453b186a26f33f4820..f95cf1184ab131f726ddcc9b5c895e4cda9e2e1f 100644 (file)
@@ -135,202 +135,14 @@ impl PostView {
   }
 }
 
-mod join_types {
-  use crate::schema::{
-    community,
-    community_follower,
-    community_user_ban,
-    post,
-    post_aggregates,
-    post_like,
-    post_read,
-    post_saved,
-    user_,
-  };
-  use diesel::{
-    pg::Pg,
-    query_builder::BoxedSelectStatement,
-    query_source::joins::{Inner, Join, JoinOn, LeftOuter},
-    sql_types::*,
-  };
-
-  // /// TODO awful, but necessary because of the boxed join
-  pub(super) type BoxedPostJoin<'a> = BoxedSelectStatement<
-    'a,
-    (
-      (
-        Integer,
-        Text,
-        Nullable<Text>,
-        Nullable<Text>,
-        Integer,
-        Integer,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Bool,
-        Bool,
-        Bool,
-        Nullable<Text>,
-        Nullable<Text>,
-        Nullable<Text>,
-        Nullable<Text>,
-        Text,
-        Bool,
-      ),
-      (
-        Integer,
-        Text,
-        Nullable<Text>,
-        Nullable<Text>,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Nullable<Text>,
-        Text,
-        Nullable<Text>,
-        Bool,
-        Nullable<Text>,
-        Bool,
-      ),
-      (
-        Integer,
-        Text,
-        Text,
-        Nullable<Text>,
-        Integer,
-        Integer,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Bool,
-        Bool,
-        Text,
-        Bool,
-        Nullable<Text>,
-        Nullable<Text>,
-      ),
-      Nullable<(Integer, Integer, Integer, Timestamp)>,
-      (Integer, Integer, BigInt, BigInt, BigInt, BigInt, Timestamp),
-      Nullable<(Integer, Integer, Integer, Timestamp, Nullable<Bool>)>,
-      Nullable<(Integer, Integer, Integer, Timestamp)>,
-      Nullable<(Integer, Integer, Integer, Timestamp)>,
-      Nullable<SmallInt>,
-    ),
-    JoinOn<
-      Join<
-        JoinOn<
-          Join<
-            JoinOn<
-              Join<
-                JoinOn<
-                  Join<
-                    JoinOn<
-                      Join<
-                        JoinOn<
-                          Join<
-                            JoinOn<
-                              Join<
-                                JoinOn<
-                                  Join<post::table, user_::table, Inner>,
-                                  diesel::expression::operators::Eq<
-                                    diesel::expression::nullable::Nullable<
-                                      post::columns::creator_id,
-                                    >,
-                                    diesel::expression::nullable::Nullable<user_::columns::id>,
-                                  >,
-                                >,
-                                community::table,
-                                Inner,
-                              >,
-                              diesel::expression::operators::Eq<
-                                diesel::expression::nullable::Nullable<post::columns::community_id>,
-                                diesel::expression::nullable::Nullable<community::columns::id>,
-                              >,
-                            >,
-                            community_user_ban::table,
-                            LeftOuter,
-                          >,
-                          diesel::expression::operators::And<
-                            diesel::expression::operators::Eq<
-                              post::columns::community_id,
-                              community_user_ban::columns::community_id,
-                            >,
-                            diesel::expression::operators::Eq<
-                              community_user_ban::columns::user_id,
-                              community::columns::creator_id,
-                            >,
-                          >,
-                        >,
-                        post_aggregates::table,
-                        Inner,
-                      >,
-                      diesel::expression::operators::Eq<
-                        diesel::expression::nullable::Nullable<post_aggregates::columns::post_id>,
-                        diesel::expression::nullable::Nullable<post::columns::id>,
-                      >,
-                    >,
-                    community_follower::table,
-                    LeftOuter,
-                  >,
-                  diesel::expression::operators::And<
-                    diesel::expression::operators::Eq<
-                      post::columns::community_id,
-                      community_follower::columns::community_id,
-                    >,
-                    diesel::expression::operators::Eq<
-                      community_follower::columns::user_id,
-                      diesel::expression::bound::Bound<Integer, i32>,
-                    >,
-                  >,
-                >,
-                post_saved::table,
-                LeftOuter,
-              >,
-              diesel::expression::operators::And<
-                diesel::expression::operators::Eq<post::columns::id, post_saved::columns::post_id>,
-                diesel::expression::operators::Eq<
-                  post_saved::columns::user_id,
-                  diesel::expression::bound::Bound<Integer, i32>,
-                >,
-              >,
-            >,
-            post_read::table,
-            LeftOuter,
-          >,
-          diesel::expression::operators::And<
-            diesel::expression::operators::Eq<post::columns::id, post_read::columns::post_id>,
-            diesel::expression::operators::Eq<
-              post_read::columns::user_id,
-              diesel::expression::bound::Bound<Integer, i32>,
-            >,
-          >,
-        >,
-        post_like::table,
-        LeftOuter,
-      >,
-      diesel::expression::operators::And<
-        diesel::expression::operators::Eq<post::columns::id, post_like::columns::post_id>,
-        diesel::expression::operators::Eq<
-          post_like::columns::user_id,
-          diesel::expression::bound::Bound<Integer, i32>,
-        >,
-      >,
-    >,
-    Pg,
-  >;
-}
-
 pub struct PostQueryBuilder<'a> {
   conn: &'a PgConnection,
-  query: join_types::BoxedPostJoin<'a>,
   listing_type: &'a ListingType,
   sort: &'a SortType,
-  for_creator_id: Option<i32>,
-  for_community_id: Option<i32>,
-  for_community_name: Option<String>,
+  creator_id: Option<i32>,
+  community_id: Option<i32>,
+  community_name: Option<String>,
+  my_user_id: Option<i32>,
   search_term: Option<String>,
   url_search: Option<String>,
   show_nsfw: bool,
@@ -341,70 +153,15 @@ pub struct PostQueryBuilder<'a> {
 }
 
 impl<'a> PostQueryBuilder<'a> {
-  pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self {
-    // The left join below will return None in this case
-    let user_id_join = my_user_id.unwrap_or(-1);
-
-    let query = post::table
-      .inner_join(user_::table)
-      .inner_join(community::table)
-      .left_join(
-        community_user_ban::table.on(
-          post::community_id
-            .eq(community_user_ban::community_id)
-            .and(community_user_ban::user_id.eq(community::creator_id)),
-        ),
-      )
-      .inner_join(post_aggregates::table)
-      .left_join(
-        community_follower::table.on(
-          post::community_id
-            .eq(community_follower::community_id)
-            .and(community_follower::user_id.eq(user_id_join)),
-        ),
-      )
-      .left_join(
-        post_saved::table.on(
-          post::id
-            .eq(post_saved::post_id)
-            .and(post_saved::user_id.eq(user_id_join)),
-        ),
-      )
-      .left_join(
-        post_read::table.on(
-          post::id
-            .eq(post_read::post_id)
-            .and(post_read::user_id.eq(user_id_join)),
-        ),
-      )
-      .left_join(
-        post_like::table.on(
-          post::id
-            .eq(post_like::post_id)
-            .and(post_like::user_id.eq(user_id_join)),
-        ),
-      )
-      .select((
-        post::all_columns,
-        User_::safe_columns_tuple(),
-        Community::safe_columns_tuple(),
-        community_user_ban::all_columns.nullable(),
-        post_aggregates::all_columns,
-        community_follower::all_columns.nullable(),
-        post_saved::all_columns.nullable(),
-        post_read::all_columns.nullable(),
-        post_like::score.nullable(),
-      ))
-      .into_boxed();
-
+  pub fn create(conn: &'a PgConnection) -> Self {
     PostQueryBuilder {
       conn,
-      query,
       listing_type: &ListingType::All,
       sort: &SortType::Hot,
-      for_creator_id: None,
-      for_community_id: None,
-      for_community_name: None,
+      creator_id: None,
+      community_id: None,
+      community_name: None,
+      my_user_id: None,
       search_term: None,
       url_search: None,
       show_nsfw: true,
@@ -425,18 +182,23 @@ impl<'a> PostQueryBuilder<'a> {
     self
   }
 
-  pub fn for_community_id<T: MaybeOptional<i32>>(mut self, for_community_id: T) -> Self {
-    self.for_community_id = for_community_id.get_optional();
+  pub fn community_id<T: MaybeOptional<i32>>(mut self, community_id: T) -> Self {
+    self.community_id = community_id.get_optional();
+    self
+  }
+
+  pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
+    self.community_id = my_user_id.get_optional();
     self
   }
 
-  pub fn for_community_name<T: MaybeOptional<String>>(mut self, for_community_name: T) -> Self {
-    self.for_community_name = for_community_name.get_optional();
+  pub fn community_name<T: MaybeOptional<String>>(mut self, community_name: T) -> Self {
+    self.community_name = community_name.get_optional();
     self
   }
 
-  pub fn for_creator_id<T: MaybeOptional<i32>>(mut self, for_creator_id: T) -> Self {
-    self.for_creator_id = for_creator_id.get_optional();
+  pub fn creator_id<T: MaybeOptional<i32>>(mut self, creator_id: T) -> Self {
+    self.creator_id = creator_id.get_optional();
     self
   }
 
@@ -473,7 +235,60 @@ impl<'a> PostQueryBuilder<'a> {
   pub fn list(self) -> Result<Vec<PostView>, Error> {
     use diesel::dsl::*;
 
-    let mut query = self.query;
+    // The left join below will return None in this case
+    let user_id_join = self.my_user_id.unwrap_or(-1);
+
+    let mut query = post::table
+      .inner_join(user_::table)
+      .inner_join(community::table)
+      .left_join(
+        community_user_ban::table.on(
+          post::community_id
+            .eq(community_user_ban::community_id)
+            .and(community_user_ban::user_id.eq(community::creator_id)),
+        ),
+      )
+      .inner_join(post_aggregates::table)
+      .left_join(
+        community_follower::table.on(
+          post::community_id
+            .eq(community_follower::community_id)
+            .and(community_follower::user_id.eq(user_id_join)),
+        ),
+      )
+      .left_join(
+        post_saved::table.on(
+          post::id
+            .eq(post_saved::post_id)
+            .and(post_saved::user_id.eq(user_id_join)),
+        ),
+      )
+      .left_join(
+        post_read::table.on(
+          post::id
+            .eq(post_read::post_id)
+            .and(post_read::user_id.eq(user_id_join)),
+        ),
+      )
+      .left_join(
+        post_like::table.on(
+          post::id
+            .eq(post_like::post_id)
+            .and(post_like::user_id.eq(user_id_join)),
+        ),
+      )
+      .select((
+        post::all_columns,
+        User_::safe_columns_tuple(),
+        Community::safe_columns_tuple(),
+        community_user_ban::all_columns.nullable(),
+        post_aggregates::all_columns,
+        community_follower::all_columns.nullable(),
+        post_saved::all_columns.nullable(),
+        post_read::all_columns.nullable(),
+        post_like::score.nullable(),
+      ))
+      .into_boxed();
 
     query = match self.listing_type {
       ListingType::Subscribed => query.filter(community_follower::user_id.is_not_null()), // TODO could be this: and(community_follower::user_id.eq(user_id_join)),
@@ -481,15 +296,15 @@ impl<'a> PostQueryBuilder<'a> {
       _ => query,
     };
 
-    if let Some(for_community_id) = self.for_community_id {
+    if let Some(community_id) = self.community_id {
       query = query
-        .filter(post::community_id.eq(for_community_id))
+        .filter(post::community_id.eq(community_id))
         .then_order_by(post::stickied.desc());
     }
 
-    if let Some(for_community_name) = self.for_community_name {
+    if let Some(community_name) = self.community_name {
       query = query
-        .filter(community::name.eq(for_community_name))
+        .filter(community::name.eq(community_name))
         .filter(community::local.eq(true))
         .then_order_by(post::stickied.desc());
     }
@@ -507,6 +322,26 @@ impl<'a> PostQueryBuilder<'a> {
       );
     }
 
+    // If its for a specific user, show the removed / deleted
+    if let Some(creator_id) = self.creator_id {
+      query = query.filter(post::creator_id.eq(creator_id));
+    }
+
+    if !self.show_nsfw {
+      query = query
+        .filter(post::nsfw.eq(false))
+        .filter(community::nsfw.eq(false));
+    };
+
+    // TODO  These two might be wrong
+    if self.saved_only {
+      query = query.filter(post_saved::id.is_not_null());
+    };
+
+    if self.unread_only {
+      query = query.filter(post_read::id.is_not_null());
+    };
+
     query = match self.sort {
       SortType::Active => query
         .then_order_by(
@@ -532,26 +367,6 @@ impl<'a> PostQueryBuilder<'a> {
         .then_order_by(post_aggregates::score.desc()),
     };
 
-    // If its for a specific user, show the removed / deleted
-    if let Some(for_creator_id) = self.for_creator_id {
-      query = query.filter(post::creator_id.eq(for_creator_id));
-    }
-
-    if !self.show_nsfw {
-      query = query
-        .filter(post::nsfw.eq(false))
-        .filter(community::nsfw.eq(false));
-    };
-
-    // TODO  These two might be wrong
-    if self.saved_only {
-      query = query.filter(post_saved::id.is_not_null());
-    };
-
-    if self.unread_only {
-      query = query.filter(post_read::id.is_not_null());
-    };
-
     let (limit, offset) = limit_and_offset(self.page, self.limit);
 
     let res = query
@@ -697,17 +512,18 @@ mod tests {
       score: 1,
     };
 
-    let read_post_listings_with_user = PostQueryBuilder::create(&conn, Some(inserted_user.id))
+    let read_post_listings_with_user = PostQueryBuilder::create(&conn)
       .listing_type(&ListingType::Community)
       .sort(&SortType::New)
-      .for_community_id(inserted_community.id)
+      .community_id(inserted_community.id)
+      .my_user_id(inserted_user.id)
       .list()
       .unwrap();
 
-    let read_post_listings_no_user = PostQueryBuilder::create(&conn, None)
+    let read_post_listings_no_user = PostQueryBuilder::create(&conn)
       .listing_type(&ListingType::Community)
       .sort(&SortType::New)
-      .for_community_id(inserted_community.id)
+      .community_id(inserted_community.id)
       .list()
       .unwrap();
 
index ba3bff546af9c2a400e57895f2eb9de1f7439988..67616fbc33bd76a697ca13ceae999506e5149cbc 100644 (file)
@@ -147,254 +147,10 @@ impl UserMentionView {
   }
 }
 
-mod join_types {
-  use crate::schema::{
-    comment,
-    comment_aggregates,
-    comment_like,
-    comment_saved,
-    community,
-    community_follower,
-    community_user_ban,
-    post,
-    user_,
-    user_alias_1,
-    user_mention,
-  };
-  use diesel::{
-    pg::Pg,
-    query_builder::BoxedSelectStatement,
-    query_source::joins::{Inner, Join, JoinOn, LeftOuter},
-    sql_types::*,
-  };
-
-  // /// TODO awful, but necessary because of the boxed join
-  pub(super) type BoxedUserMentionJoin<'a> = BoxedSelectStatement<
-    'a,
-    (
-      (Integer, Integer, Integer, Bool, Timestamp),
-      (
-        Integer,
-        Integer,
-        Integer,
-        Nullable<Integer>,
-        Text,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Bool,
-        Text,
-        Bool,
-      ),
-      (
-        Integer,
-        Text,
-        Nullable<Text>,
-        Nullable<Text>,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Nullable<Text>,
-        Text,
-        Nullable<Text>,
-        Bool,
-        Nullable<Text>,
-        Bool,
-      ),
-      (
-        Integer,
-        Text,
-        Nullable<Text>,
-        Nullable<Text>,
-        Integer,
-        Integer,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Bool,
-        Bool,
-        Bool,
-        Nullable<Text>,
-        Nullable<Text>,
-        Nullable<Text>,
-        Nullable<Text>,
-        Text,
-        Bool,
-      ),
-      (
-        Integer,
-        Text,
-        Text,
-        Nullable<Text>,
-        Integer,
-        Integer,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Bool,
-        Bool,
-        Text,
-        Bool,
-        Nullable<Text>,
-        Nullable<Text>,
-      ),
-      (
-        Integer,
-        Text,
-        Nullable<Text>,
-        Nullable<Text>,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Nullable<Text>,
-        Text,
-        Nullable<Text>,
-        Bool,
-        Nullable<Text>,
-        Bool,
-      ),
-      (Integer, Integer, BigInt, BigInt, BigInt),
-      Nullable<(Integer, Integer, Integer, Timestamp)>,
-      Nullable<(Integer, Integer, Integer, Timestamp, Nullable<Bool>)>,
-      Nullable<(Integer, Integer, Integer, Timestamp)>,
-      Nullable<SmallInt>,
-    ),
-    JoinOn<
-      Join<
-        JoinOn<
-          Join<
-            JoinOn<
-              Join<
-                JoinOn<
-                  Join<
-                    JoinOn<
-                      Join<
-                        JoinOn<
-                          Join<
-                            JoinOn<
-                              Join<
-                                JoinOn<
-                                  Join<
-                                    JoinOn<
-                                      Join<
-                                        JoinOn<
-                                          Join<user_mention::table, comment::table, Inner>,
-                                          diesel::expression::operators::Eq<
-                                            diesel::expression::nullable::Nullable<
-                                              user_mention::columns::comment_id,
-                                            >,
-                                            diesel::expression::nullable::Nullable<
-                                              comment::columns::id,
-                                            >,
-                                          >,
-                                        >,
-                                        user_::table,
-                                        Inner,
-                                      >,
-                                      diesel::expression::operators::Eq<
-                                        comment::columns::creator_id,
-                                        user_::columns::id,
-                                      >,
-                                    >,
-                                    post::table,
-                                    Inner,
-                                  >,
-                                  diesel::expression::operators::Eq<
-                                    comment::columns::post_id,
-                                    post::columns::id,
-                                  >,
-                                >,
-                                community::table,
-                                Inner,
-                              >,
-                              diesel::expression::operators::Eq<
-                                post::columns::community_id,
-                                community::columns::id,
-                              >,
-                            >,
-                            user_alias_1::table,
-                            Inner,
-                          >,
-                          diesel::expression::operators::Eq<
-                            diesel::expression::nullable::Nullable<
-                              user_mention::columns::recipient_id,
-                            >,
-                            diesel::expression::nullable::Nullable<user_alias_1::columns::id>,
-                          >,
-                        >,
-                        comment_aggregates::table,
-                        Inner,
-                      >,
-                      diesel::expression::operators::Eq<
-                        comment::columns::id,
-                        comment_aggregates::columns::comment_id,
-                      >,
-                    >,
-                    community_user_ban::table,
-                    LeftOuter,
-                  >,
-                  diesel::expression::operators::And<
-                    diesel::expression::operators::Eq<
-                      community::columns::id,
-                      community_user_ban::columns::community_id,
-                    >,
-                    diesel::expression::operators::Eq<
-                      community_user_ban::columns::user_id,
-                      comment::columns::creator_id,
-                    >,
-                  >,
-                >,
-                community_follower::table,
-                LeftOuter,
-              >,
-              diesel::expression::operators::And<
-                diesel::expression::operators::Eq<
-                  post::columns::community_id,
-                  community_follower::columns::community_id,
-                >,
-                diesel::expression::operators::Eq<
-                  community_follower::columns::user_id,
-                  diesel::expression::bound::Bound<Integer, i32>,
-                >,
-              >,
-            >,
-            comment_saved::table,
-            LeftOuter,
-          >,
-          diesel::expression::operators::And<
-            diesel::expression::operators::Eq<
-              comment::columns::id,
-              comment_saved::columns::comment_id,
-            >,
-            diesel::expression::operators::Eq<
-              comment_saved::columns::user_id,
-              diesel::expression::bound::Bound<Integer, i32>,
-            >,
-          >,
-        >,
-        comment_like::table,
-        LeftOuter,
-      >,
-      diesel::expression::operators::And<
-        diesel::expression::operators::Eq<comment::columns::id, comment_like::columns::comment_id>,
-        diesel::expression::operators::Eq<
-          comment_like::columns::user_id,
-          diesel::expression::bound::Bound<Integer, i32>,
-        >,
-      >,
-    >,
-    Pg,
-  >;
-}
-
 pub struct UserMentionQueryBuilder<'a> {
   conn: &'a PgConnection,
-  query: join_types::BoxedUserMentionJoin<'a>,
-  for_recipient_id: i32,
+  my_user_id: Option<i32>,
+  recipient_id: Option<i32>,
   sort: &'a SortType,
   unread_only: bool,
   page: Option<i64>,
@@ -402,11 +158,55 @@ pub struct UserMentionQueryBuilder<'a> {
 }
 
 impl<'a> UserMentionQueryBuilder<'a> {
-  pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>, for_recipient_id: i32) -> Self {
+  pub fn create(conn: &'a PgConnection) -> Self {
+    UserMentionQueryBuilder {
+      conn,
+      my_user_id: None,
+      recipient_id: None,
+      sort: &SortType::New,
+      unread_only: false,
+      page: None,
+      limit: None,
+    }
+  }
+
+  pub fn sort(mut self, sort: &'a SortType) -> Self {
+    self.sort = sort;
+    self
+  }
+
+  pub fn unread_only(mut self, unread_only: bool) -> Self {
+    self.unread_only = unread_only;
+    self
+  }
+
+  pub fn recipient_id<T: MaybeOptional<i32>>(mut self, recipient_id: T) -> Self {
+    self.recipient_id = recipient_id.get_optional();
+    self
+  }
+
+  pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
+    self.my_user_id = my_user_id.get_optional();
+    self
+  }
+
+  pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
+    self.page = page.get_optional();
+    self
+  }
+
+  pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
+    self.limit = limit.get_optional();
+    self
+  }
+
+  pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
+    use diesel::dsl::*;
+
     // The left join below will return None in this case
-    let user_id_join = my_user_id.unwrap_or(-1);
+    let user_id_join = self.my_user_id.unwrap_or(-1);
 
-    let query = user_mention::table
+    let mut query = user_mention::table
       .inner_join(comment::table)
       .inner_join(user_::table.on(comment::creator_id.eq(user_::id)))
       .inner_join(post::table.on(comment::post_id.eq(post::id)))
@@ -456,43 +256,9 @@ impl<'a> UserMentionQueryBuilder<'a> {
       ))
       .into_boxed();
 
-    UserMentionQueryBuilder {
-      conn,
-      query,
-      for_recipient_id,
-      sort: &SortType::New,
-      unread_only: false,
-      page: None,
-      limit: None,
+    if let Some(recipient_id) = self.recipient_id {
+      query = query.filter(user_mention::recipient_id.eq(recipient_id));
     }
-  }
-
-  pub fn sort(mut self, sort: &'a SortType) -> Self {
-    self.sort = sort;
-    self
-  }
-
-  pub fn unread_only(mut self, unread_only: bool) -> Self {
-    self.unread_only = unread_only;
-    self
-  }
-
-  pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
-    self.page = page.get_optional();
-    self
-  }
-
-  pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
-    self.limit = limit.get_optional();
-    self
-  }
-
-  pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
-    use diesel::dsl::*;
-
-    let mut query = self.query;
-
-    query = query.filter(user_mention::recipient_id.eq(self.for_recipient_id));
 
     if self.unread_only {
       query = query.filter(user_mention::read.eq(false));
index 6ce559e9597983172d2d35145224447c3e32935b..587ebf617c8da777a0544d08d9be9a4d70339b85 100644 (file)
@@ -70,69 +70,19 @@ impl UserViewSafe {
   }
 }
 
-// TODO can get rid of this by not boxing the query before the list()
-mod join_types {
-  use crate::schema::{user_, user_aggregates};
-  use diesel::{
-    pg::Pg,
-    query_builder::BoxedSelectStatement,
-    query_source::joins::{Inner, Join, JoinOn},
-    sql_types::*,
-  };
-
-  /// TODO awful, but necessary because of the boxed join
-  pub(super) type BoxedUserJoin<'a> = BoxedSelectStatement<
-    'a,
-    (
-      // UserSafe column types
-      (
-        Integer,
-        Text,
-        Nullable<Text>,
-        Nullable<Text>,
-        Bool,
-        Bool,
-        Timestamp,
-        Nullable<Timestamp>,
-        Nullable<Text>,
-        Text,
-        Nullable<Text>,
-        Bool,
-        Nullable<Text>,
-        Bool,
-      ),
-      // UserAggregates column types
-      (Integer, Integer, BigInt, BigInt, BigInt, BigInt),
-    ),
-    JoinOn<
-      Join<user_::table, user_aggregates::table, Inner>,
-      diesel::expression::operators::Eq<
-        diesel::expression::nullable::Nullable<user_aggregates::columns::user_id>,
-        diesel::expression::nullable::Nullable<user_::columns::id>,
-      >,
-    >,
-    Pg,
-  >;
-}
-
 pub struct UserQueryBuilder<'a> {
   conn: &'a PgConnection,
-  query: join_types::BoxedUserJoin<'a>,
   sort: &'a SortType,
+  search_term: Option<String>,
   page: Option<i64>,
   limit: Option<i64>,
 }
 
 impl<'a> UserQueryBuilder<'a> {
   pub fn create(conn: &'a PgConnection) -> Self {
-    let query = user_::table
-      .inner_join(user_aggregates::table)
-      .select((User_::safe_columns_tuple(), user_aggregates::all_columns))
-      .into_boxed();
-
     UserQueryBuilder {
       conn,
-      query,
+      search_term: None,
       sort: &SortType::Hot,
       page: None,
       limit: None,
@@ -145,11 +95,7 @@ impl<'a> UserQueryBuilder<'a> {
   }
 
   pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
-    if let Some(search_term) = search_term.get_optional() {
-      self.query = self
-        .query
-        .filter(user_::name.ilike(fuzzy_search(&search_term)));
-    }
+    self.search_term = search_term.get_optional();
     self
   }
 
@@ -164,7 +110,14 @@ impl<'a> UserQueryBuilder<'a> {
   }
 
   pub fn list(self) -> Result<Vec<UserViewSafe>, Error> {
-    let mut query = self.query;
+    let mut query = user_::table
+      .inner_join(user_aggregates::table)
+      .select((User_::safe_columns_tuple(), user_aggregates::all_columns))
+      .into_boxed();
+
+    if let Some(search_term) = self.search_term {
+      query = query.filter(user_::name.ilike(fuzzy_search(&search_term)));
+    }
 
     query = match self.sort {
       SortType::Hot => query
index 8c8004c1831a6b15f583166872f2eb0a9979d2f7..7a4801f403dd014f28a2b2606d39cdf4dfc4bdcb 100644 (file)
@@ -83,7 +83,7 @@ async fn get_feed_data(
 
   let listing_type_ = listing_type.clone();
   let posts = blocking(context.pool(), move |conn| {
-    PostQueryBuilder::create(&conn, None)
+    PostQueryBuilder::create(&conn)
       .listing_type(&listing_type_)
       .sort(&sort_type)
       .list()
@@ -165,10 +165,10 @@ fn get_feed_user(
   let user = User_::find_by_username(&conn, &user_name)?;
   let user_url = user.get_profile_url(&Settings::get().hostname);
 
-  let posts = PostQueryBuilder::create(&conn, None)
+  let posts = PostQueryBuilder::create(&conn)
     .listing_type(&ListingType::All)
     .sort(sort_type)
-    .for_creator_id(user.id)
+    .creator_id(user.id)
     .list()?;
 
   let items = create_post_items(posts)?;
@@ -191,10 +191,10 @@ fn get_feed_community(
   let site_view = SiteView::read(&conn)?;
   let community = Community::read_from_name(&conn, &community_name)?;
 
-  let posts = PostQueryBuilder::create(&conn, None)
+  let posts = PostQueryBuilder::create(&conn)
     .listing_type(&ListingType::All)
     .sort(sort_type)
-    .for_community_id(community.id)
+    .community_id(community.id)
     .list()?;
 
   let items = create_post_items(posts)?;
@@ -221,8 +221,9 @@ fn get_feed_front(
   let site_view = SiteView::read(&conn)?;
   let user_id = Claims::decode(&jwt)?.claims.id;
 
-  let posts = PostQueryBuilder::create(&conn, Some(user_id))
+  let posts = PostQueryBuilder::create(&conn)
     .listing_type(&ListingType::Subscribed)
+    .my_user_id(user_id)
     .sort(sort_type)
     .list()?;
 
@@ -248,12 +249,15 @@ fn get_feed_inbox(conn: &PgConnection, jwt: String) -> Result<ChannelBuilder, Le
 
   let sort = SortType::New;
 
-  let replies = CommentQueryBuilder::create(&conn, Some(user_id))
-    .for_recipient_id(user_id)
+  let replies = CommentQueryBuilder::create(&conn)
+    .recipient_id(user_id)
+    .my_user_id(user_id)
     .sort(&sort)
     .list()?;
 
-  let mentions = UserMentionQueryBuilder::create(&conn, Some(user_id), user_id)
+  let mentions = UserMentionQueryBuilder::create(&conn)
+    .recipient_id(user_id)
+    .my_user_id(user_id)
     .sort(&sort)
     .list()?;