1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{ToSafe, ViewToVec};
4 schema::{community, community_moderator, user_},
6 community::{Community, CommunitySafe},
7 user::{UserSafe, User_},
12 #[derive(Debug, Serialize, Clone)]
13 pub struct CommunityModeratorView {
14 pub community: CommunitySafe,
15 pub moderator: UserSafe,
18 type CommunityModeratorViewTuple = (CommunitySafe, UserSafe);
20 impl CommunityModeratorView {
21 pub fn for_community(conn: &PgConnection, community_id: i32) -> Result<Vec<Self>, Error> {
22 let res = community_moderator::table
23 .inner_join(community::table)
24 .inner_join(user_::table)
25 .select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
26 .filter(community_moderator::community_id.eq(community_id))
27 .order_by(community_moderator::published)
28 .load::<CommunityModeratorViewTuple>(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_moderator::table
35 .inner_join(community::table)
36 .inner_join(user_::table)
37 .select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
38 .filter(community_moderator::user_id.eq(user_id))
39 .order_by(community_moderator::published)
40 .load::<CommunityModeratorViewTuple>(conn)?;
42 Ok(Self::from_tuple_to_vec(res))
46 impl ViewToVec for CommunityModeratorView {
47 type DbTuple = CommunityModeratorViewTuple;
48 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
52 community: a.0.to_owned(),
53 moderator: a.1.to_owned(),
55 .collect::<Vec<Self>>()