]> Untitled Git - lemmy.git/blob - crates/db_views_moderator/src/mod_remove_comment_view.rs
c0b58845683c25e125ede8173946d7a274a7e53b
[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, post},
6   source::{
7     comment::Comment,
8     community::{Community, CommunitySafe},
9     moderator::ModRemoveComment,
10     person::{Person, PersonSafe},
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   PersonSafe,
22   Post,
23   CommunitySafe,
24 );
25
26 impl ModRemoveCommentView {
27   pub fn list(conn: &mut PgConnection, params: ModlogListParams) -> Result<Vec<Self>, Error> {
28     let person_alias_1 = diesel::alias!(lemmy_db_schema::schema::person as person1);
29     let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
30     let show_mod_names = !params.hide_modlog_names;
31     let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
32
33     let admin_names_join = mod_remove_comment::mod_person_id
34       .eq(person::id)
35       .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
36     let mut query = mod_remove_comment::table
37       .left_join(person::table.on(admin_names_join))
38       .inner_join(comment::table)
39       .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id))))
40       .inner_join(post::table.on(comment::post_id.eq(post::id)))
41       .inner_join(community::table.on(post::community_id.eq(community::id)))
42       .select((
43         mod_remove_comment::all_columns,
44         Person::safe_columns_tuple().nullable(),
45         comment::all_columns,
46         person_alias_1.fields(Person::safe_columns_tuple()),
47         post::all_columns,
48         Community::safe_columns_tuple(),
49       ))
50       .into_boxed();
51
52     if let Some(community_id) = params.community_id {
53       query = query.filter(post::community_id.eq(community_id));
54     };
55
56     if let Some(mod_person_id) = params.mod_person_id {
57       query = query.filter(mod_remove_comment::mod_person_id.eq(mod_person_id));
58     };
59
60     if let Some(other_person_id) = params.other_person_id {
61       query = query.filter(person_alias_1.field(person::id).eq(other_person_id));
62     };
63
64     let (limit, offset) = limit_and_offset(params.page, params.limit)?;
65
66     let res = query
67       .limit(limit)
68       .offset(offset)
69       .order_by(mod_remove_comment::when_.desc())
70       .load::<ModRemoveCommentViewTuple>(conn)?;
71
72     let results = Self::from_tuple_to_vec(res);
73     Ok(results)
74   }
75 }
76
77 impl ViewToVec for ModRemoveCommentView {
78   type DbTuple = ModRemoveCommentViewTuple;
79   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
80     items
81       .into_iter()
82       .map(|a| Self {
83         mod_remove_comment: a.0,
84         moderator: a.1,
85         comment: a.2,
86         commenter: a.3,
87         post: a.4,
88         community: a.5,
89       })
90       .collect::<Vec<Self>>()
91   }
92 }