1 use crate::structs::{ModLockPostView, ModlogListParams};
2 use diesel::{result::Error, *};
5 schema::{community, mod_lock_post, person, post},
7 community::{Community, CommunitySafe},
8 moderator::ModLockPost,
9 person::{Person, PersonSafe},
12 traits::{ToSafe, ViewToVec},
13 utils::limit_and_offset,
16 type ModLockPostViewTuple = (ModLockPost, Option<PersonSafe>, Post, CommunitySafe);
18 impl ModLockPostView {
19 pub fn list(conn: &mut PgConnection, params: ModlogListParams) -> Result<Vec<Self>, Error> {
20 let person_alias_1 = diesel::alias!(person as person1);
21 let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
22 let show_mod_names = !params.hide_modlog_names;
23 let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
25 let admin_names_join = mod_lock_post::mod_person_id
27 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
28 let mut query = mod_lock_post::table
29 .left_join(person::table.on(admin_names_join))
30 .inner_join(post::table)
31 .inner_join(community::table.on(post::community_id.eq(community::id)))
32 .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
34 mod_lock_post::all_columns,
35 Person::safe_columns_tuple().nullable(),
37 Community::safe_columns_tuple(),
41 if let Some(community_id) = params.community_id {
42 query = query.filter(post::community_id.eq(community_id));
45 if let Some(mod_person_id) = params.mod_person_id {
46 query = query.filter(mod_lock_post::mod_person_id.eq(mod_person_id));
49 if let Some(other_person_id) = params.other_person_id {
50 query = query.filter(person_alias_1.field(person::id).eq(other_person_id));
53 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
58 .order_by(mod_lock_post::when_.desc())
59 .load::<ModLockPostViewTuple>(conn)?;
61 let results = Self::from_tuple_to_vec(res);
66 impl ViewToVec for ModLockPostView {
67 type DbTuple = ModLockPostViewTuple;
68 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
77 .collect::<Vec<Self>>()