1 use crate::structs::ModHideCommunityView;
2 use diesel::{result::Error, *};
4 newtypes::{CommunityId, PersonId},
5 schema::{community, mod_hide_community, person},
7 community::{Community, CommunitySafe},
8 moderator::ModHideCommunity,
9 person::{Person, PersonSafe},
11 traits::{ToSafe, ViewToVec},
12 utils::limit_and_offset,
15 type ModHideCommunityViewTuple = (ModHideCommunity, PersonSafe, CommunitySafe);
17 impl ModHideCommunityView {
18 // Pass in mod_id as admin_id because only admins can do this action
21 community_id: Option<CommunityId>,
22 admin_id: Option<PersonId>,
25 ) -> Result<Vec<Self>, Error> {
26 let mut query = mod_hide_community::table
27 .inner_join(person::table)
28 .inner_join(community::table.on(mod_hide_community::community_id.eq(community::id)))
30 mod_hide_community::all_columns,
31 Person::safe_columns_tuple(),
32 Community::safe_columns_tuple(),
36 if let Some(community_id) = community_id {
37 query = query.filter(mod_hide_community::community_id.eq(community_id));
40 if let Some(admin_id) = admin_id {
41 query = query.filter(mod_hide_community::mod_person_id.eq(admin_id));
44 let (limit, offset) = limit_and_offset(page, limit)?;
49 .order_by(mod_hide_community::when_.desc())
50 .load::<ModHideCommunityViewTuple>(conn)?;
52 Ok(Self::from_tuple_to_vec(res))
56 impl ViewToVec for ModHideCommunityView {
57 type DbTuple = ModHideCommunityViewTuple;
58 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
62 mod_hide_community: a.0.to_owned(),
63 admin: a.1.to_owned(),
64 community: a.2.to_owned(),
66 .collect::<Vec<Self>>()