- .select((
- Community::safe_columns_tuple(),
- community_aggregates::all_columns,
- community_follower::all_columns.nullable(),
- community_block::all_columns.nullable(),
- ))
- .first::<CommunityViewTuple>(conn)?;
-
- Ok(CommunityView {
- community,
- subscribed: follower.is_some(),
- blocked: blocked.is_some(),
- counts,
- })
- }
-
- pub fn is_mod_or_admin(
- conn: &PgConnection,
- person_id: PersonId,
- community_id: CommunityId,
- ) -> bool {
- let is_mod = CommunityModeratorView::for_community(conn, community_id)
- .map(|v| {
- v.into_iter()
- .map(|m| m.moderator.id)
- .collect::<Vec<PersonId>>()
- })
- .unwrap_or_default()
- .contains(&person_id);
- if is_mod {
- return true;
- }
-
- PersonViewSafe::admins(conn)
- .map(|v| {
- v.into_iter()
- .map(|a| a.person.id)
- .collect::<Vec<PersonId>>()
- })
- .unwrap_or_default()
- .contains(&person_id)
- }
-}
-
-pub struct CommunityQueryBuilder<'a> {
- conn: &'a PgConnection,
- listing_type: Option<ListingType>,
- sort: Option<SortType>,
- my_person_id: Option<PersonId>,
- show_nsfw: Option<bool>,
- search_term: Option<String>,
- page: Option<i64>,
- limit: Option<i64>,
-}
-
-impl<'a> CommunityQueryBuilder<'a> {
- pub fn create(conn: &'a PgConnection) -> Self {
- CommunityQueryBuilder {
- conn,
- my_person_id: None,
- listing_type: None,
- sort: None,
- show_nsfw: None,
- search_term: None,
- page: None,
- limit: None,
+ };
+
+ let selection = (
+ community::all_columns,
+ community_aggregates::all_columns,
+ CommunityFollower::select_subscribed_type(),
+ community_block::id.nullable().is_not_null(),
+ );
+
+ let not_removed_or_deleted = community::removed
+ .eq(false)
+ .and(community::deleted.eq(false));
+
+ let read = move |mut conn: DbConn<'a>,
+ (community_id, my_person_id, is_mod_or_admin): (
+ CommunityId,
+ Option<PersonId>,
+ Option<bool>,
+ )| async move {
+ let mut query = all_joins(
+ community::table.find(community_id).into_boxed(),
+ my_person_id,
+ )
+ .select(selection);
+
+ // Hide deleted and removed for non-admins or mods
+ if !is_mod_or_admin.unwrap_or(false) {
+ query = query.filter(not_removed_or_deleted);