Getting rid of terrible boxedjoin types.

This commit is contained in:
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 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()

View file

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

View file

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

View file

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

View file

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

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> {
conn: &'a PgConnection,
query: join_types::BoxedCommentJoin<'a>,
listing_type: ListingType,
sort: &'a SortType,
for_community_id: Option<i32>,
for_community_name: Option<String>,
for_post_id: Option<i32>,
for_creator_id: Option<i32>,
for_recipient_id: Option<i32>,
community_id: Option<i32>,
community_name: Option<String>,
post_id: Option<i32>,
creator_id: Option<i32>,
recipient_id: Option<i32>,
my_user_id: Option<i32>,
search_term: Option<String>,
saved_only: bool,
unread_only: bool,
@ -424,11 +165,97 @@ pub struct CommentQueryBuilder<'a> {
}
impl<'a> CommentQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self {
// The left join below will return None in this case
let user_id_join = my_user_id.unwrap_or(-1);
pub fn create(conn: &'a PgConnection) -> Self {
CommentQueryBuilder {
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)
// recipient here
.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();
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
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();

View file

@ -19,24 +19,24 @@ pub struct CommunityFollowerView {
type CommunityFollowerViewTuple = (CommunitySafe, UserSafe);
impl CommunityFollowerView {
pub fn for_community(conn: &PgConnection, for_community_id: i32) -> Result<Vec<Self>, Error> {
pub fn for_community(conn: &PgConnection, community_id: i32) -> Result<Vec<Self>, Error> {
let res = community_follower::table
.inner_join(community::table)
.inner_join(user_::table)
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
.filter(community_follower::community_id.eq(for_community_id))
.filter(community_follower::community_id.eq(community_id))
.order_by(community_follower::published)
.load::<CommunityFollowerViewTuple>(conn)?;
Ok(Self::to_vec(res))
}
pub fn for_user(conn: &PgConnection, for_user_id: i32) -> Result<Vec<Self>, Error> {
pub fn for_user(conn: &PgConnection, user_id: i32) -> Result<Vec<Self>, Error> {
let res = community_follower::table
.inner_join(community::table)
.inner_join(user_::table)
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
.filter(community_follower::user_id.eq(for_user_id))
.filter(community_follower::user_id.eq(user_id))
.order_by(community_follower::published)
.load::<CommunityFollowerViewTuple>(conn)?;

View file

@ -19,24 +19,24 @@ pub struct CommunityModeratorView {
type CommunityModeratorViewTuple = (CommunitySafe, UserSafe);
impl CommunityModeratorView {
pub fn for_community(conn: &PgConnection, for_community_id: i32) -> Result<Vec<Self>, Error> {
pub fn for_community(conn: &PgConnection, community_id: i32) -> Result<Vec<Self>, Error> {
let res = community_moderator::table
.inner_join(community::table)
.inner_join(user_::table)
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
.filter(community_moderator::community_id.eq(for_community_id))
.filter(community_moderator::community_id.eq(community_id))
.order_by(community_moderator::published)
.load::<CommunityModeratorViewTuple>(conn)?;
Ok(Self::to_vec(res))
}
pub fn for_user(conn: &PgConnection, for_user_id: i32) -> Result<Vec<Self>, Error> {
pub fn for_user(conn: &PgConnection, user_id: i32) -> Result<Vec<Self>, Error> {
let res = community_moderator::table
.inner_join(community::table)
.inner_join(user_::table)
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
.filter(community_moderator::user_id.eq(for_user_id))
.filter(community_moderator::user_id.eq(user_id))
.order_by(community_moderator::published)
.load::<CommunityModeratorViewTuple>(conn)?;

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> {
conn: &'a PgConnection,
query: join_types::BoxedCommunityJoin<'a>,
sort: &'a SortType,
my_user_id: Option<i32>,
show_nsfw: bool,
search_term: Option<String>,
page: Option<i64>,
@ -182,33 +85,10 @@ pub struct CommunityQueryBuilder<'a> {
}
impl<'a> CommunityQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self {
// The left join below will return None in this case
let user_id_join = my_user_id.unwrap_or(-1);
let query = community::table
.inner_join(user_::table)
.inner_join(category::table)
.inner_join(community_aggregates::table)
.left_join(
community_follower::table.on(
community::id
.eq(community_follower::community_id)
.and(community_follower::user_id.eq(user_id_join)),
),
)
.select((
Community::safe_columns_tuple(),
User_::safe_columns_tuple(),
category::all_columns,
community_aggregates::all_columns,
community_follower::all_columns.nullable(),
))
.into_boxed();
pub fn create(conn: &'a PgConnection) -> Self {
CommunityQueryBuilder {
conn,
query,
my_user_id: None,
sort: &SortType::Hot,
show_nsfw: true,
search_term: None,
@ -232,6 +112,11 @@ impl<'a> CommunityQueryBuilder<'a> {
self
}
pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
self.my_user_id = my_user_id.get_optional();
self
}
pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
self.page = page.get_optional();
self
@ -243,7 +128,28 @@ impl<'a> CommunityQueryBuilder<'a> {
}
pub fn list(self) -> Result<Vec<CommunityView>, Error> {
let mut query = self.query;
// The left join below will return None in this case
let user_id_join = self.my_user_id.unwrap_or(-1);
let mut query = community::table
.inner_join(user_::table)
.inner_join(category::table)
.inner_join(community_aggregates::table)
.left_join(
community_follower::table.on(
community::id
.eq(community_follower::community_id)
.and(community_follower::user_id.eq(user_id_join)),
),
)
.select((
Community::safe_columns_tuple(),
User_::safe_columns_tuple(),
category::all_columns,
community_aggregates::all_columns,
community_follower::all_columns.nullable(),
))
.into_boxed();
if let Some(search_term) = self.search_term {
let searcher = fuzzy_search(&search_term);

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> {
conn: &'a PgConnection,
query: join_types::BoxedPostJoin<'a>,
listing_type: &'a ListingType,
sort: &'a SortType,
for_creator_id: Option<i32>,
for_community_id: Option<i32>,
for_community_name: Option<String>,
creator_id: Option<i32>,
community_id: Option<i32>,
community_name: Option<String>,
my_user_id: Option<i32>,
search_term: Option<String>,
url_search: Option<String>,
show_nsfw: bool,
@ -341,11 +153,92 @@ pub struct PostQueryBuilder<'a> {
}
impl<'a> PostQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self {
// The left join below will return None in this case
let user_id_join = my_user_id.unwrap_or(-1);
pub fn create(conn: &'a PgConnection) -> Self {
PostQueryBuilder {
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(community::table)
.left_join(
@ -397,99 +290,21 @@ impl<'a> PostQueryBuilder<'a> {
))
.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 {
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)),
_ => 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();

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

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

View file

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