]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/community_follower_view.rs
Diesel 2.0.0 upgrade (#2452)
[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       .order_by(community::title)
44       .load::<CommunityFollowerViewTuple>(conn)?;
45
46     Ok(Self::from_tuple_to_vec(res))
47   }
48 }
49
50 impl ViewToVec for CommunityFollowerView {
51   type DbTuple = CommunityFollowerViewTuple;
52   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
53     items
54       .into_iter()
55       .map(|a| Self {
56         community: a.0,
57         follower: a.1,
58       })
59       .collect::<Vec<Self>>()
60   }
61 }