1 use crate::structs::{CommunityModeratorView, CommunityView, PersonView};
7 NullableExpressionMethods,
8 PgTextExpressionMethods,
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
13 aggregates::structs::CommunityAggregates,
14 newtypes::{CommunityId, PersonId},
15 schema::{community, community_aggregates, community_block, community_follower, local_user},
17 community::{Community, CommunityFollower},
18 community_block::CommunityBlock,
19 local_user::LocalUser,
22 utils::{fuzzy_search, get_conn, limit_and_offset, DbPool},
26 use typed_builder::TypedBuilder;
28 type CommunityViewTuple = (
31 Option<CommunityFollower>,
32 Option<CommunityBlock>,
38 community_id: CommunityId,
39 my_person_id: Option<PersonId>,
40 is_mod_or_admin: Option<bool>,
41 ) -> Result<Self, Error> {
42 let conn = &mut get_conn(pool).await?;
43 // The left join below will return None in this case
44 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
46 let mut query = community::table
48 .inner_join(community_aggregates::table)
50 community_follower::table.on(
52 .eq(community_follower::community_id)
53 .and(community_follower::person_id.eq(person_id_join)),
57 community_block::table.on(
59 .eq(community_block::community_id)
60 .and(community_block::person_id.eq(person_id_join)),
64 community::all_columns,
65 community_aggregates::all_columns,
66 community_follower::all_columns.nullable(),
67 community_block::all_columns.nullable(),
71 // Hide deleted and removed for non-admins or mods
72 if !is_mod_or_admin.unwrap_or(false) {
74 .filter(community::removed.eq(false))
75 .filter(community::deleted.eq(false));
78 let (community, counts, follower, blocked) = query.first::<CommunityViewTuple>(conn).await?;
82 subscribed: CommunityFollower::to_subscribed_type(&follower),
83 blocked: blocked.is_some(),
88 pub async fn is_mod_or_admin(
91 community_id: CommunityId,
92 ) -> Result<bool, Error> {
93 let is_mod = CommunityModeratorView::for_community(pool, community_id)
97 .map(|m| m.moderator.id)
98 .collect::<Vec<PersonId>>()
101 .contains(&person_id);
106 let is_admin = PersonView::admins(pool)
110 .map(|a| a.person.id)
111 .collect::<Vec<PersonId>>()
114 .contains(&person_id);
119 #[derive(TypedBuilder)]
120 #[builder(field_defaults(default))]
121 pub struct CommunityQuery<'a> {
124 listing_type: Option<ListingType>,
125 sort: Option<SortType>,
126 local_user: Option<&'a LocalUser>,
127 search_term: Option<String>,
128 is_mod_or_admin: Option<bool>,
129 show_nsfw: Option<bool>,
134 impl<'a> CommunityQuery<'a> {
135 pub async fn list(self) -> Result<Vec<CommunityView>, Error> {
136 let conn = &mut get_conn(self.pool).await?;
138 // The left join below will return None in this case
139 let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
141 let mut query = community::table
142 .inner_join(community_aggregates::table)
143 .left_join(local_user::table.on(local_user::person_id.eq(person_id_join)))
145 community_follower::table.on(
147 .eq(community_follower::community_id)
148 .and(community_follower::person_id.eq(person_id_join)),
152 community_block::table.on(
154 .eq(community_block::community_id)
155 .and(community_block::person_id.eq(person_id_join)),
159 community::all_columns,
160 community_aggregates::all_columns,
161 community_follower::all_columns.nullable(),
162 community_block::all_columns.nullable(),
166 if let Some(search_term) = self.search_term {
167 let searcher = fuzzy_search(&search_term);
169 .filter(community::name.ilike(searcher.clone()))
170 .or_filter(community::title.ilike(searcher));
173 // Hide deleted and removed for non-admins or mods
174 if !self.is_mod_or_admin.unwrap_or(false) {
176 .filter(community::removed.eq(false))
177 .filter(community::deleted.eq(false))
181 .or(community_follower::person_id.eq(person_id_join)),
185 match self.sort.unwrap_or(SortType::Hot) {
186 SortType::New => query = query.order_by(community::published.desc()),
187 SortType::TopAll => query = query.order_by(community_aggregates::subscribers.desc()),
188 SortType::TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()),
189 SortType::Hot => query = query.order_by(community_aggregates::hot_rank.desc()),
190 // Covers all other sorts
191 _ => query = query.order_by(community_aggregates::users_active_month.desc()),
194 if let Some(listing_type) = self.listing_type {
195 query = match listing_type {
196 ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
197 ListingType::Local => query.filter(community::local.eq(true)),
202 // Don't show blocked communities or nsfw communities if not enabled in profile
203 if self.local_user.is_some() {
204 query = query.filter(community_block::person_id.is_null());
205 query = query.filter(community::nsfw.eq(false).or(local_user::show_nsfw.eq(true)));
207 // No person in request, only show nsfw communities if show_nsfw is passed into request
208 if !self.show_nsfw.unwrap_or(false) {
209 query = query.filter(community::nsfw.eq(false));
213 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
217 .load::<CommunityViewTuple>(conn)
220 Ok(res.into_iter().map(CommunityView::from_tuple).collect())
224 impl JoinView for CommunityView {
225 type JoinTuple = CommunityViewTuple;
226 fn from_tuple(a: Self::JoinTuple) -> Self {
230 subscribed: CommunityFollower::to_subscribed_type(&a.2),
231 blocked: a.3.is_some(),