1 use crate::structs::{ModLockPostView, ModlogListParams};
8 NullableExpressionMethods,
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
14 schema::{community, mod_lock_post, person, post},
16 community::{Community, CommunitySafe},
17 moderator::ModLockPost,
18 person::{Person, PersonSafe},
21 traits::{ToSafe, ViewToVec},
22 utils::{get_conn, limit_and_offset, DbPool},
25 type ModLockPostViewTuple = (ModLockPost, Option<PersonSafe>, Post, CommunitySafe);
27 impl ModLockPostView {
28 pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
29 let conn = &mut get_conn(pool).await?;
31 let person_alias_1 = diesel::alias!(person as person1);
32 let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1));
33 let show_mod_names = !params.hide_modlog_names;
34 let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>();
36 let admin_names_join = mod_lock_post::mod_person_id
38 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
39 let mut query = mod_lock_post::table
40 .left_join(person::table.on(admin_names_join))
41 .inner_join(post::table)
42 .inner_join(community::table.on(post::community_id.eq(community::id)))
43 .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
45 mod_lock_post::all_columns,
46 Person::safe_columns_tuple().nullable(),
48 Community::safe_columns_tuple(),
52 if let Some(community_id) = params.community_id {
53 query = query.filter(post::community_id.eq(community_id));
56 if let Some(mod_person_id) = params.mod_person_id {
57 query = query.filter(mod_lock_post::mod_person_id.eq(mod_person_id));
60 if let Some(other_person_id) = params.other_person_id {
61 query = query.filter(person_alias_1.field(person::id).eq(other_person_id));
64 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
69 .order_by(mod_lock_post::when_.desc())
70 .load::<ModLockPostViewTuple>(conn)
73 let results = Self::from_tuple_to_vec(res);
78 impl ViewToVec for ModLockPostView {
79 type DbTuple = ModLockPostViewTuple;
80 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
89 .collect::<Vec<Self>>()