1 use crate::structs::{CommunityModeratorView, CommunityView, PersonViewSafe};
2 use diesel::{result::Error, *};
4 aggregates::structs::CommunityAggregates,
5 newtypes::{CommunityId, PersonId},
6 schema::{community, community_aggregates, community_block, community_follower, local_user},
8 community::{Community, CommunityFollower, CommunitySafe},
9 community_block::CommunityBlock,
10 local_user::LocalUser,
12 traits::{ToSafe, ViewToVec},
13 utils::{functions::hot_rank, fuzzy_search, limit_and_offset},
17 use typed_builder::TypedBuilder;
19 type CommunityViewTuple = (
22 Option<CommunityFollower>,
23 Option<CommunityBlock>,
29 community_id: CommunityId,
30 my_person_id: Option<PersonId>,
31 ) -> Result<Self, Error> {
32 // The left join below will return None in this case
33 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
35 let (community, counts, follower, blocked) = community::table
37 .inner_join(community_aggregates::table)
39 community_follower::table.on(
41 .eq(community_follower::community_id)
42 .and(community_follower::person_id.eq(person_id_join)),
46 community_block::table.on(
48 .eq(community_block::community_id)
49 .and(community_block::person_id.eq(person_id_join)),
53 Community::safe_columns_tuple(),
54 community_aggregates::all_columns,
55 community_follower::all_columns.nullable(),
56 community_block::all_columns.nullable(),
58 .first::<CommunityViewTuple>(conn)?;
62 subscribed: CommunityFollower::to_subscribed_type(&follower),
63 blocked: blocked.is_some(),
68 pub fn is_mod_or_admin(
71 community_id: CommunityId,
73 let is_mod = CommunityModeratorView::for_community(conn, community_id)
76 .map(|m| m.moderator.id)
77 .collect::<Vec<PersonId>>()
80 .contains(&person_id);
85 PersonViewSafe::admins(conn)
89 .collect::<Vec<PersonId>>()
96 #[derive(TypedBuilder)]
97 #[builder(field_defaults(default))]
98 pub struct CommunityQuery<'a> {
100 conn: &'a PgConnection,
101 listing_type: Option<ListingType>,
102 sort: Option<SortType>,
103 local_user: Option<&'a LocalUser>,
104 search_term: Option<String>,
109 impl<'a> CommunityQuery<'a> {
110 pub fn list(self) -> Result<Vec<CommunityView>, Error> {
111 // The left join below will return None in this case
112 let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
114 let mut query = community::table
115 .inner_join(community_aggregates::table)
116 .left_join(local_user::table.on(local_user::person_id.eq(person_id_join)))
118 community_follower::table.on(
120 .eq(community_follower::community_id)
121 .and(community_follower::person_id.eq(person_id_join)),
125 community_block::table.on(
127 .eq(community_block::community_id)
128 .and(community_block::person_id.eq(person_id_join)),
132 Community::safe_columns_tuple(),
133 community_aggregates::all_columns,
134 community_follower::all_columns.nullable(),
135 community_block::all_columns.nullable(),
139 if let Some(search_term) = self.search_term {
140 let searcher = fuzzy_search(&search_term);
142 .filter(community::name.ilike(searcher.to_owned()))
143 .or_filter(community::title.ilike(searcher));
146 match self.sort.unwrap_or(SortType::Hot) {
147 SortType::New => query = query.order_by(community::published.desc()),
148 SortType::TopAll => query = query.order_by(community_aggregates::subscribers.desc()),
149 SortType::TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()),
154 community_aggregates::subscribers,
155 community_aggregates::published,
159 .then_order_by(community_aggregates::published.desc());
160 // Don't show hidden communities in Hot (trending)
161 query = query.filter(
164 .or(community_follower::person_id.eq(person_id_join)),
167 // Covers all other sorts
172 community_aggregates::subscribers,
173 community_aggregates::published,
177 .then_order_by(community_aggregates::published.desc())
181 if let Some(listing_type) = self.listing_type {
182 query = match listing_type {
183 ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
184 ListingType::Local => query.filter(community::local.eq(true)),
189 // Don't show blocked communities or nsfw communities if not enabled in profile
190 if self.local_user.is_some() {
191 query = query.filter(community_block::person_id.is_null());
192 query = query.filter(community::nsfw.eq(false).or(local_user::show_nsfw.eq(true)));
194 // No person in request, only show nsfw communities if show_nsfw passed into request
195 if !self.local_user.map(|l| l.show_nsfw).unwrap_or(false) {
196 query = query.filter(community::nsfw.eq(false));
200 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
204 .filter(community::removed.eq(false))
205 .filter(community::deleted.eq(false))
206 .load::<CommunityViewTuple>(self.conn)?;
208 Ok(CommunityView::from_tuple_to_vec(res))
212 impl ViewToVec for CommunityView {
213 type DbTuple = CommunityViewTuple;
214 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
220 subscribed: CommunityFollower::to_subscribed_type(&a.2),
221 blocked: a.3.is_some(),
223 .collect::<Vec<Self>>()