1 use crate::structs::{CommunityModeratorView, CommunityView, PersonViewSafe};
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, CommunitySafe},
18 community_block::CommunityBlock,
19 local_user::LocalUser,
21 traits::{ToSafe, ViewToVec},
22 utils::{functions::hot_rank, 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 ) -> 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 (community, counts, follower, blocked) = 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::safe_columns_tuple(),
64 community_aggregates::all_columns,
65 community_follower::all_columns.nullable(),
66 community_block::all_columns.nullable(),
68 .first::<CommunityViewTuple>(conn)
73 subscribed: CommunityFollower::to_subscribed_type(&follower),
74 blocked: blocked.is_some(),
79 pub async fn is_mod_or_admin(
82 community_id: CommunityId,
83 ) -> Result<bool, Error> {
84 let is_mod = CommunityModeratorView::for_community(pool, community_id)
88 .map(|m| m.moderator.id)
89 .collect::<Vec<PersonId>>()
92 .contains(&person_id);
97 let is_admin = PersonViewSafe::admins(pool)
101 .map(|a| a.person.id)
102 .collect::<Vec<PersonId>>()
105 .contains(&person_id);
110 #[derive(TypedBuilder)]
111 #[builder(field_defaults(default))]
112 pub struct CommunityQuery<'a> {
115 listing_type: Option<ListingType>,
116 sort: Option<SortType>,
117 local_user: Option<&'a LocalUser>,
118 search_term: Option<String>,
123 impl<'a> CommunityQuery<'a> {
124 pub async fn list(self) -> Result<Vec<CommunityView>, Error> {
125 let conn = &mut get_conn(self.pool).await?;
127 // The left join below will return None in this case
128 let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
130 let mut query = community::table
131 .inner_join(community_aggregates::table)
132 .left_join(local_user::table.on(local_user::person_id.eq(person_id_join)))
134 community_follower::table.on(
136 .eq(community_follower::community_id)
137 .and(community_follower::person_id.eq(person_id_join)),
141 community_block::table.on(
143 .eq(community_block::community_id)
144 .and(community_block::person_id.eq(person_id_join)),
148 Community::safe_columns_tuple(),
149 community_aggregates::all_columns,
150 community_follower::all_columns.nullable(),
151 community_block::all_columns.nullable(),
155 if let Some(search_term) = self.search_term {
156 let searcher = fuzzy_search(&search_term);
158 .filter(community::name.ilike(searcher.clone()))
159 .or_filter(community::title.ilike(searcher));
162 match self.sort.unwrap_or(SortType::Hot) {
163 SortType::New => query = query.order_by(community::published.desc()),
164 SortType::TopAll => query = query.order_by(community_aggregates::subscribers.desc()),
165 SortType::TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()),
170 community_aggregates::subscribers,
171 community_aggregates::published,
175 .then_order_by(community_aggregates::published.desc());
176 // Don't show hidden communities in Hot (trending)
177 query = query.filter(
180 .or(community_follower::person_id.eq(person_id_join)),
183 // Covers all other sorts
188 community_aggregates::subscribers,
189 community_aggregates::published,
193 .then_order_by(community_aggregates::published.desc())
197 if let Some(listing_type) = self.listing_type {
198 query = match listing_type {
199 ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
200 ListingType::Local => query.filter(community::local.eq(true)),
205 // Don't show blocked communities or nsfw communities if not enabled in profile
206 if self.local_user.is_some() {
207 query = query.filter(community_block::person_id.is_null());
208 query = query.filter(community::nsfw.eq(false).or(local_user::show_nsfw.eq(true)));
210 // No person in request, only show nsfw communities if show_nsfw passed into request
211 if !self.local_user.map(|l| l.show_nsfw).unwrap_or(false) {
212 query = query.filter(community::nsfw.eq(false));
216 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
220 .filter(community::removed.eq(false))
221 .filter(community::deleted.eq(false))
222 .load::<CommunityViewTuple>(conn)
225 Ok(CommunityView::from_tuple_to_vec(res))
229 impl ViewToVec for CommunityView {
230 type DbTuple = CommunityViewTuple;
231 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
237 subscribed: CommunityFollower::to_subscribed_type(&a.2),
238 blocked: a.3.is_some(),
240 .collect::<Vec<Self>>()