1 use crate::structs::{ModBanFromCommunityView, ModlogListParams};
2 use diesel::{result::Error, *};
5 schema::{community, mod_ban_from_community, person, person_alias_1},
7 community::{Community, CommunitySafe},
8 moderator::ModBanFromCommunity,
9 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
11 traits::{ToSafe, ViewToVec},
12 utils::limit_and_offset,
15 type ModBanFromCommunityViewTuple = (
22 impl ModBanFromCommunityView {
23 pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result<Vec<Self>, Error> {
24 let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
25 let show_mod_names = !params.hide_modlog_names;
26 let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
28 let admin_names_join = mod_ban_from_community::mod_person_id
30 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
31 let mut query = mod_ban_from_community::table
32 .left_join(person::table.on(admin_names_join))
33 .inner_join(community::table)
35 person_alias_1::table.on(mod_ban_from_community::other_person_id.eq(person_alias_1::id)),
38 mod_ban_from_community::all_columns,
39 Person::safe_columns_tuple().nullable(),
40 Community::safe_columns_tuple(),
41 PersonAlias1::safe_columns_tuple(),
45 if let Some(mod_person_id) = params.mod_person_id {
46 query = query.filter(mod_ban_from_community::mod_person_id.eq(mod_person_id));
49 if let Some(community_id) = params.community_id {
50 query = query.filter(mod_ban_from_community::community_id.eq(community_id));
53 if let Some(other_person_id) = params.other_person_id {
54 query = query.filter(mod_ban_from_community::other_person_id.eq(other_person_id));
57 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
62 .order_by(mod_ban_from_community::when_.desc())
63 .load::<ModBanFromCommunityViewTuple>(conn)?;
65 let results = Self::from_tuple_to_vec(res);
70 impl ViewToVec for ModBanFromCommunityView {
71 type DbTuple = ModBanFromCommunityViewTuple;
72 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
76 mod_ban_from_community: a.0,
81 .collect::<Vec<Self>>()