1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{ToSafe, ViewToVec};
4 schema::{community, community_follower, person},
6 community::{Community, CommunitySafe},
7 person::{PersonSafe, Person},
12 #[derive(Debug, Serialize, Clone)]
13 pub struct CommunityFollowerView {
14 pub community: CommunitySafe,
15 pub follower: PersonSafe,
18 type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe);
20 impl CommunityFollowerView {
21 pub fn for_community(conn: &PgConnection, community_id: i32) -> Result<Vec<Self>, Error> {
22 let res = community_follower::table
23 .inner_join(community::table)
24 .inner_join(person::table)
25 .select((Community::safe_columns_tuple(), Person::safe_columns_tuple()))
26 .filter(community_follower::community_id.eq(community_id))
27 .order_by(community_follower::published)
28 .load::<CommunityFollowerViewTuple>(conn)?;
30 Ok(Self::from_tuple_to_vec(res))
33 pub fn for_person(conn: &PgConnection, person_id: i32) -> Result<Vec<Self>, Error> {
34 let res = community_follower::table
35 .inner_join(community::table)
36 .inner_join(person::table)
37 .select((Community::safe_columns_tuple(), Person::safe_columns_tuple()))
38 .filter(community_follower::person_id.eq(person_id))
39 .order_by(community_follower::published)
40 .load::<CommunityFollowerViewTuple>(conn)?;
42 Ok(Self::from_tuple_to_vec(res))
46 impl ViewToVec for CommunityFollowerView {
47 type DbTuple = CommunityFollowerViewTuple;
48 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
52 community: a.0.to_owned(),
53 follower: a.1.to_owned(),
55 .collect::<Vec<Self>>()