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()
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()
// 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()
})
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()
})
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()
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)
}
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()
}
SearchType::Communities => {
communities = blocking(context.pool(), move |conn| {
- CommunityQueryBuilder::create(conn, None)
+ CommunityQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
.page(page)
}
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)
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()
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)
}
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)
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)
// 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()?;
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()
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)
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)
}
}
-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,
}
impl<'a> CommentQueryBuilder<'a> {
- pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self {
- // The left join below will return None in this case
- let user_id_join = my_user_id.unwrap_or(-1);
-
- let query = comment::table
- .inner_join(user_::table)
- // recipient here
- .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
- .left_join(user_alias_1::table.on(user_alias_1::id.eq(comment_alias_1::creator_id)))
- .inner_join(post::table)
- .inner_join(community::table.on(post::community_id.eq(community::id)))
- .inner_join(comment_aggregates::table)
- .left_join(
- community_user_ban::table.on(
- community::id
- .eq(community_user_ban::community_id)
- .and(community_user_ban::user_id.eq(comment::creator_id)),
- ),
- )
- .left_join(
- community_follower::table.on(
- post::community_id
- .eq(community_follower::community_id)
- .and(community_follower::user_id.eq(user_id_join)),
- ),
- )
- .left_join(
- comment_saved::table.on(
- comment::id
- .eq(comment_saved::comment_id)
- .and(comment_saved::user_id.eq(user_id_join)),
- ),
- )
- .left_join(
- comment_like::table.on(
- comment::id
- .eq(comment_like::comment_id)
- .and(comment_like::user_id.eq(user_id_join)),
- ),
- )
- .select((
- comment::all_columns,
- User_::safe_columns_tuple(),
- comment_alias_1::all_columns.nullable(),
- UserAlias1::safe_columns_tuple().nullable(),
- post::all_columns,
- Community::safe_columns_tuple(),
- comment_aggregates::all_columns,
- community_user_ban::all_columns.nullable(),
- community_follower::all_columns.nullable(),
- comment_saved::all_columns.nullable(),
- comment_like::score.nullable(),
- ))
- .into_boxed();
-
+ pub fn create(conn: &'a PgConnection) -> Self {
CommentQueryBuilder {
conn,
- query,
listing_type: ListingType::All,
sort: &SortType::New,
- for_community_id: None,
- for_community_name: None,
- for_post_id: None,
- for_creator_id: None,
- for_recipient_id: None,
+ community_id: None,
+ community_name: None,
+ post_id: None,
+ creator_id: None,
+ recipient_id: None,
+ my_user_id: None,
search_term: None,
saved_only: false,
unread_only: false,
self
}
- pub fn for_post_id<T: MaybeOptional<i32>>(mut self, for_post_id: T) -> Self {
- self.for_post_id = for_post_id.get_optional();
+ pub fn post_id<T: MaybeOptional<i32>>(mut self, post_id: T) -> Self {
+ self.post_id = post_id.get_optional();
+ self
+ }
+
+ pub fn creator_id<T: MaybeOptional<i32>>(mut self, creator_id: T) -> Self {
+ self.creator_id = creator_id.get_optional();
self
}
- pub fn for_creator_id<T: MaybeOptional<i32>>(mut self, for_creator_id: T) -> Self {
- self.for_creator_id = for_creator_id.get_optional();
+ pub fn recipient_id<T: MaybeOptional<i32>>(mut self, recipient_id: T) -> Self {
+ self.recipient_id = recipient_id.get_optional();
self
}
- pub fn for_recipient_id<T: MaybeOptional<i32>>(mut self, for_recipient_id: T) -> Self {
- self.for_creator_id = for_recipient_id.get_optional();
+ pub fn community_id<T: MaybeOptional<i32>>(mut self, community_id: T) -> Self {
+ self.community_id = community_id.get_optional();
self
}
- pub fn for_community_id<T: MaybeOptional<i32>>(mut self, for_community_id: T) -> Self {
- self.for_community_id = for_community_id.get_optional();
+ pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
+ self.my_user_id = my_user_id.get_optional();
self
}
- pub fn for_community_name<T: MaybeOptional<String>>(mut self, for_community_name: T) -> Self {
- self.for_community_name = for_community_name.get_optional();
+ pub fn community_name<T: MaybeOptional<String>>(mut self, community_name: T) -> Self {
+ self.community_name = community_name.get_optional();
self
}
pub fn list(self) -> Result<Vec<CommentView>, Error> {
use diesel::dsl::*;
- let mut query = self.query;
+ // The left join below will return None in this case
+ let user_id_join = self.my_user_id.unwrap_or(-1);
+
+ let mut query = comment::table
+ .inner_join(user_::table)
+ // recipient here
+ .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
+ .left_join(user_alias_1::table.on(user_alias_1::id.eq(comment_alias_1::creator_id)))
+ .inner_join(post::table)
+ .inner_join(community::table.on(post::community_id.eq(community::id)))
+ .inner_join(comment_aggregates::table)
+ .left_join(
+ community_user_ban::table.on(
+ community::id
+ .eq(community_user_ban::community_id)
+ .and(community_user_ban::user_id.eq(comment::creator_id)),
+ ),
+ )
+ .left_join(
+ community_follower::table.on(
+ post::community_id
+ .eq(community_follower::community_id)
+ .and(community_follower::user_id.eq(user_id_join)),
+ ),
+ )
+ .left_join(
+ comment_saved::table.on(
+ comment::id
+ .eq(comment_saved::comment_id)
+ .and(comment_saved::user_id.eq(user_id_join)),
+ ),
+ )
+ .left_join(
+ comment_like::table.on(
+ comment::id
+ .eq(comment_like::comment_id)
+ .and(comment_like::user_id.eq(user_id_join)),
+ ),
+ )
+ .select((
+ comment::all_columns,
+ User_::safe_columns_tuple(),
+ comment_alias_1::all_columns.nullable(),
+ UserAlias1::safe_columns_tuple().nullable(),
+ post::all_columns,
+ Community::safe_columns_tuple(),
+ comment_aggregates::all_columns,
+ community_user_ban::all_columns.nullable(),
+ community_follower::all_columns.nullable(),
+ comment_saved::all_columns.nullable(),
+ comment_like::score.nullable(),
+ ))
+ .into_boxed();
// The replies
- if let Some(for_recipient_id) = self.for_recipient_id {
+ if let Some(recipient_id) = self.recipient_id {
query = query
// TODO needs lots of testing
- .filter(user_alias_1::id.eq(for_recipient_id))
+ .filter(user_alias_1::id.eq(recipient_id))
.filter(comment::deleted.eq(false))
.filter(comment::removed.eq(false));
}
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 {
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();
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)?;
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)?;
}
}
-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>,
}
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,
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 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);
}
}
-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,
}
impl<'a> PostQueryBuilder<'a> {
- pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>) -> Self {
- // The left join below will return None in this case
- let user_id_join = my_user_id.unwrap_or(-1);
-
- let query = post::table
- .inner_join(user_::table)
- .inner_join(community::table)
- .left_join(
- community_user_ban::table.on(
- post::community_id
- .eq(community_user_ban::community_id)
- .and(community_user_ban::user_id.eq(community::creator_id)),
- ),
- )
- .inner_join(post_aggregates::table)
- .left_join(
- community_follower::table.on(
- post::community_id
- .eq(community_follower::community_id)
- .and(community_follower::user_id.eq(user_id_join)),
- ),
- )
- .left_join(
- post_saved::table.on(
- post::id
- .eq(post_saved::post_id)
- .and(post_saved::user_id.eq(user_id_join)),
- ),
- )
- .left_join(
- post_read::table.on(
- post::id
- .eq(post_read::post_id)
- .and(post_read::user_id.eq(user_id_join)),
- ),
- )
- .left_join(
- post_like::table.on(
- post::id
- .eq(post_like::post_id)
- .and(post_like::user_id.eq(user_id_join)),
- ),
- )
- .select((
- post::all_columns,
- User_::safe_columns_tuple(),
- Community::safe_columns_tuple(),
- community_user_ban::all_columns.nullable(),
- post_aggregates::all_columns,
- community_follower::all_columns.nullable(),
- post_saved::all_columns.nullable(),
- post_read::all_columns.nullable(),
- post_like::score.nullable(),
- ))
- .into_boxed();
-
+ pub fn create(conn: &'a PgConnection) -> Self {
PostQueryBuilder {
conn,
- query,
listing_type: &ListingType::All,
sort: &SortType::Hot,
- for_creator_id: None,
- for_community_id: None,
- for_community_name: None,
+ creator_id: None,
+ community_id: None,
+ community_name: None,
+ my_user_id: None,
search_term: None,
url_search: None,
show_nsfw: true,
self
}
- pub fn for_community_id<T: MaybeOptional<i32>>(mut self, for_community_id: T) -> Self {
- self.for_community_id = for_community_id.get_optional();
+ pub fn community_id<T: MaybeOptional<i32>>(mut self, community_id: T) -> Self {
+ self.community_id = community_id.get_optional();
+ self
+ }
+
+ pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
+ self.community_id = my_user_id.get_optional();
self
}
- pub fn for_community_name<T: MaybeOptional<String>>(mut self, for_community_name: T) -> Self {
- self.for_community_name = for_community_name.get_optional();
+ pub fn community_name<T: MaybeOptional<String>>(mut self, community_name: T) -> Self {
+ self.community_name = community_name.get_optional();
self
}
- pub fn for_creator_id<T: MaybeOptional<i32>>(mut self, for_creator_id: T) -> Self {
- self.for_creator_id = for_creator_id.get_optional();
+ pub fn creator_id<T: MaybeOptional<i32>>(mut self, creator_id: T) -> Self {
+ self.creator_id = creator_id.get_optional();
self
}
pub fn list(self) -> Result<Vec<PostView>, Error> {
use diesel::dsl::*;
- let mut query = self.query;
+ // The left join below will return None in this case
+ let user_id_join = self.my_user_id.unwrap_or(-1);
+
+ let mut query = post::table
+ .inner_join(user_::table)
+ .inner_join(community::table)
+ .left_join(
+ community_user_ban::table.on(
+ post::community_id
+ .eq(community_user_ban::community_id)
+ .and(community_user_ban::user_id.eq(community::creator_id)),
+ ),
+ )
+ .inner_join(post_aggregates::table)
+ .left_join(
+ community_follower::table.on(
+ post::community_id
+ .eq(community_follower::community_id)
+ .and(community_follower::user_id.eq(user_id_join)),
+ ),
+ )
+ .left_join(
+ post_saved::table.on(
+ post::id
+ .eq(post_saved::post_id)
+ .and(post_saved::user_id.eq(user_id_join)),
+ ),
+ )
+ .left_join(
+ post_read::table.on(
+ post::id
+ .eq(post_read::post_id)
+ .and(post_read::user_id.eq(user_id_join)),
+ ),
+ )
+ .left_join(
+ post_like::table.on(
+ post::id
+ .eq(post_like::post_id)
+ .and(post_like::user_id.eq(user_id_join)),
+ ),
+ )
+ .select((
+ post::all_columns,
+ User_::safe_columns_tuple(),
+ Community::safe_columns_tuple(),
+ community_user_ban::all_columns.nullable(),
+ post_aggregates::all_columns,
+ community_follower::all_columns.nullable(),
+ post_saved::all_columns.nullable(),
+ post_read::all_columns.nullable(),
+ post_like::score.nullable(),
+ ))
+ .into_boxed();
query = match self.listing_type {
ListingType::Subscribed => query.filter(community_follower::user_id.is_not_null()), // TODO could be this: and(community_follower::user_id.eq(user_id_join)),
_ => 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());
}
);
}
+ // 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(
.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
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();
}
}
-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>,
}
impl<'a> UserMentionQueryBuilder<'a> {
- pub fn create(conn: &'a PgConnection, my_user_id: Option<i32>, for_recipient_id: i32) -> Self {
+ pub fn create(conn: &'a PgConnection) -> Self {
+ UserMentionQueryBuilder {
+ conn,
+ my_user_id: None,
+ recipient_id: None,
+ sort: &SortType::New,
+ unread_only: false,
+ page: None,
+ limit: None,
+ }
+ }
+
+ pub fn sort(mut self, sort: &'a SortType) -> Self {
+ self.sort = sort;
+ self
+ }
+
+ pub fn unread_only(mut self, unread_only: bool) -> Self {
+ self.unread_only = unread_only;
+ self
+ }
+
+ pub fn recipient_id<T: MaybeOptional<i32>>(mut self, recipient_id: T) -> Self {
+ self.recipient_id = recipient_id.get_optional();
+ self
+ }
+
+ pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
+ self.my_user_id = my_user_id.get_optional();
+ self
+ }
+
+ pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
+ self.page = page.get_optional();
+ self
+ }
+
+ pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
+ self.limit = limit.get_optional();
+ self
+ }
+
+ pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
+ use diesel::dsl::*;
+
// The left join below will return None in this case
- let user_id_join = my_user_id.unwrap_or(-1);
+ let user_id_join = self.my_user_id.unwrap_or(-1);
- let query = user_mention::table
+ let mut query = user_mention::table
.inner_join(comment::table)
.inner_join(user_::table.on(comment::creator_id.eq(user_::id)))
.inner_join(post::table.on(comment::post_id.eq(post::id)))
))
.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));
}
}
-// 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,
}
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
}
}
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
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()
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)?;
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)?;
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()?;
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()?;