1 use crate::structs::{ModStickyPostView, ModlogListParams};
8 NullableExpressionMethods,
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
14 schema::{community, mod_sticky_post, person, post},
16 community::{Community, CommunitySafe},
17 moderator::ModStickyPost,
18 person::{Person, PersonSafe},
21 traits::{ToSafe, ViewToVec},
22 utils::{get_conn, limit_and_offset, DbPool},
25 type ModStickyPostViewTuple = (ModStickyPost, Option<PersonSafe>, Post, CommunitySafe);
27 impl ModStickyPostView {
28 pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
29 let conn = &mut get_conn(pool).await?;
30 let person_alias_1 = diesel::alias!(person as person1);
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_sticky_post::mod_person_id
37 .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
38 let mut query = mod_sticky_post::table
39 .left_join(person::table.on(admin_names_join))
40 .inner_join(post::table)
41 .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
42 .inner_join(community::table.on(post::community_id.eq(community::id)))
44 mod_sticky_post::all_columns,
45 Person::safe_columns_tuple().nullable(),
47 Community::safe_columns_tuple(),
51 if let Some(community_id) = params.community_id {
52 query = query.filter(post::community_id.eq(community_id));
55 if let Some(mod_person_id) = params.mod_person_id {
56 query = query.filter(mod_sticky_post::mod_person_id.eq(mod_person_id));
59 if let Some(other_person_id) = params.other_person_id {
60 query = query.filter(person_alias_1.field(person::id).eq(other_person_id));
63 let (limit, offset) = limit_and_offset(params.page, params.limit)?;
68 .order_by(mod_sticky_post::when_.desc())
69 .load::<ModStickyPostViewTuple>(conn)
72 let results = Self::from_tuple_to_vec(res);
77 impl ViewToVec for ModStickyPostView {
78 type DbTuple = ModStickyPostViewTuple;
79 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
88 .collect::<Vec<Self>>()