]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_follower_view.rs
Dont show deleted users or communities on profile page. (#2450)
[lemmy.git] / crates / db_views_actor / src / community_follower_view.rs
1 use crate::structs::CommunityFollowerView;
2 use diesel::{result::Error, *};
3 use lemmy_db_schema::{
4   newtypes::{CommunityId, PersonId},
5   schema::{community, community_follower, person},
6   source::{
7     community::{Community, CommunitySafe},
8     person::{Person, PersonSafe},
9   },
10   traits::{ToSafe, ViewToVec},
11 };
12
13 type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe);
14
15 impl CommunityFollowerView {
16   pub fn for_community(
17     conn: &mut PgConnection,
18     community_id: CommunityId,
19   ) -> Result<Vec<Self>, Error> {
20     let res = community_follower::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_follower::community_id.eq(community_id))
28       .order_by(community::title)
29       .load::<CommunityFollowerViewTuple>(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_follower::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_follower::person_id.eq(person_id))
43       .filter(community::deleted.eq(false))
44       .filter(community::removed.eq(false))
45       .order_by(community::title)
46       .load::<CommunityFollowerViewTuple>(conn)?;
47
48     Ok(Self::from_tuple_to_vec(res))
49   }
50 }
51
52 impl ViewToVec for CommunityFollowerView {
53   type DbTuple = CommunityFollowerViewTuple;
54   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
55     items
56       .into_iter()
57       .map(|a| Self {
58         community: a.0,
59         follower: a.1,
60       })
61       .collect::<Vec<Self>>()
62   }
63 }