]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_follower_view.rs
Get rid of Safe Views, use serde_skip (#2767)
[lemmy.git] / crates / db_views_actor / src / community_follower_view.rs
1 use crate::structs::CommunityFollowerView;
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_follower, person},
7   source::{community::Community, person::Person},
8   traits::JoinView,
9   utils::{get_conn, DbPool},
10 };
11
12 type CommunityFollowerViewTuple = (Community, Person);
13
14 impl CommunityFollowerView {
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_follower::table
18       .inner_join(community::table)
19       .inner_join(person::table)
20       .select((community::all_columns, person::all_columns))
21       .filter(community_follower::community_id.eq(community_id))
22       .order_by(community::title)
23       .load::<CommunityFollowerViewTuple>(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_follower::table
32       .inner_join(community::table)
33       .inner_join(person::table)
34       .select((community::all_columns, person::all_columns))
35       .filter(community_follower::person_id.eq(person_id))
36       .filter(community::deleted.eq(false))
37       .filter(community::removed.eq(false))
38       .order_by(community::title)
39       .load::<CommunityFollowerViewTuple>(conn)
40       .await?;
41
42     Ok(res.into_iter().map(Self::from_tuple).collect())
43   }
44 }
45
46 impl JoinView for CommunityFollowerView {
47   type JoinTuple = CommunityFollowerViewTuple;
48   fn from_tuple(a: Self::JoinTuple) -> Self {
49     Self {
50       community: a.0,
51       follower: a.1,
52     }
53   }
54 }