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,
14 use lemmy_db_schema::{
15 schema::{community, community_aggregates, community_follower, person},
17 community::{Community, CommunityFollower, CommunitySafe},
18 person::{Person, PersonSafe},
25 #[derive(Debug, Serialize, Clone)]
26 pub struct CommunityView {
27 pub community: CommunitySafe,
28 pub creator: PersonSafe,
30 pub counts: CommunityAggregates,
33 type CommunityViewTuple = (
37 Option<CommunityFollower>,
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));
49 let (community, creator, counts, follower) = community::table
51 .inner_join(person::table)
52 .inner_join(community_aggregates::table)
54 community_follower::table.on(
56 .eq(community_follower::community_id)
57 .and(community_follower::person_id.eq(person_id_join)),
61 Community::safe_columns_tuple(),
62 Person::safe_columns_tuple(),
63 community_aggregates::all_columns,
64 community_follower::all_columns.nullable(),
66 .first::<CommunityViewTuple>(conn)?;
71 subscribed: follower.is_some(),
76 // TODO: this function is only used by is_mod_or_admin() below, can probably be merged
77 fn community_mods_and_admins(
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())?,
86 mods_and_admins.append(
87 &mut PersonViewSafe::admins(conn).map(|v| v.into_iter().map(|a| a.person.id).collect())?,
92 pub fn is_mod_or_admin(
95 community_id: CommunityId,
97 Self::community_mods_and_admins(conn, community_id)
103 pub struct CommunityQueryBuilder<'a> {
104 conn: &'a PgConnection,
105 listing_type: &'a ListingType,
107 my_person_id: Option<PersonId>,
109 search_term: Option<String>,
114 impl<'a> CommunityQueryBuilder<'a> {
115 pub fn create(conn: &'a PgConnection) -> Self {
116 CommunityQueryBuilder {
119 listing_type: &ListingType::All,
120 sort: &SortType::Hot,
128 pub fn listing_type(mut self, listing_type: &'a ListingType) -> Self {
129 self.listing_type = listing_type;
133 pub fn sort(mut self, sort: &'a SortType) -> Self {
138 pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
139 self.show_nsfw = show_nsfw;
143 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
144 self.search_term = search_term.get_optional();
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();
153 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
154 self.page = page.get_optional();
158 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
159 self.limit = limit.get_optional();
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));
167 let mut query = community::table
168 .inner_join(person::table)
169 .inner_join(community_aggregates::table)
171 community_follower::table.on(
173 .eq(community_follower::community_id)
174 .and(community_follower::person_id.eq(person_id_join)),
178 Community::safe_columns_tuple(),
179 Person::safe_columns_tuple(),
180 community_aggregates::all_columns,
181 community_follower::all_columns.nullable(),
185 if let Some(search_term) = self.search_term {
186 let searcher = fuzzy_search(&search_term);
188 .filter(community::name.ilike(searcher.to_owned()))
189 .or_filter(community::title.ilike(searcher.to_owned()))
190 .or_filter(community::description.ilike(searcher));
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
202 community_aggregates::subscribers,
203 community_aggregates::published,
207 .then_order_by(community_aggregates::published.desc())
212 query = query.filter(community::nsfw.eq(false));
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)),
221 let (limit, offset) = limit_and_offset(self.page, self.limit);
225 .filter(community::removed.eq(false))
226 .filter(community::deleted.eq(false))
227 .load::<CommunityViewTuple>(self.conn)?;
229 Ok(CommunityView::from_tuple_to_vec(res))
233 impl ViewToVec for CommunityView {
234 type DbTuple = CommunityViewTuple;
235 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<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(),
244 .collect::<Vec<Self>>()