1 use crate::structs::{ModRemoveCommentView, ModlogListParams};
2 use diesel::{result::Error, *};
5 schema::{comment, community, mod_remove_comment, person, person_alias_1, post},
8 community::{Community, CommunitySafe},
9 moderator::ModRemoveComment,
10 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
13 traits::{ToSafe, ViewToVec},
14 utils::limit_and_offset,
17 type ModRemoveCommentViewTuple = (
26 impl ModRemoveCommentView {
27 pub fn list(conn: &PgConnection, params: ModlogListParams) -> Result<Vec<Self>, Error> {
28 let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
29 let show_mod_names = !params.hide_modlog_names;
30 let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
32 let admin_names_join = mod_remove_comment::mod_person_id
34 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
35 let mut query = mod_remove_comment::table
36 .left_join(person::table.on(admin_names_join))
37 .inner_join(comment::table)
38 .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
39 .inner_join(post::table.on(comment::post_id.eq(post::id)))
40 .inner_join(community::table.on(post::community_id.eq(community::id)))
42 mod_remove_comment::all_columns,
43 Person::safe_columns_tuple().nullable(),
45 PersonAlias1::safe_columns_tuple(),
47 Community::safe_columns_tuple(),
51 if let Some(community_id) = params.community_id {
52 query = query.filter(post::community_id.eq(community_id));
55 if let Some(mod_person_id) = params.mod_person_id {
56 query = query.filter(mod_remove_comment::mod_person_id.eq(mod_person_id));
59 if let Some(other_person_id) = params.other_person_id {
60 query = query.filter(person_alias_1::id.eq(other_person_id));
63 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
68 .order_by(mod_remove_comment::when_.desc())
69 .load::<ModRemoveCommentViewTuple>(conn)?;
71 let results = Self::from_tuple_to_vec(res);
76 impl ViewToVec for ModRemoveCommentView {
77 type DbTuple = ModRemoveCommentViewTuple;
78 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
82 mod_remove_comment: a.0,
89 .collect::<Vec<Self>>()