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},
27 type CommunityViewTuple = (
30 Option<CommunityFollower>,
31 Option<CommunityBlock>,
36 pool: &mut DbPool<'_>,
37 community_id: CommunityId,
38 my_person_id: Option<PersonId>,
39 is_mod_or_admin: Option<bool>,
40 ) -> Result<Self, Error> {
41 let conn = &mut get_conn(pool).await?;
42 // The left join below will return None in this case
43 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
45 let mut query = community::table
47 .inner_join(community_aggregates::table)
49 community_follower::table.on(
51 .eq(community_follower::community_id)
52 .and(community_follower::person_id.eq(person_id_join)),
56 community_block::table.on(
58 .eq(community_block::community_id)
59 .and(community_block::person_id.eq(person_id_join)),
63 community::all_columns,
64 community_aggregates::all_columns,
65 community_follower::all_columns.nullable(),
66 community_block::all_columns.nullable(),
70 // Hide deleted and removed for non-admins or mods
71 if !is_mod_or_admin.unwrap_or(false) {
73 .filter(community::removed.eq(false))
74 .filter(community::deleted.eq(false));
77 let (community, counts, follower, blocked) = query.first::<CommunityViewTuple>(conn).await?;
81 subscribed: CommunityFollower::to_subscribed_type(&follower),
82 blocked: blocked.is_some(),
87 pub async fn is_mod_or_admin(
88 pool: &mut DbPool<'_>,
90 community_id: CommunityId,
91 ) -> Result<bool, Error> {
93 CommunityModeratorView::is_community_moderator(pool, community_id, person_id).await?;
98 PersonView::is_admin(pool, person_id).await
103 pub struct CommunityQuery<'a> {
104 pub listing_type: Option<ListingType>,
105 pub sort: Option<SortType>,
106 pub local_user: Option<&'a LocalUser>,
107 pub search_term: Option<String>,
108 pub is_mod_or_admin: Option<bool>,
109 pub show_nsfw: Option<bool>,
110 pub page: Option<i64>,
111 pub limit: Option<i64>,
114 impl<'a> CommunityQuery<'a> {
115 pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommunityView>, Error> {
118 let conn = &mut get_conn(pool).await?;
120 // The left join below will return None in this case
121 let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
123 let mut query = community::table
124 .inner_join(community_aggregates::table)
125 .left_join(local_user::table.on(local_user::person_id.eq(person_id_join)))
127 community_follower::table.on(
129 .eq(community_follower::community_id)
130 .and(community_follower::person_id.eq(person_id_join)),
134 community_block::table.on(
136 .eq(community_block::community_id)
137 .and(community_block::person_id.eq(person_id_join)),
141 community::all_columns,
142 community_aggregates::all_columns,
143 community_follower::all_columns.nullable(),
144 community_block::all_columns.nullable(),
148 if let Some(search_term) = self.search_term {
149 let searcher = fuzzy_search(&search_term);
151 .filter(community::name.ilike(searcher.clone()))
152 .or_filter(community::title.ilike(searcher));
155 // Hide deleted and removed for non-admins or mods
156 if !self.is_mod_or_admin.unwrap_or(false) {
158 .filter(community::removed.eq(false))
159 .filter(community::deleted.eq(false))
163 .or(community_follower::person_id.eq(person_id_join)),
166 match self.sort.unwrap_or(Hot) {
167 Hot | Active => query = query.order_by(community_aggregates::hot_rank.desc()),
168 NewComments | TopDay | TopTwelveHour | TopSixHour | TopHour => {
169 query = query.order_by(community_aggregates::users_active_day.desc())
171 New => query = query.order_by(community::published.desc()),
172 Old => query = query.order_by(community::published.asc()),
173 MostComments => query = query.order_by(community_aggregates::comments.desc()),
174 TopAll | TopYear | TopNineMonths => {
175 query = query.order_by(community_aggregates::subscribers.desc())
177 TopSixMonths | TopThreeMonths => {
178 query = query.order_by(community_aggregates::users_active_half_year.desc())
180 TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()),
181 TopWeek => query = query.order_by(community_aggregates::users_active_week.desc()),
184 if let Some(listing_type) = self.listing_type {
185 query = match listing_type {
186 ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
187 ListingType::Local => query.filter(community::local.eq(true)),
192 // Don't show blocked communities or nsfw communities if not enabled in profile
193 if self.local_user.is_some() {
194 query = query.filter(community_block::person_id.is_null());
195 query = query.filter(community::nsfw.eq(false).or(local_user::show_nsfw.eq(true)));
197 // No person in request, only show nsfw communities if show_nsfw is passed into request
198 if !self.show_nsfw.unwrap_or(false) {
199 query = query.filter(community::nsfw.eq(false));
203 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
207 .load::<CommunityViewTuple>(conn)
210 Ok(res.into_iter().map(CommunityView::from_tuple).collect())
214 impl JoinView for CommunityView {
215 type JoinTuple = CommunityViewTuple;
216 fn from_tuple(a: Self::JoinTuple) -> Self {
220 subscribed: CommunityFollower::to_subscribed_type(&a.2),
221 blocked: a.3.is_some(),