]> Untitled Git - lemmy.git/blob - crates/db_views_moderator/src/mod_remove_comment_view.rs
Add Modlog Filters (#2313)
[lemmy.git] / crates / db_views_moderator / src / mod_remove_comment_view.rs
1 use crate::structs::{ModRemoveCommentView, ModlogListParams};
2 use diesel::{result::Error, *};
3 use lemmy_db_schema::{
4   newtypes::PersonId,
5   schema::{comment, community, mod_remove_comment, person, person_alias_1, post},
6   source::{
7     comment::Comment,
8     community::{Community, CommunitySafe},
9     moderator::ModRemoveComment,
10     person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
11     post::Post,
12   },
13   traits::{ToSafe, ViewToVec},
14   utils::limit_and_offset,
15 };
16
17 type ModRemoveCommentViewTuple = (
18   ModRemoveComment,
19   Option<PersonSafe>,
20   Comment,
21   PersonSafeAlias1,
22   Post,
23   CommunitySafe,
24 );
25
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>();
31
32     let admin_names_join = mod_remove_comment::mod_person_id
33       .eq(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)))
41       .select((
42         mod_remove_comment::all_columns,
43         Person::safe_columns_tuple().nullable(),
44         comment::all_columns,
45         PersonAlias1::safe_columns_tuple(),
46         post::all_columns,
47         Community::safe_columns_tuple(),
48       ))
49       .into_boxed();
50
51     if let Some(community_id) = params.community_id {
52       query = query.filter(post::community_id.eq(community_id));
53     };
54
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));
57     };
58
59     if let Some(other_person_id) = params.other_person_id {
60       query = query.filter(person_alias_1::id.eq(other_person_id));
61     };
62
63     let (limit, offset) = limit_and_offset(params.page, params.limit)?;
64
65     let res = query
66       .limit(limit)
67       .offset(offset)
68       .order_by(mod_remove_comment::when_.desc())
69       .load::<ModRemoveCommentViewTuple>(conn)?;
70
71     let results = Self::from_tuple_to_vec(res);
72     Ok(results)
73   }
74 }
75
76 impl ViewToVec for ModRemoveCommentView {
77   type DbTuple = ModRemoveCommentViewTuple;
78   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
79     items
80       .into_iter()
81       .map(|a| Self {
82         mod_remove_comment: a.0,
83         moderator: a.1,
84         comment: a.2,
85         commenter: a.3,
86         post: a.4,
87         community: a.5,
88       })
89       .collect::<Vec<Self>>()
90   }
91 }