1 use crate::structs::{ModHideCommunityView, ModlogListParams};
8 NullableExpressionMethods,
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
14 schema::{community, mod_hide_community, person},
16 community::{Community, CommunitySafe},
17 moderator::ModHideCommunity,
18 person::{Person, PersonSafe},
20 traits::{ToSafe, ViewToVec},
21 utils::{get_conn, limit_and_offset, DbPool},
24 type ModHideCommunityViewTuple = (ModHideCommunity, Option<PersonSafe>, CommunitySafe);
26 impl ModHideCommunityView {
27 // Pass in mod_id as admin_id because only admins can do this action
28 pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
29 let conn = &mut get_conn(pool).await?;
31 let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
32 let show_mod_names = !params.hide_modlog_names;
33 let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
35 let admin_names_join = mod_hide_community::mod_person_id
37 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
38 let mut query = mod_hide_community::table
39 .left_join(person::table.on(admin_names_join))
40 .inner_join(community::table.on(mod_hide_community::community_id.eq(community::id)))
42 mod_hide_community::all_columns,
43 Person::safe_columns_tuple().nullable(),
44 Community::safe_columns_tuple(),
48 if let Some(community_id) = params.community_id {
49 query = query.filter(mod_hide_community::community_id.eq(community_id));
52 if let Some(admin_id) = params.mod_person_id {
53 query = query.filter(mod_hide_community::mod_person_id.eq(admin_id));
56 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
61 .order_by(mod_hide_community::when_.desc())
62 .load::<ModHideCommunityViewTuple>(conn)
65 let results = Self::from_tuple_to_vec(res);
70 impl ViewToVec for ModHideCommunityView {
71 type DbTuple = ModHideCommunityViewTuple;
72 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
76 mod_hide_community: a.0,
80 .collect::<Vec<Self>>()