]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_view.rs
Merge pull request #1537 from LemmyNet/add_users_active_monthly_community_sort
[lemmy.git] / crates / db_views_actor / src / community_view.rs
1 use crate::{community_moderator_view::CommunityModeratorView, person_view::PersonViewSafe};
2 use diesel::{result::Error, *};
3 use lemmy_db_queries::{
4   aggregates::community_aggregates::CommunityAggregates,
5   functions::hot_rank,
6   fuzzy_search,
7   limit_and_offset,
8   ListingType,
9   MaybeOptional,
10   SortType,
11   ToSafe,
12   ViewToVec,
13 };
14 use lemmy_db_schema::{
15   schema::{community, community_aggregates, community_follower, person},
16   source::{
17     community::{Community, CommunityFollower, CommunitySafe},
18     person::{Person, PersonSafe},
19   },
20   CommunityId,
21   PersonId,
22 };
23 use serde::Serialize;
24
25 #[derive(Debug, Serialize, Clone)]
26 pub struct CommunityView {
27   pub community: CommunitySafe,
28   pub creator: PersonSafe,
29   pub subscribed: bool,
30   pub counts: CommunityAggregates,
31 }
32
33 type CommunityViewTuple = (
34   CommunitySafe,
35   PersonSafe,
36   CommunityAggregates,
37   Option<CommunityFollower>,
38 );
39
40 impl CommunityView {
41   pub fn read(
42     conn: &PgConnection,
43     community_id: CommunityId,
44     my_person_id: Option<PersonId>,
45   ) -> Result<Self, Error> {
46     // The left join below will return None in this case
47     let person_id_join = my_person_id.unwrap_or(PersonId(-1));
48
49     let (community, creator, counts, follower) = community::table
50       .find(community_id)
51       .inner_join(person::table)
52       .inner_join(community_aggregates::table)
53       .left_join(
54         community_follower::table.on(
55           community::id
56             .eq(community_follower::community_id)
57             .and(community_follower::person_id.eq(person_id_join)),
58         ),
59       )
60       .select((
61         Community::safe_columns_tuple(),
62         Person::safe_columns_tuple(),
63         community_aggregates::all_columns,
64         community_follower::all_columns.nullable(),
65       ))
66       .first::<CommunityViewTuple>(conn)?;
67
68     Ok(CommunityView {
69       community,
70       creator,
71       subscribed: follower.is_some(),
72       counts,
73     })
74   }
75
76   // TODO: this function is only used by is_mod_or_admin() below, can probably be merged
77   fn community_mods_and_admins(
78     conn: &PgConnection,
79     community_id: CommunityId,
80   ) -> Result<Vec<PersonId>, Error> {
81     let mut mods_and_admins: Vec<PersonId> = Vec::new();
82     mods_and_admins.append(
83       &mut CommunityModeratorView::for_community(conn, community_id)
84         .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?,
85     );
86     mods_and_admins.append(
87       &mut PersonViewSafe::admins(conn).map(|v| v.into_iter().map(|a| a.person.id).collect())?,
88     );
89     Ok(mods_and_admins)
90   }
91
92   pub fn is_mod_or_admin(
93     conn: &PgConnection,
94     person_id: PersonId,
95     community_id: CommunityId,
96   ) -> bool {
97     Self::community_mods_and_admins(conn, community_id)
98       .unwrap_or_default()
99       .contains(&person_id)
100   }
101 }
102
103 pub struct CommunityQueryBuilder<'a> {
104   conn: &'a PgConnection,
105   listing_type: &'a ListingType,
106   sort: &'a SortType,
107   my_person_id: Option<PersonId>,
108   show_nsfw: bool,
109   search_term: Option<String>,
110   page: Option<i64>,
111   limit: Option<i64>,
112 }
113
114 impl<'a> CommunityQueryBuilder<'a> {
115   pub fn create(conn: &'a PgConnection) -> Self {
116     CommunityQueryBuilder {
117       conn,
118       my_person_id: None,
119       listing_type: &ListingType::All,
120       sort: &SortType::Hot,
121       show_nsfw: true,
122       search_term: None,
123       page: None,
124       limit: None,
125     }
126   }
127
128   pub fn listing_type(mut self, listing_type: &'a ListingType) -> Self {
129     self.listing_type = listing_type;
130     self
131   }
132
133   pub fn sort(mut self, sort: &'a SortType) -> Self {
134     self.sort = sort;
135     self
136   }
137
138   pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
139     self.show_nsfw = show_nsfw;
140     self
141   }
142
143   pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
144     self.search_term = search_term.get_optional();
145     self
146   }
147
148   pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
149     self.my_person_id = my_person_id.get_optional();
150     self
151   }
152
153   pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
154     self.page = page.get_optional();
155     self
156   }
157
158   pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
159     self.limit = limit.get_optional();
160     self
161   }
162
163   pub fn list(self) -> Result<Vec<CommunityView>, Error> {
164     // The left join below will return None in this case
165     let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
166
167     let mut query = community::table
168       .inner_join(person::table)
169       .inner_join(community_aggregates::table)
170       .left_join(
171         community_follower::table.on(
172           community::id
173             .eq(community_follower::community_id)
174             .and(community_follower::person_id.eq(person_id_join)),
175         ),
176       )
177       .select((
178         Community::safe_columns_tuple(),
179         Person::safe_columns_tuple(),
180         community_aggregates::all_columns,
181         community_follower::all_columns.nullable(),
182       ))
183       .into_boxed();
184
185     if let Some(search_term) = self.search_term {
186       let searcher = fuzzy_search(&search_term);
187       query = query
188         .filter(community::name.ilike(searcher.to_owned()))
189         .or_filter(community::title.ilike(searcher.to_owned()))
190         .or_filter(community::description.ilike(searcher));
191     };
192
193     match self.sort {
194       SortType::New => query = query.order_by(community::published.desc()),
195       SortType::TopAll => query = query.order_by(community_aggregates::subscribers.desc()),
196       SortType::TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()),
197       // Covers all other sorts, including hot
198       _ => {
199         query = query
200           .order_by(
201             hot_rank(
202               community_aggregates::subscribers,
203               community_aggregates::published,
204             )
205             .desc(),
206           )
207           .then_order_by(community_aggregates::published.desc())
208       }
209     };
210
211     if !self.show_nsfw {
212       query = query.filter(community::nsfw.eq(false));
213     };
214
215     query = match self.listing_type {
216       ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
217       ListingType::Local => query.filter(community::local.eq(true)),
218       _ => query,
219     };
220
221     let (limit, offset) = limit_and_offset(self.page, self.limit);
222     let res = query
223       .limit(limit)
224       .offset(offset)
225       .filter(community::removed.eq(false))
226       .filter(community::deleted.eq(false))
227       .load::<CommunityViewTuple>(self.conn)?;
228
229     Ok(CommunityView::from_tuple_to_vec(res))
230   }
231 }
232
233 impl ViewToVec for CommunityView {
234   type DbTuple = CommunityViewTuple;
235   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
236     items
237       .iter()
238       .map(|a| Self {
239         community: a.0.to_owned(),
240         creator: a.1.to_owned(),
241         counts: a.2.to_owned(),
242         subscribed: a.3.is_some(),
243       })
244       .collect::<Vec<Self>>()
245   }
246 }