- .select((
- Community::safe_columns_tuple(),
- Person::safe_columns_tuple(),
- community_aggregates::all_columns,
- community_follower::all_columns.nullable(),
- ))
- .first::<CommunityViewTuple>(conn)?;
-
- Ok(CommunityView {
- community,
- creator,
- subscribed: follower.is_some(),
- counts,
- })
- }
-
- // TODO: this function is only used by is_mod_or_admin() below, can probably be merged
- fn community_mods_and_admins(
- conn: &PgConnection,
- community_id: CommunityId,
- ) -> Result<Vec<PersonId>, Error> {
- let mut mods_and_admins: Vec<PersonId> = Vec::new();
- mods_and_admins.append(
- &mut CommunityModeratorView::for_community(conn, community_id)
- .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?,
- );
- mods_and_admins.append(
- &mut PersonViewSafe::admins(conn).map(|v| v.into_iter().map(|a| a.person.id).collect())?,
- );
- Ok(mods_and_admins)
- }
-
- pub fn is_mod_or_admin(
- conn: &PgConnection,
- person_id: PersonId,
- community_id: CommunityId,
- ) -> bool {
- Self::community_mods_and_admins(conn, community_id)
- .unwrap_or_default()
- .contains(&person_id)
- }
-}
-
-pub struct CommunityQueryBuilder<'a> {
- conn: &'a PgConnection,
- listing_type: &'a ListingType,
- sort: &'a SortType,
- my_person_id: Option<PersonId>,
- show_nsfw: 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: &ListingType::All,
- sort: &SortType::Hot,
- show_nsfw: true,
- search_term: None,
- page: None,
- limit: None,
+ .left_join(
+ community_block::table.on(
+ community::id
+ .eq(community_block::community_id)
+ .and(community_block::person_id.eq(person_id_join)),
+ ),
+ )
+ };
+
+ let selection = (
+ community::all_columns,
+ community_aggregates::all_columns,
+ community_follower::all_columns.nullable(),
+ community_block::all_columns.nullable(),
+ );
+
+ 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);