]> Untitled Git - lemmy.git/blob - crates/api/src/post/feature.rs
add enable_federated_downvotes site option
[lemmy.git] / crates / api / src / post / feature.rs
1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4   build_response::build_post_response,
5   context::LemmyContext,
6   post::{FeaturePost, PostResponse},
7   send_activity::{ActivityChannel, SendActivityData},
8   utils::{
9     check_community_ban,
10     check_community_deleted_or_removed,
11     is_admin,
12     is_mod_or_admin,
13     local_user_view_from_jwt,
14   },
15 };
16 use lemmy_db_schema::{
17   source::{
18     moderator::{ModFeaturePost, ModFeaturePostForm},
19     post::{Post, PostUpdateForm},
20   },
21   traits::Crud,
22   PostFeatureType,
23 };
24 use lemmy_utils::error::LemmyError;
25
26 #[tracing::instrument(skip(context))]
27 pub async fn feature_post(
28   data: Json<FeaturePost>,
29   context: Data<LemmyContext>,
30 ) -> Result<Json<PostResponse>, LemmyError> {
31   let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
32
33   let post_id = data.post_id;
34   let orig_post = Post::read(&mut context.pool(), post_id).await?;
35
36   check_community_ban(
37     local_user_view.person.id,
38     orig_post.community_id,
39     &mut context.pool(),
40   )
41   .await?;
42   check_community_deleted_or_removed(orig_post.community_id, &mut context.pool()).await?;
43
44   if data.feature_type == PostFeatureType::Community {
45     // Verify that only the mods can feature in community
46     is_mod_or_admin(
47       &mut context.pool(),
48       local_user_view.person.id,
49       orig_post.community_id,
50     )
51     .await?;
52   } else {
53     is_admin(&local_user_view)?;
54   }
55
56   // Update the post
57   let post_id = data.post_id;
58   let new_post: PostUpdateForm = if data.feature_type == PostFeatureType::Community {
59     PostUpdateForm {
60       featured_community: Some(data.featured),
61       ..Default::default()
62     }
63   } else {
64     PostUpdateForm {
65       featured_local: Some(data.featured),
66       ..Default::default()
67     }
68   };
69   let post = Post::update(&mut context.pool(), post_id, &new_post).await?;
70
71   // Mod tables
72   let form = ModFeaturePostForm {
73     mod_person_id: local_user_view.person.id,
74     post_id: data.post_id,
75     featured: data.featured,
76     is_featured_community: data.feature_type == PostFeatureType::Community,
77   };
78
79   ModFeaturePost::create(&mut context.pool(), &form).await?;
80
81   let person_id = local_user_view.person.id;
82   ActivityChannel::submit_activity(
83     SendActivityData::FeaturePost(post, local_user_view.person, data.featured),
84     &context,
85   )
86   .await?;
87
88   build_post_response(&context, orig_post.community_id, person_id, post_id).await
89 }