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.to_owned()))
144 .or_filter(community::description.ilike(searcher));
147 match self.sort.unwrap_or(SortType::Hot) {
148 SortType::New => query = query.order_by(community::published.desc()),
149 SortType::TopAll => query = query.order_by(community_aggregates::subscribers.desc()),
150 SortType::TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()),
155 community_aggregates::subscribers,
156 community_aggregates::published,
160 .then_order_by(community_aggregates::published.desc());
161 // Don't show hidden communities in Hot (trending)
162 query = query.filter(
165 .or(community_follower::person_id.eq(person_id_join)),
168 // Covers all other sorts
173 community_aggregates::subscribers,
174 community_aggregates::published,
178 .then_order_by(community_aggregates::published.desc())
182 if let Some(listing_type) = self.listing_type {
183 query = match listing_type {
184 ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
185 ListingType::Local => query.filter(community::local.eq(true)),
190 // Don't show blocked communities or nsfw communities if not enabled in profile
191 if self.local_user.is_some() {
192 query = query.filter(community_block::person_id.is_null());
193 query = query.filter(community::nsfw.eq(false).or(local_user::show_nsfw.eq(true)));
195 // No person in request, only show nsfw communities if show_nsfw passed into request
196 if !self.local_user.map(|l| l.show_nsfw).unwrap_or(false) {
197 query = query.filter(community::nsfw.eq(false));
201 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
205 .filter(community::removed.eq(false))
206 .filter(community::deleted.eq(false))
207 .load::<CommunityViewTuple>(self.conn)?;
209 Ok(CommunityView::from_tuple_to_vec(res))
213 impl ViewToVec for CommunityView {
214 type DbTuple = CommunityViewTuple;
215 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
221 subscribed: CommunityFollower::to_subscribed_type(&a.2),
222 blocked: a.3.is_some(),
224 .collect::<Vec<Self>>()