1 use crate::structs::{CommunityModeratorView, CommunityView, PersonView};
8 NullableExpressionMethods,
9 PgTextExpressionMethods,
12 use diesel_async::RunQueryDsl;
13 use lemmy_db_schema::{
14 aggregates::structs::CommunityAggregates,
15 newtypes::{CommunityId, PersonId},
16 schema::{community, community_aggregates, community_block, community_follower, local_user},
18 community::{Community, CommunityFollower},
19 community_block::CommunityBlock,
20 local_user::LocalUser,
23 utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
28 type CommunityViewTuple = (
31 Option<CommunityFollower>,
32 Option<CommunityBlock>,
35 fn queries<'a>() -> Queries<
36 impl ReadFn<'a, CommunityView, (CommunityId, Option<PersonId>, Option<bool>)>,
37 impl ListFn<'a, CommunityView, CommunityQuery<'a>>,
39 let all_joins = |query: community::BoxedQuery<'a, Pg>, my_person_id: Option<PersonId>| {
40 // The left join below will return None in this case
41 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
44 .inner_join(community_aggregates::table)
46 community_follower::table.on(
48 .eq(community_follower::community_id)
49 .and(community_follower::person_id.eq(person_id_join)),
53 community_block::table.on(
55 .eq(community_block::community_id)
56 .and(community_block::person_id.eq(person_id_join)),
62 community::all_columns,
63 community_aggregates::all_columns,
64 community_follower::all_columns.nullable(),
65 community_block::all_columns.nullable(),
68 let not_removed_or_deleted = community::removed
70 .and(community::deleted.eq(false));
72 let read = move |mut conn: DbConn<'a>,
73 (community_id, my_person_id, is_mod_or_admin): (
78 let mut query = all_joins(
79 community::table.find(community_id).into_boxed(),
84 // Hide deleted and removed for non-admins or mods
85 if !is_mod_or_admin.unwrap_or(false) {
86 query = query.filter(not_removed_or_deleted);
89 query.first::<CommunityViewTuple>(&mut conn).await
92 let list = move |mut conn: DbConn<'a>, options: CommunityQuery<'a>| async move {
95 let my_person_id = options.local_user.map(|l| l.person_id);
97 // The left join below will return None in this case
98 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
100 let mut query = all_joins(community::table.into_boxed(), my_person_id)
101 .left_join(local_user::table.on(local_user::person_id.eq(person_id_join)))
104 if let Some(search_term) = options.search_term {
105 let searcher = fuzzy_search(&search_term);
107 .filter(community::name.ilike(searcher.clone()))
108 .or_filter(community::title.ilike(searcher))
111 // Hide deleted and removed for non-admins or mods
112 if !options.is_mod_or_admin.unwrap_or(false) {
113 query = query.filter(not_removed_or_deleted).filter(
116 .or(community_follower::person_id.eq(person_id_join)),
120 match options.sort.unwrap_or(Hot) {
121 Hot | Active => query = query.order_by(community_aggregates::hot_rank.desc()),
122 NewComments | TopDay | TopTwelveHour | TopSixHour | TopHour => {
123 query = query.order_by(community_aggregates::users_active_day.desc())
125 New => query = query.order_by(community::published.desc()),
126 Old => query = query.order_by(community::published.asc()),
127 // Controversial is temporary until a CommentSortType is created
128 MostComments | Controversial => query = query.order_by(community_aggregates::comments.desc()),
129 TopAll | TopYear | TopNineMonths => {
130 query = query.order_by(community_aggregates::subscribers.desc())
132 TopSixMonths | TopThreeMonths => {
133 query = query.order_by(community_aggregates::users_active_half_year.desc())
135 TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()),
136 TopWeek => query = query.order_by(community_aggregates::users_active_week.desc()),
139 if let Some(listing_type) = options.listing_type {
140 query = match listing_type {
141 ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
142 ListingType::Local => query.filter(community::local.eq(true)),
147 // Don't show blocked communities or nsfw communities if not enabled in profile
148 if options.local_user.is_some() {
149 query = query.filter(community_block::person_id.is_null());
150 query = query.filter(community::nsfw.eq(false).or(local_user::show_nsfw.eq(true)));
152 // No person in request, only show nsfw communities if show_nsfw is passed into request
153 if !options.show_nsfw.unwrap_or(false) {
154 query = query.filter(community::nsfw.eq(false));
158 let (limit, offset) = limit_and_offset(options.page, options.limit)?;
162 .load::<CommunityViewTuple>(&mut conn)
166 Queries::new(read, list)
171 pool: &mut DbPool<'_>,
172 community_id: CommunityId,
173 my_person_id: Option<PersonId>,
174 is_mod_or_admin: Option<bool>,
175 ) -> Result<Self, Error> {
177 .read(pool, (community_id, my_person_id, is_mod_or_admin))
181 pub async fn is_mod_or_admin(
182 pool: &mut DbPool<'_>,
184 community_id: CommunityId,
185 ) -> Result<bool, Error> {
187 CommunityModeratorView::is_community_moderator(pool, community_id, person_id).await?;
192 PersonView::is_admin(pool, person_id).await
197 pub struct CommunityQuery<'a> {
198 pub listing_type: Option<ListingType>,
199 pub sort: Option<SortType>,
200 pub local_user: Option<&'a LocalUser>,
201 pub search_term: Option<String>,
202 pub is_mod_or_admin: Option<bool>,
203 pub show_nsfw: Option<bool>,
204 pub page: Option<i64>,
205 pub limit: Option<i64>,
208 impl<'a> CommunityQuery<'a> {
209 pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommunityView>, Error> {
210 queries().list(pool, self).await
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(),