1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{ToSafe, ViewToVec};
4 schema::{community, community_follower, user_},
6 community::{Community, CommunitySafe},
7 user::{UserSafe, User_},
12 #[derive(Debug, Serialize, Clone)]
13 pub struct CommunityFollowerView {
14 pub community: CommunitySafe,
15 pub follower: UserSafe,
18 type CommunityFollowerViewTuple = (CommunitySafe, UserSafe);
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(user_::table)
25 .select((Community::safe_columns_tuple(), User_::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_user(conn: &PgConnection, user_id: i32) -> Result<Vec<Self>, Error> {
34 let res = community_follower::table
35 .inner_join(community::table)
36 .inner_join(user_::table)
37 .select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
38 .filter(community_follower::user_id.eq(user_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>>()