From 57c2f2ef1c8082f342f1d22f5f7195280702fc10 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 16 Dec 2020 13:59:43 -0500 Subject: [PATCH] Getting rid of terrible boxedjoin types. --- lemmy_api/src/comment.rs | 7 +- lemmy_api/src/community.rs | 9 +- lemmy_api/src/post.rs | 12 +- lemmy_api/src/site.rs | 30 +- lemmy_api/src/user.rs | 26 +- lemmy_db/src/views/comment_view.rs | 447 ++++-------------- lemmy_db/src/views/community_follower_view.rs | 8 +- .../src/views/community_moderator_view.rs | 8 +- lemmy_db/src/views/community_view.rs | 154 ++---- lemmy_db/src/views/post_view.rs | 390 ++++----------- lemmy_db/src/views/user_mention_view.rs | 336 ++----------- lemmy_db/src/views/user_view.rs | 69 +-- src/routes/feeds.rs | 22 +- 13 files changed, 361 insertions(+), 1157 deletions(-) diff --git a/lemmy_api/src/comment.rs b/lemmy_api/src/comment.rs index eed2cb70..b39444ef 100644 --- a/lemmy_api/src/comment.rs +++ b/lemmy_api/src/comment.rs @@ -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() diff --git a/lemmy_api/src/community.rs b/lemmy_api/src/community.rs index d35a4a6c..04059a7c 100644 --- a/lemmy_api/src/community.rs +++ b/lemmy_api/src/community.rs @@ -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() }) diff --git a/lemmy_api/src/post.rs b/lemmy_api/src/post.rs index b0af10a4..d998e7d9 100644 --- a/lemmy_api/src/post.rs +++ b/lemmy_api/src/post.rs @@ -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() diff --git a/lemmy_api/src/site.rs b/lemmy_api/src/site.rs index 3c13b5a0..e8dfaca3 100644 --- a/lemmy_api/src/site.rs +++ b/lemmy_api/src/site.rs @@ -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) diff --git a/lemmy_api/src/user.rs b/lemmy_api/src/user.rs index ddd03d23..17e3fac6 100644 --- a/lemmy_api/src/user.rs +++ b/lemmy_api/src/user.rs @@ -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) diff --git a/lemmy_db/src/views/comment_view.rs b/lemmy_db/src/views/comment_view.rs index 129970af..d85f654f 100644 --- a/lemmy_db/src/views/comment_view.rs +++ b/lemmy_db/src/views/comment_view.rs @@ -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, - Text, - Bool, - Bool, - Timestamp, - Nullable, - Bool, - Text, - Bool, - ), - ( - Integer, - Text, - Nullable, - Nullable, - Bool, - Bool, - Timestamp, - Nullable, - Nullable, - Text, - Nullable, - Bool, - Nullable, - Bool, - ), - Nullable<( - Integer, - Integer, - Integer, - Nullable, - Text, - Bool, - Bool, - Timestamp, - Nullable, - Bool, - Text, - Bool, - )>, - Nullable<( - Integer, - Text, - Nullable, - Nullable, - Bool, - Bool, - Timestamp, - Nullable, - Nullable, - Text, - Nullable, - Bool, - Nullable, - Bool, - )>, - ( - Integer, - Text, - Nullable, - Nullable, - Integer, - Integer, - Bool, - Bool, - Timestamp, - Nullable, - Bool, - Bool, - Bool, - Nullable, - Nullable, - Nullable, - Nullable, - Text, - Bool, - ), - ( - Integer, - Text, - Text, - Nullable, - Integer, - Integer, - Bool, - Timestamp, - Nullable, - Bool, - Bool, - Text, - Bool, - Nullable, - Nullable, - ), - (Integer, Integer, BigInt, BigInt, BigInt), - Nullable<(Integer, Integer, Integer, Timestamp)>, - Nullable<(Integer, Integer, Integer, Timestamp, Nullable)>, - Nullable<(Integer, Integer, Integer, Timestamp)>, - Nullable, - ), - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join, - 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, - diesel::expression::nullable::Nullable, - >, - >, - 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, - >, - >, - 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, - >, - >, - >, - 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, - >, - >, - >, - comment_like::table, - LeftOuter, - >, - diesel::expression::operators::And< - diesel::expression::operators::Eq, - diesel::expression::operators::Eq< - comment_like::columns::user_id, - diesel::expression::bound::Bound, - >, - >, - >, - Pg, - >; -} - pub struct CommentQueryBuilder<'a> { conn: &'a PgConnection, - query: join_types::BoxedCommentJoin<'a>, listing_type: ListingType, sort: &'a SortType, - for_community_id: Option, - for_community_name: Option, - for_post_id: Option, - for_creator_id: Option, - for_recipient_id: Option, + community_id: Option, + community_name: Option, + post_id: Option, + creator_id: Option, + recipient_id: Option, + my_user_id: Option, search_term: Option, 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) -> 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>(mut self, for_post_id: T) -> Self { - self.for_post_id = for_post_id.get_optional(); + pub fn post_id>(mut self, post_id: T) -> Self { + self.post_id = post_id.get_optional(); + self + } + + pub fn creator_id>(mut self, creator_id: T) -> Self { + self.creator_id = creator_id.get_optional(); self } - pub fn for_creator_id>(mut self, for_creator_id: T) -> Self { - self.for_creator_id = for_creator_id.get_optional(); + pub fn recipient_id>(mut self, recipient_id: T) -> Self { + self.recipient_id = recipient_id.get_optional(); self } - pub fn for_recipient_id>(mut self, for_recipient_id: T) -> Self { - self.for_creator_id = for_recipient_id.get_optional(); + pub fn community_id>(mut self, community_id: T) -> Self { + self.community_id = community_id.get_optional(); self } - pub fn for_community_id>(mut self, for_community_id: T) -> Self { - self.for_community_id = for_community_id.get_optional(); + pub fn my_user_id>(mut self, my_user_id: T) -> Self { + self.my_user_id = my_user_id.get_optional(); self } - pub fn for_community_name>(mut self, for_community_name: T) -> Self { - self.for_community_name = for_community_name.get_optional(); + pub fn community_name>(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, 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(); diff --git a/lemmy_db/src/views/community_follower_view.rs b/lemmy_db/src/views/community_follower_view.rs index 64adae3b..7de9cc3a 100644 --- a/lemmy_db/src/views/community_follower_view.rs +++ b/lemmy_db/src/views/community_follower_view.rs @@ -19,24 +19,24 @@ pub struct CommunityFollowerView { type CommunityFollowerViewTuple = (CommunitySafe, UserSafe); impl CommunityFollowerView { - pub fn for_community(conn: &PgConnection, for_community_id: i32) -> Result, Error> { + pub fn for_community(conn: &PgConnection, community_id: i32) -> Result, 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::(conn)?; Ok(Self::to_vec(res)) } - pub fn for_user(conn: &PgConnection, for_user_id: i32) -> Result, Error> { + pub fn for_user(conn: &PgConnection, user_id: i32) -> Result, 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::(conn)?; diff --git a/lemmy_db/src/views/community_moderator_view.rs b/lemmy_db/src/views/community_moderator_view.rs index c98f072a..751e4623 100644 --- a/lemmy_db/src/views/community_moderator_view.rs +++ b/lemmy_db/src/views/community_moderator_view.rs @@ -19,24 +19,24 @@ pub struct CommunityModeratorView { type CommunityModeratorViewTuple = (CommunitySafe, UserSafe); impl CommunityModeratorView { - pub fn for_community(conn: &PgConnection, for_community_id: i32) -> Result, Error> { + pub fn for_community(conn: &PgConnection, community_id: i32) -> Result, 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::(conn)?; Ok(Self::to_vec(res)) } - pub fn for_user(conn: &PgConnection, for_user_id: i32) -> Result, Error> { + pub fn for_user(conn: &PgConnection, user_id: i32) -> Result, 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::(conn)?; diff --git a/lemmy_db/src/views/community_view.rs b/lemmy_db/src/views/community_view.rs index d4518f7f..fcc707c0 100644 --- a/lemmy_db/src/views/community_view.rs +++ b/lemmy_db/src/views/community_view.rs @@ -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, - Integer, - Integer, - Bool, - Timestamp, - Nullable, - Bool, - Bool, - Text, - Bool, - Nullable, - Nullable, - ), - ( - Integer, - Text, - Nullable, - Nullable, - Bool, - Bool, - Timestamp, - Nullable, - Nullable, - Text, - Nullable, - Bool, - Nullable, - Bool, - ), - (Integer, Text), - (Integer, Integer, BigInt, BigInt, BigInt), - Nullable<(Integer, Integer, Integer, Timestamp, Nullable)>, - ), - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join, - diesel::expression::operators::Eq< - diesel::expression::nullable::Nullable, - diesel::expression::nullable::Nullable, - >, - >, - category::table, - Inner, - >, - diesel::expression::operators::Eq< - diesel::expression::nullable::Nullable, - diesel::expression::nullable::Nullable, - >, - >, - community_aggregates::table, - Inner, - >, - diesel::expression::operators::Eq< - diesel::expression::nullable::Nullable, - diesel::expression::nullable::Nullable, - >, - >, - 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, - >, - >, - >, - Pg, - >; -} - pub struct CommunityQueryBuilder<'a> { conn: &'a PgConnection, - query: join_types::BoxedCommunityJoin<'a>, sort: &'a SortType, + my_user_id: Option, show_nsfw: bool, search_term: Option, page: Option, @@ -182,33 +85,10 @@ pub struct CommunityQueryBuilder<'a> { } impl<'a> CommunityQueryBuilder<'a> { - pub fn create(conn: &'a PgConnection, my_user_id: Option) -> 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>(mut self, my_user_id: T) -> Self { + self.my_user_id = my_user_id.get_optional(); + self + } + pub fn page>(mut self, page: T) -> Self { self.page = page.get_optional(); self @@ -243,7 +128,28 @@ impl<'a> CommunityQueryBuilder<'a> { } pub fn list(self) -> Result, 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); diff --git a/lemmy_db/src/views/post_view.rs b/lemmy_db/src/views/post_view.rs index 9791d0a8..f95cf118 100644 --- a/lemmy_db/src/views/post_view.rs +++ b/lemmy_db/src/views/post_view.rs @@ -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, - Nullable, - Integer, - Integer, - Bool, - Bool, - Timestamp, - Nullable, - Bool, - Bool, - Bool, - Nullable, - Nullable, - Nullable, - Nullable, - Text, - Bool, - ), - ( - Integer, - Text, - Nullable, - Nullable, - Bool, - Bool, - Timestamp, - Nullable, - Nullable, - Text, - Nullable, - Bool, - Nullable, - Bool, - ), - ( - Integer, - Text, - Text, - Nullable, - Integer, - Integer, - Bool, - Timestamp, - Nullable, - Bool, - Bool, - Text, - Bool, - Nullable, - Nullable, - ), - Nullable<(Integer, Integer, Integer, Timestamp)>, - (Integer, Integer, BigInt, BigInt, BigInt, BigInt, Timestamp), - Nullable<(Integer, Integer, Integer, Timestamp, Nullable)>, - Nullable<(Integer, Integer, Integer, Timestamp)>, - Nullable<(Integer, Integer, Integer, Timestamp)>, - Nullable, - ), - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join, - diesel::expression::operators::Eq< - diesel::expression::nullable::Nullable< - post::columns::creator_id, - >, - diesel::expression::nullable::Nullable, - >, - >, - community::table, - Inner, - >, - diesel::expression::operators::Eq< - diesel::expression::nullable::Nullable, - diesel::expression::nullable::Nullable, - >, - >, - 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, - diesel::expression::nullable::Nullable, - >, - >, - 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, - >, - >, - >, - post_saved::table, - LeftOuter, - >, - diesel::expression::operators::And< - diesel::expression::operators::Eq, - diesel::expression::operators::Eq< - post_saved::columns::user_id, - diesel::expression::bound::Bound, - >, - >, - >, - post_read::table, - LeftOuter, - >, - diesel::expression::operators::And< - diesel::expression::operators::Eq, - diesel::expression::operators::Eq< - post_read::columns::user_id, - diesel::expression::bound::Bound, - >, - >, - >, - post_like::table, - LeftOuter, - >, - diesel::expression::operators::And< - diesel::expression::operators::Eq, - diesel::expression::operators::Eq< - post_like::columns::user_id, - diesel::expression::bound::Bound, - >, - >, - >, - Pg, - >; -} - pub struct PostQueryBuilder<'a> { conn: &'a PgConnection, - query: join_types::BoxedPostJoin<'a>, listing_type: &'a ListingType, sort: &'a SortType, - for_creator_id: Option, - for_community_id: Option, - for_community_name: Option, + creator_id: Option, + community_id: Option, + community_name: Option, + my_user_id: Option, search_term: Option, url_search: Option, 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) -> 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>(mut self, for_community_id: T) -> Self { - self.for_community_id = for_community_id.get_optional(); + pub fn community_id>(mut self, community_id: T) -> Self { + self.community_id = community_id.get_optional(); + self + } + + pub fn my_user_id>(mut self, my_user_id: T) -> Self { + self.community_id = my_user_id.get_optional(); self } - pub fn for_community_name>(mut self, for_community_name: T) -> Self { - self.for_community_name = for_community_name.get_optional(); + pub fn community_name>(mut self, community_name: T) -> Self { + self.community_name = community_name.get_optional(); self } - pub fn for_creator_id>(mut self, for_creator_id: T) -> Self { - self.for_creator_id = for_creator_id.get_optional(); + pub fn creator_id>(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, 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(); diff --git a/lemmy_db/src/views/user_mention_view.rs b/lemmy_db/src/views/user_mention_view.rs index ba3bff54..67616fbc 100644 --- a/lemmy_db/src/views/user_mention_view.rs +++ b/lemmy_db/src/views/user_mention_view.rs @@ -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, - Text, - Bool, - Bool, - Timestamp, - Nullable, - Bool, - Text, - Bool, - ), - ( - Integer, - Text, - Nullable, - Nullable, - Bool, - Bool, - Timestamp, - Nullable, - Nullable, - Text, - Nullable, - Bool, - Nullable, - Bool, - ), - ( - Integer, - Text, - Nullable, - Nullable, - Integer, - Integer, - Bool, - Bool, - Timestamp, - Nullable, - Bool, - Bool, - Bool, - Nullable, - Nullable, - Nullable, - Nullable, - Text, - Bool, - ), - ( - Integer, - Text, - Text, - Nullable, - Integer, - Integer, - Bool, - Timestamp, - Nullable, - Bool, - Bool, - Text, - Bool, - Nullable, - Nullable, - ), - ( - Integer, - Text, - Nullable, - Nullable, - Bool, - Bool, - Timestamp, - Nullable, - Nullable, - Text, - Nullable, - Bool, - Nullable, - Bool, - ), - (Integer, Integer, BigInt, BigInt, BigInt), - Nullable<(Integer, Integer, Integer, Timestamp)>, - Nullable<(Integer, Integer, Integer, Timestamp, Nullable)>, - Nullable<(Integer, Integer, Integer, Timestamp)>, - Nullable, - ), - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join< - JoinOn< - Join, - 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, - >, - >, - 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, - >, - >, - >, - 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, - >, - >, - >, - comment_like::table, - LeftOuter, - >, - diesel::expression::operators::And< - diesel::expression::operators::Eq, - diesel::expression::operators::Eq< - comment_like::columns::user_id, - diesel::expression::bound::Bound, - >, - >, - >, - Pg, - >; -} - pub struct UserMentionQueryBuilder<'a> { conn: &'a PgConnection, - query: join_types::BoxedUserMentionJoin<'a>, - for_recipient_id: i32, + my_user_id: Option, + recipient_id: Option, sort: &'a SortType, unread_only: bool, page: Option, @@ -402,11 +158,55 @@ pub struct UserMentionQueryBuilder<'a> { } impl<'a> UserMentionQueryBuilder<'a> { - pub fn create(conn: &'a PgConnection, my_user_id: Option, 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>(mut self, recipient_id: T) -> Self { + self.recipient_id = recipient_id.get_optional(); + self + } + + pub fn my_user_id>(mut self, my_user_id: T) -> Self { + self.my_user_id = my_user_id.get_optional(); + self + } + + pub fn page>(mut self, page: T) -> Self { + self.page = page.get_optional(); + self + } + + pub fn limit>(mut self, limit: T) -> Self { + self.limit = limit.get_optional(); + self + } + + pub fn list(self) -> Result, 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>(mut self, page: T) -> Self { - self.page = page.get_optional(); - self - } - - pub fn limit>(mut self, limit: T) -> Self { - self.limit = limit.get_optional(); - self - } - - pub fn list(self) -> Result, 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)); diff --git a/lemmy_db/src/views/user_view.rs b/lemmy_db/src/views/user_view.rs index 6ce559e9..587ebf61 100644 --- a/lemmy_db/src/views/user_view.rs +++ b/lemmy_db/src/views/user_view.rs @@ -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, - Nullable, - Bool, - Bool, - Timestamp, - Nullable, - Nullable, - Text, - Nullable, - Bool, - Nullable, - Bool, - ), - // UserAggregates column types - (Integer, Integer, BigInt, BigInt, BigInt, BigInt), - ), - JoinOn< - Join, - diesel::expression::operators::Eq< - diesel::expression::nullable::Nullable, - diesel::expression::nullable::Nullable, - >, - >, - Pg, - >; -} - pub struct UserQueryBuilder<'a> { conn: &'a PgConnection, - query: join_types::BoxedUserJoin<'a>, sort: &'a SortType, + search_term: Option, page: Option, limit: Option, } 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>(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, 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 diff --git a/src/routes/feeds.rs b/src/routes/feeds.rs index 8c8004c1..7a4801f4 100644 --- a/src/routes/feeds.rs +++ b/src/routes/feeds.rs @@ -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