]> Untitled Git - lemmy.git/blob - crates/db_views_moderator/src/mod_remove_comment_view.rs
0d33974b834f2e6554e12ee2ca0dad862df7433f
[lemmy.git] / crates / db_views_moderator / src / mod_remove_comment_view.rs
1 use crate::structs::{ModRemoveCommentView, ModlogListParams};
2 use diesel::{
3   result::Error,
4   BoolExpressionMethods,
5   ExpressionMethods,
6   IntoSql,
7   JoinOnDsl,
8   NullableExpressionMethods,
9   QueryDsl,
10 };
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
13   newtypes::PersonId,
14   schema::{comment, community, mod_remove_comment, person, post},
15   source::{
16     comment::Comment,
17     community::Community,
18     moderator::ModRemoveComment,
19     person::Person,
20     post::Post,
21   },
22   traits::JoinView,
23   utils::{get_conn, limit_and_offset, DbPool},
24 };
25
26 type ModRemoveCommentViewTuple = (
27   ModRemoveComment,
28   Option<Person>,
29   Comment,
30   Person,
31   Post,
32   Community,
33 );
34
35 impl ModRemoveCommentView {
36   pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
37     let conn = &mut get_conn(pool).await?;
38     let person_alias_1 = diesel::alias!(lemmy_db_schema::schema::person as person1);
39     let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
40     let show_mod_names = !params.hide_modlog_names;
41     let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
42
43     let admin_names_join = mod_remove_comment::mod_person_id
44       .eq(person::id)
45       .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
46     let mut query = mod_remove_comment::table
47       .left_join(person::table.on(admin_names_join))
48       .inner_join(comment::table)
49       .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id))))
50       .inner_join(post::table.on(comment::post_id.eq(post::id)))
51       .inner_join(community::table.on(post::community_id.eq(community::id)))
52       .select((
53         mod_remove_comment::all_columns,
54         person::all_columns.nullable(),
55         comment::all_columns,
56         person_alias_1.fields(person::all_columns),
57         post::all_columns,
58         community::all_columns,
59       ))
60       .into_boxed();
61
62     if let Some(community_id) = params.community_id {
63       query = query.filter(post::community_id.eq(community_id));
64     };
65
66     if let Some(mod_person_id) = params.mod_person_id {
67       query = query.filter(mod_remove_comment::mod_person_id.eq(mod_person_id));
68     };
69
70     if let Some(other_person_id) = params.other_person_id {
71       query = query.filter(person_alias_1.field(person::id).eq(other_person_id));
72     };
73
74     let (limit, offset) = limit_and_offset(params.page, params.limit)?;
75
76     let res = query
77       .limit(limit)
78       .offset(offset)
79       .order_by(mod_remove_comment::when_.desc())
80       .load::<ModRemoveCommentViewTuple>(conn)
81       .await?;
82
83     let results = res.into_iter().map(Self::from_tuple).collect();
84     Ok(results)
85   }
86 }
87
88 impl JoinView for ModRemoveCommentView {
89   type JoinTuple = ModRemoveCommentViewTuple;
90   fn from_tuple(a: Self::JoinTuple) -> Self {
91     Self {
92       mod_remove_comment: a.0,
93       moderator: a.1,
94       comment: a.2,
95       commenter: a.3,
96       post: a.4,
97       community: a.5,
98     }
99   }
100 }