- .select((
- person_mention::all_columns,
- comment::all_columns,
- Person::safe_columns_tuple(),
- post::all_columns,
- Community::safe_columns_tuple(),
- PersonAlias1::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::<PersonMentionViewTuple>(conn)?;
-
- Ok(PersonMentionView {
- person_mention,
- comment,
- creator,
- post,
- community,
- recipient,
- 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 number of unread mentions
- pub fn get_unread_mentions(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
- use diesel::dsl::*;
-
- person_mention::table
- .filter(person_mention::recipient_id.eq(my_person_id))
- .filter(person_mention::read.eq(false))
- .select(count(person_mention::id))
- .first::<i64>(conn)
- }
-}
-
-pub struct PersonMentionQueryBuilder<'a> {
- conn: &'a PgConnection,
- my_person_id: Option<PersonId>,
- recipient_id: Option<PersonId>,
- sort: Option<SortType>,
- unread_only: Option<bool>,
- page: Option<i64>,
- limit: Option<i64>,
-}
-
-impl<'a> PersonMentionQueryBuilder<'a> {
- pub fn create(conn: &'a PgConnection) -> Self {
- PersonMentionQueryBuilder {
- conn,
- my_person_id: None,
- recipient_id: None,
- sort: None,
- unread_only: None,
- page: None,
- limit: None,
- }
- }
-
- pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
- self.sort = sort.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 recipient_id<T: MaybeOptional<PersonId>>(mut self, recipient_id: T) -> Self {
- self.recipient_id = recipient_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 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<PersonMentionView>, Error> {
- use diesel::dsl::*;
-
- // The left join below will return None in this case
- let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
+ };
+
+ let selection = (
+ person_mention::all_columns,
+ comment::all_columns,
+ person::all_columns,
+ post::all_columns,
+ community::all_columns,
+ aliases::person1.fields(person::all_columns),
+ comment_aggregates::all_columns,
+ community_person_ban::id.nullable().is_not_null(),
+ CommunityFollower::select_subscribed_type(),
+ comment_saved::id.nullable().is_not_null(),
+ person_block::id.nullable().is_not_null(),
+ comment_like::score.nullable(),
+ );
+
+ let read =
+ move |mut conn: DbConn<'a>,
+ (person_mention_id, my_person_id): (PersonMentionId, Option<PersonId>)| async move {
+ all_joins(
+ person_mention::table.find(person_mention_id).into_boxed(),
+ my_person_id,
+ )
+ .left_join(
+ community_person_ban::table.on(
+ community::id
+ .eq(community_person_ban::community_id)
+ .and(community_person_ban::person_id.eq(comment::creator_id)),
+ ),
+ )
+ .select(selection)
+ .first::<PersonMentionViewTuple>(&mut conn)
+ .await
+ };