]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_moderator_view.rs
Dont show deleted users or communities on profile page. (#2450)
[lemmy.git] / crates / db_views_actor / src / community_moderator_view.rs
1 use crate::structs::CommunityModeratorView;
2 use diesel::{result::Error, *};
3 use lemmy_db_schema::{
4   newtypes::{CommunityId, PersonId},
5   schema::{community, community_moderator, person},
6   source::{
7     community::{Community, CommunitySafe},
8     person::{Person, PersonSafe},
9   },
10   traits::{ToSafe, ViewToVec},
11 };
12
13 type CommunityModeratorViewTuple = (CommunitySafe, PersonSafe);
14
15 impl CommunityModeratorView {
16   pub fn for_community(
17     conn: &mut PgConnection,
18     community_id: CommunityId,
19   ) -> Result<Vec<Self>, Error> {
20     let res = community_moderator::table
21       .inner_join(community::table)
22       .inner_join(person::table)
23       .select((
24         Community::safe_columns_tuple(),
25         Person::safe_columns_tuple(),
26       ))
27       .filter(community_moderator::community_id.eq(community_id))
28       .order_by(community_moderator::published)
29       .load::<CommunityModeratorViewTuple>(conn)?;
30
31     Ok(Self::from_tuple_to_vec(res))
32   }
33
34   pub fn for_person(conn: &mut PgConnection, person_id: PersonId) -> Result<Vec<Self>, Error> {
35     let res = community_moderator::table
36       .inner_join(community::table)
37       .inner_join(person::table)
38       .select((
39         Community::safe_columns_tuple(),
40         Person::safe_columns_tuple(),
41       ))
42       .filter(community_moderator::person_id.eq(person_id))
43       .filter(community::deleted.eq(false))
44       .filter(community::removed.eq(false))
45       .order_by(community_moderator::published)
46       .load::<CommunityModeratorViewTuple>(conn)?;
47
48     Ok(Self::from_tuple_to_vec(res))
49   }
50
51   /// Finds all communities first mods / creators
52   /// Ideally this should be a group by, but diesel doesn't support it yet
53   pub fn get_community_first_mods(conn: &mut PgConnection) -> Result<Vec<Self>, Error> {
54     let res = community_moderator::table
55       .inner_join(community::table)
56       .inner_join(person::table)
57       .select((
58         Community::safe_columns_tuple(),
59         Person::safe_columns_tuple(),
60       ))
61       // A hacky workaround instead of group_bys
62       // https://stackoverflow.com/questions/24042359/how-to-join-only-one-row-in-joined-table-with-postgres
63       .distinct_on(community_moderator::community_id)
64       .order_by((
65         community_moderator::community_id,
66         community_moderator::person_id,
67       ))
68       .load::<CommunityModeratorViewTuple>(conn)?;
69
70     Ok(Self::from_tuple_to_vec(res))
71   }
72 }
73
74 impl ViewToVec for CommunityModeratorView {
75   type DbTuple = CommunityModeratorViewTuple;
76   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
77     items
78       .into_iter()
79       .map(|a| Self {
80         community: a.0,
81         moderator: a.1,
82       })
83       .collect::<Vec<Self>>()
84   }
85 }