]> Untitled Git - lemmy.git/blob - crates/db_views_moderator/src/mod_remove_comment_view.rs
29d3a19c13407d19f361513e06d76c10b55b51cc
[lemmy.git] / crates / db_views_moderator / src / mod_remove_comment_view.rs
1 use crate::structs::ModRemoveCommentView;
2 use diesel::{result::Error, *};
3 use lemmy_db_schema::{
4   newtypes::{CommunityId, 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   PersonSafe,
20   Comment,
21   PersonSafeAlias1,
22   Post,
23   CommunitySafe,
24 );
25
26 impl ModRemoveCommentView {
27   pub fn list(
28     conn: &PgConnection,
29     community_id: Option<CommunityId>,
30     mod_person_id: Option<PersonId>,
31     page: Option<i64>,
32     limit: Option<i64>,
33   ) -> Result<Vec<Self>, Error> {
34     let mut query = mod_remove_comment::table
35       .inner_join(person::table)
36       .inner_join(comment::table)
37       .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
38       .inner_join(post::table.on(comment::post_id.eq(post::id)))
39       .inner_join(community::table.on(post::community_id.eq(community::id)))
40       .select((
41         mod_remove_comment::all_columns,
42         Person::safe_columns_tuple(),
43         comment::all_columns,
44         PersonAlias1::safe_columns_tuple(),
45         post::all_columns,
46         Community::safe_columns_tuple(),
47       ))
48       .into_boxed();
49
50     if let Some(community_id) = community_id {
51       query = query.filter(post::community_id.eq(community_id));
52     };
53
54     if let Some(mod_person_id) = mod_person_id {
55       query = query.filter(mod_remove_comment::mod_person_id.eq(mod_person_id));
56     };
57
58     let (limit, offset) = limit_and_offset(page, limit)?;
59
60     let res = query
61       .limit(limit)
62       .offset(offset)
63       .order_by(mod_remove_comment::when_.desc())
64       .load::<ModRemoveCommentViewTuple>(conn)?;
65
66     Ok(Self::from_tuple_to_vec(res))
67   }
68 }
69
70 impl ViewToVec for ModRemoveCommentView {
71   type DbTuple = ModRemoveCommentViewTuple;
72   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
73     items
74       .into_iter()
75       .map(|a| Self {
76         mod_remove_comment: a.0,
77         moderator: a.1,
78         comment: a.2,
79         commenter: a.3,
80         post: a.4,
81         community: a.5,
82       })
83       .collect::<Vec<Self>>()
84   }
85 }