1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{comment, community, mod_remove_comment, person, person_alias_1, post},
7 community::{Community, CommunitySafe},
8 moderator::ModRemoveComment,
9 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
17 #[derive(Debug, Serialize, Clone)]
18 pub struct ModRemoveCommentView {
19 pub mod_remove_comment: ModRemoveComment,
20 pub moderator: PersonSafe,
22 pub commenter: PersonSafeAlias1,
24 pub community: CommunitySafe,
27 type ModRemoveCommentViewTuple = (
36 impl ModRemoveCommentView {
39 community_id: Option<CommunityId>,
40 mod_person_id: Option<PersonId>,
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)))
51 mod_remove_comment::all_columns,
52 Person::safe_columns_tuple(),
54 PersonAlias1::safe_columns_tuple(),
56 Community::safe_columns_tuple(),
60 if let Some(community_id) = community_id {
61 query = query.filter(post::community_id.eq(community_id));
64 if let Some(mod_person_id) = mod_person_id {
65 query = query.filter(mod_remove_comment::mod_person_id.eq(mod_person_id));
68 let (limit, offset) = limit_and_offset(page, limit);
73 .order_by(mod_remove_comment::when_.desc())
74 .load::<ModRemoveCommentViewTuple>(conn)?;
76 Ok(Self::from_tuple_to_vec(res))
80 impl ViewToVec for ModRemoveCommentView {
81 type DbTuple = ModRemoveCommentViewTuple;
82 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<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(),
91 community: a.5.to_owned(),
93 .collect::<Vec<Self>>()