1 use crate::{community_moderator_view::CommunityModeratorView, user_view::UserViewSafe};
2 use diesel::{result::Error, *};
3 use lemmy_db_queries::{
4 aggregates::community_aggregates::CommunityAggregates,
14 use lemmy_db_schema::{
15 schema::{category, community, community_aggregates, community_follower, user_},
18 community::{Community, CommunityFollower, CommunitySafe},
19 user::{UserSafe, User_},
24 #[derive(Debug, Serialize, Clone)]
25 pub struct CommunityView {
26 pub community: CommunitySafe,
27 pub creator: UserSafe,
28 pub category: Category,
30 pub counts: CommunityAggregates,
33 type CommunityViewTuple = (
38 Option<CommunityFollower>,
45 my_user_id: Option<i32>,
46 ) -> Result<Self, Error> {
47 // The left join below will return None in this case
48 let user_id_join = my_user_id.unwrap_or(-1);
50 let (community, creator, category, counts, follower) = community::table
52 .inner_join(user_::table)
53 .inner_join(category::table)
54 .inner_join(community_aggregates::table)
56 community_follower::table.on(
58 .eq(community_follower::community_id)
59 .and(community_follower::user_id.eq(user_id_join)),
63 Community::safe_columns_tuple(),
64 User_::safe_columns_tuple(),
65 category::all_columns,
66 community_aggregates::all_columns,
67 community_follower::all_columns.nullable(),
69 .first::<CommunityViewTuple>(conn)?;
75 subscribed: follower.is_some(),
80 // TODO: this function is only used by is_mod_or_admin() below, can probably be merged
81 fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result<Vec<i32>, Error> {
82 let mut mods_and_admins: Vec<i32> = Vec::new();
83 mods_and_admins.append(
84 &mut CommunityModeratorView::for_community(conn, community_id)
85 .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?,
88 .append(&mut UserViewSafe::admins(conn).map(|v| v.into_iter().map(|a| a.user.id).collect())?);
92 pub fn is_mod_or_admin(conn: &PgConnection, user_id: i32, community_id: i32) -> bool {
93 Self::community_mods_and_admins(conn, community_id)
99 pub struct CommunityQueryBuilder<'a> {
100 conn: &'a PgConnection,
101 listing_type: &'a ListingType,
103 my_user_id: Option<i32>,
105 search_term: Option<String>,
110 impl<'a> CommunityQueryBuilder<'a> {
111 pub fn create(conn: &'a PgConnection) -> Self {
112 CommunityQueryBuilder {
115 listing_type: &ListingType::All,
116 sort: &SortType::Hot,
124 pub fn listing_type(mut self, listing_type: &'a ListingType) -> Self {
125 self.listing_type = listing_type;
129 pub fn sort(mut self, sort: &'a SortType) -> Self {
134 pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
135 self.show_nsfw = show_nsfw;
139 pub fn search_term<T: MaybeOptional<String>>(mut self, search_term: T) -> Self {
140 self.search_term = search_term.get_optional();
144 pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
145 self.my_user_id = my_user_id.get_optional();
149 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
150 self.page = page.get_optional();
154 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
155 self.limit = limit.get_optional();
159 pub fn list(self) -> Result<Vec<CommunityView>, Error> {
160 // The left join below will return None in this case
161 let user_id_join = self.my_user_id.unwrap_or(-1);
163 let mut query = community::table
164 .inner_join(user_::table)
165 .inner_join(category::table)
166 .inner_join(community_aggregates::table)
168 community_follower::table.on(
170 .eq(community_follower::community_id)
171 .and(community_follower::user_id.eq(user_id_join)),
175 Community::safe_columns_tuple(),
176 User_::safe_columns_tuple(),
177 category::all_columns,
178 community_aggregates::all_columns,
179 community_follower::all_columns.nullable(),
183 if let Some(search_term) = self.search_term {
184 let searcher = fuzzy_search(&search_term);
186 .filter(community::name.ilike(searcher.to_owned()))
187 .or_filter(community::title.ilike(searcher.to_owned()))
188 .or_filter(community::description.ilike(searcher));
192 SortType::New => query = query.order_by(community::published.desc()),
193 SortType::TopAll => query = query.order_by(community_aggregates::subscribers.desc()),
194 // Covers all other sorts, including hot
199 community_aggregates::subscribers,
200 community_aggregates::published,
204 .then_order_by(community_aggregates::published.desc())
209 query = query.filter(community::nsfw.eq(false));
212 query = match self.listing_type {
213 ListingType::Subscribed => query.filter(community_follower::user_id.is_not_null()), // TODO could be this: and(community_follower::user_id.eq(user_id_join)),
214 ListingType::Local => query.filter(community::local.eq(true)),
218 let (limit, offset) = limit_and_offset(self.page, self.limit);
222 .filter(community::removed.eq(false))
223 .filter(community::deleted.eq(false))
224 .load::<CommunityViewTuple>(self.conn)?;
226 Ok(CommunityView::from_tuple_to_vec(res))
230 impl ViewToVec for CommunityView {
231 type DbTuple = CommunityViewTuple;
232 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
236 community: a.0.to_owned(),
237 creator: a.1.to_owned(),
238 category: a.2.to_owned(),
239 counts: a.3.to_owned(),
240 subscribed: a.4.is_some(),
242 .collect::<Vec<Self>>()