]> Untitled Git - lemmy.git/blob - crates/db_views_moderator/src/mod_feature_post_view.rs
Add support for Featured Posts (#2585)
[lemmy.git] / crates / db_views_moderator / src / mod_feature_post_view.rs
1 use crate::structs::{ModFeaturePostView, ModlogListParams};
2 use diesel::{
3   result::Error,
4   BoolExpressionMethods,
5   ExpressionMethods,
6   IntoSql,
7   JoinOnDsl,
8   NullableExpressionMethods,
9   QueryDsl,
10 };
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
13   newtypes::PersonId,
14   schema::{community, mod_feature_post, person, post},
15   source::{
16     community::{Community, CommunitySafe},
17     moderator::ModFeaturePost,
18     person::{Person, PersonSafe},
19     post::Post,
20   },
21   traits::{ToSafe, ViewToVec},
22   utils::{get_conn, limit_and_offset, DbPool},
23 };
24
25 type ModFeaturePostViewTuple = (ModFeaturePost, Option<PersonSafe>, Post, CommunitySafe);
26
27 impl ModFeaturePostView {
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>();
34
35     let admin_names_join = mod_feature_post::mod_person_id
36       .eq(person::id)
37       .and(show_mod_names_expr.or(person::id.eq(admin_person_id_join)));
38     let mut query = mod_feature_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)))
43       .select((
44         mod_feature_post::all_columns,
45         Person::safe_columns_tuple().nullable(),
46         post::all_columns,
47         Community::safe_columns_tuple(),
48       ))
49       .into_boxed();
50
51     if let Some(community_id) = params.community_id {
52       query = query.filter(post::community_id.eq(community_id));
53     };
54
55     if let Some(mod_person_id) = params.mod_person_id {
56       query = query.filter(mod_feature_post::mod_person_id.eq(mod_person_id));
57     };
58
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));
61     };
62
63     let (limit, offset) = limit_and_offset(params.page, params.limit)?;
64
65     let res = query
66       .limit(limit)
67       .offset(offset)
68       .order_by(mod_feature_post::when_.desc())
69       .load::<ModFeaturePostViewTuple>(conn)
70       .await?;
71
72     let results = Self::from_tuple_to_vec(res);
73     Ok(results)
74   }
75 }
76
77 impl ViewToVec for ModFeaturePostView {
78   type DbTuple = ModFeaturePostViewTuple;
79   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
80     items
81       .into_iter()
82       .map(|a| Self {
83         mod_feature_post: a.0,
84         moderator: a.1,
85         post: a.2,
86         community: a.3,
87       })
88       .collect::<Vec<Self>>()
89   }
90 }