1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{community, mod_lock_post, post, person},
6 community::{Community, CommunitySafe},
7 moderator::ModLockPost,
9 person::{PersonSafe, Person},
14 #[derive(Debug, Serialize, Clone)]
15 pub struct ModLockPostView {
16 pub mod_lock_post: ModLockPost,
17 pub moderator: PersonSafe,
19 pub community: CommunitySafe,
22 type ModLockPostViewTuple = (ModLockPost, PersonSafe, Post, CommunitySafe);
24 impl ModLockPostView {
27 community_id: Option<i32>,
28 mod_person_id: Option<i32>,
31 ) -> Result<Vec<Self>, Error> {
32 let mut query = mod_lock_post::table
33 .inner_join(person::table)
34 .inner_join(post::table)
35 .inner_join(community::table.on(post::community_id.eq(community::id)))
37 mod_lock_post::all_columns,
38 Person::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_person_id) = mod_person_id {
49 query = query.filter(mod_lock_post::mod_person_id.eq(mod_person_id));
52 let (limit, offset) = limit_and_offset(page, limit);
57 .order_by(mod_lock_post::when_.desc())
58 .load::<ModLockPostViewTuple>(conn)?;
60 Ok(Self::from_tuple_to_vec(res))
64 impl ViewToVec for ModLockPostView {
65 type DbTuple = ModLockPostViewTuple;
66 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
70 mod_lock_post: a.0.to_owned(),
71 moderator: a.1.to_owned(),
73 community: a.3.to_owned(),
75 .collect::<Vec<Self>>()