1 use crate::structs::{ModAddCommunityView, ModlogListParams};
2 use diesel::{result::Error, *};
5 schema::{community, mod_add_community, person},
7 community::{Community, CommunitySafe},
8 moderator::ModAddCommunity,
9 person::{Person, PersonSafe},
11 traits::{ToSafe, ViewToVec},
12 utils::limit_and_offset,
15 type ModAddCommunityViewTuple = (
22 impl ModAddCommunityView {
23 pub fn list(conn: &mut PgConnection, params: ModlogListParams) -> Result<Vec<Self>, Error> {
24 let person_alias_1 = diesel::alias!(person as person1);
25 let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
26 let show_mod_names = !params.hide_modlog_names;
27 let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
29 let admin_names_join = mod_add_community::mod_person_id
31 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
32 let mut query = mod_add_community::table
33 .left_join(person::table.on(admin_names_join))
34 .inner_join(community::table)
36 person_alias_1.on(mod_add_community::other_person_id.eq(person_alias_1.field(person::id))),
39 mod_add_community::all_columns,
40 Person::safe_columns_tuple().nullable(),
41 Community::safe_columns_tuple(),
42 person_alias_1.fields(Person::safe_columns_tuple()),
46 if let Some(mod_person_id) = params.mod_person_id {
47 query = query.filter(mod_add_community::mod_person_id.eq(mod_person_id));
50 if let Some(community_id) = params.community_id {
51 query = query.filter(mod_add_community::community_id.eq(community_id));
54 if let Some(other_person_id) = params.other_person_id {
55 query = query.filter(person_alias_1.field(person::id).eq(other_person_id));
58 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
63 .order_by(mod_add_community::when_.desc())
64 .load::<ModAddCommunityViewTuple>(conn)?;
66 let results = Self::from_tuple_to_vec(res);
71 impl ViewToVec for ModAddCommunityView {
72 type DbTuple = ModAddCommunityViewTuple;
73 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
77 mod_add_community: a.0,
82 .collect::<Vec<Self>>()