1 use crate::structs::{ModRemoveCommunityView, ModlogListParams};
2 use diesel::{result::Error, *};
5 schema::{community, mod_remove_community, person},
7 community::{Community, CommunitySafe},
8 moderator::ModRemoveCommunity,
9 person::{Person, PersonSafe},
11 traits::{ToSafe, ViewToVec},
12 utils::limit_and_offset,
15 type ModRemoveCommunityTuple = (ModRemoveCommunity, Option<PersonSafe>, CommunitySafe);
17 impl ModRemoveCommunityView {
18 pub fn list(conn: &mut PgConnection, params: ModlogListParams) -> Result<Vec<Self>, Error> {
19 let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
20 let show_mod_names = !params.hide_modlog_names;
21 let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
23 let admin_names_join = mod_remove_community::mod_person_id
25 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
26 let mut query = mod_remove_community::table
27 .left_join(person::table.on(admin_names_join))
28 .inner_join(community::table)
30 mod_remove_community::all_columns,
31 Person::safe_columns_tuple().nullable(),
32 Community::safe_columns_tuple(),
36 if let Some(mod_person_id) = params.mod_person_id {
37 query = query.filter(mod_remove_community::mod_person_id.eq(mod_person_id));
40 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
45 .order_by(mod_remove_community::when_.desc())
46 .load::<ModRemoveCommunityTuple>(conn)?;
48 let results = Self::from_tuple_to_vec(res);
53 impl ViewToVec for ModRemoveCommunityView {
54 type DbTuple = ModRemoveCommunityTuple;
55 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
59 mod_remove_community: a.0,
63 .collect::<Vec<Self>>()