1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{community, mod_ban_from_community, user_, user_alias_1},
6 community::{Community, CommunitySafe},
7 moderator::ModBanFromCommunity,
8 user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
13 #[derive(Debug, Serialize, Clone)]
14 pub struct ModBanFromCommunityView {
15 pub mod_ban_from_community: ModBanFromCommunity,
16 pub moderator: UserSafe,
17 pub community: CommunitySafe,
18 pub banned_user: UserSafeAlias1,
21 type ModBanFromCommunityViewTuple = (ModBanFromCommunity, UserSafe, CommunitySafe, UserSafeAlias1);
23 impl ModBanFromCommunityView {
26 community_id: Option<i32>,
27 mod_user_id: Option<i32>,
30 ) -> Result<Vec<Self>, Error> {
31 let mut query = mod_ban_from_community::table
32 .inner_join(user_::table.on(mod_ban_from_community::mod_user_id.eq(user_::id)))
33 .inner_join(community::table)
35 user_alias_1::table.on(mod_ban_from_community::other_user_id.eq(user_alias_1::id)),
38 mod_ban_from_community::all_columns,
39 User_::safe_columns_tuple(),
40 Community::safe_columns_tuple(),
41 UserAlias1::safe_columns_tuple(),
45 if let Some(mod_user_id) = mod_user_id {
46 query = query.filter(mod_ban_from_community::mod_user_id.eq(mod_user_id));
49 if let Some(community_id) = community_id {
50 query = query.filter(mod_ban_from_community::community_id.eq(community_id));
53 let (limit, offset) = limit_and_offset(page, limit);
58 .order_by(mod_ban_from_community::when_.desc())
59 .load::<ModBanFromCommunityViewTuple>(conn)?;
61 Ok(Self::from_tuple_to_vec(res))
65 impl ViewToVec for ModBanFromCommunityView {
66 type DbTuple = ModBanFromCommunityViewTuple;
67 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
71 mod_ban_from_community: a.0.to_owned(),
72 moderator: a.1.to_owned(),
73 community: a.2.to_owned(),
74 banned_user: a.3.to_owned(),
76 .collect::<Vec<Self>>()