1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{community, mod_remove_post, post, user_},
6 community::{Community, CommunitySafe},
7 moderator::ModRemovePost,
9 user::{UserSafe, User_},
14 #[derive(Debug, Serialize, Clone)]
15 pub struct ModRemovePostView {
16 pub mod_remove_post: ModRemovePost,
17 pub moderator: UserSafe,
19 pub community: CommunitySafe,
22 type ModRemovePostViewTuple = (ModRemovePost, UserSafe, Post, CommunitySafe);
24 impl ModRemovePostView {
27 community_id: Option<i32>,
28 mod_user_id: Option<i32>,
31 ) -> Result<Vec<Self>, Error> {
32 let mut query = mod_remove_post::table
33 .inner_join(user_::table)
34 .inner_join(post::table)
35 .inner_join(community::table.on(post::community_id.eq(community::id)))
37 mod_remove_post::all_columns,
38 User_::safe_columns_tuple(),
40 Community::safe_columns_tuple(),
44 if let Some(community_id) = community_id {
45 query = query.filter(post::community_id.eq(community_id));
48 if let Some(mod_user_id) = mod_user_id {
49 query = query.filter(mod_remove_post::mod_user_id.eq(mod_user_id));
52 let (limit, offset) = limit_and_offset(page, limit);
57 .order_by(mod_remove_post::when_.desc())
58 .load::<ModRemovePostViewTuple>(conn)?;
60 Ok(Self::from_tuple_to_vec(res))
64 impl ViewToVec for ModRemovePostView {
65 type DbTuple = ModRemovePostViewTuple;
66 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
70 mod_remove_post: a.0.to_owned(),
71 moderator: a.1.to_owned(),
73 community: a.3.to_owned(),
75 .collect::<Vec<Self>>()