1 use crate::structs::{AdminPurgeCommentView, ModlogListParams};
2 use diesel::{result::Error, *};
5 schema::{admin_purge_comment, person, post},
7 moderator::AdminPurgeComment,
8 person::{Person, PersonSafe},
11 traits::{ToSafe, ViewToVec},
12 utils::limit_and_offset,
15 type AdminPurgeCommentViewTuple = (AdminPurgeComment, Option<PersonSafe>, Post);
17 impl AdminPurgeCommentView {
18 pub fn list(conn: &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 = admin_purge_comment::admin_person_id
25 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
27 let mut query = admin_purge_comment::table
28 .left_join(person::table.on(admin_names_join))
29 .inner_join(post::table)
31 admin_purge_comment::all_columns,
32 Person::safe_columns_tuple().nullable(),
37 if let Some(admin_person_id) = params.mod_person_id {
38 query = query.filter(admin_purge_comment::admin_person_id.eq(admin_person_id));
41 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
46 .order_by(admin_purge_comment::when_.desc())
47 .load::<AdminPurgeCommentViewTuple>(conn)?;
49 let results = Self::from_tuple_to_vec(res);
54 impl ViewToVec for AdminPurgeCommentView {
55 type DbTuple = AdminPurgeCommentViewTuple;
56 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
60 admin_purge_comment: a.0,
64 .collect::<Vec<Self>>()