1 use crate::structs::{AdminPurgeCommunityView, ModlogListParams};
2 use diesel::{result::Error, *};
5 schema::{admin_purge_community, person},
7 moderator::AdminPurgeCommunity,
8 person::{Person, PersonSafe},
10 traits::{ToSafe, ViewToVec},
11 utils::limit_and_offset,
14 type AdminPurgeCommunityViewTuple = (AdminPurgeCommunity, Option<PersonSafe>);
16 impl AdminPurgeCommunityView {
17 pub fn list(conn: &mut PgConnection, params: ModlogListParams) -> Result<Vec<Self>, Error> {
18 let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
19 let show_mod_names = !params.hide_modlog_names;
20 let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
22 let admin_names_join = admin_purge_community::admin_person_id
24 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
26 let mut query = admin_purge_community::table
27 .left_join(person::table.on(admin_names_join))
29 admin_purge_community::all_columns,
30 Person::safe_columns_tuple().nullable(),
34 if let Some(admin_person_id) = params.mod_person_id {
35 query = query.filter(admin_purge_community::admin_person_id.eq(admin_person_id));
38 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
43 .order_by(admin_purge_community::when_.desc())
44 .load::<AdminPurgeCommunityViewTuple>(conn)?;
46 let results = Self::from_tuple_to_vec(res);
51 impl ViewToVec for AdminPurgeCommunityView {
52 type DbTuple = AdminPurgeCommunityViewTuple;
53 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
57 admin_purge_community: a.0,
60 .collect::<Vec<Self>>()