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},
16 source::community::{Community, CommunityFollower, CommunitySafe},
22 #[derive(Debug, Serialize, Clone)]
23 pub struct CommunityView {
24 pub community: CommunitySafe,
26 pub counts: CommunityAggregates,
29 type CommunityViewTuple = (
32 Option<CommunityFollower>,
38 community_id: CommunityId,
39 my_person_id: Option<PersonId>,
40 ) -> Result<Self, Error> {
41 // The left join below will return None in this case
42 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
44 let (community, counts, follower) = community::table
46 .inner_join(community_aggregates::table)
48 community_follower::table.on(
50 .eq(community_follower::community_id)
51 .and(community_follower::person_id.eq(person_id_join)),
55 Community::safe_columns_tuple(),
56 community_aggregates::all_columns,
57 community_follower::all_columns.nullable(),
59 .first::<CommunityViewTuple>(conn)?;
63 subscribed: follower.is_some(),
68 // TODO: this function is only used by is_mod_or_admin() below, can probably be merged
69 fn community_mods_and_admins(
71 community_id: CommunityId,
72 ) -> Result<Vec<PersonId>, Error> {
73 let mut mods_and_admins: Vec<PersonId> = Vec::new();
74 mods_and_admins.append(
75 &mut CommunityModeratorView::for_community(conn, community_id)
76 .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?,
78 mods_and_admins.append(
79 &mut PersonViewSafe::admins(conn).map(|v| v.into_iter().map(|a| a.person.id).collect())?,
84 pub fn is_mod_or_admin(
87 community_id: CommunityId,
89 Self::community_mods_and_admins(conn, community_id)
95 pub struct CommunityQueryBuilder<'a> {
96 conn: &'a PgConnection,
97 listing_type: Option<ListingType>,
98 sort: Option<SortType>,
99 my_person_id: Option<PersonId>,
100 show_nsfw: Option<bool>,
101 search_term: Option<String>,
106 impl<'a> CommunityQueryBuilder<'a> {
107 pub fn create(conn: &'a PgConnection) -> Self {
108 CommunityQueryBuilder {
120 pub fn listing_type<T: MaybeOptional<ListingType>>(mut self, listing_type: T) -> Self {
121 self.listing_type = listing_type.get_optional();
125 pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
126 self.sort = sort.get_optional();
130 pub fn show_nsfw<T: MaybeOptional<bool>>(mut self, show_nsfw: T) -> Self {
131 self.show_nsfw = show_nsfw.get_optional();
135 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
136 self.search_term = search_term.get_optional();
140 pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
141 self.my_person_id = my_person_id.get_optional();
145 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
146 self.page = page.get_optional();
150 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
151 self.limit = limit.get_optional();
155 pub fn list(self) -> Result<Vec<CommunityView>, Error> {
156 // The left join below will return None in this case
157 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
159 let mut query = community::table
160 .inner_join(community_aggregates::table)
162 community_follower::table.on(
164 .eq(community_follower::community_id)
165 .and(community_follower::person_id.eq(person_id_join)),
169 Community::safe_columns_tuple(),
170 community_aggregates::all_columns,
171 community_follower::all_columns.nullable(),
175 if let Some(search_term) = self.search_term {
176 let searcher = fuzzy_search(&search_term);
178 .filter(community::name.ilike(searcher.to_owned()))
179 .or_filter(community::title.ilike(searcher.to_owned()))
180 .or_filter(community::description.ilike(searcher));
183 match self.sort.unwrap_or(SortType::Hot) {
184 SortType::New => query = query.order_by(community::published.desc()),
185 SortType::TopAll => query = query.order_by(community_aggregates::subscribers.desc()),
186 SortType::TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()),
187 // Covers all other sorts, including hot
192 community_aggregates::subscribers,
193 community_aggregates::published,
197 .then_order_by(community_aggregates::published.desc())
201 if !self.show_nsfw.unwrap_or(true) {
202 query = query.filter(community::nsfw.eq(false));
205 if let Some(listing_type) = self.listing_type {
206 query = match listing_type {
207 ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)),
208 ListingType::Local => query.filter(community::local.eq(true)),
213 let (limit, offset) = limit_and_offset(self.page, self.limit);
217 .filter(community::removed.eq(false))
218 .filter(community::deleted.eq(false))
219 .load::<CommunityViewTuple>(self.conn)?;
221 Ok(CommunityView::from_tuple_to_vec(res))
225 impl ViewToVec for CommunityView {
226 type DbTuple = CommunityViewTuple;
227 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
231 community: a.0.to_owned(),
232 counts: a.1.to_owned(),
233 subscribed: a.2.is_some(),
235 .collect::<Vec<Self>>()