]> Untitled Git - lemmy.git/blob - crates/db_views_moderator/src/mod_remove_comment_view.rs
Merge branch 'main' into federated-moderation
[lemmy.git] / crates / db_views_moderator / src / mod_remove_comment_view.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
3 use lemmy_db_schema::{
4   schema::{comment, community, mod_remove_comment, person, person_alias_1, post},
5   source::{
6     comment::Comment,
7     community::{Community, CommunitySafe},
8     moderator::ModRemoveComment,
9     person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
10     post::Post,
11   },
12   CommunityId,
13   PersonId,
14 };
15 use serde::Serialize;
16
17 #[derive(Debug, Serialize, Clone)]
18 pub struct ModRemoveCommentView {
19   pub mod_remove_comment: ModRemoveComment,
20   pub moderator: PersonSafe,
21   pub comment: Comment,
22   pub commenter: PersonSafeAlias1,
23   pub post: Post,
24   pub community: CommunitySafe,
25 }
26
27 type ModRemoveCommentViewTuple = (
28   ModRemoveComment,
29   PersonSafe,
30   Comment,
31   PersonSafeAlias1,
32   Post,
33   CommunitySafe,
34 );
35
36 impl ModRemoveCommentView {
37   pub fn list(
38     conn: &PgConnection,
39     community_id: Option<CommunityId>,
40     mod_person_id: Option<PersonId>,
41     page: Option<i64>,
42     limit: Option<i64>,
43   ) -> Result<Vec<Self>, Error> {
44     let mut query = mod_remove_comment::table
45       .inner_join(person::table)
46       .inner_join(comment::table)
47       .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
48       .inner_join(post::table.on(comment::post_id.eq(post::id)))
49       .inner_join(community::table.on(post::community_id.eq(community::id)))
50       .select((
51         mod_remove_comment::all_columns,
52         Person::safe_columns_tuple(),
53         comment::all_columns,
54         PersonAlias1::safe_columns_tuple(),
55         post::all_columns,
56         Community::safe_columns_tuple(),
57       ))
58       .into_boxed();
59
60     if let Some(community_id) = community_id {
61       query = query.filter(post::community_id.eq(community_id));
62     };
63
64     if let Some(mod_person_id) = mod_person_id {
65       query = query.filter(mod_remove_comment::mod_person_id.eq(mod_person_id));
66     };
67
68     let (limit, offset) = limit_and_offset(page, limit);
69
70     let res = query
71       .limit(limit)
72       .offset(offset)
73       .order_by(mod_remove_comment::when_.desc())
74       .load::<ModRemoveCommentViewTuple>(conn)?;
75
76     Ok(Self::from_tuple_to_vec(res))
77   }
78 }
79
80 impl ViewToVec for ModRemoveCommentView {
81   type DbTuple = ModRemoveCommentViewTuple;
82   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
83     items
84       .iter()
85       .map(|a| Self {
86         mod_remove_comment: a.0.to_owned(),
87         moderator: a.1.to_owned(),
88         comment: a.2.to_owned(),
89         commenter: a.3.to_owned(),
90         post: a.4.to_owned(),
91         community: a.5.to_owned(),
92       })
93       .collect::<Vec<Self>>()
94   }
95 }