]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_follower_view.rs
Revert "Add pending, and change use specific API response for FollowCommunity…" ...
[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(conn: &PgConnection, community_id: CommunityId) -> Result<Vec<Self>, Error> {
17     let res = community_follower::table
18       .inner_join(community::table)
19       .inner_join(person::table)
20       .select((
21         Community::safe_columns_tuple(),
22         Person::safe_columns_tuple(),
23       ))
24       .filter(community_follower::community_id.eq(community_id))
25       .order_by(community::title)
26       .load::<CommunityFollowerViewTuple>(conn)?;
27
28     Ok(Self::from_tuple_to_vec(res))
29   }
30
31   pub fn for_person(conn: &PgConnection, person_id: PersonId) -> Result<Vec<Self>, Error> {
32     let res = community_follower::table
33       .inner_join(community::table)
34       .inner_join(person::table)
35       .select((
36         Community::safe_columns_tuple(),
37         Person::safe_columns_tuple(),
38       ))
39       .filter(community_follower::person_id.eq(person_id))
40       .order_by(community::title)
41       .load::<CommunityFollowerViewTuple>(conn)?;
42
43     Ok(Self::from_tuple_to_vec(res))
44   }
45 }
46
47 impl ViewToVec for CommunityFollowerView {
48   type DbTuple = CommunityFollowerViewTuple;
49   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
50     items
51       .iter()
52       .map(|a| Self {
53         community: a.0.to_owned(),
54         follower: a.1.to_owned(),
55       })
56       .collect::<Vec<Self>>()
57   }
58 }