1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{comment, community, mod_remove_comment, post, user_, user_alias_1},
7 community::{Community, CommunitySafe},
8 moderator::ModRemoveComment,
10 user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
15 #[derive(Debug, Serialize, Clone)]
16 pub struct ModRemoveCommentView {
17 pub mod_remove_comment: ModRemoveComment,
18 pub moderator: UserSafe,
20 pub commenter: UserSafeAlias1,
22 pub community: CommunitySafe,
25 type ModRemoveCommentViewTuple = (
34 impl ModRemoveCommentView {
37 community_id: Option<i32>,
38 mod_user_id: Option<i32>,
41 ) -> Result<Vec<Self>, Error> {
42 let mut query = mod_remove_comment::table
43 .inner_join(user_::table)
44 .inner_join(comment::table)
45 .inner_join(user_alias_1::table.on(comment::creator_id.eq(user_alias_1::id)))
46 .inner_join(post::table.on(comment::post_id.eq(post::id)))
47 .inner_join(community::table.on(post::community_id.eq(community::id)))
49 mod_remove_comment::all_columns,
50 User_::safe_columns_tuple(),
52 UserAlias1::safe_columns_tuple(),
54 Community::safe_columns_tuple(),
58 if let Some(community_id) = community_id {
59 query = query.filter(post::community_id.eq(community_id));
62 if let Some(mod_user_id) = mod_user_id {
63 query = query.filter(mod_remove_comment::mod_user_id.eq(mod_user_id));
66 let (limit, offset) = limit_and_offset(page, limit);
71 .order_by(mod_remove_comment::when_.desc())
72 .load::<ModRemoveCommentViewTuple>(conn)?;
74 Ok(Self::from_tuple_to_vec(res))
78 impl ViewToVec for ModRemoveCommentView {
79 type DbTuple = ModRemoveCommentViewTuple;
80 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
84 mod_remove_comment: a.0.to_owned(),
85 moderator: a.1.to_owned(),
86 comment: a.2.to_owned(),
87 commenter: a.3.to_owned(),
89 community: a.5.to_owned(),
91 .collect::<Vec<Self>>()