1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{limit_and_offset, ToSafe, ViewToVec};
4 schema::{community, mod_sticky_post, person, post},
6 community::{Community, CommunitySafe},
7 moderator::ModStickyPost,
8 person::{Person, PersonSafe},
16 #[derive(Debug, Serialize, Clone)]
17 pub struct ModStickyPostView {
18 pub mod_sticky_post: ModStickyPost,
19 pub moderator: PersonSafe,
21 pub community: CommunitySafe,
24 type ModStickyPostViewTuple = (ModStickyPost, PersonSafe, Post, CommunitySafe);
26 impl ModStickyPostView {
29 community_id: Option<CommunityId>,
30 mod_person_id: Option<PersonId>,
33 ) -> Result<Vec<Self>, Error> {
34 let mut query = mod_sticky_post::table
35 .inner_join(person::table)
36 .inner_join(post::table)
37 .inner_join(community::table.on(post::community_id.eq(community::id)))
39 mod_sticky_post::all_columns,
40 Person::safe_columns_tuple(),
42 Community::safe_columns_tuple(),
46 if let Some(community_id) = community_id {
47 query = query.filter(post::community_id.eq(community_id));
50 if let Some(mod_person_id) = mod_person_id {
51 query = query.filter(mod_sticky_post::mod_person_id.eq(mod_person_id));
54 let (limit, offset) = limit_and_offset(page, limit);
59 .order_by(mod_sticky_post::when_.desc())
60 .load::<ModStickyPostViewTuple>(conn)?;
62 Ok(Self::from_tuple_to_vec(res))
66 impl ViewToVec for ModStickyPostView {
67 type DbTuple = ModStickyPostViewTuple;
68 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
72 mod_sticky_post: a.0.to_owned(),
73 moderator: a.1.to_owned(),
75 community: a.3.to_owned(),
77 .collect::<Vec<Self>>()