1 use crate::structs::{ModBanFromCommunityView, ModlogListParams};
2 use diesel::{result::Error, *};
5 schema::{community, mod_ban_from_community, person},
7 community::{Community, CommunitySafe},
8 moderator::ModBanFromCommunity,
9 person::{Person, PersonSafe},
11 traits::{ToSafe, ViewToVec},
12 utils::limit_and_offset,
15 type ModBanFromCommunityViewTuple = (
22 impl ModBanFromCommunityView {
23 pub fn list(conn: &mut PgConnection, params: ModlogListParams) -> Result<Vec<Self>, Error> {
24 let person_alias_1 = diesel::alias!(person as person1);
25 let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
26 let show_mod_names = !params.hide_modlog_names;
27 let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
29 let admin_names_join = mod_ban_from_community::mod_person_id
31 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
32 let mut query = mod_ban_from_community::table
33 .left_join(person::table.on(admin_names_join))
34 .inner_join(community::table)
37 .on(mod_ban_from_community::other_person_id.eq(person_alias_1.field(person::id))),
40 mod_ban_from_community::all_columns,
41 Person::safe_columns_tuple().nullable(),
42 Community::safe_columns_tuple(),
43 person_alias_1.fields(Person::safe_columns_tuple()),
47 if let Some(mod_person_id) = params.mod_person_id {
48 query = query.filter(mod_ban_from_community::mod_person_id.eq(mod_person_id));
51 if let Some(community_id) = params.community_id {
52 query = query.filter(mod_ban_from_community::community_id.eq(community_id));
55 if let Some(other_person_id) = params.other_person_id {
56 query = query.filter(mod_ban_from_community::other_person_id.eq(other_person_id));
59 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
64 .order_by(mod_ban_from_community::when_.desc())
65 .load::<ModBanFromCommunityViewTuple>(conn)?;
67 let results = Self::from_tuple_to_vec(res);
72 impl ViewToVec for ModBanFromCommunityView {
73 type DbTuple = ModBanFromCommunityViewTuple;
74 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
78 mod_ban_from_community: a.0,
83 .collect::<Vec<Self>>()