- .select((
- post::all_columns,
- Person::safe_columns_tuple(),
- Community::safe_columns_tuple(),
- community_person_ban::all_columns.nullable(),
- post_aggregates::all_columns,
- community_follower::all_columns.nullable(),
- post_saved::all_columns.nullable(),
- post_read::all_columns.nullable(),
- person_block::all_columns.nullable(),
- post_like::score.nullable(),
- ))
- .first::<PostViewTuple>(conn)?;
-
- // If a person is given, then my_vote, if None, should be 0, not null
- // Necessary to differentiate between other person's votes
- let my_vote = if my_person_id.is_some() && post_like.is_none() {
- Some(0)
- } else {
- post_like
- };
-
- Ok(PostView {
- post,
- creator,
- community,
- creator_banned_from_community: creator_banned_from_community.is_some(),
- counts,
- subscribed: follower.is_some(),
- saved: saved.is_some(),
- read: read.is_some(),
- creator_blocked: creator_blocked.is_some(),
- my_vote,
- })
- }
-}
-
-pub struct PostQueryBuilder<'a> {
- conn: &'a PgConnection,
- listing_type: Option<ListingType>,
- sort: Option<SortType>,
- creator_id: Option<PersonId>,
- community_id: Option<CommunityId>,
- community_actor_id: Option<DbUrl>,
- my_person_id: Option<PersonId>,
- search_term: Option<String>,
- url_search: Option<String>,
- show_nsfw: Option<bool>,
- show_bot_accounts: Option<bool>,
- show_read_posts: Option<bool>,
- saved_only: Option<bool>,
- page: Option<i64>,
- limit: Option<i64>,
-}
-
-impl<'a> PostQueryBuilder<'a> {
- pub fn create(conn: &'a PgConnection) -> Self {
- PostQueryBuilder {
- conn,
- listing_type: None,
- sort: None,
- creator_id: None,
- community_id: None,
- community_actor_id: None,
- my_person_id: None,
- search_term: None,
- url_search: None,
- show_nsfw: None,
- show_bot_accounts: None,
- show_read_posts: None,
- saved_only: None,
- page: None,
- limit: None,
- }
- }
-
- pub fn listing_type<T: MaybeOptional<ListingType>>(mut self, listing_type: T) -> Self {
- self.listing_type = listing_type.get_optional();
- self
- }
-
- pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
- self.sort = sort.get_optional();
- self
- }
-
- pub fn community_id<T: MaybeOptional<CommunityId>>(mut self, community_id: T) -> Self {
- self.community_id = community_id.get_optional();
- self
- }
-
- pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
- self.my_person_id = my_person_id.get_optional();
- self
- }
-
- pub fn community_actor_id<T: MaybeOptional<DbUrl>>(mut self, community_actor_id: T) -> Self {
- self.community_actor_id = community_actor_id.get_optional();
- self
- }
-
- pub fn creator_id<T: MaybeOptional<PersonId>>(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<T: MaybeOptional<bool>>(mut self, show_nsfw: T) -> Self {
- self.show_nsfw = show_nsfw.get_optional();
- self
- }
-
- pub fn show_bot_accounts<T: MaybeOptional<bool>>(mut self, show_bot_accounts: T) -> Self {
- self.show_bot_accounts = show_bot_accounts.get_optional();
- self
- }
-
- pub fn show_read_posts<T: MaybeOptional<bool>>(mut self, show_read_posts: T) -> Self {
- self.show_read_posts = show_read_posts.get_optional();
- self
- }
-
- pub fn saved_only<T: MaybeOptional<bool>>(mut self, saved_only: T) -> Self {
- self.saved_only = saved_only.get_optional();
- self
- }