1 use crate::structs::{ModBanView, ModlogListParams};
8 NullableExpressionMethods,
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
14 schema::{mod_ban, person},
17 person::{Person, PersonSafe},
19 traits::{ToSafe, ViewToVec},
20 utils::{get_conn, limit_and_offset, DbPool},
23 type ModBanViewTuple = (ModBan, Option<PersonSafe>, PersonSafe);
26 pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
27 let conn = &mut get_conn(pool).await?;
28 let person_alias_1 = diesel::alias!(person as person1);
29 let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
30 let show_mod_names = !params.hide_modlog_names;
31 let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
33 let admin_names_join = mod_ban::mod_person_id
35 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
36 let mut query = mod_ban::table
37 .left_join(person::table.on(admin_names_join))
38 .inner_join(person_alias_1.on(mod_ban::other_person_id.eq(person_alias_1.field(person::id))))
41 Person::safe_columns_tuple().nullable(),
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_ban::mod_person_id.eq(mod_person_id));
50 if let Some(other_person_id) = params.other_person_id {
51 query = query.filter(person_alias_1.field(person::id).eq(other_person_id));
54 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
59 .order_by(mod_ban::when_.desc())
60 .load::<ModBanViewTuple>(conn)
63 let results = Self::from_tuple_to_vec(res);
68 impl ViewToVec for ModBanView {
69 type DbTuple = ModBanViewTuple;
70 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
78 .collect::<Vec<Self>>()