Getting rid of terrible boxedjoin types.

pull/1329/head
Dessalines 2020-12-16 13:59:43 -05:00
parent 711db4c790
commit 57c2f2ef1c
13 changed files with 392 additions and 1188 deletions

View File

@ -669,11 +669,12 @@ impl Perform for GetComments {
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;
let comments = blocking(context.pool(), move |conn| { let comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn, user_id) CommentQueryBuilder::create(conn)
.listing_type(type_) .listing_type(type_)
.sort(&sort) .sort(&sort)
.for_community_id(community_id) .community_id(community_id)
.for_community_name(community_name) .community_name(community_name)
.my_user_id(user_id)
.page(page) .page(page)
.limit(limit) .limit(limit)
.list() .list()

View File

@ -438,9 +438,10 @@ impl Perform for ListCommunities {
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;
let communities = blocking(context.pool(), move |conn| { let communities = blocking(context.pool(), move |conn| {
CommunityQueryBuilder::create(conn, user_id) CommunityQueryBuilder::create(conn)
.sort(&sort) .sort(&sort)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.my_user_id(user_id)
.page(page) .page(page)
.limit(limit) .limit(limit)
.list() .list()
@ -591,9 +592,9 @@ impl Perform for BanFromCommunity {
// Comments // Comments
// Diesel doesn't allow updates with joins, so this has to be a loop // Diesel doesn't allow updates with joins, so this has to be a loop
let comments = blocking(context.pool(), move |conn| { let comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn, None) CommentQueryBuilder::create(conn)
.for_creator_id(banned_user_id) .creator_id(banned_user_id)
.for_community_id(community_id) .community_id(community_id)
.limit(std::i64::MAX) .limit(std::i64::MAX)
.list() .list()
}) })

View File

@ -181,8 +181,9 @@ impl Perform for GetPost {
let id = data.id; let id = data.id;
let comments = blocking(context.pool(), move |conn| { let comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn, user_id) CommentQueryBuilder::create(conn)
.for_post_id(id) .my_user_id(user_id)
.post_id(id)
.limit(9999) .limit(9999)
.list() .list()
}) })
@ -247,12 +248,13 @@ impl Perform for GetPosts {
let community_id = data.community_id; let community_id = data.community_id;
let community_name = data.community_name.to_owned(); let community_name = data.community_name.to_owned();
let posts = match blocking(context.pool(), move |conn| { let posts = match blocking(context.pool(), move |conn| {
PostQueryBuilder::create(conn, user_id) PostQueryBuilder::create(conn)
.listing_type(&type_) .listing_type(&type_)
.sort(&sort) .sort(&sort)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.for_community_id(community_id) .community_id(community_id)
.for_community_name(community_name) .community_name(community_name)
.my_user_id(user_id)
.page(page) .page(page)
.limit(limit) .limit(limit)
.list() .list()

View File

@ -363,11 +363,12 @@ impl Perform for Search {
match type_ { match type_ {
SearchType::Posts => { SearchType::Posts => {
posts = blocking(context.pool(), move |conn| { posts = blocking(context.pool(), move |conn| {
PostQueryBuilder::create(conn, user_id) PostQueryBuilder::create(conn)
.sort(&sort) .sort(&sort)
.show_nsfw(true) .show_nsfw(true)
.for_community_id(community_id) .community_id(community_id)
.for_community_name(community_name) .community_name(community_name)
.my_user_id(user_id)
.search_term(q) .search_term(q)
.page(page) .page(page)
.limit(limit) .limit(limit)
@ -377,9 +378,10 @@ impl Perform for Search {
} }
SearchType::Comments => { SearchType::Comments => {
comments = blocking(context.pool(), move |conn| { comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(&conn, user_id) CommentQueryBuilder::create(&conn)
.sort(&sort) .sort(&sort)
.search_term(q) .search_term(q)
.my_user_id(user_id)
.page(page) .page(page)
.limit(limit) .limit(limit)
.list() .list()
@ -388,7 +390,7 @@ impl Perform for Search {
} }
SearchType::Communities => { SearchType::Communities => {
communities = blocking(context.pool(), move |conn| { communities = blocking(context.pool(), move |conn| {
CommunityQueryBuilder::create(conn, None) CommunityQueryBuilder::create(conn)
.sort(&sort) .sort(&sort)
.search_term(q) .search_term(q)
.page(page) .page(page)
@ -410,11 +412,12 @@ impl Perform for Search {
} }
SearchType::All => { SearchType::All => {
posts = blocking(context.pool(), move |conn| { posts = blocking(context.pool(), move |conn| {
PostQueryBuilder::create(conn, user_id) PostQueryBuilder::create(conn)
.sort(&sort) .sort(&sort)
.show_nsfw(true) .show_nsfw(true)
.for_community_id(community_id) .community_id(community_id)
.for_community_name(community_name) .community_name(community_name)
.my_user_id(user_id)
.search_term(q) .search_term(q)
.page(page) .page(page)
.limit(limit) .limit(limit)
@ -426,9 +429,10 @@ impl Perform for Search {
let sort = SortType::from_str(&data.sort)?; let sort = SortType::from_str(&data.sort)?;
comments = blocking(context.pool(), move |conn| { comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn, user_id) CommentQueryBuilder::create(conn)
.sort(&sort) .sort(&sort)
.search_term(q) .search_term(q)
.my_user_id(user_id)
.page(page) .page(page)
.limit(limit) .limit(limit)
.list() .list()
@ -439,7 +443,7 @@ impl Perform for Search {
let sort = SortType::from_str(&data.sort)?; let sort = SortType::from_str(&data.sort)?;
communities = blocking(context.pool(), move |conn| { communities = blocking(context.pool(), move |conn| {
CommunityQueryBuilder::create(conn, None) CommunityQueryBuilder::create(conn)
.sort(&sort) .sort(&sort)
.search_term(q) .search_term(q)
.page(page) .page(page)
@ -463,11 +467,11 @@ impl Perform for Search {
} }
SearchType::Url => { SearchType::Url => {
posts = blocking(context.pool(), move |conn| { posts = blocking(context.pool(), move |conn| {
PostQueryBuilder::create(conn, None) PostQueryBuilder::create(conn)
.sort(&sort) .sort(&sort)
.show_nsfw(true) .show_nsfw(true)
.for_community_id(community_id) .community_id(community_id)
.for_community_name(community_name) .community_name(community_name)
.url_search(q) .url_search(q)
.page(page) .page(page)
.limit(limit) .limit(limit)

View File

@ -536,15 +536,17 @@ impl Perform for GetUserDetails {
let community_id = data.community_id; let community_id = data.community_id;
let (posts, comments) = blocking(context.pool(), move |conn| { 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) .sort(&sort)
.show_nsfw(show_nsfw) .show_nsfw(show_nsfw)
.saved_only(saved_only) .saved_only(saved_only)
.for_community_id(community_id) .community_id(community_id)
.my_user_id(user_id)
.page(page) .page(page)
.limit(limit); .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) .sort(&sort)
.saved_only(saved_only) .saved_only(saved_only)
.page(page) .page(page)
@ -553,8 +555,8 @@ impl Perform for GetUserDetails {
// If its saved only, you don't care what creator it was // 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 // Or, if its not saved, then you only want it for that specific creator
if !saved_only { if !saved_only {
posts_query = posts_query.for_creator_id(user_details_id); posts_query = posts_query.creator_id(user_details_id);
comments_query = comments_query.for_creator_id(user_details_id); comments_query = comments_query.creator_id(user_details_id);
} }
let posts = posts_query.list()?; let posts = posts_query.list()?;
@ -741,10 +743,11 @@ impl Perform for GetReplies {
let unread_only = data.unread_only; let unread_only = data.unread_only;
let user_id = user.id; let user_id = user.id;
let replies = blocking(context.pool(), move |conn| { let replies = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn, Some(user_id)) CommentQueryBuilder::create(conn)
.sort(&sort) .sort(&sort)
.unread_only(unread_only) .unread_only(unread_only)
.for_recipient_id(user_id) .recipient_id(user_id)
.my_user_id(user_id)
.page(page) .page(page)
.limit(limit) .limit(limit)
.list() .list()
@ -774,7 +777,9 @@ impl Perform for GetUserMentions {
let unread_only = data.unread_only; let unread_only = data.unread_only;
let user_id = user.id; let user_id = user.id;
let mentions = blocking(context.pool(), move |conn| { 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) .sort(&sort)
.unread_only(unread_only) .unread_only(unread_only)
.page(page) .page(page)
@ -841,8 +846,9 @@ impl Perform for MarkAllAsRead {
let user_id = user.id; let user_id = user.id;
let replies = blocking(context.pool(), move |conn| { let replies = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn, Some(user_id)) CommentQueryBuilder::create(conn)
.for_recipient_id(user_id) .my_user_id(user_id)
.recipient_id(user_id)
.unread_only(true) .unread_only(true)
.page(1) .page(1)
.limit(999) .limit(999)

View 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> { pub struct CommentQueryBuilder<'a> {
conn: &'a PgConnection, conn: &'a PgConnection,
query: join_types::BoxedCommentJoin<'a>,
listing_type: ListingType, listing_type: ListingType,
sort: &'a SortType, sort: &'a SortType,
for_community_id: Option<i32>, community_id: Option<i32>,
for_community_name: Option<String>, community_name: Option<String>,
for_post_id: Option<i32>, post_id: Option<i32>,
for_creator_id: Option<i32>, creator_id: Option<i32>,
for_recipient_id: Option<i32>, recipient_id: Option<i32>,
my_user_id: Option<i32>,
search_term: Option<String>, search_term: Option<String>,
saved_only: bool, saved_only: bool,
unread_only: bool, unread_only: bool,
@ -424,11 +165,97 @@ pub struct CommentQueryBuilder<'a> {
} }
impl<'a> CommentQueryBuilder<'a> { impl<'a> CommentQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self { pub fn create(conn: &'a PgConnection) -> Self {
// The left join below will return None in this case CommentQueryBuilder {
let user_id_join = my_user_id.unwrap_or(-1); conn,
listing_type: ListingType::All,
sort: &SortType::New,
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,
page: None,
limit: None,
}
}
let query = comment::table pub fn listing_type(mut self, listing_type: ListingType) -> Self {
self.listing_type = listing_type;
self
}
pub fn sort(mut self, sort: &'a SortType) -> Self {
self.sort = sort;
self
}
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 recipient_id<T: MaybeOptional<i32>>(mut self, recipient_id: T) -> Self {
self.recipient_id = recipient_id.get_optional();
self
}
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.my_user_id = my_user_id.get_optional();
self
}
pub fn community_name<T: MaybeOptional<String>>(mut self, community_name: T) -> Self {
self.community_name = community_name.get_optional();
self
}
pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
self.search_term = search_term.get_optional();
self
}
pub fn saved_only(mut self, saved_only: bool) -> Self {
self.saved_only = saved_only;
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<CommentView>, Error> {
use diesel::dsl::*;
// 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) .inner_join(user_::table)
// recipient here // recipient here
.left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id))) .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
@ -479,94 +306,11 @@ impl<'a> CommentQueryBuilder<'a> {
)) ))
.into_boxed(); .into_boxed();
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,
search_term: None,
saved_only: false,
unread_only: false,
page: None,
limit: None,
}
}
pub fn listing_type(mut self, listing_type: ListingType) -> Self {
self.listing_type = listing_type;
self
}
pub fn sort(mut self, sort: &'a SortType) -> Self {
self.sort = sort;
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();
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();
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();
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();
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();
self
}
pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
self.search_term = search_term.get_optional();
self
}
pub fn saved_only(mut self, saved_only: bool) -> Self {
self.saved_only = saved_only;
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<CommentView>, Error> {
use diesel::dsl::*;
let mut query = self.query;
// The replies // The replies
if let Some(for_recipient_id) = self.for_recipient_id { if let Some(recipient_id) = self.recipient_id {
query = query query = query
// TODO needs lots of testing // 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::deleted.eq(false))
.filter(comment::removed.eq(false)); .filter(comment::removed.eq(false));
} }
@ -575,22 +319,22 @@ impl<'a> CommentQueryBuilder<'a> {
query = query.filter(comment::read.eq(false)); query = query.filter(comment::read.eq(false));
} }
if let Some(for_creator_id) = self.for_creator_id { if let Some(creator_id) = self.creator_id {
query = query.filter(comment::creator_id.eq(for_creator_id)); query = query.filter(comment::creator_id.eq(creator_id));
}; };
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)); 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 query = query
.filter(community::name.eq(for_community_name)) .filter(community::name.eq(community_name))
.filter(comment::local.eq(true)); .filter(comment::local.eq(true));
} }
if let Some(for_post_id) = self.for_post_id { if let Some(post_id) = self.post_id {
query = query.filter(comment::post_id.eq(for_post_id)); query = query.filter(comment::post_id.eq(post_id));
}; };
if let Some(search_term) = self.search_term { 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(); let mut expected_comment_view_with_user = expected_comment_view_no_user.to_owned();
expected_comment_view_with_user.my_vote = Some(1); expected_comment_view_with_user.my_vote = Some(1);
let read_comment_views_no_user = CommentQueryBuilder::create(&conn, None) let read_comment_views_no_user = CommentQueryBuilder::create(&conn)
.for_post_id(inserted_post.id) .post_id(inserted_post.id)
.list() .list()
.unwrap(); .unwrap();
let read_comment_views_with_user = CommentQueryBuilder::create(&conn, Some(inserted_user.id)) let read_comment_views_with_user = CommentQueryBuilder::create(&conn)
.for_post_id(inserted_post.id) .post_id(inserted_post.id)
.my_user_id(inserted_user.id)
.list() .list()
.unwrap(); .unwrap();

View File

@ -19,24 +19,24 @@ pub struct CommunityFollowerView {
type CommunityFollowerViewTuple = (CommunitySafe, UserSafe); type CommunityFollowerViewTuple = (CommunitySafe, UserSafe);
impl CommunityFollowerView { 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 let res = community_follower::table
.inner_join(community::table) .inner_join(community::table)
.inner_join(user_::table) .inner_join(user_::table)
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple())) .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) .order_by(community_follower::published)
.load::<CommunityFollowerViewTuple>(conn)?; .load::<CommunityFollowerViewTuple>(conn)?;
Ok(Self::to_vec(res)) 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 let res = community_follower::table
.inner_join(community::table) .inner_join(community::table)
.inner_join(user_::table) .inner_join(user_::table)
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple())) .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) .order_by(community_follower::published)
.load::<CommunityFollowerViewTuple>(conn)?; .load::<CommunityFollowerViewTuple>(conn)?;

View File

@ -19,24 +19,24 @@ pub struct CommunityModeratorView {
type CommunityModeratorViewTuple = (CommunitySafe, UserSafe); type CommunityModeratorViewTuple = (CommunitySafe, UserSafe);
impl CommunityModeratorView { 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 let res = community_moderator::table
.inner_join(community::table) .inner_join(community::table)
.inner_join(user_::table) .inner_join(user_::table)
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple())) .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) .order_by(community_moderator::published)
.load::<CommunityModeratorViewTuple>(conn)?; .load::<CommunityModeratorViewTuple>(conn)?;
Ok(Self::to_vec(res)) 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 let res = community_moderator::table
.inner_join(community::table) .inner_join(community::table)
.inner_join(user_::table) .inner_join(user_::table)
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple())) .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) .order_by(community_moderator::published)
.load::<CommunityModeratorViewTuple>(conn)?; .load::<CommunityModeratorViewTuple>(conn)?;

View 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> { pub struct CommunityQueryBuilder<'a> {
conn: &'a PgConnection, conn: &'a PgConnection,
query: join_types::BoxedCommunityJoin<'a>,
sort: &'a SortType, sort: &'a SortType,
my_user_id: Option<i32>,
show_nsfw: bool, show_nsfw: bool,
search_term: Option<String>, search_term: Option<String>,
page: Option<i64>, page: Option<i64>,
@ -182,33 +85,10 @@ pub struct CommunityQueryBuilder<'a> {
} }
impl<'a> CommunityQueryBuilder<'a> { impl<'a> CommunityQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self { pub fn create(conn: &'a PgConnection) -> 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();
CommunityQueryBuilder { CommunityQueryBuilder {
conn, conn,
query, my_user_id: None,
sort: &SortType::Hot, sort: &SortType::Hot,
show_nsfw: true, show_nsfw: true,
search_term: None, search_term: None,
@ -232,6 +112,11 @@ impl<'a> CommunityQueryBuilder<'a> {
self 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 { pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
self.page = page.get_optional(); self.page = page.get_optional();
self self
@ -243,7 +128,28 @@ impl<'a> CommunityQueryBuilder<'a> {
} }
pub fn list(self) -> Result<Vec<CommunityView>, Error> { 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 { if let Some(search_term) = self.search_term {
let searcher = fuzzy_search(&search_term); let searcher = fuzzy_search(&search_term);

View 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> { pub struct PostQueryBuilder<'a> {
conn: &'a PgConnection, conn: &'a PgConnection,
query: join_types::BoxedPostJoin<'a>,
listing_type: &'a ListingType, listing_type: &'a ListingType,
sort: &'a SortType, sort: &'a SortType,
for_creator_id: Option<i32>, creator_id: Option<i32>,
for_community_id: Option<i32>, community_id: Option<i32>,
for_community_name: Option<String>, community_name: Option<String>,
my_user_id: Option<i32>,
search_term: Option<String>, search_term: Option<String>,
url_search: Option<String>, url_search: Option<String>,
show_nsfw: bool, show_nsfw: bool,
@ -341,11 +153,92 @@ pub struct PostQueryBuilder<'a> {
} }
impl<'a> PostQueryBuilder<'a> { impl<'a> PostQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self { pub fn create(conn: &'a PgConnection) -> Self {
// The left join below will return None in this case PostQueryBuilder {
let user_id_join = my_user_id.unwrap_or(-1); conn,
listing_type: &ListingType::All,
sort: &SortType::Hot,
creator_id: None,
community_id: None,
community_name: None,
my_user_id: None,
search_term: None,
url_search: None,
show_nsfw: true,
saved_only: false,
unread_only: false,
page: None,
limit: None,
}
}
let query = post::table pub fn listing_type(mut self, listing_type: &'a ListingType) -> Self {
self.listing_type = listing_type;
self
}
pub fn sort(mut self, sort: &'a SortType) -> Self {
self.sort = sort;
self
}
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 community_name<T: MaybeOptional<String>>(mut self, community_name: T) -> Self {
self.community_name = community_name.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 search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
self.search_term = search_term.get_optional();
self
}
pub fn url_search<T: MaybeOptional<String>>(mut self, url_search: T) -> Self {
self.url_search = url_search.get_optional();
self
}
pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
self.show_nsfw = show_nsfw;
self
}
pub fn saved_only(mut self, saved_only: bool) -> Self {
self.saved_only = saved_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<PostView>, Error> {
use diesel::dsl::*;
// 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(user_::table)
.inner_join(community::table) .inner_join(community::table)
.left_join( .left_join(
@ -397,99 +290,21 @@ impl<'a> PostQueryBuilder<'a> {
)) ))
.into_boxed(); .into_boxed();
PostQueryBuilder {
conn,
query,
listing_type: &ListingType::All,
sort: &SortType::Hot,
for_creator_id: None,
for_community_id: None,
for_community_name: None,
search_term: None,
url_search: None,
show_nsfw: true,
saved_only: false,
unread_only: false,
page: None,
limit: None,
}
}
pub fn listing_type(mut self, listing_type: &'a ListingType) -> Self {
self.listing_type = listing_type;
self
}
pub fn sort(mut self, sort: &'a SortType) -> Self {
self.sort = sort;
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();
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();
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();
self
}
pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
self.search_term = search_term.get_optional();
self
}
pub fn url_search<T: MaybeOptional<String>>(mut self, url_search: T) -> Self {
self.url_search = url_search.get_optional();
self
}
pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
self.show_nsfw = show_nsfw;
self
}
pub fn saved_only(mut self, saved_only: bool) -> Self {
self.saved_only = saved_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<PostView>, Error> {
use diesel::dsl::*;
let mut query = self.query;
query = match self.listing_type { 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)), ListingType::Subscribed => query.filter(community_follower::user_id.is_not_null()), // TODO could be this: and(community_follower::user_id.eq(user_id_join)),
ListingType::Local => query.filter(community::local.eq(true)), ListingType::Local => query.filter(community::local.eq(true)),
_ => query, _ => query,
}; };
if let Some(for_community_id) = self.for_community_id { if let Some(community_id) = self.community_id {
query = query query = query
.filter(post::community_id.eq(for_community_id)) .filter(post::community_id.eq(community_id))
.then_order_by(post::stickied.desc()); .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 query = query
.filter(community::name.eq(for_community_name)) .filter(community::name.eq(community_name))
.filter(community::local.eq(true)) .filter(community::local.eq(true))
.then_order_by(post::stickied.desc()); .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 { query = match self.sort {
SortType::Active => query SortType::Active => query
.then_order_by( .then_order_by(
@ -532,26 +367,6 @@ impl<'a> PostQueryBuilder<'a> {
.then_order_by(post_aggregates::score.desc()), .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 (limit, offset) = limit_and_offset(self.page, self.limit);
let res = query let res = query
@ -697,17 +512,18 @@ mod tests {
score: 1, 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) .listing_type(&ListingType::Community)
.sort(&SortType::New) .sort(&SortType::New)
.for_community_id(inserted_community.id) .community_id(inserted_community.id)
.my_user_id(inserted_user.id)
.list() .list()
.unwrap(); .unwrap();
let read_post_listings_no_user = PostQueryBuilder::create(&conn, None) let read_post_listings_no_user = PostQueryBuilder::create(&conn)
.listing_type(&ListingType::Community) .listing_type(&ListingType::Community)
.sort(&SortType::New) .sort(&SortType::New)
.for_community_id(inserted_community.id) .community_id(inserted_community.id)
.list() .list()
.unwrap(); .unwrap();

View 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> { pub struct UserMentionQueryBuilder<'a> {
conn: &'a PgConnection, conn: &'a PgConnection,
query: join_types::BoxedUserMentionJoin<'a>, my_user_id: Option<i32>,
for_recipient_id: i32, recipient_id: Option<i32>,
sort: &'a SortType, sort: &'a SortType,
unread_only: bool, unread_only: bool,
page: Option<i64>, page: Option<i64>,
@ -402,11 +158,55 @@ pub struct UserMentionQueryBuilder<'a> {
} }
impl<'a> 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 {
// The left join below will return None in this case UserMentionQueryBuilder {
let user_id_join = my_user_id.unwrap_or(-1); conn,
my_user_id: None,
recipient_id: None,
sort: &SortType::New,
unread_only: false,
page: None,
limit: None,
}
}
let query = user_mention::table 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 = self.my_user_id.unwrap_or(-1);
let mut query = user_mention::table
.inner_join(comment::table) .inner_join(comment::table)
.inner_join(user_::table.on(comment::creator_id.eq(user_::id))) .inner_join(user_::table.on(comment::creator_id.eq(user_::id)))
.inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(post::table.on(comment::post_id.eq(post::id)))
@ -456,43 +256,9 @@ impl<'a> UserMentionQueryBuilder<'a> {
)) ))
.into_boxed(); .into_boxed();
UserMentionQueryBuilder { if let Some(recipient_id) = self.recipient_id {
conn, query = query.filter(user_mention::recipient_id.eq(recipient_id));
query,
for_recipient_id,
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 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 { if self.unread_only {
query = query.filter(user_mention::read.eq(false)); query = query.filter(user_mention::read.eq(false));

View 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> { pub struct UserQueryBuilder<'a> {
conn: &'a PgConnection, conn: &'a PgConnection,
query: join_types::BoxedUserJoin<'a>,
sort: &'a SortType, sort: &'a SortType,
search_term: Option<String>,
page: Option<i64>, page: Option<i64>,
limit: Option<i64>, limit: Option<i64>,
} }
impl<'a> UserQueryBuilder<'a> { impl<'a> UserQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection) -> Self { 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 { UserQueryBuilder {
conn, conn,
query, search_term: None,
sort: &SortType::Hot, sort: &SortType::Hot,
page: None, page: None,
limit: None, limit: None,
@ -145,11 +95,7 @@ impl<'a> UserQueryBuilder<'a> {
} }
pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self { pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
if let Some(search_term) = search_term.get_optional() { self.search_term = search_term.get_optional();
self.query = self
.query
.filter(user_::name.ilike(fuzzy_search(&search_term)));
}
self self
} }
@ -164,7 +110,14 @@ impl<'a> UserQueryBuilder<'a> {
} }
pub fn list(self) -> Result<Vec<UserViewSafe>, Error> { 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 { query = match self.sort {
SortType::Hot => query SortType::Hot => query

View File

@ -83,7 +83,7 @@ async fn get_feed_data(
let listing_type_ = listing_type.clone(); let listing_type_ = listing_type.clone();
let posts = blocking(context.pool(), move |conn| { let posts = blocking(context.pool(), move |conn| {
PostQueryBuilder::create(&conn, None) PostQueryBuilder::create(&conn)
.listing_type(&listing_type_) .listing_type(&listing_type_)
.sort(&sort_type) .sort(&sort_type)
.list() .list()
@ -165,10 +165,10 @@ fn get_feed_user(
let user = User_::find_by_username(&conn, &user_name)?; let user = User_::find_by_username(&conn, &user_name)?;
let user_url = user.get_profile_url(&Settings::get().hostname); 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) .listing_type(&ListingType::All)
.sort(sort_type) .sort(sort_type)
.for_creator_id(user.id) .creator_id(user.id)
.list()?; .list()?;
let items = create_post_items(posts)?; let items = create_post_items(posts)?;
@ -191,10 +191,10 @@ fn get_feed_community(
let site_view = SiteView::read(&conn)?; let site_view = SiteView::read(&conn)?;
let community = Community::read_from_name(&conn, &community_name)?; let community = Community::read_from_name(&conn, &community_name)?;
let posts = PostQueryBuilder::create(&conn, None) let posts = PostQueryBuilder::create(&conn)
.listing_type(&ListingType::All) .listing_type(&ListingType::All)
.sort(sort_type) .sort(sort_type)
.for_community_id(community.id) .community_id(community.id)
.list()?; .list()?;
let items = create_post_items(posts)?; let items = create_post_items(posts)?;
@ -221,8 +221,9 @@ fn get_feed_front(
let site_view = SiteView::read(&conn)?; let site_view = SiteView::read(&conn)?;
let user_id = Claims::decode(&jwt)?.claims.id; 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) .listing_type(&ListingType::Subscribed)
.my_user_id(user_id)
.sort(sort_type) .sort(sort_type)
.list()?; .list()?;
@ -248,12 +249,15 @@ fn get_feed_inbox(conn: &PgConnection, jwt: String) -> Result<ChannelBuilder, Le
let sort = SortType::New; let sort = SortType::New;
let replies = CommentQueryBuilder::create(&conn, Some(user_id)) let replies = CommentQueryBuilder::create(&conn)
.for_recipient_id(user_id) .recipient_id(user_id)
.my_user_id(user_id)
.sort(&sort) .sort(&sort)
.list()?; .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) .sort(&sort)
.list()?; .list()?;