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,
11 traits::{ToSafe, ViewToVec},
12 utils::{functions::hot_rank, fuzzy_search, limit_and_offset},
16 use typed_builder::TypedBuilder;
18 type CommunityViewTuple = (
21 Option<CommunityFollower>,
22 Option<CommunityBlock>,
28 community_id: CommunityId,
29 my_person_id: Option<PersonId>,
30 ) -> Result<Self, Error> {
31 // The left join below will return None in this case
32 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
34 let (community, counts, follower, blocked) = community::table
36 .inner_join(community_aggregates::table)
38 community_follower::table.on(
40 .eq(community_follower::community_id)
41 .and(community_follower::person_id.eq(person_id_join)),
45 community_block::table.on(
47 .eq(community_block::community_id)
48 .and(community_block::person_id.eq(person_id_join)),
52 Community::safe_columns_tuple(),
53 community_aggregates::all_columns,
54 community_follower::all_columns.nullable(),
55 community_block::all_columns.nullable(),
57 .first::<CommunityViewTuple>(conn)?;
61 subscribed: CommunityFollower::to_subscribed_type(&follower),
62 blocked: blocked.is_some(),
67 pub fn is_mod_or_admin(
70 community_id: CommunityId,
72 let is_mod = CommunityModeratorView::for_community(conn, community_id)
75 .map(|m| m.moderator.id)
76 .collect::<Vec<PersonId>>()
79 .contains(&person_id);
84 PersonViewSafe::admins(conn)
88 .collect::<Vec<PersonId>>()
95 #[derive(TypedBuilder)]
96 #[builder(field_defaults(default))]
97 pub struct CommunityQuery<'a> {
99 conn: &'a PgConnection,
100 listing_type: Option<ListingType>,
101 sort: Option<SortType>,
102 my_person_id: Option<PersonId>,
103 show_nsfw: Option<bool>,
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.my_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.my_person_id.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.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>>()