1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{mod_ban, user_, user_alias_1},
7 user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
12 #[derive(Debug, Serialize, Clone)]
13 pub struct ModBanView {
15 pub moderator: UserSafe,
16 pub banned_user: UserSafeAlias1,
19 type ModBanViewTuple = (ModBan, UserSafe, UserSafeAlias1);
24 mod_user_id: Option<i32>,
27 ) -> Result<Vec<Self>, Error> {
28 let mut query = mod_ban::table
29 .inner_join(user_::table.on(mod_ban::mod_user_id.eq(user_::id)))
30 .inner_join(user_alias_1::table.on(mod_ban::other_user_id.eq(user_::id)))
33 User_::safe_columns_tuple(),
34 UserAlias1::safe_columns_tuple(),
38 if let Some(mod_user_id) = mod_user_id {
39 query = query.filter(mod_ban::mod_user_id.eq(mod_user_id));
42 let (limit, offset) = limit_and_offset(page, limit);
47 .order_by(mod_ban::when_.desc())
48 .load::<ModBanViewTuple>(conn)?;
50 Ok(Self::from_tuple_to_vec(res))
54 impl ViewToVec for ModBanView {
55 type DbTuple = ModBanViewTuple;
56 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
60 mod_ban: a.0.to_owned(),
61 moderator: a.1.to_owned(),
62 banned_user: a.2.to_owned(),
64 .collect::<Vec<Self>>()