1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4 build_response::build_post_response,
6 post::{FeaturePost, PostResponse},
7 send_activity::{ActivityChannel, SendActivityData},
10 check_community_deleted_or_removed,
13 local_user_view_from_jwt,
16 use lemmy_db_schema::{
18 moderator::{ModFeaturePost, ModFeaturePostForm},
19 post::{Post, PostUpdateForm},
24 use lemmy_utils::error::LemmyError;
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?;
33 let post_id = data.post_id;
34 let orig_post = Post::read(&mut context.pool(), post_id).await?;
37 local_user_view.person.id,
38 orig_post.community_id,
42 check_community_deleted_or_removed(orig_post.community_id, &mut context.pool()).await?;
44 if data.feature_type == PostFeatureType::Community {
45 // Verify that only the mods can feature in community
48 local_user_view.person.id,
49 orig_post.community_id,
53 is_admin(&local_user_view)?;
57 let post_id = data.post_id;
58 let new_post: PostUpdateForm = if data.feature_type == PostFeatureType::Community {
60 featured_community: Some(data.featured),
65 featured_local: Some(data.featured),
69 let post = Post::update(&mut context.pool(), post_id, &new_post).await?;
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,
79 ModFeaturePost::create(&mut context.pool(), &form).await?;
81 let person_id = local_user_view.person.id;
82 ActivityChannel::submit_activity(
83 SendActivityData::FeaturePost(post, local_user_view.person, data.featured),
88 build_post_response(&context, orig_post.community_id, person_id, post_id).await