- .select((
- comment::all_columns,
- Person::safe_columns_tuple(),
- comment_alias_1::all_columns.nullable(),
- PersonAlias1::safe_columns_tuple().nullable(),
- post::all_columns,
- Community::safe_columns_tuple(),
- comment_aggregates::all_columns,
- community_person_ban::all_columns.nullable(),
- community_follower::all_columns.nullable(),
- comment_saved::all_columns.nullable(),
- person_block::all_columns.nullable(),
- comment_like::score.nullable(),
- ))
- .first::<CommentViewTuple>(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() && comment_like.is_none() {
- Some(0)
- } else {
- comment_like
- };
-
- Ok(CommentView {
- comment,
- recipient,
- post,
- creator,
- community,
- counts,
- creator_banned_from_community: creator_banned_from_community.is_some(),
- subscribed: CommunityFollower::to_subscribed_type(&follower),
- saved: saved.is_some(),
- creator_blocked: creator_blocked.is_some(),
- my_vote,
- })
- }
-
- /// Gets the recipient person id.
- /// If there is no parent comment, its the post creator
- pub fn get_recipient_id(&self) -> PersonId {
- match &self.recipient {
- Some(parent_commenter) => parent_commenter.id,
- None => self.post.creator_id,
- }
- }
-
- /// Gets the number of unread replies
- pub fn get_unread_replies(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
- use diesel::dsl::*;
-
- comment::table
- // recipient here
- .left_join(comment_alias_1::table.on(comment_alias_1::id.nullable().eq(comment::parent_id)))
- .left_join(person_alias_1::table.on(person_alias_1::id.eq(comment_alias_1::creator_id)))
- .inner_join(post::table)
- .inner_join(community::table.on(post::community_id.eq(community::id)))
- .left_join(
- person_block::table.on(
- comment::creator_id
- .eq(person_block::target_id)
- .and(person_block::person_id.eq(my_person_id)),
- ),
- )
- .left_join(
- community_block::table.on(
- community::id
- .eq(community_block::community_id)
- .and(community_block::person_id.eq(my_person_id)),
- ),
- )
- .filter(person_alias_1::id.eq(my_person_id)) // Gets the comment replies
- .or_filter(
- comment::parent_id
- .is_null()
- .and(post::creator_id.eq(my_person_id)),
- ) // Gets the top level replies
- .filter(comment::read.eq(false))
- .filter(comment::deleted.eq(false))
- .filter(comment::removed.eq(false))
- // Don't show blocked communities or persons
- .filter(community_block::person_id.is_null())
- .filter(person_block::person_id.is_null())
- .select(count(comment::id))
- .first::<i64>(conn)
- }
-}
-
-pub struct CommentQueryBuilder<'a> {
- conn: &'a PgConnection,
- listing_type: Option<ListingType>,
- sort: Option<SortType>,
- community_id: Option<CommunityId>,
- community_actor_id: Option<DbUrl>,
- post_id: Option<PostId>,
- creator_id: Option<PersonId>,
- recipient_id: Option<PersonId>,
- my_person_id: Option<PersonId>,
- search_term: Option<String>,
- saved_only: Option<bool>,
- unread_only: Option<bool>,
- show_bot_accounts: Option<bool>,
- page: Option<i64>,
- limit: Option<i64>,
-}
-
-impl<'a> CommentQueryBuilder<'a> {
- pub fn create(conn: &'a PgConnection) -> Self {
- CommentQueryBuilder {
- conn,
- listing_type: Some(ListingType::All),
- sort: None,
- community_id: None,
- community_actor_id: None,
- post_id: None,
- creator_id: None,
- recipient_id: None,
- my_person_id: None,
- search_term: None,
- saved_only: None,
- unread_only: None,
- show_bot_accounts: 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 post_id<T: MaybeOptional<PostId>>(mut self, post_id: T) -> Self {
- self.post_id = post_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 recipient_id<T: MaybeOptional<PersonId>>(mut self, recipient_id: T) -> Self {
- self.recipient_id = recipient_id.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 search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
- self.search_term = search_term.get_optional();
- self
- }
-
- pub fn saved_only<T: MaybeOptional<bool>>(mut self, saved_only: T) -> Self {
- self.saved_only = saved_only.get_optional();
- self
- }
-
- pub fn unread_only<T: MaybeOptional<bool>>(mut self, unread_only: T) -> Self {
- self.unread_only = unread_only.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 page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
- self.page = page.get_optional();
- self
- }