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